@geekmidas/schema 10.0.0-alpha.98 → 10.0.0
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/CHANGELOG.md +65 -0
- package/README.md +6 -6
- package/dist/conversion-CIBtqJkS.mjs +106 -0
- package/dist/conversion-CIBtqJkS.mjs.map +1 -0
- package/dist/conversion-Cf9R-jGc.cjs +148 -0
- package/dist/conversion-Cf9R-jGc.cjs.map +1 -0
- package/dist/conversion-JY1Wixil.d.cts +39 -0
- package/dist/conversion-JY1Wixil.d.cts.map +1 -0
- package/dist/conversion-e4k9rTxy.d.mts +39 -0
- package/dist/conversion-e4k9rTxy.d.mts.map +1 -0
- package/dist/conversion.cjs +8 -106
- package/dist/conversion.d.cts +2 -37
- package/dist/conversion.d.mts +2 -37
- package/dist/conversion.mjs +2 -99
- package/dist/index.cjs +4 -4
- package/dist/index.d.cts +4 -4
- package/dist/index.d.mts +4 -4
- package/dist/index.mjs +4 -3
- package/dist/openapi-Aeb_1e9T.d.cts +28 -0
- package/dist/openapi-Aeb_1e9T.d.cts.map +1 -0
- package/dist/openapi-DH5yCqKh.mjs +46 -0
- package/dist/openapi-DH5yCqKh.mjs.map +1 -0
- package/dist/openapi-DVvLYx-8.cjs +58 -0
- package/dist/openapi-DVvLYx-8.cjs.map +1 -0
- package/dist/openapi-Dxc4FuMP.d.mts +28 -0
- package/dist/openapi-Dxc4FuMP.d.mts.map +1 -0
- package/dist/openapi.cjs +3 -56
- package/dist/openapi.d.cts +2 -27
- package/dist/openapi.d.mts +2 -27
- package/dist/openapi.mjs +2 -54
- package/dist/parser.cjs +3 -3
- package/dist/parser.cjs.map +1 -1
- package/dist/parser.d.cts +8 -3
- package/dist/parser.d.cts.map +1 -1
- package/dist/parser.d.mts +8 -3
- package/dist/parser.d.mts.map +1 -1
- package/dist/parser.mjs +2 -2
- package/dist/parser.mjs.map +1 -1
- package/dist/types-Bm3VhqVE.d.cts +26 -0
- package/dist/types-Bm3VhqVE.d.cts.map +1 -0
- package/dist/types-xm7EGhin.d.mts +26 -0
- package/dist/types-xm7EGhin.d.mts.map +1 -0
- package/dist/types.d.cts +2 -22
- package/dist/types.d.mts +2 -22
- package/dist/types.mjs +0 -1
- package/package.json +8 -15
- package/src/__benchmarks__/conversion.bench.ts +88 -0
- package/src/__tests__/conversion.spec.ts +502 -0
- package/src/__tests__/openapi.spec.ts +396 -0
- package/src/__tests__/parser.spec.ts +236 -0
- package/src/conversion.ts +220 -0
- package/src/index.ts +15 -0
- package/src/openapi.ts +75 -0
- package/src/parser.ts +30 -0
- package/src/types.ts +37 -0
- package/tsconfig.json +9 -0
- package/tsdown.config.ts +5 -0
- package/dist/conversion.cjs.map +0 -1
- package/dist/conversion.d.cts.map +0 -1
- package/dist/conversion.d.mts.map +0 -1
- package/dist/conversion.mjs.map +0 -1
- package/dist/openapi.cjs.map +0 -1
- package/dist/openapi.d.cts.map +0 -1
- package/dist/openapi.d.mts.map +0 -1
- package/dist/openapi.mjs.map +0 -1
- package/dist/types.d.cts.map +0 -1
- package/dist/types.d.mts.map +0 -1
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
|
+
|
|
3
|
+
export enum SchemaVendor {
|
|
4
|
+
zod = 'zod',
|
|
5
|
+
valibot = 'valibot',
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export type VendorConvertor = (schema: any) => Promise<any>;
|
|
9
|
+
export type VendorConvertors = Record<SchemaVendor, VendorConvertor>;
|
|
10
|
+
|
|
11
|
+
function isSchemaVendor(vendor?: string): vendor is SchemaVendor {
|
|
12
|
+
if (!vendor) {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return Object.values(SchemaVendor).includes(vendor as SchemaVendor);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const StandardSchemaJsonSchema: VendorConvertors = {
|
|
20
|
+
zod: async (schema): Promise<any> => {
|
|
21
|
+
try {
|
|
22
|
+
const { z } = await import('zod/v4').catch(() => ({ z: {} }));
|
|
23
|
+
|
|
24
|
+
if ('toJSONSchema' in z && typeof z.toJSONSchema === 'function') {
|
|
25
|
+
return z.toJSONSchema(schema);
|
|
26
|
+
}
|
|
27
|
+
const { zodToJsonSchema } = await import('zod-to-json-schema');
|
|
28
|
+
|
|
29
|
+
const result = zodToJsonSchema(schema, {
|
|
30
|
+
removeAdditionalStrategy: 'strict',
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
return result;
|
|
34
|
+
} catch {
|
|
35
|
+
// Fallback to basic conversion if zod-to-json-schema is not available
|
|
36
|
+
return { type: 'object' };
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
valibot: async (schema): Promise<any> => {
|
|
40
|
+
const { toJsonSchema } = await import('@valibot/to-json-schema');
|
|
41
|
+
return toJsonSchema(schema as any);
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
function extractAndConvertDefs(
|
|
46
|
+
jsonSchema: any,
|
|
47
|
+
componentCollector?: {
|
|
48
|
+
addSchema(id: string, schema: any): void;
|
|
49
|
+
getReference(id: string): { $ref: string };
|
|
50
|
+
},
|
|
51
|
+
): any {
|
|
52
|
+
if (!jsonSchema || typeof jsonSchema !== 'object') {
|
|
53
|
+
return jsonSchema;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Process the schema recursively to update references
|
|
57
|
+
const processSchema = (schema: any): any => {
|
|
58
|
+
if (!schema || typeof schema !== 'object') {
|
|
59
|
+
return schema;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Handle $ref
|
|
63
|
+
if (schema.$ref && typeof schema.$ref === 'string') {
|
|
64
|
+
// Convert #/$defs/X to #/components/schemas/X
|
|
65
|
+
if (schema.$ref.startsWith('#/$defs/')) {
|
|
66
|
+
const refName = schema.$ref.replace('#/$defs/', '');
|
|
67
|
+
return componentCollector
|
|
68
|
+
? componentCollector.getReference(refName)
|
|
69
|
+
: schema;
|
|
70
|
+
}
|
|
71
|
+
return schema;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Handle arrays
|
|
75
|
+
if (Array.isArray(schema)) {
|
|
76
|
+
return schema.map(processSchema);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Process all properties recursively
|
|
80
|
+
const processed: any = {};
|
|
81
|
+
for (const [key, value] of Object.entries(schema)) {
|
|
82
|
+
if (key === '$defs') {
|
|
83
|
+
// Skip $defs as they've been extracted
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
processed[key] = processSchema(value);
|
|
87
|
+
}
|
|
88
|
+
return processed;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
// Extract $defs if present
|
|
92
|
+
if (jsonSchema.$defs && componentCollector) {
|
|
93
|
+
for (const [defName, defSchema] of Object.entries(jsonSchema.$defs)) {
|
|
94
|
+
// Process the definition recursively to handle nested $refs
|
|
95
|
+
const processedDefSchema = processSchema(defSchema);
|
|
96
|
+
// Add each definition to the component collector
|
|
97
|
+
componentCollector.addSchema(defName, processedDefSchema);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Process the schema and remove $defs
|
|
102
|
+
const { $defs, ...schemaWithoutDefs } = jsonSchema;
|
|
103
|
+
return processSchema(schemaWithoutDefs);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export async function convertStandardSchemaToJsonSchema(
|
|
107
|
+
schema?: StandardSchemaV1,
|
|
108
|
+
componentCollector?: {
|
|
109
|
+
addSchema(id: string, schema: any): void;
|
|
110
|
+
getReference(id: string): { $ref: string };
|
|
111
|
+
},
|
|
112
|
+
): Promise<any> {
|
|
113
|
+
if (!schema) {
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const vendor = schema['~standard']?.vendor;
|
|
118
|
+
if (!isSchemaVendor(vendor)) {
|
|
119
|
+
throw new Error(
|
|
120
|
+
`Unsupported or missing vendor "${vendor}" for Standard Schema. Supported vendors are: ${Object.keys(StandardSchemaJsonSchema).join(', ')}`,
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const toJSONSchema = StandardSchemaJsonSchema[vendor];
|
|
125
|
+
const jsonSchema = await toJSONSchema(schema);
|
|
126
|
+
|
|
127
|
+
// Extract and convert $defs to components
|
|
128
|
+
return extractAndConvertDefs(jsonSchema, componentCollector);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export async function getZodMetadata(
|
|
132
|
+
schema: StandardSchemaV1,
|
|
133
|
+
): Promise<SchemaMeta | undefined> {
|
|
134
|
+
const { ZodObject } = await import('zod/v4');
|
|
135
|
+
|
|
136
|
+
if (schema instanceof ZodObject) {
|
|
137
|
+
return schema.meta();
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return undefined;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Return JSON Schema for every schema registered in zod v4's global registry
|
|
145
|
+
* via `.meta({ id })`. Returns an empty object when zod v4 is unavailable or
|
|
146
|
+
* no schemas are registered.
|
|
147
|
+
*
|
|
148
|
+
* `unrepresentable: 'any'` keeps a single unrepresentable schema from
|
|
149
|
+
* collapsing the whole result.
|
|
150
|
+
*/
|
|
151
|
+
export async function getRegisteredZodJsonSchemas(): Promise<
|
|
152
|
+
Record<string, any>
|
|
153
|
+
> {
|
|
154
|
+
try {
|
|
155
|
+
const { z } = await import('zod/v4').catch(() => ({ z: null as any }));
|
|
156
|
+
if (!z?.toJSONSchema || !z.globalRegistry) {
|
|
157
|
+
return {};
|
|
158
|
+
}
|
|
159
|
+
const result = z.toJSONSchema(z.globalRegistry, {
|
|
160
|
+
unrepresentable: 'any',
|
|
161
|
+
});
|
|
162
|
+
return (result?.schemas ?? {}) as Record<string, any>;
|
|
163
|
+
} catch {
|
|
164
|
+
return {};
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export async function getSchemaMetadata(
|
|
169
|
+
schema: StandardSchemaV1,
|
|
170
|
+
): Promise<SchemaMeta | undefined> {
|
|
171
|
+
const vendor = schema['~standard']?.vendor;
|
|
172
|
+
|
|
173
|
+
if (vendor === 'zod') {
|
|
174
|
+
return getZodMetadata(schema);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return undefined;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
interface SchemaMeta {
|
|
181
|
+
id?: string;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export async function convertSchemaWithComponents(
|
|
185
|
+
schema: StandardSchemaV1 | undefined,
|
|
186
|
+
componentCollector?: {
|
|
187
|
+
addSchema(id: string, schema: any): void;
|
|
188
|
+
getReference(id: string): { $ref: string };
|
|
189
|
+
},
|
|
190
|
+
): Promise<any> {
|
|
191
|
+
if (!schema) {
|
|
192
|
+
return undefined;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// Convert to JSON Schema with component collector to handle $defs
|
|
196
|
+
const jsonSchema = await convertStandardSchemaToJsonSchema(
|
|
197
|
+
schema,
|
|
198
|
+
componentCollector,
|
|
199
|
+
);
|
|
200
|
+
|
|
201
|
+
if (!componentCollector) {
|
|
202
|
+
return jsonSchema;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// Check if this schema has metadata with an ID
|
|
206
|
+
const metadata = await getSchemaMetadata(schema);
|
|
207
|
+
|
|
208
|
+
// Also check if the JSON Schema itself has an id field (from Zod's meta)
|
|
209
|
+
const schemaId = metadata?.id || jsonSchema?.id;
|
|
210
|
+
|
|
211
|
+
if (schemaId) {
|
|
212
|
+
// Remove the id from the schema before adding to components
|
|
213
|
+
const { id, ...schemaWithoutId } = jsonSchema;
|
|
214
|
+
// Add this schema to components and return a reference
|
|
215
|
+
componentCollector.addSchema(schemaId, schemaWithoutId);
|
|
216
|
+
return componentCollector.getReference(schemaId);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
return jsonSchema;
|
|
220
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// Re-export conversion utilities for convenience
|
|
2
|
+
export {
|
|
3
|
+
convertSchemaWithComponents,
|
|
4
|
+
convertStandardSchemaToJsonSchema,
|
|
5
|
+
} from './conversion';
|
|
6
|
+
export type { ComponentCollector, OpenApiSchemaOptions } from './openapi';
|
|
7
|
+
|
|
8
|
+
// Re-export OpenAPI utilities for convenience
|
|
9
|
+
export { buildOpenApiSchema, createComponentCollector } from './openapi';
|
|
10
|
+
export type {
|
|
11
|
+
ComposableStandardSchema,
|
|
12
|
+
InferComposableStandardSchema,
|
|
13
|
+
InferStandardSchema,
|
|
14
|
+
InferStandardSchemaInput,
|
|
15
|
+
} from './types';
|
package/src/openapi.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { OpenAPIV3_1 } from 'openapi-types';
|
|
2
|
+
|
|
3
|
+
export interface OpenApiSchemaOptions {
|
|
4
|
+
title?: string;
|
|
5
|
+
version?: string;
|
|
6
|
+
description?: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface ComponentCollector {
|
|
10
|
+
schemas: Record<string, OpenAPIV3_1.SchemaObject>;
|
|
11
|
+
addSchema(id: string, schema: OpenAPIV3_1.SchemaObject): void;
|
|
12
|
+
getReference(id: string): OpenAPIV3_1.ReferenceObject;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function createComponentCollector(): ComponentCollector {
|
|
16
|
+
const schemas: Record<string, OpenAPIV3_1.SchemaObject> = {};
|
|
17
|
+
|
|
18
|
+
return {
|
|
19
|
+
schemas,
|
|
20
|
+
addSchema(id: string, schema: OpenAPIV3_1.SchemaObject) {
|
|
21
|
+
schemas[id] = schema;
|
|
22
|
+
},
|
|
23
|
+
getReference(id: string): OpenAPIV3_1.ReferenceObject {
|
|
24
|
+
return { $ref: `#/components/schemas/${id}` };
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Builds OpenAPI 3.1 schema from an array of endpoints.
|
|
31
|
+
*
|
|
32
|
+
* Note: This function requires endpoints with toOpenApi3Route method.
|
|
33
|
+
* The actual implementation is in @geekmidas/constructs to avoid circular dependencies.
|
|
34
|
+
*/
|
|
35
|
+
export async function buildOpenApiSchema(
|
|
36
|
+
endpoints: Array<{
|
|
37
|
+
toOpenApi3Route(collector?: ComponentCollector): Promise<any>;
|
|
38
|
+
}>,
|
|
39
|
+
options: OpenApiSchemaOptions = {},
|
|
40
|
+
): Promise<OpenAPIV3_1.Document> {
|
|
41
|
+
const { title = 'API', version = '1.0.0', description } = options;
|
|
42
|
+
const paths: OpenAPIV3_1.PathsObject = {};
|
|
43
|
+
const componentCollector = createComponentCollector();
|
|
44
|
+
|
|
45
|
+
for (const endpoint of endpoints) {
|
|
46
|
+
const route = await endpoint.toOpenApi3Route(componentCollector);
|
|
47
|
+
|
|
48
|
+
// Merge the route into the paths object
|
|
49
|
+
for (const [path, methods] of Object.entries(route)) {
|
|
50
|
+
if (!paths[path]) {
|
|
51
|
+
paths[path] = {};
|
|
52
|
+
}
|
|
53
|
+
Object.assign(paths[path], methods);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const doc: OpenAPIV3_1.Document = {
|
|
58
|
+
openapi: '3.0.0',
|
|
59
|
+
info: {
|
|
60
|
+
title,
|
|
61
|
+
version,
|
|
62
|
+
...(description && { description }),
|
|
63
|
+
},
|
|
64
|
+
paths,
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
// Add components if any schemas were collected
|
|
68
|
+
if (Object.keys(componentCollector.schemas).length > 0) {
|
|
69
|
+
doc.components = {
|
|
70
|
+
schemas: componentCollector.schemas,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return doc;
|
|
75
|
+
}
|
package/src/parser.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
|
+
import type { InferStandardSchema } from './types';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Validates data against a StandardSchema.
|
|
6
|
+
*
|
|
7
|
+
* @param schema - The StandardSchema to validate against
|
|
8
|
+
* @param data - The data to validate
|
|
9
|
+
* @returns Validation result with value or issues
|
|
10
|
+
*/
|
|
11
|
+
export function validate<T extends StandardSchemaV1>(schema: T, data: unknown) {
|
|
12
|
+
return schema['~standard'].validate(data);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export async function parseSchema<T extends StandardSchemaV1>(
|
|
16
|
+
schema: T,
|
|
17
|
+
data: unknown,
|
|
18
|
+
): Promise<InferStandardSchema<T>> {
|
|
19
|
+
if (!schema) {
|
|
20
|
+
return undefined as InferStandardSchema<T>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const parsed = await validate(schema as unknown as StandardSchemaV1, data);
|
|
24
|
+
|
|
25
|
+
if (parsed.issues) {
|
|
26
|
+
throw parsed.issues;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return parsed.value as InferStandardSchema<T>;
|
|
30
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
|
+
|
|
3
|
+
export type InferStandardSchema<T> = T extends StandardSchemaV1
|
|
4
|
+
? StandardSchemaV1.InferOutput<T>
|
|
5
|
+
: never;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The *input* type a Standard Schema accepts (before any transform/coercion),
|
|
9
|
+
* as opposed to {@link InferStandardSchema} which is the output type.
|
|
10
|
+
*
|
|
11
|
+
* Use this for the value a producer must HAND TO the schema (e.g. an endpoint
|
|
12
|
+
* handler's return that will be parsed by its output schema): the schema may
|
|
13
|
+
* coerce it (a `Date` → an ISO `string`, a default applied, etc.), so the
|
|
14
|
+
* producer should be allowed to supply the looser input type while consumers
|
|
15
|
+
* still see the narrower output type.
|
|
16
|
+
*/
|
|
17
|
+
export type InferStandardSchemaInput<T> = T extends StandardSchemaV1
|
|
18
|
+
? StandardSchemaV1.InferInput<T>
|
|
19
|
+
: never;
|
|
20
|
+
|
|
21
|
+
export type ComposableStandardSchema =
|
|
22
|
+
| StandardSchemaV1
|
|
23
|
+
| {
|
|
24
|
+
[key: string]: StandardSchemaV1 | undefined;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export type InferComposableStandardSchema<T> = T extends StandardSchemaV1
|
|
28
|
+
? StandardSchemaV1.InferOutput<T>
|
|
29
|
+
: T extends { [key: string]: StandardSchemaV1 | undefined }
|
|
30
|
+
? {
|
|
31
|
+
[K in keyof T as T[K] extends StandardSchemaV1
|
|
32
|
+
? K
|
|
33
|
+
: never]: T[K] extends StandardSchemaV1
|
|
34
|
+
? StandardSchemaV1.InferOutput<T[K]>
|
|
35
|
+
: never;
|
|
36
|
+
}
|
|
37
|
+
: {};
|
package/tsconfig.json
ADDED
package/tsdown.config.ts
ADDED
package/dist/conversion.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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, schemaWitho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ACJ;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"}
|
|
@@ -1 +0,0 @@
|
|
|
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 +0,0 @@
|
|
|
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.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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,S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AAC5D;AAEA,eAAsB,eACrB,QACkC;CAClC,MAAM,EAAE,cAAc,MAAM,OAAO;CAEnC,IAAI,kBAAkB,WACrB,OAAO,OAAO,KAAK;AAIrB;;;;;;;;;AAUA,eAAsB,8BAEpB;CACD,IAAI;EACH,MAAM,EAAE,MAAM,MAAM,OAAO,SAAS,CAAC,aAAa,EAAE,GAAG,KAAY,EAAE;EACrE,IAAI,CAAC,GAAG,gBAAgB,CAAC,EAAE,gBAC1B,OAAO,CAAC;EAKT,OAHe,EAAE,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.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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/types.d.cts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.cts","names":[],"sources":["../src/types.ts"],"mappings":";;YAEY,oBAAoB,KAAK,UAAU,mBAC5C,iBAAiB,YAAY;;;;;;;;;;;YAapB,yBAAyB,KAAK,UAAU,mBACjD,iBAAiB,WAAW;YAGnB,2BACT;GAEC,cAAc;;YAGN,8BAA8B,KAAK,UAAU,mBACtD,iBAAiB,YAAY,KAC7B;GAAa,cAAc;OAEzB,WAAW,KAAK,EAAE,WAAW,mBAC3B,YACQ,EAAE,WAAW,mBACrB,iBAAiB,YAAY,EAAE"}
|
package/dist/types.d.mts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.mts","names":[],"sources":["../src/types.ts"],"mappings":";;YAEY,oBAAoB,KAAK,UAAU,mBAC5C,iBAAiB,YAAY;;;;;;;;;;;YAapB,yBAAyB,KAAK,UAAU,mBACjD,iBAAiB,WAAW;YAGnB,2BACT;GAEC,cAAc;;YAGN,8BAA8B,KAAK,UAAU,mBACtD,iBAAiB,YAAY,KAC7B;GAAa,cAAc;OAEzB,WAAW,KAAK,EAAE,WAAW,mBAC3B,YACQ,EAAE,WAAW,mBACrB,iBAAiB,YAAY,EAAE"}
|