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
1212 OpenApiToolDefinitionSchema,
1313 type OpenApiToolDefinition,
1414 } 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";
1620 export {
1721 OpenApiToolInvocationError,
1822 createOpenApiToolsFromManifest,
packages/codemode-openapi/src/openapi-tool-presentation.ts+37−2
@@ -18,6 +18,41 @@ const parseJson = (value: string): unknown | undefined => { · 17 unmodified lines
1818 }
1919 };
2020
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+
2156 const firstExample = (
2257 examples: ReadonlyArray<OpenApiExample> | undefined,
2358 ): OpenApiExample | undefined => examples?.[0];
@@ -82,8 +117,8 @@ export const buildOpenApiToolPresentation = (input: { · 58 unmodified lines
82117 const exampleOutputJson = buildExampleOutputJson(input.definition.documentation);
83118
84119 return {
85− inputType: typeSignatureFromSchemaJson(inputSchemaJson, "unknown", 320),
86− outputType: typeSignatureFromSchemaJson(outputSchemaJson, "unknown", 320),
120+ inputType: typeSignatureFromSchemaJson(inputSchemaJson, "unknown", Infinity),
121+ outputType: openApiOutputTypeSignatureFromSchemaJson(outputSchemaJson, Infinity),
87122 ...(inputSchemaJson ? { inputSchemaJson } : {}),
88123 ...(outputSchemaJson ? { outputSchemaJson } : {}),
89124 ...(exampleInputJson ? { exampleInputJson } : {}),