History

nicer ui

Rhys Sullivan committed 8 months ago

Commit ece99cf1 parent 8f20d5b14 files · +843 −160Browse files

Showing the changes to packages/codemode-openapi (2 of 14 files). Show all

Unified · Split

2 files
  1. packages/codemode-openapi/src/index.ts +5 −1
  2. packages/codemode-openapi/src/openapi-tool-presentation.ts +37 −2
packages/codemode-openapi/src/index.ts+5−1
@@ -12,7 +12,11 @@ export { · 11 unmodified lines
12 OpenApiToolDefinitionSchema,12 OpenApiToolDefinitionSchema,
13 type OpenApiToolDefinition,13 type OpenApiToolDefinition,
14} from "./openapi-definitions";14} from "./openapi-definitions";
15export { buildOpenApiToolPresentation, type OpenApiToolPresentation } from "./openapi-tool-presentation";15export {
16 buildOpenApiToolPresentation,
17 openApiOutputTypeSignatureFromSchemaJson,
18 type OpenApiToolPresentation,
19} from "./openapi-tool-presentation";
16export {20export {
17 OpenApiToolInvocationError,21 OpenApiToolInvocationError,
18 createOpenApiToolsFromManifest,22 createOpenApiToolsFromManifest,
packages/codemode-openapi/src/openapi-tool-presentation.ts+37−2
@@ -18,6 +18,41 @@ const parseJson = (value: string): unknown | undefined => { · 17 unmodified lines
18 }18 }
19};19};
20 20
21const asRecord = (value: unknown): Record<string, unknown> =>
22 typeof value === "object" && value !== null && !Array.isArray(value)
23 ? (value as Record<string, unknown>)
24 : {};
25
26const isStrictEmptyObjectSchema = (value: unknown): boolean => {
27 const schema = asRecord(value);
28 if (schema.type !== "object" && schema.properties === undefined) {
29 return false;
30 }
31
32 const properties = asRecord(schema.properties);
33 return Object.keys(properties).length === 0 && schema.additionalProperties === false;
34};
35
36export const openApiOutputTypeSignatureFromSchemaJson = (
37 schemaJson: string | undefined,
38 maxLength: number = 320,
39 ): string => {
40 if (!schemaJson) {
41 return "void";
42 }
43
44 const parsed = parseJson(schemaJson);
45 if (parsed === undefined) {
46 return "unknown";
47 }
48
49 if (isStrictEmptyObjectSchema(parsed)) {
50 return "{}";
51 }
52
53 return typeSignatureFromSchemaJson(schemaJson, "unknown", maxLength);
54};
55
21const firstExample = (56const firstExample = (
22 examples: ReadonlyArray<OpenApiExample> | undefined,57 examples: ReadonlyArray<OpenApiExample> | undefined,
23): OpenApiExample | undefined => examples?.[0];58): OpenApiExample | undefined => examples?.[0];
@@ -82,8 +117,8 @@ export const buildOpenApiToolPresentation = (input: { · 58 unmodified lines
82 const exampleOutputJson = buildExampleOutputJson(input.definition.documentation);117 const exampleOutputJson = buildExampleOutputJson(input.definition.documentation);
83 118
84 return {119 return {
85 inputType: typeSignatureFromSchemaJson(inputSchemaJson, "unknown", 320),120 inputType: typeSignatureFromSchemaJson(inputSchemaJson, "unknown", Infinity),
86 outputType: typeSignatureFromSchemaJson(outputSchemaJson, "unknown", 320),121 outputType: openApiOutputTypeSignatureFromSchemaJson(outputSchemaJson, Infinity),
87 ...(inputSchemaJson ? { inputSchemaJson } : {}),122 ...(inputSchemaJson ? { inputSchemaJson } : {}),
88 ...(outputSchemaJson ? { outputSchemaJson } : {}),123 ...(outputSchemaJson ? { outputSchemaJson } : {}),
89 ...(exampleInputJson ? { exampleInputJson } : {}),124 ...(exampleInputJson ? { exampleInputJson } : {}),