Showing the changes to packages/codemode-openapi/src (2 of 14 files). Show all
Unified · Split
| @@ -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"; |
| 15 | export { buildOpenApiToolPresentation, type OpenApiToolPresentation } from "./openapi-tool-presentation"; | 15 | export { |
| 16 | buildOpenApiToolPresentation, | ||
| 17 | openApiOutputTypeSignatureFromSchemaJson, | ||
| 18 | type OpenApiToolPresentation, | ||
| 19 | } from "./openapi-tool-presentation"; | ||
| 16 | export { | 20 | export { |
| 17 | OpenApiToolInvocationError, | 21 | OpenApiToolInvocationError, |
| 18 | createOpenApiToolsFromManifest, | 22 | createOpenApiToolsFromManifest, |
| @@ -18,6 +18,41 @@ const parseJson = (value: string): unknown | undefined => { · 17 unmodified lines | |||
| 18 | } | 18 | } |
| 19 | }; | 19 | }; |
| 20 | 20 | ||
| 21 | const asRecord = (value: unknown): Record<string, unknown> => | ||
| 22 | typeof value === "object" && value !== null && !Array.isArray(value) | ||
| 23 | ? (value as Record<string, unknown>) | ||
| 24 | : {}; | ||
| 25 | |||
| 26 | const 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 | |||
| 36 | export 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 | |||
| 21 | const firstExample = ( | 56 | const 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 } : {}), |