@geekmidas/schema 10.0.0-alpha.4 → 10.0.0-alpha.41
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 +106 -8
- package/dist/conversion.cjs.map +1 -0
- package/dist/conversion.d.cts +37 -2
- package/dist/conversion.d.cts.map +1 -0
- package/dist/conversion.d.mts +37 -2
- package/dist/conversion.d.mts.map +1 -0
- package/dist/conversion.mjs +99 -2
- package/dist/conversion.mjs.map +1 -0
- package/dist/index.cjs +4 -4
- package/dist/index.d.cts +4 -4
- package/dist/index.d.mts +4 -4
- package/dist/index.mjs +3 -4
- package/dist/openapi.cjs +56 -3
- package/dist/openapi.cjs.map +1 -0
- package/dist/openapi.d.cts +27 -2
- package/dist/openapi.d.cts.map +1 -0
- package/dist/openapi.d.mts +27 -2
- package/dist/openapi.d.mts.map +1 -0
- package/dist/openapi.mjs +54 -2
- package/dist/openapi.mjs.map +1 -0
- package/dist/parser.cjs +3 -3
- package/dist/parser.cjs.map +1 -1
- package/dist/parser.d.cts +3 -8
- package/dist/parser.d.cts.map +1 -1
- package/dist/parser.d.mts +3 -8
- package/dist/parser.d.mts.map +1 -1
- package/dist/parser.mjs +2 -2
- package/dist/parser.mjs.map +1 -1
- package/dist/types.d.cts +22 -2
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.mts +22 -2
- package/dist/types.d.mts.map +1 -0
- package/dist/types.mjs +1 -0
- package/package.json +9 -6
- package/dist/conversion-CIBtqJkS.mjs +0 -106
- package/dist/conversion-CIBtqJkS.mjs.map +0 -1
- package/dist/conversion-Cf9R-jGc.cjs +0 -148
- package/dist/conversion-Cf9R-jGc.cjs.map +0 -1
- package/dist/conversion-JY1Wixil.d.cts +0 -39
- package/dist/conversion-JY1Wixil.d.cts.map +0 -1
- package/dist/conversion-e4k9rTxy.d.mts +0 -39
- package/dist/conversion-e4k9rTxy.d.mts.map +0 -1
- package/dist/openapi-Aeb_1e9T.d.cts +0 -28
- package/dist/openapi-Aeb_1e9T.d.cts.map +0 -1
- package/dist/openapi-DH5yCqKh.mjs +0 -46
- package/dist/openapi-DH5yCqKh.mjs.map +0 -1
- package/dist/openapi-DVvLYx-8.cjs +0 -58
- package/dist/openapi-DVvLYx-8.cjs.map +0 -1
- package/dist/openapi-Dxc4FuMP.d.mts +0 -28
- package/dist/openapi-Dxc4FuMP.d.mts.map +0 -1
- package/dist/types-Bm3VhqVE.d.cts +0 -26
- package/dist/types-Bm3VhqVE.d.cts.map +0 -1
- package/dist/types-xm7EGhin.d.mts +0 -26
- package/dist/types-xm7EGhin.d.mts.map +0 -1
package/dist/conversion.cjs
CHANGED
|
@@ -1,9 +1,107 @@
|
|
|
1
|
-
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/conversion.ts
|
|
3
|
+
let SchemaVendor = /* @__PURE__ */ function(SchemaVendor) {
|
|
4
|
+
SchemaVendor["zod"] = "zod";
|
|
5
|
+
SchemaVendor["valibot"] = "valibot";
|
|
6
|
+
return SchemaVendor;
|
|
7
|
+
}({});
|
|
8
|
+
function isSchemaVendor(vendor) {
|
|
9
|
+
if (!vendor) return false;
|
|
10
|
+
return Object.values(SchemaVendor).includes(vendor);
|
|
11
|
+
}
|
|
12
|
+
const StandardSchemaJsonSchema = {
|
|
13
|
+
zod: async (schema) => {
|
|
14
|
+
try {
|
|
15
|
+
const { z } = await import("zod/v4").catch(() => ({ z: {} }));
|
|
16
|
+
if ("toJSONSchema" in z && typeof z.toJSONSchema === "function") return z.toJSONSchema(schema);
|
|
17
|
+
const { zodToJsonSchema } = await import("zod-to-json-schema");
|
|
18
|
+
return zodToJsonSchema(schema, { removeAdditionalStrategy: "strict" });
|
|
19
|
+
} catch {
|
|
20
|
+
return { type: "object" };
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
valibot: async (schema) => {
|
|
24
|
+
const { toJsonSchema } = await import("@valibot/to-json-schema");
|
|
25
|
+
return toJsonSchema(schema);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
function extractAndConvertDefs(jsonSchema, componentCollector) {
|
|
29
|
+
if (!jsonSchema || typeof jsonSchema !== "object") return jsonSchema;
|
|
30
|
+
const processSchema = (schema) => {
|
|
31
|
+
if (!schema || typeof schema !== "object") return schema;
|
|
32
|
+
if (schema.$ref && typeof schema.$ref === "string") {
|
|
33
|
+
if (schema.$ref.startsWith("#/$defs/")) {
|
|
34
|
+
const refName = schema.$ref.replace("#/$defs/", "");
|
|
35
|
+
return componentCollector ? componentCollector.getReference(refName) : schema;
|
|
36
|
+
}
|
|
37
|
+
return schema;
|
|
38
|
+
}
|
|
39
|
+
if (Array.isArray(schema)) return schema.map(processSchema);
|
|
40
|
+
const processed = {};
|
|
41
|
+
for (const [key, value] of Object.entries(schema)) {
|
|
42
|
+
if (key === "$defs" || key === "$schema") continue;
|
|
43
|
+
processed[key] = processSchema(value);
|
|
44
|
+
}
|
|
45
|
+
return processed;
|
|
46
|
+
};
|
|
47
|
+
if (jsonSchema.$defs && componentCollector) for (const [defName, defSchema] of Object.entries(jsonSchema.$defs)) {
|
|
48
|
+
const processedDefSchema = processSchema(defSchema);
|
|
49
|
+
componentCollector.addSchema(defName, processedDefSchema);
|
|
50
|
+
}
|
|
51
|
+
const { $defs, ...schemaWithoutDefs } = jsonSchema;
|
|
52
|
+
return processSchema(schemaWithoutDefs);
|
|
53
|
+
}
|
|
54
|
+
async function convertStandardSchemaToJsonSchema(schema, componentCollector) {
|
|
55
|
+
if (!schema) return;
|
|
56
|
+
const vendor = schema["~standard"]?.vendor;
|
|
57
|
+
if (!isSchemaVendor(vendor)) throw new Error(`Unsupported or missing vendor "${vendor}" for Standard Schema. Supported vendors are: ${Object.keys(StandardSchemaJsonSchema).join(", ")}`);
|
|
58
|
+
const toJSONSchema = StandardSchemaJsonSchema[vendor];
|
|
59
|
+
return extractAndConvertDefs(await toJSONSchema(schema), componentCollector);
|
|
60
|
+
}
|
|
61
|
+
async function getZodMetadata(schema) {
|
|
62
|
+
const { ZodObject } = await import("zod/v4");
|
|
63
|
+
if (schema instanceof ZodObject) return schema.meta();
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Return JSON Schema for every schema registered in zod v4's global registry
|
|
67
|
+
* via `.meta({ id })`. Returns an empty object when zod v4 is unavailable or
|
|
68
|
+
* no schemas are registered.
|
|
69
|
+
*
|
|
70
|
+
* `unrepresentable: 'any'` keeps a single unrepresentable schema from
|
|
71
|
+
* collapsing the whole result.
|
|
72
|
+
*/
|
|
73
|
+
async function getRegisteredZodJsonSchemas() {
|
|
74
|
+
try {
|
|
75
|
+
const { z } = await import("zod/v4").catch(() => ({ z: null }));
|
|
76
|
+
if (!z?.toJSONSchema || !z.globalRegistry) return {};
|
|
77
|
+
return z.toJSONSchema(z.globalRegistry, { unrepresentable: "any" })?.schemas ?? {};
|
|
78
|
+
} catch {
|
|
79
|
+
return {};
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
async function getSchemaMetadata(schema) {
|
|
83
|
+
if (schema["~standard"]?.vendor === "zod") return getZodMetadata(schema);
|
|
84
|
+
}
|
|
85
|
+
async function convertSchemaWithComponents(schema, componentCollector) {
|
|
86
|
+
if (!schema) return;
|
|
87
|
+
const jsonSchema = await convertStandardSchemaToJsonSchema(schema, componentCollector);
|
|
88
|
+
if (!componentCollector) return jsonSchema;
|
|
89
|
+
const schemaId = (await getSchemaMetadata(schema))?.id || jsonSchema?.id;
|
|
90
|
+
if (schemaId) {
|
|
91
|
+
if (typeof jsonSchema?.$ref === "string") return componentCollector.getReference(schemaId);
|
|
92
|
+
const { id, ...schemaWithoutId } = jsonSchema;
|
|
93
|
+
componentCollector.addSchema(schemaId, schemaWithoutId);
|
|
94
|
+
return componentCollector.getReference(schemaId);
|
|
95
|
+
}
|
|
96
|
+
return jsonSchema;
|
|
97
|
+
}
|
|
98
|
+
//#endregion
|
|
99
|
+
exports.SchemaVendor = SchemaVendor;
|
|
100
|
+
exports.StandardSchemaJsonSchema = StandardSchemaJsonSchema;
|
|
101
|
+
exports.convertSchemaWithComponents = convertSchemaWithComponents;
|
|
102
|
+
exports.convertStandardSchemaToJsonSchema = convertStandardSchemaToJsonSchema;
|
|
103
|
+
exports.getRegisteredZodJsonSchemas = getRegisteredZodJsonSchemas;
|
|
104
|
+
exports.getSchemaMetadata = getSchemaMetadata;
|
|
105
|
+
exports.getZodMetadata = getZodMetadata;
|
|
2
106
|
|
|
3
|
-
|
|
4
|
-
exports.StandardSchemaJsonSchema = require_conversion.StandardSchemaJsonSchema;
|
|
5
|
-
exports.convertSchemaWithComponents = require_conversion.convertSchemaWithComponents;
|
|
6
|
-
exports.convertStandardSchemaToJsonSchema = require_conversion.convertStandardSchemaToJsonSchema;
|
|
7
|
-
exports.getRegisteredZodJsonSchemas = require_conversion.getRegisteredZodJsonSchemas;
|
|
8
|
-
exports.getSchemaMetadata = require_conversion.getSchemaMetadata;
|
|
9
|
-
exports.getZodMetadata = require_conversion.getZodMetadata;
|
|
107
|
+
//# sourceMappingURL=conversion.cjs.map
|
|
@@ -0,0 +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\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":"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"}
|
package/dist/conversion.d.cts
CHANGED
|
@@ -1,2 +1,37 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
2
|
+
//#region src/conversion.d.ts
|
|
3
|
+
export declare enum SchemaVendor {
|
|
4
|
+
zod = "zod",
|
|
5
|
+
valibot = "valibot"
|
|
6
|
+
}
|
|
7
|
+
export type VendorConvertor = (schema: any) => Promise<any>;
|
|
8
|
+
export type VendorConvertors = Record<SchemaVendor, VendorConvertor>;
|
|
9
|
+
export declare const StandardSchemaJsonSchema: VendorConvertors;
|
|
10
|
+
export declare function convertStandardSchemaToJsonSchema(schema?: StandardSchemaV1, componentCollector?: {
|
|
11
|
+
addSchema(id: string, schema: any): void;
|
|
12
|
+
getReference(id: string): {
|
|
13
|
+
$ref: string;
|
|
14
|
+
};
|
|
15
|
+
}): Promise<any>;
|
|
16
|
+
export declare function getZodMetadata(schema: StandardSchemaV1): Promise<SchemaMeta | undefined>;
|
|
17
|
+
/**
|
|
18
|
+
* Return JSON Schema for every schema registered in zod v4's global registry
|
|
19
|
+
* via `.meta({ id })`. Returns an empty object when zod v4 is unavailable or
|
|
20
|
+
* no schemas are registered.
|
|
21
|
+
*
|
|
22
|
+
* `unrepresentable: 'any'` keeps a single unrepresentable schema from
|
|
23
|
+
* collapsing the whole result.
|
|
24
|
+
*/
|
|
25
|
+
export declare function getRegisteredZodJsonSchemas(): Promise<Record<string, any>>;
|
|
26
|
+
export declare function getSchemaMetadata(schema: StandardSchemaV1): Promise<SchemaMeta | undefined>;
|
|
27
|
+
interface SchemaMeta {
|
|
28
|
+
id?: string;
|
|
29
|
+
}
|
|
30
|
+
export declare function convertSchemaWithComponents(schema: StandardSchemaV1 | undefined, componentCollector?: {
|
|
31
|
+
addSchema(id: string, schema: any): void;
|
|
32
|
+
getReference(id: string): {
|
|
33
|
+
$ref: string;
|
|
34
|
+
};
|
|
35
|
+
}): Promise<any>;
|
|
36
|
+
//#endregion
|
|
37
|
+
//# sourceMappingURL=conversion.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
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"}
|
package/dist/conversion.d.mts
CHANGED
|
@@ -1,2 +1,37 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
2
|
+
//#region src/conversion.d.ts
|
|
3
|
+
export declare enum SchemaVendor {
|
|
4
|
+
zod = "zod",
|
|
5
|
+
valibot = "valibot"
|
|
6
|
+
}
|
|
7
|
+
export type VendorConvertor = (schema: any) => Promise<any>;
|
|
8
|
+
export type VendorConvertors = Record<SchemaVendor, VendorConvertor>;
|
|
9
|
+
export declare const StandardSchemaJsonSchema: VendorConvertors;
|
|
10
|
+
export declare function convertStandardSchemaToJsonSchema(schema?: StandardSchemaV1, componentCollector?: {
|
|
11
|
+
addSchema(id: string, schema: any): void;
|
|
12
|
+
getReference(id: string): {
|
|
13
|
+
$ref: string;
|
|
14
|
+
};
|
|
15
|
+
}): Promise<any>;
|
|
16
|
+
export declare function getZodMetadata(schema: StandardSchemaV1): Promise<SchemaMeta | undefined>;
|
|
17
|
+
/**
|
|
18
|
+
* Return JSON Schema for every schema registered in zod v4's global registry
|
|
19
|
+
* via `.meta({ id })`. Returns an empty object when zod v4 is unavailable or
|
|
20
|
+
* no schemas are registered.
|
|
21
|
+
*
|
|
22
|
+
* `unrepresentable: 'any'` keeps a single unrepresentable schema from
|
|
23
|
+
* collapsing the whole result.
|
|
24
|
+
*/
|
|
25
|
+
export declare function getRegisteredZodJsonSchemas(): Promise<Record<string, any>>;
|
|
26
|
+
export declare function getSchemaMetadata(schema: StandardSchemaV1): Promise<SchemaMeta | undefined>;
|
|
27
|
+
interface SchemaMeta {
|
|
28
|
+
id?: string;
|
|
29
|
+
}
|
|
30
|
+
export declare function convertSchemaWithComponents(schema: StandardSchemaV1 | undefined, componentCollector?: {
|
|
31
|
+
addSchema(id: string, schema: any): void;
|
|
32
|
+
getReference(id: string): {
|
|
33
|
+
$ref: string;
|
|
34
|
+
};
|
|
35
|
+
}): Promise<any>;
|
|
36
|
+
//#endregion
|
|
37
|
+
//# sourceMappingURL=conversion.d.mts.map
|
|
@@ -0,0 +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;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
|
@@ -1,3 +1,100 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/conversion.ts
|
|
2
|
+
let SchemaVendor = /* @__PURE__ */ function(SchemaVendor) {
|
|
3
|
+
SchemaVendor["zod"] = "zod";
|
|
4
|
+
SchemaVendor["valibot"] = "valibot";
|
|
5
|
+
return SchemaVendor;
|
|
6
|
+
}({});
|
|
7
|
+
function isSchemaVendor(vendor) {
|
|
8
|
+
if (!vendor) return false;
|
|
9
|
+
return Object.values(SchemaVendor).includes(vendor);
|
|
10
|
+
}
|
|
11
|
+
const StandardSchemaJsonSchema = {
|
|
12
|
+
zod: async (schema) => {
|
|
13
|
+
try {
|
|
14
|
+
const { z } = await import("zod/v4").catch(() => ({ z: {} }));
|
|
15
|
+
if ("toJSONSchema" in z && typeof z.toJSONSchema === "function") return z.toJSONSchema(schema);
|
|
16
|
+
const { zodToJsonSchema } = await import("zod-to-json-schema");
|
|
17
|
+
return zodToJsonSchema(schema, { removeAdditionalStrategy: "strict" });
|
|
18
|
+
} catch {
|
|
19
|
+
return { type: "object" };
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
valibot: async (schema) => {
|
|
23
|
+
const { toJsonSchema } = await import("@valibot/to-json-schema");
|
|
24
|
+
return toJsonSchema(schema);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
function extractAndConvertDefs(jsonSchema, componentCollector) {
|
|
28
|
+
if (!jsonSchema || typeof jsonSchema !== "object") return jsonSchema;
|
|
29
|
+
const processSchema = (schema) => {
|
|
30
|
+
if (!schema || typeof schema !== "object") return schema;
|
|
31
|
+
if (schema.$ref && typeof schema.$ref === "string") {
|
|
32
|
+
if (schema.$ref.startsWith("#/$defs/")) {
|
|
33
|
+
const refName = schema.$ref.replace("#/$defs/", "");
|
|
34
|
+
return componentCollector ? componentCollector.getReference(refName) : schema;
|
|
35
|
+
}
|
|
36
|
+
return schema;
|
|
37
|
+
}
|
|
38
|
+
if (Array.isArray(schema)) return schema.map(processSchema);
|
|
39
|
+
const processed = {};
|
|
40
|
+
for (const [key, value] of Object.entries(schema)) {
|
|
41
|
+
if (key === "$defs" || key === "$schema") continue;
|
|
42
|
+
processed[key] = processSchema(value);
|
|
43
|
+
}
|
|
44
|
+
return processed;
|
|
45
|
+
};
|
|
46
|
+
if (jsonSchema.$defs && componentCollector) for (const [defName, defSchema] of Object.entries(jsonSchema.$defs)) {
|
|
47
|
+
const processedDefSchema = processSchema(defSchema);
|
|
48
|
+
componentCollector.addSchema(defName, processedDefSchema);
|
|
49
|
+
}
|
|
50
|
+
const { $defs, ...schemaWithoutDefs } = jsonSchema;
|
|
51
|
+
return processSchema(schemaWithoutDefs);
|
|
52
|
+
}
|
|
53
|
+
async function convertStandardSchemaToJsonSchema(schema, componentCollector) {
|
|
54
|
+
if (!schema) return;
|
|
55
|
+
const vendor = schema["~standard"]?.vendor;
|
|
56
|
+
if (!isSchemaVendor(vendor)) throw new Error(`Unsupported or missing vendor "${vendor}" for Standard Schema. Supported vendors are: ${Object.keys(StandardSchemaJsonSchema).join(", ")}`);
|
|
57
|
+
const toJSONSchema = StandardSchemaJsonSchema[vendor];
|
|
58
|
+
return extractAndConvertDefs(await toJSONSchema(schema), componentCollector);
|
|
59
|
+
}
|
|
60
|
+
async function getZodMetadata(schema) {
|
|
61
|
+
const { ZodObject } = await import("zod/v4");
|
|
62
|
+
if (schema instanceof ZodObject) return schema.meta();
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Return JSON Schema for every schema registered in zod v4's global registry
|
|
66
|
+
* via `.meta({ id })`. Returns an empty object when zod v4 is unavailable or
|
|
67
|
+
* no schemas are registered.
|
|
68
|
+
*
|
|
69
|
+
* `unrepresentable: 'any'` keeps a single unrepresentable schema from
|
|
70
|
+
* collapsing the whole result.
|
|
71
|
+
*/
|
|
72
|
+
async function getRegisteredZodJsonSchemas() {
|
|
73
|
+
try {
|
|
74
|
+
const { z } = await import("zod/v4").catch(() => ({ z: null }));
|
|
75
|
+
if (!z?.toJSONSchema || !z.globalRegistry) return {};
|
|
76
|
+
return z.toJSONSchema(z.globalRegistry, { unrepresentable: "any" })?.schemas ?? {};
|
|
77
|
+
} catch {
|
|
78
|
+
return {};
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
async function getSchemaMetadata(schema) {
|
|
82
|
+
if (schema["~standard"]?.vendor === "zod") return getZodMetadata(schema);
|
|
83
|
+
}
|
|
84
|
+
async function convertSchemaWithComponents(schema, componentCollector) {
|
|
85
|
+
if (!schema) return;
|
|
86
|
+
const jsonSchema = await convertStandardSchemaToJsonSchema(schema, componentCollector);
|
|
87
|
+
if (!componentCollector) return jsonSchema;
|
|
88
|
+
const schemaId = (await getSchemaMetadata(schema))?.id || jsonSchema?.id;
|
|
89
|
+
if (schemaId) {
|
|
90
|
+
if (typeof jsonSchema?.$ref === "string") return componentCollector.getReference(schemaId);
|
|
91
|
+
const { id, ...schemaWithoutId } = jsonSchema;
|
|
92
|
+
componentCollector.addSchema(schemaId, schemaWithoutId);
|
|
93
|
+
return componentCollector.getReference(schemaId);
|
|
94
|
+
}
|
|
95
|
+
return jsonSchema;
|
|
96
|
+
}
|
|
97
|
+
//#endregion
|
|
98
|
+
export { SchemaVendor, StandardSchemaJsonSchema, convertSchemaWithComponents, convertStandardSchemaToJsonSchema, getRegisteredZodJsonSchemas, getSchemaMetadata, getZodMetadata };
|
|
2
99
|
|
|
3
|
-
|
|
100
|
+
//# sourceMappingURL=conversion.mjs.map
|
|
@@ -0,0 +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\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":"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"}
|
package/dist/index.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
const
|
|
3
|
-
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_conversion = require("./conversion.cjs");
|
|
3
|
+
const require_openapi = require("./openapi.cjs");
|
|
4
4
|
exports.buildOpenApiSchema = require_openapi.buildOpenApiSchema;
|
|
5
5
|
exports.convertSchemaWithComponents = require_conversion.convertSchemaWithComponents;
|
|
6
6
|
exports.convertStandardSchemaToJsonSchema = require_conversion.convertStandardSchemaToJsonSchema;
|
|
7
|
-
exports.createComponentCollector = require_openapi.createComponentCollector;
|
|
7
|
+
exports.createComponentCollector = require_openapi.createComponentCollector;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { convertSchemaWithComponents, convertStandardSchemaToJsonSchema } from "./conversion
|
|
2
|
-
import { ComponentCollector, OpenApiSchemaOptions, buildOpenApiSchema, createComponentCollector } from "./openapi
|
|
3
|
-
import { ComposableStandardSchema, InferComposableStandardSchema, InferStandardSchema, InferStandardSchemaInput } from "./types
|
|
4
|
-
export { ComponentCollector, ComposableStandardSchema, InferComposableStandardSchema, InferStandardSchema, InferStandardSchemaInput, OpenApiSchemaOptions, buildOpenApiSchema, convertSchemaWithComponents, convertStandardSchemaToJsonSchema, createComponentCollector };
|
|
1
|
+
import { convertSchemaWithComponents, convertStandardSchemaToJsonSchema } from "./conversion.cjs";
|
|
2
|
+
import { ComponentCollector, OpenApiSchemaOptions, buildOpenApiSchema, createComponentCollector } from "./openapi.cjs";
|
|
3
|
+
import { ComposableStandardSchema, InferComposableStandardSchema, InferStandardSchema, InferStandardSchemaInput } from "./types.cjs";
|
|
4
|
+
export { type ComponentCollector, type ComposableStandardSchema, type InferComposableStandardSchema, type InferStandardSchema, type InferStandardSchemaInput, type OpenApiSchemaOptions, buildOpenApiSchema, convertSchemaWithComponents, convertStandardSchemaToJsonSchema, createComponentCollector };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { convertSchemaWithComponents, convertStandardSchemaToJsonSchema } from "./conversion
|
|
2
|
-
import { ComponentCollector, OpenApiSchemaOptions, buildOpenApiSchema, createComponentCollector } from "./openapi
|
|
3
|
-
import { ComposableStandardSchema, InferComposableStandardSchema, InferStandardSchema, InferStandardSchemaInput } from "./types
|
|
4
|
-
export { ComponentCollector, ComposableStandardSchema, InferComposableStandardSchema, InferStandardSchema, InferStandardSchemaInput, OpenApiSchemaOptions, buildOpenApiSchema, convertSchemaWithComponents, convertStandardSchemaToJsonSchema, createComponentCollector };
|
|
1
|
+
import { convertSchemaWithComponents, convertStandardSchemaToJsonSchema } from "./conversion.mjs";
|
|
2
|
+
import { ComponentCollector, OpenApiSchemaOptions, buildOpenApiSchema, createComponentCollector } from "./openapi.mjs";
|
|
3
|
+
import { ComposableStandardSchema, InferComposableStandardSchema, InferStandardSchema, InferStandardSchemaInput } from "./types.mjs";
|
|
4
|
+
export { type ComponentCollector, type ComposableStandardSchema, type InferComposableStandardSchema, type InferStandardSchema, type InferStandardSchemaInput, type OpenApiSchemaOptions, buildOpenApiSchema, convertSchemaWithComponents, convertStandardSchemaToJsonSchema, createComponentCollector };
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { convertSchemaWithComponents, convertStandardSchemaToJsonSchema } from "./conversion
|
|
2
|
-
import { buildOpenApiSchema, createComponentCollector } from "./openapi
|
|
3
|
-
|
|
4
|
-
export { buildOpenApiSchema, convertSchemaWithComponents, convertStandardSchemaToJsonSchema, createComponentCollector };
|
|
1
|
+
import { convertSchemaWithComponents, convertStandardSchemaToJsonSchema } from "./conversion.mjs";
|
|
2
|
+
import { buildOpenApiSchema, createComponentCollector } from "./openapi.mjs";
|
|
3
|
+
export { buildOpenApiSchema, convertSchemaWithComponents, convertStandardSchemaToJsonSchema, createComponentCollector };
|
package/dist/openapi.cjs
CHANGED
|
@@ -1,4 +1,57 @@
|
|
|
1
|
-
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/openapi.ts
|
|
3
|
+
function createComponentCollector() {
|
|
4
|
+
const schemas = {};
|
|
5
|
+
const securitySchemes = {};
|
|
6
|
+
return {
|
|
7
|
+
schemas,
|
|
8
|
+
securitySchemes,
|
|
9
|
+
addSchema(id, schema) {
|
|
10
|
+
schemas[id] = schema;
|
|
11
|
+
},
|
|
12
|
+
addSecurityScheme(name, scheme) {
|
|
13
|
+
securitySchemes[name] = scheme;
|
|
14
|
+
},
|
|
15
|
+
getReference(id) {
|
|
16
|
+
return { $ref: `#/components/schemas/${id}` };
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Builds OpenAPI 3.1 schema from an array of endpoints.
|
|
22
|
+
*
|
|
23
|
+
* Note: This function requires endpoints with toOpenApi3Route method.
|
|
24
|
+
* The actual implementation is in @geekmidas/constructs to avoid circular dependencies.
|
|
25
|
+
*/
|
|
26
|
+
async function buildOpenApiSchema(endpoints, options = {}) {
|
|
27
|
+
const { title = "API", version = "1.0.0", description } = options;
|
|
28
|
+
const paths = {};
|
|
29
|
+
const componentCollector = createComponentCollector();
|
|
30
|
+
for (const endpoint of endpoints) {
|
|
31
|
+
const route = await endpoint.toOpenApi3Route(componentCollector);
|
|
32
|
+
for (const [path, methods] of Object.entries(route)) {
|
|
33
|
+
if (!paths[path]) paths[path] = {};
|
|
34
|
+
Object.assign(paths[path], methods);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
const doc = {
|
|
38
|
+
openapi: "3.1.0",
|
|
39
|
+
info: {
|
|
40
|
+
title,
|
|
41
|
+
version,
|
|
42
|
+
...description && { description }
|
|
43
|
+
},
|
|
44
|
+
paths
|
|
45
|
+
};
|
|
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
|
+
};
|
|
51
|
+
return doc;
|
|
52
|
+
}
|
|
53
|
+
//#endregion
|
|
54
|
+
exports.buildOpenApiSchema = buildOpenApiSchema;
|
|
55
|
+
exports.createComponentCollector = createComponentCollector;
|
|
2
56
|
|
|
3
|
-
|
|
4
|
-
exports.createComponentCollector = require_openapi.createComponentCollector;
|
|
57
|
+
//# sourceMappingURL=openapi.cjs.map
|
|
@@ -0,0 +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\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
|
@@ -1,2 +1,27 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { OpenAPIV3_1 } from "openapi-types";
|
|
2
|
+
//#region src/openapi.d.ts
|
|
3
|
+
export interface OpenApiSchemaOptions {
|
|
4
|
+
title?: string;
|
|
5
|
+
version?: string;
|
|
6
|
+
description?: string;
|
|
7
|
+
}
|
|
8
|
+
export interface ComponentCollector {
|
|
9
|
+
schemas: Record<string, OpenAPIV3_1.SchemaObject>;
|
|
10
|
+
/** The schemes operations name in their `security` requirements. */
|
|
11
|
+
securitySchemes: Record<string, OpenAPIV3_1.SecuritySchemeObject>;
|
|
12
|
+
addSchema(id: string, schema: OpenAPIV3_1.SchemaObject): void;
|
|
13
|
+
addSecurityScheme(name: string, scheme: OpenAPIV3_1.SecuritySchemeObject): void;
|
|
14
|
+
getReference(id: string): OpenAPIV3_1.ReferenceObject;
|
|
15
|
+
}
|
|
16
|
+
export declare function createComponentCollector(): ComponentCollector;
|
|
17
|
+
/**
|
|
18
|
+
* Builds OpenAPI 3.1 schema from an array of endpoints.
|
|
19
|
+
*
|
|
20
|
+
* Note: This function requires endpoints with toOpenApi3Route method.
|
|
21
|
+
* The actual implementation is in @geekmidas/constructs to avoid circular dependencies.
|
|
22
|
+
*/
|
|
23
|
+
export declare function buildOpenApiSchema(endpoints: Array<{
|
|
24
|
+
toOpenApi3Route(collector?: ComponentCollector): Promise<any>;
|
|
25
|
+
}>, options?: OpenApiSchemaOptions): Promise<OpenAPIV3_1.Document>;
|
|
26
|
+
//#endregion
|
|
27
|
+
//# sourceMappingURL=openapi.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
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
|
@@ -1,2 +1,27 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { OpenAPIV3_1 } from "openapi-types";
|
|
2
|
+
//#region src/openapi.d.ts
|
|
3
|
+
export interface OpenApiSchemaOptions {
|
|
4
|
+
title?: string;
|
|
5
|
+
version?: string;
|
|
6
|
+
description?: string;
|
|
7
|
+
}
|
|
8
|
+
export interface ComponentCollector {
|
|
9
|
+
schemas: Record<string, OpenAPIV3_1.SchemaObject>;
|
|
10
|
+
/** The schemes operations name in their `security` requirements. */
|
|
11
|
+
securitySchemes: Record<string, OpenAPIV3_1.SecuritySchemeObject>;
|
|
12
|
+
addSchema(id: string, schema: OpenAPIV3_1.SchemaObject): void;
|
|
13
|
+
addSecurityScheme(name: string, scheme: OpenAPIV3_1.SecuritySchemeObject): void;
|
|
14
|
+
getReference(id: string): OpenAPIV3_1.ReferenceObject;
|
|
15
|
+
}
|
|
16
|
+
export declare function createComponentCollector(): ComponentCollector;
|
|
17
|
+
/**
|
|
18
|
+
* Builds OpenAPI 3.1 schema from an array of endpoints.
|
|
19
|
+
*
|
|
20
|
+
* Note: This function requires endpoints with toOpenApi3Route method.
|
|
21
|
+
* The actual implementation is in @geekmidas/constructs to avoid circular dependencies.
|
|
22
|
+
*/
|
|
23
|
+
export declare function buildOpenApiSchema(endpoints: Array<{
|
|
24
|
+
toOpenApi3Route(collector?: ComponentCollector): Promise<any>;
|
|
25
|
+
}>, options?: OpenApiSchemaOptions): Promise<OpenAPIV3_1.Document>;
|
|
26
|
+
//#endregion
|
|
27
|
+
//# sourceMappingURL=openapi.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
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,3 +1,55 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/openapi.ts
|
|
2
|
+
function createComponentCollector() {
|
|
3
|
+
const schemas = {};
|
|
4
|
+
const securitySchemes = {};
|
|
5
|
+
return {
|
|
6
|
+
schemas,
|
|
7
|
+
securitySchemes,
|
|
8
|
+
addSchema(id, schema) {
|
|
9
|
+
schemas[id] = schema;
|
|
10
|
+
},
|
|
11
|
+
addSecurityScheme(name, scheme) {
|
|
12
|
+
securitySchemes[name] = scheme;
|
|
13
|
+
},
|
|
14
|
+
getReference(id) {
|
|
15
|
+
return { $ref: `#/components/schemas/${id}` };
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Builds OpenAPI 3.1 schema from an array of endpoints.
|
|
21
|
+
*
|
|
22
|
+
* Note: This function requires endpoints with toOpenApi3Route method.
|
|
23
|
+
* The actual implementation is in @geekmidas/constructs to avoid circular dependencies.
|
|
24
|
+
*/
|
|
25
|
+
async function buildOpenApiSchema(endpoints, options = {}) {
|
|
26
|
+
const { title = "API", version = "1.0.0", description } = options;
|
|
27
|
+
const paths = {};
|
|
28
|
+
const componentCollector = createComponentCollector();
|
|
29
|
+
for (const endpoint of endpoints) {
|
|
30
|
+
const route = await endpoint.toOpenApi3Route(componentCollector);
|
|
31
|
+
for (const [path, methods] of Object.entries(route)) {
|
|
32
|
+
if (!paths[path]) paths[path] = {};
|
|
33
|
+
Object.assign(paths[path], methods);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
const doc = {
|
|
37
|
+
openapi: "3.1.0",
|
|
38
|
+
info: {
|
|
39
|
+
title,
|
|
40
|
+
version,
|
|
41
|
+
...description && { description }
|
|
42
|
+
},
|
|
43
|
+
paths
|
|
44
|
+
};
|
|
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
|
+
};
|
|
50
|
+
return doc;
|
|
51
|
+
}
|
|
52
|
+
//#endregion
|
|
53
|
+
export { buildOpenApiSchema, createComponentCollector };
|
|
2
54
|
|
|
3
|
-
|
|
55
|
+
//# sourceMappingURL=openapi.mjs.map
|
|
@@ -0,0 +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\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/parser.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
//#region src/parser.ts
|
|
3
3
|
/**
|
|
4
4
|
* Validates data against a StandardSchema.
|
|
@@ -11,13 +11,13 @@ function validate(schema, data) {
|
|
|
11
11
|
return schema["~standard"].validate(data);
|
|
12
12
|
}
|
|
13
13
|
async function parseSchema(schema, data) {
|
|
14
|
-
if (!schema) return
|
|
14
|
+
if (!schema) return;
|
|
15
15
|
const parsed = await validate(schema, data);
|
|
16
16
|
if (parsed.issues) throw parsed.issues;
|
|
17
17
|
return parsed.value;
|
|
18
18
|
}
|
|
19
|
-
|
|
20
19
|
//#endregion
|
|
21
20
|
exports.parseSchema = parseSchema;
|
|
22
21
|
exports.validate = validate;
|
|
22
|
+
|
|
23
23
|
//# sourceMappingURL=parser.cjs.map
|