History

nicer ui

Rhys Sullivan committed 8 months ago

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

Showing the changes to packages/codemode-openapi/src/openapi-tool-presentation.ts (1 of 14 files). Show all

Unified · Split

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 } : {}),