@geekmidas/schema 10.0.0-alpha.13 → 10.0.0-alpha.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/conversion.cjs +2 -1
- package/dist/conversion.cjs.map +1 -1
- package/dist/conversion.d.cts.map +1 -1
- package/dist/conversion.d.mts.map +1 -1
- package/dist/conversion.mjs +2 -1
- package/dist/conversion.mjs.map +1 -1
- package/dist/openapi.cjs +11 -2
- package/dist/openapi.cjs.map +1 -1
- package/dist/openapi.d.cts +3 -0
- package/dist/openapi.d.cts.map +1 -1
- package/dist/openapi.d.mts +3 -0
- package/dist/openapi.d.mts.map +1 -1
- package/dist/openapi.mjs +11 -2
- package/dist/openapi.mjs.map +1 -1
- package/package.json +1 -1
package/dist/conversion.cjs
CHANGED
|
@@ -39,7 +39,7 @@ function extractAndConvertDefs(jsonSchema, componentCollector) {
|
|
|
39
39
|
if (Array.isArray(schema)) return schema.map(processSchema);
|
|
40
40
|
const processed = {};
|
|
41
41
|
for (const [key, value] of Object.entries(schema)) {
|
|
42
|
-
if (key === "$defs") continue;
|
|
42
|
+
if (key === "$defs" || key === "$schema") continue;
|
|
43
43
|
processed[key] = processSchema(value);
|
|
44
44
|
}
|
|
45
45
|
return processed;
|
|
@@ -88,6 +88,7 @@ async function convertSchemaWithComponents(schema, componentCollector) {
|
|
|
88
88
|
if (!componentCollector) return jsonSchema;
|
|
89
89
|
const schemaId = (await getSchemaMetadata(schema))?.id || jsonSchema?.id;
|
|
90
90
|
if (schemaId) {
|
|
91
|
+
if (typeof jsonSchema?.$ref === "string") return componentCollector.getReference(schemaId);
|
|
91
92
|
const { id, ...schemaWithoutId } = jsonSchema;
|
|
92
93
|
componentCollector.addSchema(schemaId, schemaWithoutId);
|
|
93
94
|
return componentCollector.getReference(schemaId);
|
package/dist/conversion.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"conversion.cjs","names":[],"sources":["../src/conversion.ts"],"sourcesContent":["import type { StandardSchemaV1 } from '@standard-schema/spec';\n\nexport enum SchemaVendor {\n\tzod = 'zod',\n\tvalibot = 'valibot',\n}\n\nexport type VendorConvertor = (schema: any) => Promise<any>;\nexport type VendorConvertors = Record<SchemaVendor, VendorConvertor>;\n\nfunction isSchemaVendor(vendor?: string): vendor is SchemaVendor {\n\tif (!vendor) {\n\t\treturn false;\n\t}\n\n\treturn Object.values(SchemaVendor).includes(vendor as SchemaVendor);\n}\n\nexport const StandardSchemaJsonSchema: VendorConvertors = {\n\tzod: async (schema): Promise<any> => {\n\t\ttry {\n\t\t\tconst { z } = await import('zod/v4').catch(() => ({ z: {} }));\n\n\t\t\tif ('toJSONSchema' in z && typeof z.toJSONSchema === 'function') {\n\t\t\t\treturn z.toJSONSchema(schema);\n\t\t\t}\n\t\t\tconst { zodToJsonSchema } = await import('zod-to-json-schema');\n\n\t\t\tconst result = zodToJsonSchema(schema, {\n\t\t\t\tremoveAdditionalStrategy: 'strict',\n\t\t\t});\n\n\t\t\treturn result;\n\t\t} catch {\n\t\t\t// Fallback to basic conversion if zod-to-json-schema is not available\n\t\t\treturn { type: 'object' };\n\t\t}\n\t},\n\tvalibot: async (schema): Promise<any> => {\n\t\tconst { toJsonSchema } = await import('@valibot/to-json-schema');\n\t\treturn toJsonSchema(schema as any);\n\t},\n};\n\nfunction extractAndConvertDefs(\n\tjsonSchema: any,\n\tcomponentCollector?: {\n\t\taddSchema(id: string, schema: any): void;\n\t\tgetReference(id: string): { $ref: string };\n\t},\n): any {\n\tif (!jsonSchema || typeof jsonSchema !== 'object') {\n\t\treturn jsonSchema;\n\t}\n\n\t// Process the schema recursively to update references\n\tconst processSchema = (schema: any): any => {\n\t\tif (!schema || typeof schema !== 'object') {\n\t\t\treturn schema;\n\t\t}\n\n\t\t// Handle $ref\n\t\tif (schema.$ref && typeof schema.$ref === 'string') {\n\t\t\t// Convert #/$defs/X to #/components/schemas/X\n\t\t\tif (schema.$ref.startsWith('#/$defs/')) {\n\t\t\t\tconst refName = schema.$ref.replace('#/$defs/', '');\n\t\t\t\treturn componentCollector\n\t\t\t\t\t? componentCollector.getReference(refName)\n\t\t\t\t\t: schema;\n\t\t\t}\n\t\t\treturn schema;\n\t\t}\n\n\t\t// Handle arrays\n\t\tif (Array.isArray(schema)) {\n\t\t\treturn schema.map(processSchema);\n\t\t}\n\n\t\t// Process all properties recursively\n\t\tconst processed: any = {};\n\t\tfor (const [key, value] of Object.entries(schema)) {\n\t\t\
|
|
1
|
+
{"version":3,"file":"conversion.cjs","names":[],"sources":["../src/conversion.ts"],"sourcesContent":["import type { StandardSchemaV1 } from '@standard-schema/spec';\n\nexport enum SchemaVendor {\n\tzod = 'zod',\n\tvalibot = 'valibot',\n}\n\nexport type VendorConvertor = (schema: any) => Promise<any>;\nexport type VendorConvertors = Record<SchemaVendor, VendorConvertor>;\n\nfunction isSchemaVendor(vendor?: string): vendor is SchemaVendor {\n\tif (!vendor) {\n\t\treturn false;\n\t}\n\n\treturn Object.values(SchemaVendor).includes(vendor as SchemaVendor);\n}\n\nexport const StandardSchemaJsonSchema: VendorConvertors = {\n\tzod: async (schema): Promise<any> => {\n\t\ttry {\n\t\t\tconst { z } = await import('zod/v4').catch(() => ({ z: {} }));\n\n\t\t\tif ('toJSONSchema' in z && typeof z.toJSONSchema === 'function') {\n\t\t\t\treturn z.toJSONSchema(schema);\n\t\t\t}\n\t\t\tconst { zodToJsonSchema } = await import('zod-to-json-schema');\n\n\t\t\tconst result = zodToJsonSchema(schema, {\n\t\t\t\tremoveAdditionalStrategy: 'strict',\n\t\t\t});\n\n\t\t\treturn result;\n\t\t} catch {\n\t\t\t// Fallback to basic conversion if zod-to-json-schema is not available\n\t\t\treturn { type: 'object' };\n\t\t}\n\t},\n\tvalibot: async (schema): Promise<any> => {\n\t\tconst { toJsonSchema } = await import('@valibot/to-json-schema');\n\t\treturn toJsonSchema(schema as any);\n\t},\n};\n\nfunction extractAndConvertDefs(\n\tjsonSchema: any,\n\tcomponentCollector?: {\n\t\taddSchema(id: string, schema: any): void;\n\t\tgetReference(id: string): { $ref: string };\n\t},\n): any {\n\tif (!jsonSchema || typeof jsonSchema !== 'object') {\n\t\treturn jsonSchema;\n\t}\n\n\t// Process the schema recursively to update references\n\tconst processSchema = (schema: any): any => {\n\t\tif (!schema || typeof schema !== 'object') {\n\t\t\treturn schema;\n\t\t}\n\n\t\t// Handle $ref\n\t\tif (schema.$ref && typeof schema.$ref === 'string') {\n\t\t\t// Convert #/$defs/X to #/components/schemas/X\n\t\t\tif (schema.$ref.startsWith('#/$defs/')) {\n\t\t\t\tconst refName = schema.$ref.replace('#/$defs/', '');\n\t\t\t\treturn componentCollector\n\t\t\t\t\t? componentCollector.getReference(refName)\n\t\t\t\t\t: schema;\n\t\t\t}\n\t\t\treturn schema;\n\t\t}\n\n\t\t// Handle arrays\n\t\tif (Array.isArray(schema)) {\n\t\t\treturn schema.map(processSchema);\n\t\t}\n\n\t\t// Process all properties recursively\n\t\tconst processed: any = {};\n\t\tfor (const [key, value] of Object.entries(schema)) {\n\t\t\t// `$defs` has been extracted. `$schema` names the dialect the converter\n\t\t\t// wrote, which the document declares once for every schema in it; a\n\t\t\t// copy on each one is noise the document does not need.\n\t\t\tif (key === '$defs' || key === '$schema') {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tprocessed[key] = processSchema(value);\n\t\t}\n\t\treturn processed;\n\t};\n\n\t// Extract $defs if present\n\tif (jsonSchema.$defs && componentCollector) {\n\t\tfor (const [defName, defSchema] of Object.entries(jsonSchema.$defs)) {\n\t\t\t// Process the definition recursively to handle nested $refs\n\t\t\tconst processedDefSchema = processSchema(defSchema);\n\t\t\t// Add each definition to the component collector\n\t\t\tcomponentCollector.addSchema(defName, processedDefSchema);\n\t\t}\n\t}\n\n\t// Process the schema and remove $defs\n\tconst { $defs, ...schemaWithoutDefs } = jsonSchema;\n\treturn processSchema(schemaWithoutDefs);\n}\n\nexport async function convertStandardSchemaToJsonSchema(\n\tschema?: StandardSchemaV1,\n\tcomponentCollector?: {\n\t\taddSchema(id: string, schema: any): void;\n\t\tgetReference(id: string): { $ref: string };\n\t},\n): Promise<any> {\n\tif (!schema) {\n\t\treturn undefined;\n\t}\n\n\tconst vendor = schema['~standard']?.vendor;\n\tif (!isSchemaVendor(vendor)) {\n\t\tthrow new Error(\n\t\t\t`Unsupported or missing vendor \"${vendor}\" for Standard Schema. Supported vendors are: ${Object.keys(StandardSchemaJsonSchema).join(', ')}`,\n\t\t);\n\t}\n\n\tconst toJSONSchema = StandardSchemaJsonSchema[vendor];\n\tconst jsonSchema = await toJSONSchema(schema);\n\n\t// Extract and convert $defs to components\n\treturn extractAndConvertDefs(jsonSchema, componentCollector);\n}\n\nexport async function getZodMetadata(\n\tschema: StandardSchemaV1,\n): Promise<SchemaMeta | undefined> {\n\tconst { ZodObject } = await import('zod/v4');\n\n\tif (schema instanceof ZodObject) {\n\t\treturn schema.meta();\n\t}\n\n\treturn undefined;\n}\n\n/**\n * Return JSON Schema for every schema registered in zod v4's global registry\n * via `.meta({ id })`. Returns an empty object when zod v4 is unavailable or\n * no schemas are registered.\n *\n * `unrepresentable: 'any'` keeps a single unrepresentable schema from\n * collapsing the whole result.\n */\nexport async function getRegisteredZodJsonSchemas(): Promise<\n\tRecord<string, any>\n> {\n\ttry {\n\t\tconst { z } = await import('zod/v4').catch(() => ({ z: null as any }));\n\t\tif (!z?.toJSONSchema || !z.globalRegistry) {\n\t\t\treturn {};\n\t\t}\n\t\tconst result = z.toJSONSchema(z.globalRegistry, {\n\t\t\tunrepresentable: 'any',\n\t\t});\n\t\treturn (result?.schemas ?? {}) as Record<string, any>;\n\t} catch {\n\t\treturn {};\n\t}\n}\n\nexport async function getSchemaMetadata(\n\tschema: StandardSchemaV1,\n): Promise<SchemaMeta | undefined> {\n\tconst vendor = schema['~standard']?.vendor;\n\n\tif (vendor === 'zod') {\n\t\treturn getZodMetadata(schema);\n\t}\n\n\treturn undefined;\n}\n\ninterface SchemaMeta {\n\tid?: string;\n}\n\nexport async function convertSchemaWithComponents(\n\tschema: StandardSchemaV1 | undefined,\n\tcomponentCollector?: {\n\t\taddSchema(id: string, schema: any): void;\n\t\tgetReference(id: string): { $ref: string };\n\t},\n): Promise<any> {\n\tif (!schema) {\n\t\treturn undefined;\n\t}\n\n\t// Convert to JSON Schema with component collector to handle $defs\n\tconst jsonSchema = await convertStandardSchemaToJsonSchema(\n\t\tschema,\n\t\tcomponentCollector,\n\t);\n\n\tif (!componentCollector) {\n\t\treturn jsonSchema;\n\t}\n\n\t// Check if this schema has metadata with an ID\n\tconst metadata = await getSchemaMetadata(schema);\n\n\t// Also check if the JSON Schema itself has an id field (from Zod's meta)\n\tconst schemaId = metadata?.id || jsonSchema?.id;\n\n\tif (schemaId) {\n\t\t// Zod 4.6 already turns a registered schema into a `$ref` to its own\n\t\t// `$defs` entry, which the conversion above moved into components. Adding\n\t\t// that reference under the same id would replace the definition with a\n\t\t// pointer to itself — `User: { $ref: '#/components/schemas/User' }` — and\n\t\t// leave the document with no User at all.\n\t\tif (typeof jsonSchema?.$ref === 'string') {\n\t\t\treturn componentCollector.getReference(schemaId);\n\t\t}\n\t\t// Remove the id from the schema before adding to components\n\t\tconst { id, ...schemaWithoutId } = jsonSchema;\n\t\t// Add this schema to components and return a reference\n\t\tcomponentCollector.addSchema(schemaId, schemaWithoutId);\n\t\treturn componentCollector.getReference(schemaId);\n\t}\n\n\treturn jsonSchema;\n}\n"],"mappings":";;AAEA,IAAY,eAAL,yBAAA,cAAA;CACN,aAAA,SAAA;CACA,aAAA,aAAA;;AACD,EAAA,CAAA,CAAA;AAKA,SAAS,eAAe,QAAyC;CAChE,IAAI,CAAC,QACJ,OAAO;CAGR,OAAO,OAAO,OAAO,YAAY,CAAC,CAAC,SAAS,MAAsB;AACnE;AAEA,MAAa,2BAA6C;CACzD,KAAK,OAAO,WAAyB;EACpC,IAAI;GACH,MAAM,EAAE,MAAM,MAAM,OAAO,SAAS,CAAC,aAAa,EAAE,GAA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"file":"conversion.d.cts","names":[],"sources":["../src/conversion.ts"],"mappings":";;oBAEY;EACX;EACA;;YAGW,mBAAmB,gBAAgB;YACnC,mBAAmB,OAAO,cAAc;qBAUvC,0BAA0B;
|
|
1
|
+
{"version":3,"file":"conversion.d.cts","names":[],"sources":["../src/conversion.ts"],"mappings":";;oBAEY;EACX;EACA;;YAGW,mBAAmB,gBAAgB;YACnC,mBAAmB,OAAO,cAAc;qBAUvC,0BAA0B;wBAyFjB,kCACrB,SAAS,kBACT;EACC,UAAU,YAAY;EACtB,aAAa;IAAe;;IAE3B;wBAmBmB,eACrB,QAAQ,mBACN,QAAQ;;;;;;;;;wBAkBW,+BAA+B,QACpD;wBAgBqB,kBACrB,QAAQ,mBACN,QAAQ;UAUD;EACT;;wBAGqB,4BACrB,QAAQ,8BACR;EACC,UAAU,YAAY;EACtB,aAAa;IAAe;;IAE3B"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"conversion.d.mts","names":[],"sources":["../src/conversion.ts"],"mappings":";;oBAEY;EACX;EACA;;YAGW,mBAAmB,gBAAgB;YACnC,mBAAmB,OAAO,cAAc;qBAUvC,0BAA0B;
|
|
1
|
+
{"version":3,"file":"conversion.d.mts","names":[],"sources":["../src/conversion.ts"],"mappings":";;oBAEY;EACX;EACA;;YAGW,mBAAmB,gBAAgB;YACnC,mBAAmB,OAAO,cAAc;qBAUvC,0BAA0B;wBAyFjB,kCACrB,SAAS,kBACT;EACC,UAAU,YAAY;EACtB,aAAa;IAAe;;IAE3B;wBAmBmB,eACrB,QAAQ,mBACN,QAAQ;;;;;;;;;wBAkBW,+BAA+B,QACpD;wBAgBqB,kBACrB,QAAQ,mBACN,QAAQ;UAUD;EACT;;wBAGqB,4BACrB,QAAQ,8BACR;EACC,UAAU,YAAY;EACtB,aAAa;IAAe;;IAE3B"}
|
package/dist/conversion.mjs
CHANGED
|
@@ -38,7 +38,7 @@ function extractAndConvertDefs(jsonSchema, componentCollector) {
|
|
|
38
38
|
if (Array.isArray(schema)) return schema.map(processSchema);
|
|
39
39
|
const processed = {};
|
|
40
40
|
for (const [key, value] of Object.entries(schema)) {
|
|
41
|
-
if (key === "$defs") continue;
|
|
41
|
+
if (key === "$defs" || key === "$schema") continue;
|
|
42
42
|
processed[key] = processSchema(value);
|
|
43
43
|
}
|
|
44
44
|
return processed;
|
|
@@ -87,6 +87,7 @@ async function convertSchemaWithComponents(schema, componentCollector) {
|
|
|
87
87
|
if (!componentCollector) return jsonSchema;
|
|
88
88
|
const schemaId = (await getSchemaMetadata(schema))?.id || jsonSchema?.id;
|
|
89
89
|
if (schemaId) {
|
|
90
|
+
if (typeof jsonSchema?.$ref === "string") return componentCollector.getReference(schemaId);
|
|
90
91
|
const { id, ...schemaWithoutId } = jsonSchema;
|
|
91
92
|
componentCollector.addSchema(schemaId, schemaWithoutId);
|
|
92
93
|
return componentCollector.getReference(schemaId);
|
package/dist/conversion.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"conversion.mjs","names":[],"sources":["../src/conversion.ts"],"sourcesContent":["import type { StandardSchemaV1 } from '@standard-schema/spec';\n\nexport enum SchemaVendor {\n\tzod = 'zod',\n\tvalibot = 'valibot',\n}\n\nexport type VendorConvertor = (schema: any) => Promise<any>;\nexport type VendorConvertors = Record<SchemaVendor, VendorConvertor>;\n\nfunction isSchemaVendor(vendor?: string): vendor is SchemaVendor {\n\tif (!vendor) {\n\t\treturn false;\n\t}\n\n\treturn Object.values(SchemaVendor).includes(vendor as SchemaVendor);\n}\n\nexport const StandardSchemaJsonSchema: VendorConvertors = {\n\tzod: async (schema): Promise<any> => {\n\t\ttry {\n\t\t\tconst { z } = await import('zod/v4').catch(() => ({ z: {} }));\n\n\t\t\tif ('toJSONSchema' in z && typeof z.toJSONSchema === 'function') {\n\t\t\t\treturn z.toJSONSchema(schema);\n\t\t\t}\n\t\t\tconst { zodToJsonSchema } = await import('zod-to-json-schema');\n\n\t\t\tconst result = zodToJsonSchema(schema, {\n\t\t\t\tremoveAdditionalStrategy: 'strict',\n\t\t\t});\n\n\t\t\treturn result;\n\t\t} catch {\n\t\t\t// Fallback to basic conversion if zod-to-json-schema is not available\n\t\t\treturn { type: 'object' };\n\t\t}\n\t},\n\tvalibot: async (schema): Promise<any> => {\n\t\tconst { toJsonSchema } = await import('@valibot/to-json-schema');\n\t\treturn toJsonSchema(schema as any);\n\t},\n};\n\nfunction extractAndConvertDefs(\n\tjsonSchema: any,\n\tcomponentCollector?: {\n\t\taddSchema(id: string, schema: any): void;\n\t\tgetReference(id: string): { $ref: string };\n\t},\n): any {\n\tif (!jsonSchema || typeof jsonSchema !== 'object') {\n\t\treturn jsonSchema;\n\t}\n\n\t// Process the schema recursively to update references\n\tconst processSchema = (schema: any): any => {\n\t\tif (!schema || typeof schema !== 'object') {\n\t\t\treturn schema;\n\t\t}\n\n\t\t// Handle $ref\n\t\tif (schema.$ref && typeof schema.$ref === 'string') {\n\t\t\t// Convert #/$defs/X to #/components/schemas/X\n\t\t\tif (schema.$ref.startsWith('#/$defs/')) {\n\t\t\t\tconst refName = schema.$ref.replace('#/$defs/', '');\n\t\t\t\treturn componentCollector\n\t\t\t\t\t? componentCollector.getReference(refName)\n\t\t\t\t\t: schema;\n\t\t\t}\n\t\t\treturn schema;\n\t\t}\n\n\t\t// Handle arrays\n\t\tif (Array.isArray(schema)) {\n\t\t\treturn schema.map(processSchema);\n\t\t}\n\n\t\t// Process all properties recursively\n\t\tconst processed: any = {};\n\t\tfor (const [key, value] of Object.entries(schema)) {\n\t\t\
|
|
1
|
+
{"version":3,"file":"conversion.mjs","names":[],"sources":["../src/conversion.ts"],"sourcesContent":["import type { StandardSchemaV1 } from '@standard-schema/spec';\n\nexport enum SchemaVendor {\n\tzod = 'zod',\n\tvalibot = 'valibot',\n}\n\nexport type VendorConvertor = (schema: any) => Promise<any>;\nexport type VendorConvertors = Record<SchemaVendor, VendorConvertor>;\n\nfunction isSchemaVendor(vendor?: string): vendor is SchemaVendor {\n\tif (!vendor) {\n\t\treturn false;\n\t}\n\n\treturn Object.values(SchemaVendor).includes(vendor as SchemaVendor);\n}\n\nexport const StandardSchemaJsonSchema: VendorConvertors = {\n\tzod: async (schema): Promise<any> => {\n\t\ttry {\n\t\t\tconst { z } = await import('zod/v4').catch(() => ({ z: {} }));\n\n\t\t\tif ('toJSONSchema' in z && typeof z.toJSONSchema === 'function') {\n\t\t\t\treturn z.toJSONSchema(schema);\n\t\t\t}\n\t\t\tconst { zodToJsonSchema } = await import('zod-to-json-schema');\n\n\t\t\tconst result = zodToJsonSchema(schema, {\n\t\t\t\tremoveAdditionalStrategy: 'strict',\n\t\t\t});\n\n\t\t\treturn result;\n\t\t} catch {\n\t\t\t// Fallback to basic conversion if zod-to-json-schema is not available\n\t\t\treturn { type: 'object' };\n\t\t}\n\t},\n\tvalibot: async (schema): Promise<any> => {\n\t\tconst { toJsonSchema } = await import('@valibot/to-json-schema');\n\t\treturn toJsonSchema(schema as any);\n\t},\n};\n\nfunction extractAndConvertDefs(\n\tjsonSchema: any,\n\tcomponentCollector?: {\n\t\taddSchema(id: string, schema: any): void;\n\t\tgetReference(id: string): { $ref: string };\n\t},\n): any {\n\tif (!jsonSchema || typeof jsonSchema !== 'object') {\n\t\treturn jsonSchema;\n\t}\n\n\t// Process the schema recursively to update references\n\tconst processSchema = (schema: any): any => {\n\t\tif (!schema || typeof schema !== 'object') {\n\t\t\treturn schema;\n\t\t}\n\n\t\t// Handle $ref\n\t\tif (schema.$ref && typeof schema.$ref === 'string') {\n\t\t\t// Convert #/$defs/X to #/components/schemas/X\n\t\t\tif (schema.$ref.startsWith('#/$defs/')) {\n\t\t\t\tconst refName = schema.$ref.replace('#/$defs/', '');\n\t\t\t\treturn componentCollector\n\t\t\t\t\t? componentCollector.getReference(refName)\n\t\t\t\t\t: schema;\n\t\t\t}\n\t\t\treturn schema;\n\t\t}\n\n\t\t// Handle arrays\n\t\tif (Array.isArray(schema)) {\n\t\t\treturn schema.map(processSchema);\n\t\t}\n\n\t\t// Process all properties recursively\n\t\tconst processed: any = {};\n\t\tfor (const [key, value] of Object.entries(schema)) {\n\t\t\t// `$defs` has been extracted. `$schema` names the dialect the converter\n\t\t\t// wrote, which the document declares once for every schema in it; a\n\t\t\t// copy on each one is noise the document does not need.\n\t\t\tif (key === '$defs' || key === '$schema') {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tprocessed[key] = processSchema(value);\n\t\t}\n\t\treturn processed;\n\t};\n\n\t// Extract $defs if present\n\tif (jsonSchema.$defs && componentCollector) {\n\t\tfor (const [defName, defSchema] of Object.entries(jsonSchema.$defs)) {\n\t\t\t// Process the definition recursively to handle nested $refs\n\t\t\tconst processedDefSchema = processSchema(defSchema);\n\t\t\t// Add each definition to the component collector\n\t\t\tcomponentCollector.addSchema(defName, processedDefSchema);\n\t\t}\n\t}\n\n\t// Process the schema and remove $defs\n\tconst { $defs, ...schemaWithoutDefs } = jsonSchema;\n\treturn processSchema(schemaWithoutDefs);\n}\n\nexport async function convertStandardSchemaToJsonSchema(\n\tschema?: StandardSchemaV1,\n\tcomponentCollector?: {\n\t\taddSchema(id: string, schema: any): void;\n\t\tgetReference(id: string): { $ref: string };\n\t},\n): Promise<any> {\n\tif (!schema) {\n\t\treturn undefined;\n\t}\n\n\tconst vendor = schema['~standard']?.vendor;\n\tif (!isSchemaVendor(vendor)) {\n\t\tthrow new Error(\n\t\t\t`Unsupported or missing vendor \"${vendor}\" for Standard Schema. Supported vendors are: ${Object.keys(StandardSchemaJsonSchema).join(', ')}`,\n\t\t);\n\t}\n\n\tconst toJSONSchema = StandardSchemaJsonSchema[vendor];\n\tconst jsonSchema = await toJSONSchema(schema);\n\n\t// Extract and convert $defs to components\n\treturn extractAndConvertDefs(jsonSchema, componentCollector);\n}\n\nexport async function getZodMetadata(\n\tschema: StandardSchemaV1,\n): Promise<SchemaMeta | undefined> {\n\tconst { ZodObject } = await import('zod/v4');\n\n\tif (schema instanceof ZodObject) {\n\t\treturn schema.meta();\n\t}\n\n\treturn undefined;\n}\n\n/**\n * Return JSON Schema for every schema registered in zod v4's global registry\n * via `.meta({ id })`. Returns an empty object when zod v4 is unavailable or\n * no schemas are registered.\n *\n * `unrepresentable: 'any'` keeps a single unrepresentable schema from\n * collapsing the whole result.\n */\nexport async function getRegisteredZodJsonSchemas(): Promise<\n\tRecord<string, any>\n> {\n\ttry {\n\t\tconst { z } = await import('zod/v4').catch(() => ({ z: null as any }));\n\t\tif (!z?.toJSONSchema || !z.globalRegistry) {\n\t\t\treturn {};\n\t\t}\n\t\tconst result = z.toJSONSchema(z.globalRegistry, {\n\t\t\tunrepresentable: 'any',\n\t\t});\n\t\treturn (result?.schemas ?? {}) as Record<string, any>;\n\t} catch {\n\t\treturn {};\n\t}\n}\n\nexport async function getSchemaMetadata(\n\tschema: StandardSchemaV1,\n): Promise<SchemaMeta | undefined> {\n\tconst vendor = schema['~standard']?.vendor;\n\n\tif (vendor === 'zod') {\n\t\treturn getZodMetadata(schema);\n\t}\n\n\treturn undefined;\n}\n\ninterface SchemaMeta {\n\tid?: string;\n}\n\nexport async function convertSchemaWithComponents(\n\tschema: StandardSchemaV1 | undefined,\n\tcomponentCollector?: {\n\t\taddSchema(id: string, schema: any): void;\n\t\tgetReference(id: string): { $ref: string };\n\t},\n): Promise<any> {\n\tif (!schema) {\n\t\treturn undefined;\n\t}\n\n\t// Convert to JSON Schema with component collector to handle $defs\n\tconst jsonSchema = await convertStandardSchemaToJsonSchema(\n\t\tschema,\n\t\tcomponentCollector,\n\t);\n\n\tif (!componentCollector) {\n\t\treturn jsonSchema;\n\t}\n\n\t// Check if this schema has metadata with an ID\n\tconst metadata = await getSchemaMetadata(schema);\n\n\t// Also check if the JSON Schema itself has an id field (from Zod's meta)\n\tconst schemaId = metadata?.id || jsonSchema?.id;\n\n\tif (schemaId) {\n\t\t// Zod 4.6 already turns a registered schema into a `$ref` to its own\n\t\t// `$defs` entry, which the conversion above moved into components. Adding\n\t\t// that reference under the same id would replace the definition with a\n\t\t// pointer to itself — `User: { $ref: '#/components/schemas/User' }` — and\n\t\t// leave the document with no User at all.\n\t\tif (typeof jsonSchema?.$ref === 'string') {\n\t\t\treturn componentCollector.getReference(schemaId);\n\t\t}\n\t\t// Remove the id from the schema before adding to components\n\t\tconst { id, ...schemaWithoutId } = jsonSchema;\n\t\t// Add this schema to components and return a reference\n\t\tcomponentCollector.addSchema(schemaId, schemaWithoutId);\n\t\treturn componentCollector.getReference(schemaId);\n\t}\n\n\treturn jsonSchema;\n}\n"],"mappings":";AAEA,IAAY,eAAL,yBAAA,cAAA;CACN,aAAA,SAAA;CACA,aAAA,aAAA;;AACD,EAAA,CAAA,CAAA;AAKA,SAAS,eAAe,QAAyC;CAChE,IAAI,CAAC,QACJ,OAAO;CAGR,OAAO,OAAO,OAAO,YAAY,CAAC,CAAC,SAAS,MAAsB;AACnE;AAEA,MAAa,2BAA6C;CACzD,KAAK,OAAO,WAAyB;EACpC,IAAI;GACH,MAAM,EAAE,MAAM,MAAM,OAAO,SAAS,CAAC,aAAa,EAAE,GAAG,CAAC,EAAE,EAAE;GAE5D,IAAI,kBAAkB,KAAK,OAAO,EAAE,iBAAiB,YACpD,OAAO,EAAE,aAAa,MAAM;GAE7B,MAAM,EAAE,oBAAoB,MAAM,OAAO;GAMzC,OAJe,gBAAgB,QAAQ,EACtC,0BAA0B,SAC3B,CAEY;EACb,QAAQ;GAEP,OAAO,EAAE,MAAM,SAAS;EACzB;CACD;CACA,SAAS,OAAO,WAAyB;EACxC,MAAM,EAAE,iBAAiB,MAAM,OAAO;EACtC,OAAO,aAAa,MAAa;CAClC;AACD;AAEA,SAAS,sBACR,YACA,oBAIM;CACN,IAAI,CAAC,cAAc,OAAO,eAAe,UACxC,OAAO;CAIR,MAAM,iBAAiB,WAAqB;EAC3C,IAAI,CAAC,UAAU,OAAO,WAAW,UAChC,OAAO;EAIR,IAAI,OAAO,QAAQ,OAAO,OAAO,SAAS,UAAU;GAEnD,IAAI,OAAO,KAAK,WAA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aAAa,EAAE,gBAAgB,EAC/C,iBAAiB,MAClB,CACa,CAAC,EAAE,WAAW,CAAC;CAC7B,QAAQ;EACP,OAAO,CAAC;CACT;AACD;AAEA,eAAsB,kBACrB,QACkC;CAGlC,IAFe,OAAO,YAAY,EAAE,WAErB,OACd,OAAO,eAAe,MAAM;AAI9B;AAMA,eAAsB,4BACrB,QACA,oBAIe;CACf,IAAI,CAAC,QACJ;CAID,MAAM,aAAa,MAAM,kCACxB,QACA,kBACD;CAEA,IAAI,CAAC,oBACJ,OAAO;CAOR,MAAM,YAAW,MAHM,kBAAkB,MAAM,EAAA,EAGpB,MAAM,YAAY;CAE7C,IAAI,UAAU;EAMb,IAAI,OAAO,YAAY,SAAS,UAC/B,OAAO,mBAAmB,aAAa,QAAQ;EAGhD,MAAM,EAAE,IAAI,GAAG,oBAAoB;EAEnC,mBAAmB,UAAU,UAAU,eAAe;EACtD,OAAO,mBAAmB,aAAa,QAAQ;CAChD;CAEA,OAAO;AACR"}
|
package/dist/openapi.cjs
CHANGED
|
@@ -2,11 +2,16 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
2
2
|
//#region src/openapi.ts
|
|
3
3
|
function createComponentCollector() {
|
|
4
4
|
const schemas = {};
|
|
5
|
+
const securitySchemes = {};
|
|
5
6
|
return {
|
|
6
7
|
schemas,
|
|
8
|
+
securitySchemes,
|
|
7
9
|
addSchema(id, schema) {
|
|
8
10
|
schemas[id] = schema;
|
|
9
11
|
},
|
|
12
|
+
addSecurityScheme(name, scheme) {
|
|
13
|
+
securitySchemes[name] = scheme;
|
|
14
|
+
},
|
|
10
15
|
getReference(id) {
|
|
11
16
|
return { $ref: `#/components/schemas/${id}` };
|
|
12
17
|
}
|
|
@@ -30,7 +35,7 @@ async function buildOpenApiSchema(endpoints, options = {}) {
|
|
|
30
35
|
}
|
|
31
36
|
}
|
|
32
37
|
const doc = {
|
|
33
|
-
openapi: "3.
|
|
38
|
+
openapi: "3.1.0",
|
|
34
39
|
info: {
|
|
35
40
|
title,
|
|
36
41
|
version,
|
|
@@ -38,7 +43,11 @@ async function buildOpenApiSchema(endpoints, options = {}) {
|
|
|
38
43
|
},
|
|
39
44
|
paths
|
|
40
45
|
};
|
|
41
|
-
|
|
46
|
+
const { schemas, securitySchemes } = componentCollector;
|
|
47
|
+
if (Object.keys(schemas).length > 0 || Object.keys(securitySchemes).length > 0) doc.components = {
|
|
48
|
+
...Object.keys(schemas).length > 0 && { schemas },
|
|
49
|
+
...Object.keys(securitySchemes).length > 0 && { securitySchemes }
|
|
50
|
+
};
|
|
42
51
|
return doc;
|
|
43
52
|
}
|
|
44
53
|
//#endregion
|
package/dist/openapi.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openapi.cjs","names":[],"sources":["../src/openapi.ts"],"sourcesContent":["import type { OpenAPIV3_1 } from 'openapi-types';\n\nexport interface OpenApiSchemaOptions {\n\ttitle?: string;\n\tversion?: string;\n\tdescription?: string;\n}\n\nexport interface ComponentCollector {\n\tschemas: Record<string, OpenAPIV3_1.SchemaObject>;\n\taddSchema(id: string, schema: OpenAPIV3_1.SchemaObject): void;\n\tgetReference(id: string): OpenAPIV3_1.ReferenceObject;\n}\n\nexport function createComponentCollector(): ComponentCollector {\n\tconst schemas: Record<string, OpenAPIV3_1.SchemaObject> = {};\n\n\treturn {\n\t\tschemas,\n\t\taddSchema(id: string, schema: OpenAPIV3_1.SchemaObject) {\n\t\t\tschemas[id] = schema;\n\t\t},\n\t\tgetReference(id: string): OpenAPIV3_1.ReferenceObject {\n\t\t\treturn { $ref: `#/components/schemas/${id}` };\n\t\t},\n\t};\n}\n\n/**\n * Builds OpenAPI 3.1 schema from an array of endpoints.\n *\n * Note: This function requires endpoints with toOpenApi3Route method.\n * The actual implementation is in @geekmidas/constructs to avoid circular dependencies.\n */\nexport async function buildOpenApiSchema(\n\tendpoints: Array<{\n\t\ttoOpenApi3Route(collector?: ComponentCollector): Promise<any>;\n\t}>,\n\toptions: OpenApiSchemaOptions = {},\n): Promise<OpenAPIV3_1.Document> {\n\tconst { title = 'API', version = '1.0.0', description } = options;\n\tconst paths: OpenAPIV3_1.PathsObject = {};\n\tconst componentCollector = createComponentCollector();\n\n\tfor (const endpoint of endpoints) {\n\t\tconst route = await endpoint.toOpenApi3Route(componentCollector);\n\n\t\t// Merge the route into the paths object\n\t\tfor (const [path, methods] of Object.entries(route)) {\n\t\t\tif (!paths[path]) {\n\t\t\t\tpaths[path] = {};\n\t\t\t}\n\t\t\tObject.assign(paths[path], methods);\n\t\t}\n\t}\n\n\tconst doc: OpenAPIV3_1.Document = {\n\t\topenapi: '3.
|
|
1
|
+
{"version":3,"file":"openapi.cjs","names":[],"sources":["../src/openapi.ts"],"sourcesContent":["import type { OpenAPIV3_1 } from 'openapi-types';\n\nexport interface OpenApiSchemaOptions {\n\ttitle?: string;\n\tversion?: string;\n\tdescription?: string;\n}\n\nexport interface ComponentCollector {\n\tschemas: Record<string, OpenAPIV3_1.SchemaObject>;\n\t/** The schemes operations name in their `security` requirements. */\n\tsecuritySchemes: Record<string, OpenAPIV3_1.SecuritySchemeObject>;\n\taddSchema(id: string, schema: OpenAPIV3_1.SchemaObject): void;\n\taddSecurityScheme(\n\t\tname: string,\n\t\tscheme: OpenAPIV3_1.SecuritySchemeObject,\n\t): void;\n\tgetReference(id: string): OpenAPIV3_1.ReferenceObject;\n}\n\nexport function createComponentCollector(): ComponentCollector {\n\tconst schemas: Record<string, OpenAPIV3_1.SchemaObject> = {};\n\tconst securitySchemes: Record<string, OpenAPIV3_1.SecuritySchemeObject> = {};\n\n\treturn {\n\t\tschemas,\n\t\tsecuritySchemes,\n\t\taddSchema(id: string, schema: OpenAPIV3_1.SchemaObject) {\n\t\t\tschemas[id] = schema;\n\t\t},\n\t\taddSecurityScheme(name, scheme) {\n\t\t\tsecuritySchemes[name] = scheme;\n\t\t},\n\t\tgetReference(id: string): OpenAPIV3_1.ReferenceObject {\n\t\t\treturn { $ref: `#/components/schemas/${id}` };\n\t\t},\n\t};\n}\n\n/**\n * Builds OpenAPI 3.1 schema from an array of endpoints.\n *\n * Note: This function requires endpoints with toOpenApi3Route method.\n * The actual implementation is in @geekmidas/constructs to avoid circular dependencies.\n */\nexport async function buildOpenApiSchema(\n\tendpoints: Array<{\n\t\ttoOpenApi3Route(collector?: ComponentCollector): Promise<any>;\n\t}>,\n\toptions: OpenApiSchemaOptions = {},\n): Promise<OpenAPIV3_1.Document> {\n\tconst { title = 'API', version = '1.0.0', description } = options;\n\tconst paths: OpenAPIV3_1.PathsObject = {};\n\tconst componentCollector = createComponentCollector();\n\n\tfor (const endpoint of endpoints) {\n\t\tconst route = await endpoint.toOpenApi3Route(componentCollector);\n\n\t\t// Merge the route into the paths object\n\t\tfor (const [path, methods] of Object.entries(route)) {\n\t\t\tif (!paths[path]) {\n\t\t\t\tpaths[path] = {};\n\t\t\t}\n\t\t\tObject.assign(paths[path], methods);\n\t\t}\n\t}\n\n\tconst doc: OpenAPIV3_1.Document = {\n\t\t// 3.1, not 3.0: the schemas are JSON Schema 2020-12 — what Zod 4 and\n\t\t// Valibot emit — and 3.1 is the version whose schema objects are that\n\t\t// dialect. Declared 3.0, `type: ['string', 'null']`, `const` and\n\t\t// `prefixItems` were all invalid there.\n\t\topenapi: '3.1.0',\n\t\tinfo: {\n\t\t\ttitle,\n\t\t\tversion,\n\t\t\t...(description && { description }),\n\t\t},\n\t\tpaths,\n\t};\n\n\t// Add components if any schemas or security schemes were collected. A\n\t// `security` requirement naming a scheme the document does not define is\n\t// invalid, so the two travel together.\n\tconst { schemas, securitySchemes } = componentCollector;\n\tif (\n\t\tObject.keys(schemas).length > 0 ||\n\t\tObject.keys(securitySchemes).length > 0\n\t) {\n\t\tdoc.components = {\n\t\t\t...(Object.keys(schemas).length > 0 && { schemas }),\n\t\t\t...(Object.keys(securitySchemes).length > 0 && { securitySchemes }),\n\t\t};\n\t}\n\n\treturn doc;\n}\n"],"mappings":";;AAoBA,SAAgB,2BAA+C;CAC9D,MAAM,UAAoD,CAAC;CAC3D,MAAM,kBAAoE,CAAC;CAE3E,OAAO;EACN;EACA;EACA,UAAU,IAAY,QAAkC;GACvD,QAAQ,MAAM;EACf;EACA,kBAAkB,MAAM,QAAQ;GAC/B,gBAAgB,QAAQ;EACzB;EACA,aAAa,IAAyC;GACrD,OAAO,EAAE,MAAM,wBAAwB,KAAK;EAC7C;CACD;AACD;;;;;;;AAQA,eAAsB,mBACrB,WAGA,UAAgC,CAAC,GACD;CAChC,MAAM,EAAE,QAAQ,OAAO,UAAU,SAAS,gBAAgB;CAC1D,MAAM,QAAiC,CAAC;CACxC,MAAM,qBAAqB,yBAAyB;CAEpD,KAAK,MAAM,YAAY,WAAW;EACjC,MAAM,QAAQ,MAAM,SAAS,gBAAgB,kBAAkB;EAG/D,KAAK,MAAM,CAAC,MAAM,YAAY,OAAO,QAAQ,KAAK,GAAG;GACpD,IAAI,CAAC,MAAM,OACV,MAAM,QAAQ,CAAC;GAEhB,OAAO,OAAO,MAAM,OAAO,OAAO;EACnC;CACD;CAEA,MAAM,MAA4B;EAKjC,SAAS;EACT,MAAM;GACL;GACA;GACA,GAAI,eAAe,EAAE,YAAY;EAClC;EACA;CACD;CAKA,MAAM,EAAE,SAAS,oBAAoB;CACrC,IACC,OAAO,KAAK,OAAO,CAAC,CAAC,SAAS,KAC9B,OAAO,KAAK,eAAe,CAAC,CAAC,SAAS,GAEtC,IAAI,aAAa;EAChB,GAAI,OAAO,KAAK,OAAO,CAAC,CAAC,SAAS,KAAK,EAAE,QAAQ;EACjD,GAAI,OAAO,KAAK,eAAe,CAAC,CAAC,SAAS,KAAK,EAAE,gBAAgB;CAClE;CAGD,OAAO;AACR"}
|
package/dist/openapi.d.cts
CHANGED
|
@@ -7,7 +7,10 @@ export interface OpenApiSchemaOptions {
|
|
|
7
7
|
}
|
|
8
8
|
export interface ComponentCollector {
|
|
9
9
|
schemas: Record<string, OpenAPIV3_1.SchemaObject>;
|
|
10
|
+
/** The schemes operations name in their `security` requirements. */
|
|
11
|
+
securitySchemes: Record<string, OpenAPIV3_1.SecuritySchemeObject>;
|
|
10
12
|
addSchema(id: string, schema: OpenAPIV3_1.SchemaObject): void;
|
|
13
|
+
addSecurityScheme(name: string, scheme: OpenAPIV3_1.SecuritySchemeObject): void;
|
|
11
14
|
getReference(id: string): OpenAPIV3_1.ReferenceObject;
|
|
12
15
|
}
|
|
13
16
|
export declare function createComponentCollector(): ComponentCollector;
|
package/dist/openapi.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openapi.d.cts","names":[],"sources":["../src/openapi.ts"],"mappings":";;iBAEiB;EAChB;EACA;EACA;;iBAGgB;EAChB,SAAS,eAAe,YAAY;
|
|
1
|
+
{"version":3,"file":"openapi.d.cts","names":[],"sources":["../src/openapi.ts"],"mappings":";;iBAEiB;EAChB;EACA;EACA;;iBAGgB;EAChB,SAAS,eAAe,YAAY;;EAEpC,iBAAiB,eAAe,YAAY;EAC5C,UAAU,YAAY,QAAQ,YAAY;EAC1C,kBACC,cACA,QAAQ,YAAY;EAErB,aAAa,aAAa,YAAY;;wBAGvB,4BAA4B;;;;;;;wBAyBtB,mBACrB,WAAW;EACV,gBAAgB,YAAY,qBAAqB;IAElD,UAAS,uBACP,QAAQ,YAAY"}
|
package/dist/openapi.d.mts
CHANGED
|
@@ -7,7 +7,10 @@ export interface OpenApiSchemaOptions {
|
|
|
7
7
|
}
|
|
8
8
|
export interface ComponentCollector {
|
|
9
9
|
schemas: Record<string, OpenAPIV3_1.SchemaObject>;
|
|
10
|
+
/** The schemes operations name in their `security` requirements. */
|
|
11
|
+
securitySchemes: Record<string, OpenAPIV3_1.SecuritySchemeObject>;
|
|
10
12
|
addSchema(id: string, schema: OpenAPIV3_1.SchemaObject): void;
|
|
13
|
+
addSecurityScheme(name: string, scheme: OpenAPIV3_1.SecuritySchemeObject): void;
|
|
11
14
|
getReference(id: string): OpenAPIV3_1.ReferenceObject;
|
|
12
15
|
}
|
|
13
16
|
export declare function createComponentCollector(): ComponentCollector;
|
package/dist/openapi.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openapi.d.mts","names":[],"sources":["../src/openapi.ts"],"mappings":";;iBAEiB;EAChB;EACA;EACA;;iBAGgB;EAChB,SAAS,eAAe,YAAY;
|
|
1
|
+
{"version":3,"file":"openapi.d.mts","names":[],"sources":["../src/openapi.ts"],"mappings":";;iBAEiB;EAChB;EACA;EACA;;iBAGgB;EAChB,SAAS,eAAe,YAAY;;EAEpC,iBAAiB,eAAe,YAAY;EAC5C,UAAU,YAAY,QAAQ,YAAY;EAC1C,kBACC,cACA,QAAQ,YAAY;EAErB,aAAa,aAAa,YAAY;;wBAGvB,4BAA4B;;;;;;;wBAyBtB,mBACrB,WAAW;EACV,gBAAgB,YAAY,qBAAqB;IAElD,UAAS,uBACP,QAAQ,YAAY"}
|
package/dist/openapi.mjs
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
//#region src/openapi.ts
|
|
2
2
|
function createComponentCollector() {
|
|
3
3
|
const schemas = {};
|
|
4
|
+
const securitySchemes = {};
|
|
4
5
|
return {
|
|
5
6
|
schemas,
|
|
7
|
+
securitySchemes,
|
|
6
8
|
addSchema(id, schema) {
|
|
7
9
|
schemas[id] = schema;
|
|
8
10
|
},
|
|
11
|
+
addSecurityScheme(name, scheme) {
|
|
12
|
+
securitySchemes[name] = scheme;
|
|
13
|
+
},
|
|
9
14
|
getReference(id) {
|
|
10
15
|
return { $ref: `#/components/schemas/${id}` };
|
|
11
16
|
}
|
|
@@ -29,7 +34,7 @@ async function buildOpenApiSchema(endpoints, options = {}) {
|
|
|
29
34
|
}
|
|
30
35
|
}
|
|
31
36
|
const doc = {
|
|
32
|
-
openapi: "3.
|
|
37
|
+
openapi: "3.1.0",
|
|
33
38
|
info: {
|
|
34
39
|
title,
|
|
35
40
|
version,
|
|
@@ -37,7 +42,11 @@ async function buildOpenApiSchema(endpoints, options = {}) {
|
|
|
37
42
|
},
|
|
38
43
|
paths
|
|
39
44
|
};
|
|
40
|
-
|
|
45
|
+
const { schemas, securitySchemes } = componentCollector;
|
|
46
|
+
if (Object.keys(schemas).length > 0 || Object.keys(securitySchemes).length > 0) doc.components = {
|
|
47
|
+
...Object.keys(schemas).length > 0 && { schemas },
|
|
48
|
+
...Object.keys(securitySchemes).length > 0 && { securitySchemes }
|
|
49
|
+
};
|
|
41
50
|
return doc;
|
|
42
51
|
}
|
|
43
52
|
//#endregion
|
package/dist/openapi.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openapi.mjs","names":[],"sources":["../src/openapi.ts"],"sourcesContent":["import type { OpenAPIV3_1 } from 'openapi-types';\n\nexport interface OpenApiSchemaOptions {\n\ttitle?: string;\n\tversion?: string;\n\tdescription?: string;\n}\n\nexport interface ComponentCollector {\n\tschemas: Record<string, OpenAPIV3_1.SchemaObject>;\n\taddSchema(id: string, schema: OpenAPIV3_1.SchemaObject): void;\n\tgetReference(id: string): OpenAPIV3_1.ReferenceObject;\n}\n\nexport function createComponentCollector(): ComponentCollector {\n\tconst schemas: Record<string, OpenAPIV3_1.SchemaObject> = {};\n\n\treturn {\n\t\tschemas,\n\t\taddSchema(id: string, schema: OpenAPIV3_1.SchemaObject) {\n\t\t\tschemas[id] = schema;\n\t\t},\n\t\tgetReference(id: string): OpenAPIV3_1.ReferenceObject {\n\t\t\treturn { $ref: `#/components/schemas/${id}` };\n\t\t},\n\t};\n}\n\n/**\n * Builds OpenAPI 3.1 schema from an array of endpoints.\n *\n * Note: This function requires endpoints with toOpenApi3Route method.\n * The actual implementation is in @geekmidas/constructs to avoid circular dependencies.\n */\nexport async function buildOpenApiSchema(\n\tendpoints: Array<{\n\t\ttoOpenApi3Route(collector?: ComponentCollector): Promise<any>;\n\t}>,\n\toptions: OpenApiSchemaOptions = {},\n): Promise<OpenAPIV3_1.Document> {\n\tconst { title = 'API', version = '1.0.0', description } = options;\n\tconst paths: OpenAPIV3_1.PathsObject = {};\n\tconst componentCollector = createComponentCollector();\n\n\tfor (const endpoint of endpoints) {\n\t\tconst route = await endpoint.toOpenApi3Route(componentCollector);\n\n\t\t// Merge the route into the paths object\n\t\tfor (const [path, methods] of Object.entries(route)) {\n\t\t\tif (!paths[path]) {\n\t\t\t\tpaths[path] = {};\n\t\t\t}\n\t\t\tObject.assign(paths[path], methods);\n\t\t}\n\t}\n\n\tconst doc: OpenAPIV3_1.Document = {\n\t\topenapi: '3.
|
|
1
|
+
{"version":3,"file":"openapi.mjs","names":[],"sources":["../src/openapi.ts"],"sourcesContent":["import type { OpenAPIV3_1 } from 'openapi-types';\n\nexport interface OpenApiSchemaOptions {\n\ttitle?: string;\n\tversion?: string;\n\tdescription?: string;\n}\n\nexport interface ComponentCollector {\n\tschemas: Record<string, OpenAPIV3_1.SchemaObject>;\n\t/** The schemes operations name in their `security` requirements. */\n\tsecuritySchemes: Record<string, OpenAPIV3_1.SecuritySchemeObject>;\n\taddSchema(id: string, schema: OpenAPIV3_1.SchemaObject): void;\n\taddSecurityScheme(\n\t\tname: string,\n\t\tscheme: OpenAPIV3_1.SecuritySchemeObject,\n\t): void;\n\tgetReference(id: string): OpenAPIV3_1.ReferenceObject;\n}\n\nexport function createComponentCollector(): ComponentCollector {\n\tconst schemas: Record<string, OpenAPIV3_1.SchemaObject> = {};\n\tconst securitySchemes: Record<string, OpenAPIV3_1.SecuritySchemeObject> = {};\n\n\treturn {\n\t\tschemas,\n\t\tsecuritySchemes,\n\t\taddSchema(id: string, schema: OpenAPIV3_1.SchemaObject) {\n\t\t\tschemas[id] = schema;\n\t\t},\n\t\taddSecurityScheme(name, scheme) {\n\t\t\tsecuritySchemes[name] = scheme;\n\t\t},\n\t\tgetReference(id: string): OpenAPIV3_1.ReferenceObject {\n\t\t\treturn { $ref: `#/components/schemas/${id}` };\n\t\t},\n\t};\n}\n\n/**\n * Builds OpenAPI 3.1 schema from an array of endpoints.\n *\n * Note: This function requires endpoints with toOpenApi3Route method.\n * The actual implementation is in @geekmidas/constructs to avoid circular dependencies.\n */\nexport async function buildOpenApiSchema(\n\tendpoints: Array<{\n\t\ttoOpenApi3Route(collector?: ComponentCollector): Promise<any>;\n\t}>,\n\toptions: OpenApiSchemaOptions = {},\n): Promise<OpenAPIV3_1.Document> {\n\tconst { title = 'API', version = '1.0.0', description } = options;\n\tconst paths: OpenAPIV3_1.PathsObject = {};\n\tconst componentCollector = createComponentCollector();\n\n\tfor (const endpoint of endpoints) {\n\t\tconst route = await endpoint.toOpenApi3Route(componentCollector);\n\n\t\t// Merge the route into the paths object\n\t\tfor (const [path, methods] of Object.entries(route)) {\n\t\t\tif (!paths[path]) {\n\t\t\t\tpaths[path] = {};\n\t\t\t}\n\t\t\tObject.assign(paths[path], methods);\n\t\t}\n\t}\n\n\tconst doc: OpenAPIV3_1.Document = {\n\t\t// 3.1, not 3.0: the schemas are JSON Schema 2020-12 — what Zod 4 and\n\t\t// Valibot emit — and 3.1 is the version whose schema objects are that\n\t\t// dialect. Declared 3.0, `type: ['string', 'null']`, `const` and\n\t\t// `prefixItems` were all invalid there.\n\t\topenapi: '3.1.0',\n\t\tinfo: {\n\t\t\ttitle,\n\t\t\tversion,\n\t\t\t...(description && { description }),\n\t\t},\n\t\tpaths,\n\t};\n\n\t// Add components if any schemas or security schemes were collected. A\n\t// `security` requirement naming a scheme the document does not define is\n\t// invalid, so the two travel together.\n\tconst { schemas, securitySchemes } = componentCollector;\n\tif (\n\t\tObject.keys(schemas).length > 0 ||\n\t\tObject.keys(securitySchemes).length > 0\n\t) {\n\t\tdoc.components = {\n\t\t\t...(Object.keys(schemas).length > 0 && { schemas }),\n\t\t\t...(Object.keys(securitySchemes).length > 0 && { securitySchemes }),\n\t\t};\n\t}\n\n\treturn doc;\n}\n"],"mappings":";AAoBA,SAAgB,2BAA+C;CAC9D,MAAM,UAAoD,CAAC;CAC3D,MAAM,kBAAoE,CAAC;CAE3E,OAAO;EACN;EACA;EACA,UAAU,IAAY,QAAkC;GACvD,QAAQ,MAAM;EACf;EACA,kBAAkB,MAAM,QAAQ;GAC/B,gBAAgB,QAAQ;EACzB;EACA,aAAa,IAAyC;GACrD,OAAO,EAAE,MAAM,wBAAwB,KAAK;EAC7C;CACD;AACD;;;;;;;AAQA,eAAsB,mBACrB,WAGA,UAAgC,CAAC,GACD;CAChC,MAAM,EAAE,QAAQ,OAAO,UAAU,SAAS,gBAAgB;CAC1D,MAAM,QAAiC,CAAC;CACxC,MAAM,qBAAqB,yBAAyB;CAEpD,KAAK,MAAM,YAAY,WAAW;EACjC,MAAM,QAAQ,MAAM,SAAS,gBAAgB,kBAAkB;EAG/D,KAAK,MAAM,CAAC,MAAM,YAAY,OAAO,QAAQ,KAAK,GAAG;GACpD,IAAI,CAAC,MAAM,OACV,MAAM,QAAQ,CAAC;GAEhB,OAAO,OAAO,MAAM,OAAO,OAAO;EACnC;CACD;CAEA,MAAM,MAA4B;EAKjC,SAAS;EACT,MAAM;GACL;GACA;GACA,GAAI,eAAe,EAAE,YAAY;EAClC;EACA;CACD;CAKA,MAAM,EAAE,SAAS,oBAAoB;CACrC,IACC,OAAO,KAAK,OAAO,CAAC,CAAC,SAAS,KAC9B,OAAO,KAAK,eAAe,CAAC,CAAC,SAAS,GAEtC,IAAI,aAAa;EAChB,GAAI,OAAO,KAAK,OAAO,CAAC,CAAC,SAAS,KAAK,EAAE,QAAQ;EACjD,GAAI,OAAO,KAAK,eAAe,CAAC,CAAC,SAAS,KAAK,EAAE,gBAAgB;CAClE;CAGD,OAAO;AACR"}
|