zod-openapi 2.9.1 → 2.10.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/README.md +4 -111
- package/lib-commonjs/index.js +1687 -32
- package/lib-esm/index.js +1664 -0
- package/lib-types/extendZod.d.ts +4 -0
- package/package.json +4 -4
- package/lib-commonjs/api.js +0 -11
- package/lib-commonjs/api.js.map +0 -1
- package/lib-commonjs/create/components.js +0 -264
- package/lib-commonjs/create/components.js.map +0 -1
- package/lib-commonjs/create/content.js +0 -41
- package/lib-commonjs/create/content.js.map +0 -1
- package/lib-commonjs/create/document.js +0 -20
- package/lib-commonjs/create/document.js.map +0 -1
- package/lib-commonjs/create/parameters.js +0 -113
- package/lib-commonjs/create/parameters.js.map +0 -1
- package/lib-commonjs/create/paths.js +0 -85
- package/lib-commonjs/create/paths.js.map +0 -1
- package/lib-commonjs/create/responses.js +0 -114
- package/lib-commonjs/create/responses.js.map +0 -1
- package/lib-commonjs/create/schema/index.js +0 -101
- package/lib-commonjs/create/schema/index.js.map +0 -1
- package/lib-commonjs/create/schema/metadata.js +0 -19
- package/lib-commonjs/create/schema/metadata.js.map +0 -1
- package/lib-commonjs/create/schema/parsers/array.js +0 -17
- package/lib-commonjs/create/schema/parsers/array.js.map +0 -1
- package/lib-commonjs/create/schema/parsers/boolean.js +0 -8
- package/lib-commonjs/create/schema/parsers/boolean.js.map +0 -1
- package/lib-commonjs/create/schema/parsers/brand.js +0 -7
- package/lib-commonjs/create/schema/parsers/brand.js.map +0 -1
- package/lib-commonjs/create/schema/parsers/catch.js +0 -7
- package/lib-commonjs/create/schema/parsers/catch.js.map +0 -1
- package/lib-commonjs/create/schema/parsers/date.js +0 -8
- package/lib-commonjs/create/schema/parsers/date.js.map +0 -1
- package/lib-commonjs/create/schema/parsers/default.js +0 -15
- package/lib-commonjs/create/schema/parsers/default.js.map +0 -1
- package/lib-commonjs/create/schema/parsers/discriminatedUnion.js +0 -46
- package/lib-commonjs/create/schema/parsers/discriminatedUnion.js.map +0 -1
- package/lib-commonjs/create/schema/parsers/enum.js +0 -9
- package/lib-commonjs/create/schema/parsers/enum.js.map +0 -1
- package/lib-commonjs/create/schema/parsers/index.js +0 -131
- package/lib-commonjs/create/schema/parsers/index.js.map +0 -1
- package/lib-commonjs/create/schema/parsers/intersection.js +0 -14
- package/lib-commonjs/create/schema/parsers/intersection.js.map +0 -1
- package/lib-commonjs/create/schema/parsers/lazy.js +0 -10
- package/lib-commonjs/create/schema/parsers/lazy.js.map +0 -1
- package/lib-commonjs/create/schema/parsers/literal.js +0 -9
- package/lib-commonjs/create/schema/parsers/literal.js.map +0 -1
- package/lib-commonjs/create/schema/parsers/manual.js +0 -19
- package/lib-commonjs/create/schema/parsers/manual.js.map +0 -1
- package/lib-commonjs/create/schema/parsers/nativeEnum.js +0 -43
- package/lib-commonjs/create/schema/parsers/nativeEnum.js.map +0 -1
- package/lib-commonjs/create/schema/parsers/null.js +0 -8
- package/lib-commonjs/create/schema/parsers/null.js.map +0 -1
- package/lib-commonjs/create/schema/parsers/nullable.js +0 -61
- package/lib-commonjs/create/schema/parsers/nullable.js.map +0 -1
- package/lib-commonjs/create/schema/parsers/number.js +0 -50
- package/lib-commonjs/create/schema/parsers/number.js.map +0 -1
- package/lib-commonjs/create/schema/parsers/object.js +0 -107
- package/lib-commonjs/create/schema/parsers/object.js.map +0 -1
- package/lib-commonjs/create/schema/parsers/optional.js +0 -45
- package/lib-commonjs/create/schema/parsers/optional.js.map +0 -1
- package/lib-commonjs/create/schema/parsers/pipeline.js +0 -20
- package/lib-commonjs/create/schema/parsers/pipeline.js.map +0 -1
- package/lib-commonjs/create/schema/parsers/preprocess.js +0 -7
- package/lib-commonjs/create/schema/parsers/preprocess.js.map +0 -1
- package/lib-commonjs/create/schema/parsers/readonly.js +0 -8
- package/lib-commonjs/create/schema/parsers/readonly.js.map +0 -1
- package/lib-commonjs/create/schema/parsers/record.js +0 -43
- package/lib-commonjs/create/schema/parsers/record.js.map +0 -1
- package/lib-commonjs/create/schema/parsers/refine.js +0 -7
- package/lib-commonjs/create/schema/parsers/refine.js.map +0 -1
- package/lib-commonjs/create/schema/parsers/set.js +0 -18
- package/lib-commonjs/create/schema/parsers/set.js.map +0 -1
- package/lib-commonjs/create/schema/parsers/string.js +0 -95
- package/lib-commonjs/create/schema/parsers/string.js.map +0 -1
- package/lib-commonjs/create/schema/parsers/transform.js +0 -28
- package/lib-commonjs/create/schema/parsers/transform.js.map +0 -1
- package/lib-commonjs/create/schema/parsers/tuple.js +0 -54
- package/lib-commonjs/create/schema/parsers/tuple.js.map +0 -1
- package/lib-commonjs/create/schema/parsers/union.js +0 -12
- package/lib-commonjs/create/schema/parsers/union.js.map +0 -1
- package/lib-commonjs/create/schema/parsers/unknown.js +0 -6
- package/lib-commonjs/create/schema/parsers/unknown.js.map +0 -1
- package/lib-commonjs/create/specificationExtension.js +0 -6
- package/lib-commonjs/create/specificationExtension.js.map +0 -1
- package/lib-commonjs/extendZod.js +0 -48
- package/lib-commonjs/extendZod.js.map +0 -1
- package/lib-commonjs/index.js.map +0 -1
- package/lib-commonjs/openapi.js +0 -13
- package/lib-commonjs/openapi.js.map +0 -1
- package/lib-commonjs/openapi3-ts/dist/dsl/openapi-builder30.js +0 -3
- package/lib-commonjs/openapi3-ts/dist/dsl/openapi-builder30.js.map +0 -1
- package/lib-commonjs/openapi3-ts/dist/dsl/openapi-builder31.js +0 -3
- package/lib-commonjs/openapi3-ts/dist/dsl/openapi-builder31.js.map +0 -1
- package/lib-commonjs/openapi3-ts/dist/index.js +0 -29
- package/lib-commonjs/openapi3-ts/dist/index.js.map +0 -1
- package/lib-commonjs/openapi3-ts/dist/model/oas-common.js +0 -3
- package/lib-commonjs/openapi3-ts/dist/model/oas-common.js.map +0 -1
- package/lib-commonjs/openapi3-ts/dist/model/openapi30.js +0 -18
- package/lib-commonjs/openapi3-ts/dist/model/openapi30.js.map +0 -1
- package/lib-commonjs/openapi3-ts/dist/model/openapi31.js +0 -18
- package/lib-commonjs/openapi3-ts/dist/model/openapi31.js.map +0 -1
- package/lib-commonjs/openapi3-ts/dist/model/server.js +0 -3
- package/lib-commonjs/openapi3-ts/dist/model/server.js.map +0 -1
- package/lib-commonjs/openapi3-ts/dist/model/specification-extension.js +0 -3
- package/lib-commonjs/openapi3-ts/dist/model/specification-extension.js.map +0 -1
- package/lib-commonjs/openapi3-ts/dist/oas30.js +0 -19
- package/lib-commonjs/openapi3-ts/dist/oas30.js.map +0 -1
- package/lib-commonjs/openapi3-ts/dist/oas31.js +0 -19
- package/lib-commonjs/openapi3-ts/dist/oas31.js.map +0 -1
- package/lib-commonjs/openapi3-ts/oas30.js +0 -19
- package/lib-commonjs/openapi3-ts/oas30.js.map +0 -1
- package/lib-commonjs/openapi3-ts/oas31.js +0 -19
- package/lib-commonjs/openapi3-ts/oas31.js.map +0 -1
- package/lib-commonjs/zodType.js +0 -9
- package/lib-commonjs/zodType.js.map +0 -1
- package/lib-es2015/api.js +0 -4
- package/lib-es2015/api.js.map +0 -1
- package/lib-es2015/create/components.js +0 -256
- package/lib-es2015/create/components.js.map +0 -1
- package/lib-es2015/create/content.js +0 -36
- package/lib-es2015/create/content.js.map +0 -1
- package/lib-es2015/create/document.js +0 -16
- package/lib-es2015/create/document.js.map +0 -1
- package/lib-es2015/create/parameters.js +0 -105
- package/lib-es2015/create/parameters.js.map +0 -1
- package/lib-es2015/create/paths.js +0 -80
- package/lib-es2015/create/paths.js.map +0 -1
- package/lib-es2015/create/responses.js +0 -105
- package/lib-es2015/create/responses.js.map +0 -1
- package/lib-es2015/create/schema/index.js +0 -92
- package/lib-es2015/create/schema/index.js.map +0 -1
- package/lib-es2015/create/schema/metadata.js +0 -15
- package/lib-es2015/create/schema/metadata.js.map +0 -1
- package/lib-es2015/create/schema/parsers/array.js +0 -13
- package/lib-es2015/create/schema/parsers/array.js.map +0 -1
- package/lib-es2015/create/schema/parsers/boolean.js +0 -4
- package/lib-es2015/create/schema/parsers/boolean.js.map +0 -1
- package/lib-es2015/create/schema/parsers/brand.js +0 -3
- package/lib-es2015/create/schema/parsers/brand.js.map +0 -1
- package/lib-es2015/create/schema/parsers/catch.js +0 -3
- package/lib-es2015/create/schema/parsers/catch.js.map +0 -1
- package/lib-es2015/create/schema/parsers/date.js +0 -4
- package/lib-es2015/create/schema/parsers/date.js.map +0 -1
- package/lib-es2015/create/schema/parsers/default.js +0 -11
- package/lib-es2015/create/schema/parsers/default.js.map +0 -1
- package/lib-es2015/create/schema/parsers/discriminatedUnion.js +0 -41
- package/lib-es2015/create/schema/parsers/discriminatedUnion.js.map +0 -1
- package/lib-es2015/create/schema/parsers/enum.js +0 -5
- package/lib-es2015/create/schema/parsers/enum.js.map +0 -1
- package/lib-es2015/create/schema/parsers/index.js +0 -127
- package/lib-es2015/create/schema/parsers/index.js.map +0 -1
- package/lib-es2015/create/schema/parsers/intersection.js +0 -10
- package/lib-es2015/create/schema/parsers/intersection.js.map +0 -1
- package/lib-es2015/create/schema/parsers/lazy.js +0 -6
- package/lib-es2015/create/schema/parsers/lazy.js.map +0 -1
- package/lib-es2015/create/schema/parsers/literal.js +0 -5
- package/lib-es2015/create/schema/parsers/literal.js.map +0 -1
- package/lib-es2015/create/schema/parsers/manual.js +0 -15
- package/lib-es2015/create/schema/parsers/manual.js.map +0 -1
- package/lib-es2015/create/schema/parsers/nativeEnum.js +0 -37
- package/lib-es2015/create/schema/parsers/nativeEnum.js.map +0 -1
- package/lib-es2015/create/schema/parsers/null.js +0 -4
- package/lib-es2015/create/schema/parsers/null.js.map +0 -1
- package/lib-es2015/create/schema/parsers/nullable.js +0 -57
- package/lib-es2015/create/schema/parsers/nullable.js.map +0 -1
- package/lib-es2015/create/schema/parsers/number.js +0 -44
- package/lib-es2015/create/schema/parsers/number.js.map +0 -1
- package/lib-es2015/create/schema/parsers/object.js +0 -99
- package/lib-es2015/create/schema/parsers/object.js.map +0 -1
- package/lib-es2015/create/schema/parsers/optional.js +0 -40
- package/lib-es2015/create/schema/parsers/optional.js.map +0 -1
- package/lib-es2015/create/schema/parsers/pipeline.js +0 -16
- package/lib-es2015/create/schema/parsers/pipeline.js.map +0 -1
- package/lib-es2015/create/schema/parsers/preprocess.js +0 -3
- package/lib-es2015/create/schema/parsers/preprocess.js.map +0 -1
- package/lib-es2015/create/schema/parsers/readonly.js +0 -4
- package/lib-es2015/create/schema/parsers/readonly.js.map +0 -1
- package/lib-es2015/create/schema/parsers/record.js +0 -39
- package/lib-es2015/create/schema/parsers/record.js.map +0 -1
- package/lib-es2015/create/schema/parsers/refine.js +0 -3
- package/lib-es2015/create/schema/parsers/refine.js.map +0 -1
- package/lib-es2015/create/schema/parsers/set.js +0 -14
- package/lib-es2015/create/schema/parsers/set.js.map +0 -1
- package/lib-es2015/create/schema/parsers/string.js +0 -91
- package/lib-es2015/create/schema/parsers/string.js.map +0 -1
- package/lib-es2015/create/schema/parsers/transform.js +0 -23
- package/lib-es2015/create/schema/parsers/transform.js.map +0 -1
- package/lib-es2015/create/schema/parsers/tuple.js +0 -50
- package/lib-es2015/create/schema/parsers/tuple.js.map +0 -1
- package/lib-es2015/create/schema/parsers/union.js +0 -8
- package/lib-es2015/create/schema/parsers/union.js.map +0 -1
- package/lib-es2015/create/schema/parsers/unknown.js +0 -2
- package/lib-es2015/create/schema/parsers/unknown.js.map +0 -1
- package/lib-es2015/create/specificationExtension.js +0 -2
- package/lib-es2015/create/specificationExtension.js.map +0 -1
- package/lib-es2015/extendZod.js +0 -44
- package/lib-es2015/extendZod.js.map +0 -1
- package/lib-es2015/index.js +0 -6
- package/lib-es2015/index.js.map +0 -1
- package/lib-es2015/openapi.js +0 -9
- package/lib-es2015/openapi.js.map +0 -1
- package/lib-es2015/openapi3-ts/dist/dsl/openapi-builder30.js +0 -2
- package/lib-es2015/openapi3-ts/dist/dsl/openapi-builder30.js.map +0 -1
- package/lib-es2015/openapi3-ts/dist/dsl/openapi-builder31.js +0 -2
- package/lib-es2015/openapi3-ts/dist/dsl/openapi-builder31.js.map +0 -1
- package/lib-es2015/openapi3-ts/dist/index.js +0 -5
- package/lib-es2015/openapi3-ts/dist/index.js.map +0 -1
- package/lib-es2015/openapi3-ts/dist/model/oas-common.js +0 -2
- package/lib-es2015/openapi3-ts/dist/model/oas-common.js.map +0 -1
- package/lib-es2015/openapi3-ts/dist/model/openapi30.js +0 -2
- package/lib-es2015/openapi3-ts/dist/model/openapi30.js.map +0 -1
- package/lib-es2015/openapi3-ts/dist/model/openapi31.js +0 -2
- package/lib-es2015/openapi3-ts/dist/model/openapi31.js.map +0 -1
- package/lib-es2015/openapi3-ts/dist/model/server.js +0 -2
- package/lib-es2015/openapi3-ts/dist/model/server.js.map +0 -1
- package/lib-es2015/openapi3-ts/dist/model/specification-extension.js +0 -2
- package/lib-es2015/openapi3-ts/dist/model/specification-extension.js.map +0 -1
- package/lib-es2015/openapi3-ts/dist/oas30.js +0 -3
- package/lib-es2015/openapi3-ts/dist/oas30.js.map +0 -1
- package/lib-es2015/openapi3-ts/dist/oas31.js +0 -3
- package/lib-es2015/openapi3-ts/dist/oas31.js.map +0 -1
- package/lib-es2015/openapi3-ts/oas30.js +0 -3
- package/lib-es2015/openapi3-ts/oas30.js.map +0 -1
- package/lib-es2015/openapi3-ts/oas31.js +0 -3
- package/lib-es2015/openapi3-ts/oas31.js.map +0 -1
- package/lib-es2015/zodType.js +0 -4
- package/lib-es2015/zodType.js.map +0 -1
package/lib-commonjs/index.js
CHANGED
|
@@ -1,34 +1,1689 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
var
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
var
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
api: () => api_exports,
|
|
24
|
+
createDocument: () => createDocument,
|
|
25
|
+
extendZodWithOpenApi: () => extendZodWithOpenApi,
|
|
26
|
+
oas30: () => oas30_exports,
|
|
27
|
+
oas31: () => oas31_exports
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(src_exports);
|
|
30
|
+
|
|
31
|
+
// src/zodType.ts
|
|
32
|
+
var isZodType = (zodType, typeName) => zodType?._def?.typeName === typeName;
|
|
33
|
+
var isAnyZodType = (zodType) => Boolean(
|
|
34
|
+
zodType?._def?.typeName
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
// src/create/schema/metadata.ts
|
|
38
|
+
var enhanceWithMetadata = (schemaObject, metadata) => {
|
|
39
|
+
if ("$ref" in schemaObject) {
|
|
40
|
+
if (Object.values(metadata).every((val) => val === void 0)) {
|
|
41
|
+
return schemaObject;
|
|
42
|
+
}
|
|
43
|
+
return {
|
|
44
|
+
allOf: [schemaObject, metadata]
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
return {
|
|
48
|
+
...schemaObject,
|
|
49
|
+
...metadata
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
// src/create/schema/parsers/array.ts
|
|
54
|
+
var createArraySchema = (zodArray, state) => {
|
|
55
|
+
const zodType = zodArray._def.type;
|
|
56
|
+
const minItems = zodArray._def.exactLength?.value ?? zodArray._def.minLength?.value;
|
|
57
|
+
const maxItems = zodArray._def.exactLength?.value ?? zodArray._def.maxLength?.value;
|
|
58
|
+
return {
|
|
59
|
+
type: "array",
|
|
60
|
+
items: createSchemaObject(zodType, state, ["array items"]),
|
|
61
|
+
...minItems !== void 0 && { minItems },
|
|
62
|
+
...maxItems !== void 0 && { maxItems }
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
// src/create/schema/parsers/boolean.ts
|
|
67
|
+
var createBooleanSchema = (_zodBoolean) => ({
|
|
68
|
+
type: "boolean"
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// src/create/schema/parsers/brand.ts
|
|
72
|
+
var createBrandedSchema = (zodBranded, state) => createSchemaObject(zodBranded._def.type, state, ["brand"]);
|
|
73
|
+
|
|
74
|
+
// src/create/schema/parsers/catch.ts
|
|
75
|
+
var createCatchSchema = (zodCatch, state) => createSchemaObject(zodCatch._def.innerType, state, ["catch"]);
|
|
76
|
+
|
|
77
|
+
// src/create/schema/parsers/date.ts
|
|
78
|
+
var createDateSchema = (_zodDate) => ({
|
|
79
|
+
type: "string"
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
// src/create/schema/parsers/default.ts
|
|
83
|
+
var createDefaultSchema = (zodDefault, state) => {
|
|
84
|
+
const schemaObject = createSchemaObject(zodDefault._def.innerType, state, [
|
|
85
|
+
"default"
|
|
86
|
+
]);
|
|
87
|
+
return enhanceWithMetadata(schemaObject, {
|
|
88
|
+
default: zodDefault._def.defaultValue()
|
|
89
|
+
});
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
// src/create/schema/parsers/discriminatedUnion.ts
|
|
93
|
+
var createDiscriminatedUnionSchema = (zodDiscriminatedUnion, state) => {
|
|
94
|
+
const options = zodDiscriminatedUnion.options;
|
|
95
|
+
const schemas = options.map(
|
|
96
|
+
(option, index) => createSchemaObject(option, state, [`discriminated union option ${index}`])
|
|
97
|
+
);
|
|
98
|
+
const discriminator = mapDiscriminator(
|
|
99
|
+
schemas,
|
|
100
|
+
options,
|
|
101
|
+
zodDiscriminatedUnion.discriminator,
|
|
102
|
+
state
|
|
103
|
+
);
|
|
104
|
+
return {
|
|
105
|
+
oneOf: schemas,
|
|
106
|
+
...discriminator && { discriminator }
|
|
107
|
+
};
|
|
108
|
+
};
|
|
109
|
+
var mapDiscriminator = (schemas, zodObjects, discriminator, state) => {
|
|
110
|
+
if (typeof discriminator !== "string") {
|
|
111
|
+
return void 0;
|
|
112
|
+
}
|
|
113
|
+
const mapping = {};
|
|
114
|
+
for (const [index, zodObject] of zodObjects.entries()) {
|
|
115
|
+
const schema = schemas[index];
|
|
116
|
+
const componentSchemaRef = "$ref" in schema ? schema?.$ref : void 0;
|
|
117
|
+
if (!componentSchemaRef) {
|
|
118
|
+
return void 0;
|
|
119
|
+
}
|
|
120
|
+
const value = zodObject.shape[discriminator];
|
|
121
|
+
if (isZodType(value, "ZodEnum")) {
|
|
122
|
+
for (const enumValue of value._def.values) {
|
|
123
|
+
mapping[enumValue] = componentSchemaRef;
|
|
124
|
+
}
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
const literalValue = (value?._def).value;
|
|
128
|
+
if (typeof literalValue !== "string") {
|
|
129
|
+
throw new Error(
|
|
130
|
+
`Discriminator ${discriminator} could not be found in on index ${index} of a discriminated union at ${state.path.join(
|
|
131
|
+
" > "
|
|
132
|
+
)}`
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
mapping[literalValue] = componentSchemaRef;
|
|
136
|
+
}
|
|
137
|
+
return {
|
|
138
|
+
propertyName: discriminator,
|
|
139
|
+
mapping
|
|
140
|
+
};
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
// src/create/schema/parsers/enum.ts
|
|
144
|
+
var createEnumSchema = (zodEnum) => ({
|
|
145
|
+
type: "string",
|
|
146
|
+
enum: zodEnum._def.values
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
// src/create/schema/parsers/intersection.ts
|
|
150
|
+
var createIntersectionSchema = (zodIntersection, state) => ({
|
|
151
|
+
allOf: [
|
|
152
|
+
createSchemaObject(zodIntersection._def.left, state, ["intersection left"]),
|
|
153
|
+
createSchemaObject(zodIntersection._def.right, state, [
|
|
154
|
+
"intersection right"
|
|
155
|
+
])
|
|
156
|
+
]
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
// src/create/schema/parsers/lazy.ts
|
|
160
|
+
var createLazySchema = (zodLazy, state) => {
|
|
161
|
+
const innerSchema = zodLazy._def.getter();
|
|
162
|
+
return createSchemaObject(innerSchema, state, ["lazy schema"]);
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
// src/create/schema/parsers/literal.ts
|
|
166
|
+
var createLiteralSchema = (zodLiteral) => ({
|
|
167
|
+
type: typeof zodLiteral.value,
|
|
168
|
+
enum: [zodLiteral._def.value]
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
// src/create/schema/parsers/manual.ts
|
|
172
|
+
var createManualTypeSchema = (zodSchema, state) => {
|
|
173
|
+
if (!zodSchema._def.openapi?.type) {
|
|
174
|
+
const zodType = zodSchema.constructor.name;
|
|
175
|
+
if (isZodType(zodSchema, "ZodEffects")) {
|
|
176
|
+
const schemaName = `${zodType} - ${zodSchema._def.effect.type}`;
|
|
177
|
+
throw new Error(
|
|
178
|
+
`Unknown schema ${schemaName} at ${state.path.join(
|
|
179
|
+
" > "
|
|
180
|
+
)}. Please assign it a manual 'type', wrap it in a ZodPipeline or change the 'effectType'.`
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
throw new Error(
|
|
184
|
+
`Unknown schema ${zodType}. Please assign it a manual 'type'.`
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
return {
|
|
188
|
+
type: zodSchema._def.openapi.type
|
|
189
|
+
};
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
// src/openapi.ts
|
|
193
|
+
var openApiVersions = [
|
|
194
|
+
"3.0.0",
|
|
195
|
+
"3.0.1",
|
|
196
|
+
"3.0.2",
|
|
197
|
+
"3.0.3",
|
|
198
|
+
"3.1.0"
|
|
199
|
+
];
|
|
200
|
+
var satisfiesVersion = (test, against) => openApiVersions.indexOf(test) >= openApiVersions.indexOf(against);
|
|
201
|
+
|
|
202
|
+
// src/create/schema/parsers/nativeEnum.ts
|
|
203
|
+
var createNativeEnumSchema = (zodEnum, state) => {
|
|
204
|
+
const enumValues = getValidEnumValues(zodEnum._def.values);
|
|
205
|
+
const { numbers, strings } = sortStringsAndNumbers(enumValues);
|
|
206
|
+
if (strings.length && numbers.length) {
|
|
207
|
+
if (satisfiesVersion(state.components.openapi, "3.1.0"))
|
|
208
|
+
return {
|
|
209
|
+
type: ["string", "number"],
|
|
210
|
+
enum: [...strings, ...numbers]
|
|
211
|
+
};
|
|
212
|
+
return {
|
|
213
|
+
oneOf: [
|
|
214
|
+
{ type: "string", enum: strings },
|
|
215
|
+
{ type: "number", enum: numbers }
|
|
216
|
+
]
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
if (strings.length) {
|
|
220
|
+
return {
|
|
221
|
+
type: "string",
|
|
222
|
+
enum: strings
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
return {
|
|
226
|
+
type: "number",
|
|
227
|
+
enum: numbers
|
|
228
|
+
};
|
|
229
|
+
};
|
|
230
|
+
var getValidEnumValues = (enumValues) => {
|
|
231
|
+
const keys = Object.keys(enumValues).filter(
|
|
232
|
+
(key) => typeof enumValues[enumValues[key]] !== "number"
|
|
233
|
+
);
|
|
234
|
+
return keys.map((key) => enumValues[key]);
|
|
235
|
+
};
|
|
236
|
+
var sortStringsAndNumbers = (values) => ({
|
|
237
|
+
strings: values.filter((value) => typeof value === "string"),
|
|
238
|
+
numbers: values.filter((value) => typeof value === "number")
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
// src/create/schema/parsers/null.ts
|
|
242
|
+
var createNullSchema = (_zodNull) => ({
|
|
243
|
+
type: "null"
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
// src/create/schema/parsers/nullable.ts
|
|
247
|
+
var createNullableSchema = (zodNullable, state) => {
|
|
248
|
+
const schemaObject = createSchemaObject(zodNullable.unwrap(), state, [
|
|
249
|
+
"nullable"
|
|
250
|
+
]);
|
|
251
|
+
if ("$ref" in schemaObject || schemaObject.allOf) {
|
|
252
|
+
return {
|
|
253
|
+
oneOf: mapNullOf([schemaObject], state.components.openapi)
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
if (schemaObject.oneOf) {
|
|
257
|
+
const { oneOf, ...schema2 } = schemaObject;
|
|
258
|
+
return {
|
|
259
|
+
oneOf: mapNullOf(oneOf, state.components.openapi),
|
|
260
|
+
...schema2
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
if (schemaObject.anyOf) {
|
|
264
|
+
const { anyOf, ...schema2 } = schemaObject;
|
|
265
|
+
return {
|
|
266
|
+
anyOf: mapNullOf(anyOf, state.components.openapi),
|
|
267
|
+
...schema2
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
const { type, ...schema } = schemaObject;
|
|
271
|
+
if (satisfiesVersion(state.components.openapi, "3.1.0")) {
|
|
272
|
+
return {
|
|
273
|
+
type: mapNullType(type),
|
|
274
|
+
...schema
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
return {
|
|
278
|
+
type,
|
|
279
|
+
nullable: true,
|
|
280
|
+
...schema,
|
|
281
|
+
// https://github.com/OAI/OpenAPI-Specification/blob/main/proposals/2019-10-31-Clarify-Nullable.md#if-a-schema-specifies-nullable-true-and-enum-1-2-3-does-that-schema-allow-null-values-see-1900
|
|
282
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
283
|
+
...schema.enum && { enum: [...schema.enum, null] }
|
|
284
|
+
};
|
|
285
|
+
};
|
|
286
|
+
var mapNullType = (type) => {
|
|
287
|
+
if (!type) {
|
|
288
|
+
return "null";
|
|
289
|
+
}
|
|
290
|
+
if (Array.isArray(type)) {
|
|
291
|
+
return [...type, "null"];
|
|
292
|
+
}
|
|
293
|
+
return [type, "null"];
|
|
294
|
+
};
|
|
295
|
+
var mapNullOf = (ofSchema, openapi) => {
|
|
296
|
+
if (satisfiesVersion(openapi, "3.1.0")) {
|
|
297
|
+
return [...ofSchema, { type: "null" }];
|
|
298
|
+
}
|
|
299
|
+
return [...ofSchema, { nullable: true }];
|
|
300
|
+
};
|
|
301
|
+
|
|
302
|
+
// src/create/schema/parsers/number.ts
|
|
303
|
+
var createNumberSchema = (zodNumber, state) => {
|
|
304
|
+
const zodNumberChecks = getZodNumberChecks(zodNumber);
|
|
305
|
+
const minimum = mapMinimum(zodNumberChecks, state.components.openapi);
|
|
306
|
+
const maximum = mapMaximum(zodNumberChecks, state.components.openapi);
|
|
307
|
+
return {
|
|
308
|
+
type: mapNumberType(zodNumberChecks),
|
|
309
|
+
...minimum && minimum,
|
|
310
|
+
// Union types are not easy to tame
|
|
311
|
+
...maximum && maximum
|
|
312
|
+
};
|
|
313
|
+
};
|
|
314
|
+
var mapMaximum = (zodNumberCheck, openapi) => {
|
|
315
|
+
if (!zodNumberCheck.max) {
|
|
316
|
+
return void 0;
|
|
317
|
+
}
|
|
318
|
+
const maximum = zodNumberCheck.max.value;
|
|
319
|
+
if (zodNumberCheck.max.inclusive) {
|
|
320
|
+
return { ...maximum !== void 0 && { maximum } };
|
|
321
|
+
}
|
|
322
|
+
if (satisfiesVersion(openapi, "3.1.0")) {
|
|
323
|
+
return { exclusiveMaximum: maximum };
|
|
324
|
+
}
|
|
325
|
+
return { maximum, exclusiveMaximum: true };
|
|
326
|
+
};
|
|
327
|
+
var mapMinimum = (zodNumberCheck, openapi) => {
|
|
328
|
+
if (!zodNumberCheck.min) {
|
|
329
|
+
return void 0;
|
|
330
|
+
}
|
|
331
|
+
const minimum = zodNumberCheck.min.value;
|
|
332
|
+
if (zodNumberCheck.min.inclusive) {
|
|
333
|
+
return { ...minimum !== void 0 && { minimum } };
|
|
334
|
+
}
|
|
335
|
+
if (satisfiesVersion(openapi, "3.1.0")) {
|
|
336
|
+
return { exclusiveMinimum: minimum };
|
|
337
|
+
}
|
|
338
|
+
return { minimum, exclusiveMinimum: true };
|
|
339
|
+
};
|
|
340
|
+
var getZodNumberChecks = (zodNumber) => zodNumber._def.checks.reduce((acc, check) => {
|
|
341
|
+
acc[check.kind] = check;
|
|
342
|
+
return acc;
|
|
343
|
+
}, {});
|
|
344
|
+
var mapNumberType = (zodNumberChecks) => zodNumberChecks.int ? "integer" : "number";
|
|
345
|
+
|
|
346
|
+
// src/create/schema/parsers/optional.ts
|
|
347
|
+
var createOptionalSchema = (zodOptional, state) => (
|
|
348
|
+
// Optional doesn't change OpenAPI schema
|
|
349
|
+
createSchemaObject(zodOptional.unwrap(), state, ["optional"])
|
|
350
|
+
);
|
|
351
|
+
var isOptionalSchema = (zodSchema, state) => {
|
|
352
|
+
if (isZodType(zodSchema, "ZodOptional")) {
|
|
353
|
+
return true;
|
|
354
|
+
}
|
|
355
|
+
if (isZodType(zodSchema, "ZodDefault")) {
|
|
356
|
+
return state.type === "input";
|
|
357
|
+
}
|
|
358
|
+
if (isZodType(zodSchema, "ZodNullable") || isZodType(zodSchema, "ZodCatch")) {
|
|
359
|
+
return isOptionalSchema(zodSchema._def.innerType, state);
|
|
360
|
+
}
|
|
361
|
+
if (isZodType(zodSchema, "ZodEffects")) {
|
|
362
|
+
return isOptionalSchema(zodSchema._def.schema, state);
|
|
363
|
+
}
|
|
364
|
+
if (isZodType(zodSchema, "ZodUnion") || isZodType(zodSchema, "ZodDiscriminatedUnion")) {
|
|
365
|
+
return zodSchema._def.options.some(
|
|
366
|
+
(schema) => isOptionalSchema(schema, state)
|
|
367
|
+
);
|
|
368
|
+
}
|
|
369
|
+
if (isZodType(zodSchema, "ZodIntersection")) {
|
|
370
|
+
return [zodSchema._def.left, zodSchema._def.right].some(
|
|
371
|
+
(schema) => isOptionalSchema(schema, state)
|
|
372
|
+
);
|
|
373
|
+
}
|
|
374
|
+
if (isZodType(zodSchema, "ZodPipeline")) {
|
|
375
|
+
if (state.effectType === "input" || state.type === "input" && state.effectType !== "output") {
|
|
376
|
+
return isOptionalSchema(zodSchema._def.in, state);
|
|
377
|
+
}
|
|
378
|
+
if (state.effectType === "output" || state.type === "output" && state.effectType !== "input") {
|
|
379
|
+
return isOptionalSchema(zodSchema._def.out, state);
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
if (isZodType(zodSchema, "ZodLazy")) {
|
|
383
|
+
return isOptionalSchema(zodSchema._def.getter(), state);
|
|
384
|
+
}
|
|
385
|
+
return zodSchema.isOptional();
|
|
386
|
+
};
|
|
387
|
+
|
|
388
|
+
// src/create/schema/parsers/object.ts
|
|
389
|
+
var createObjectSchema = (zodObject, state) => {
|
|
390
|
+
const extendedSchema = createExtendedSchema(
|
|
391
|
+
zodObject,
|
|
392
|
+
zodObject._def.extendMetadata?.extends,
|
|
393
|
+
state
|
|
394
|
+
);
|
|
395
|
+
if (extendedSchema) {
|
|
396
|
+
return extendedSchema;
|
|
397
|
+
}
|
|
398
|
+
return createObjectSchemaFromShape(
|
|
399
|
+
zodObject.shape,
|
|
400
|
+
{
|
|
401
|
+
unknownKeys: zodObject._def.unknownKeys,
|
|
402
|
+
catchAll: zodObject._def.catchall
|
|
403
|
+
},
|
|
404
|
+
state
|
|
405
|
+
);
|
|
406
|
+
};
|
|
407
|
+
var createExtendedSchema = (zodObject, baseZodObject, state) => {
|
|
408
|
+
if (!baseZodObject) {
|
|
409
|
+
return void 0;
|
|
410
|
+
}
|
|
411
|
+
const component = state.components.schemas.get(baseZodObject);
|
|
412
|
+
if (component ?? baseZodObject._def.openapi?.ref) {
|
|
413
|
+
createSchemaObject(baseZodObject, state, ["extended schema"]);
|
|
414
|
+
}
|
|
415
|
+
const completeComponent = state.components.schemas.get(baseZodObject);
|
|
416
|
+
if (!completeComponent) {
|
|
417
|
+
return void 0;
|
|
418
|
+
}
|
|
419
|
+
const diffOpts = createDiffOpts(
|
|
420
|
+
{
|
|
421
|
+
unknownKeys: baseZodObject._def.unknownKeys,
|
|
422
|
+
catchAll: baseZodObject._def.catchall
|
|
423
|
+
},
|
|
424
|
+
{
|
|
425
|
+
unknownKeys: zodObject._def.unknownKeys,
|
|
426
|
+
catchAll: zodObject._def.catchall
|
|
427
|
+
}
|
|
428
|
+
);
|
|
429
|
+
if (!diffOpts) {
|
|
430
|
+
return void 0;
|
|
431
|
+
}
|
|
432
|
+
const diffShape = createShapeDiff(
|
|
433
|
+
baseZodObject._def.shape(),
|
|
434
|
+
zodObject._def.shape()
|
|
435
|
+
);
|
|
436
|
+
if (!diffShape) {
|
|
437
|
+
return void 0;
|
|
438
|
+
}
|
|
439
|
+
return {
|
|
440
|
+
allOf: [{ $ref: createComponentSchemaRef(completeComponent.ref) }],
|
|
441
|
+
...createObjectSchemaFromShape(diffShape, diffOpts, state)
|
|
442
|
+
};
|
|
443
|
+
};
|
|
444
|
+
var createDiffOpts = (baseOpts, extendedOpts) => {
|
|
445
|
+
if (baseOpts.unknownKeys === "strict" || !isZodType(baseOpts.catchAll, "ZodNever")) {
|
|
446
|
+
return void 0;
|
|
447
|
+
}
|
|
448
|
+
return {
|
|
449
|
+
catchAll: extendedOpts.catchAll,
|
|
450
|
+
unknownKeys: extendedOpts.unknownKeys
|
|
451
|
+
};
|
|
452
|
+
};
|
|
453
|
+
var createShapeDiff = (baseObj, extendedObj) => {
|
|
454
|
+
const acc = {};
|
|
455
|
+
for (const [key, val] of Object.entries(extendedObj)) {
|
|
456
|
+
const baseValue = baseObj[key];
|
|
457
|
+
if (val === baseValue) {
|
|
458
|
+
continue;
|
|
459
|
+
}
|
|
460
|
+
if (baseValue === void 0) {
|
|
461
|
+
acc[key] = extendedObj[key];
|
|
462
|
+
continue;
|
|
463
|
+
}
|
|
464
|
+
return null;
|
|
465
|
+
}
|
|
466
|
+
return acc;
|
|
467
|
+
};
|
|
468
|
+
var createObjectSchemaFromShape = (shape, { unknownKeys, catchAll }, state) => {
|
|
469
|
+
const properties = mapProperties(shape, state);
|
|
470
|
+
const required = mapRequired(shape, state);
|
|
471
|
+
return {
|
|
472
|
+
type: "object",
|
|
473
|
+
...properties && { properties },
|
|
474
|
+
...required && { required },
|
|
475
|
+
...unknownKeys === "strict" && { additionalProperties: false },
|
|
476
|
+
...!isZodType(catchAll, "ZodNever") && {
|
|
477
|
+
additionalProperties: createSchemaObject(catchAll, state, [
|
|
478
|
+
"additional properties"
|
|
479
|
+
])
|
|
480
|
+
}
|
|
481
|
+
};
|
|
482
|
+
};
|
|
483
|
+
var mapRequired = (shape, state) => {
|
|
484
|
+
const required = Object.entries(shape).filter(([_key, zodSchema]) => !isOptionalSchema(zodSchema, state)).map(([key]) => key);
|
|
485
|
+
if (!required.length) {
|
|
486
|
+
return void 0;
|
|
487
|
+
}
|
|
488
|
+
return required;
|
|
489
|
+
};
|
|
490
|
+
var mapProperties = (shape, state) => Object.entries(shape).reduce(
|
|
491
|
+
(acc, [key, zodSchema]) => {
|
|
492
|
+
acc[key] = createSchemaObject(zodSchema, state, [`property: ${key}`]);
|
|
493
|
+
return acc;
|
|
494
|
+
},
|
|
495
|
+
{}
|
|
496
|
+
);
|
|
497
|
+
|
|
498
|
+
// src/create/schema/parsers/pipeline.ts
|
|
499
|
+
var createPipelineSchema = (zodPipeline, state) => {
|
|
500
|
+
if (zodPipeline._def.openapi?.effectType === "input") {
|
|
501
|
+
return createSchemaObject(zodPipeline._def.in, state, ["pipeline input"]);
|
|
502
|
+
}
|
|
503
|
+
if (zodPipeline._def.openapi?.effectType === "output") {
|
|
504
|
+
return createSchemaObject(zodPipeline._def.out, state, ["pipeline output"]);
|
|
505
|
+
}
|
|
506
|
+
if (state.type === "input") {
|
|
507
|
+
state.effectType = "input";
|
|
508
|
+
return createSchemaObject(zodPipeline._def.in, state, ["pipeline input"]);
|
|
509
|
+
}
|
|
510
|
+
state.effectType = "output";
|
|
511
|
+
return createSchemaObject(zodPipeline._def.out, state, ["pipeline output"]);
|
|
512
|
+
};
|
|
513
|
+
|
|
514
|
+
// src/create/schema/parsers/preprocess.ts
|
|
515
|
+
var createPreprocessSchema = (zodPreprocess, state) => createSchemaObject(zodPreprocess._def.schema, state, ["preprocess schema"]);
|
|
516
|
+
|
|
517
|
+
// src/create/schema/parsers/readonly.ts
|
|
518
|
+
var createReadonlySchema = (zodReadonly, state) => (
|
|
519
|
+
// Readonly doesn't change OpenAPI schema
|
|
520
|
+
createSchemaObject(zodReadonly._def.innerType, state, ["readonly"])
|
|
521
|
+
);
|
|
522
|
+
|
|
523
|
+
// src/create/schema/parsers/record.ts
|
|
524
|
+
var createRecordSchema = (zodRecord, state) => {
|
|
525
|
+
const additionalProperties = createSchemaObject(
|
|
526
|
+
zodRecord.valueSchema,
|
|
527
|
+
state,
|
|
528
|
+
["record value"]
|
|
529
|
+
);
|
|
530
|
+
const keySchema = createSchemaObject(zodRecord.keySchema, state, [
|
|
531
|
+
"record key"
|
|
532
|
+
]);
|
|
533
|
+
const maybeComponent = "$ref" in keySchema && state.components.schemas.get(zodRecord.keySchema);
|
|
534
|
+
const maybeSchema = maybeComponent && maybeComponent.type === "complete" && maybeComponent.schemaObject;
|
|
535
|
+
const renderedKeySchema = maybeSchema || keySchema;
|
|
536
|
+
if ("enum" in renderedKeySchema && renderedKeySchema.enum) {
|
|
537
|
+
return {
|
|
538
|
+
type: "object",
|
|
539
|
+
properties: renderedKeySchema.enum.reduce((acc, key) => {
|
|
540
|
+
acc[key] = additionalProperties;
|
|
541
|
+
return acc;
|
|
542
|
+
}, {}),
|
|
543
|
+
additionalProperties: false
|
|
544
|
+
};
|
|
545
|
+
}
|
|
546
|
+
if (satisfiesVersion(state.components.openapi, "3.1.0") && "type" in renderedKeySchema && renderedKeySchema.type === "string" && Object.keys(renderedKeySchema).length > 1) {
|
|
547
|
+
return {
|
|
548
|
+
type: "object",
|
|
549
|
+
// @ts-expect-error FIXME: https://github.com/metadevpro/openapi3-ts/pull/120
|
|
550
|
+
propertyNames: keySchema,
|
|
551
|
+
additionalProperties
|
|
552
|
+
};
|
|
553
|
+
}
|
|
554
|
+
return {
|
|
555
|
+
type: "object",
|
|
556
|
+
additionalProperties
|
|
557
|
+
};
|
|
558
|
+
};
|
|
559
|
+
|
|
560
|
+
// src/create/schema/parsers/refine.ts
|
|
561
|
+
var createRefineSchema = (zodRefine, state) => createSchemaObject(zodRefine._def.schema, state, ["refine schema"]);
|
|
562
|
+
|
|
563
|
+
// src/create/schema/parsers/set.ts
|
|
564
|
+
var createSetSchema = (zodSet, state) => {
|
|
565
|
+
const schema = zodSet._def.valueType;
|
|
566
|
+
const minItems = zodSet._def.minSize?.value;
|
|
567
|
+
const maxItems = zodSet._def.maxSize?.value;
|
|
568
|
+
return {
|
|
569
|
+
type: "array",
|
|
570
|
+
items: createSchemaObject(schema, state, ["set items"]),
|
|
571
|
+
uniqueItems: true,
|
|
572
|
+
...minItems !== void 0 && { minItems },
|
|
573
|
+
...maxItems !== void 0 && { maxItems }
|
|
574
|
+
};
|
|
575
|
+
};
|
|
576
|
+
|
|
577
|
+
// src/create/schema/parsers/string.ts
|
|
578
|
+
var createStringSchema = (zodString) => {
|
|
579
|
+
const zodStringChecks = getZodStringChecks(zodString);
|
|
580
|
+
const format = mapStringFormat(zodStringChecks);
|
|
581
|
+
const patterns = mapPatterns(zodStringChecks);
|
|
582
|
+
const minLength = zodStringChecks.length?.[0]?.value ?? zodStringChecks.min?.[0]?.value;
|
|
583
|
+
const maxLength = zodStringChecks.length?.[0]?.value ?? zodStringChecks.max?.[0]?.value;
|
|
584
|
+
if (patterns.length <= 1) {
|
|
585
|
+
return {
|
|
586
|
+
type: "string",
|
|
587
|
+
...format && { format },
|
|
588
|
+
...patterns[0] && { pattern: patterns[0] },
|
|
589
|
+
...minLength !== void 0 && { minLength },
|
|
590
|
+
...maxLength !== void 0 && { maxLength }
|
|
591
|
+
};
|
|
592
|
+
}
|
|
593
|
+
return {
|
|
594
|
+
allOf: [
|
|
595
|
+
{
|
|
596
|
+
type: "string",
|
|
597
|
+
...format && { format },
|
|
598
|
+
...patterns[0] && { pattern: patterns[0] },
|
|
599
|
+
...minLength !== void 0 && { minLength },
|
|
600
|
+
...maxLength !== void 0 && { maxLength }
|
|
601
|
+
},
|
|
602
|
+
...patterns.slice(1).map(
|
|
603
|
+
(pattern) => ({
|
|
604
|
+
type: "string",
|
|
605
|
+
pattern
|
|
606
|
+
})
|
|
607
|
+
)
|
|
608
|
+
]
|
|
609
|
+
};
|
|
610
|
+
};
|
|
611
|
+
var getZodStringChecks = (zodString) => zodString._def.checks.reduce(
|
|
612
|
+
(acc, check) => {
|
|
613
|
+
const mapping = acc[check.kind];
|
|
614
|
+
if (mapping) {
|
|
615
|
+
mapping.push(check);
|
|
616
|
+
return acc;
|
|
617
|
+
}
|
|
618
|
+
acc[check.kind] = [check];
|
|
619
|
+
return acc;
|
|
620
|
+
},
|
|
621
|
+
{}
|
|
622
|
+
);
|
|
623
|
+
var mapPatterns = (zodStringChecks) => {
|
|
624
|
+
const startsWith = mapStartsWith(zodStringChecks);
|
|
625
|
+
const endsWith = mapEndsWith(zodStringChecks);
|
|
626
|
+
const regex = mapRegex(zodStringChecks);
|
|
627
|
+
const includes = mapIncludes(zodStringChecks);
|
|
628
|
+
const patterns = [
|
|
629
|
+
...regex ?? [],
|
|
630
|
+
...startsWith ? [startsWith] : [],
|
|
631
|
+
...endsWith ? [endsWith] : [],
|
|
632
|
+
...includes ?? []
|
|
633
|
+
];
|
|
634
|
+
return patterns;
|
|
635
|
+
};
|
|
636
|
+
var mapStartsWith = (zodStringChecks) => {
|
|
637
|
+
if (zodStringChecks.startsWith?.[0]?.value) {
|
|
638
|
+
return `^${zodStringChecks.startsWith[0].value}`;
|
|
639
|
+
}
|
|
640
|
+
return void 0;
|
|
641
|
+
};
|
|
642
|
+
var mapEndsWith = (zodStringChecks) => {
|
|
643
|
+
if (zodStringChecks.endsWith?.[0]?.value) {
|
|
644
|
+
return `${zodStringChecks.endsWith[0].value}$`;
|
|
645
|
+
}
|
|
646
|
+
return void 0;
|
|
647
|
+
};
|
|
648
|
+
var mapRegex = (zodStringChecks) => zodStringChecks.regex?.map((regexCheck) => regexCheck.regex.source);
|
|
649
|
+
var mapIncludes = (zodStringChecks) => zodStringChecks.includes?.map((includeCheck) => {
|
|
650
|
+
if (includeCheck.position === 0) {
|
|
651
|
+
return `^${includeCheck.value}`;
|
|
652
|
+
}
|
|
653
|
+
if (includeCheck.position) {
|
|
654
|
+
return `^.{${includeCheck.position}}${includeCheck.value}`;
|
|
655
|
+
}
|
|
656
|
+
return includeCheck.value;
|
|
657
|
+
});
|
|
658
|
+
var mapStringFormat = (zodStringChecks) => {
|
|
659
|
+
if (zodStringChecks.uuid) {
|
|
660
|
+
return "uuid";
|
|
661
|
+
}
|
|
662
|
+
if (zodStringChecks.datetime) {
|
|
663
|
+
return "date-time";
|
|
664
|
+
}
|
|
665
|
+
if (zodStringChecks.email) {
|
|
666
|
+
return "email";
|
|
667
|
+
}
|
|
668
|
+
if (zodStringChecks.url) {
|
|
669
|
+
return "uri";
|
|
670
|
+
}
|
|
671
|
+
return void 0;
|
|
672
|
+
};
|
|
673
|
+
|
|
674
|
+
// src/create/schema/parsers/transform.ts
|
|
675
|
+
var createTransformSchema = (zodTransform, state) => {
|
|
676
|
+
if (zodTransform._def.openapi?.effectType === "output") {
|
|
677
|
+
return createManualTypeSchema(zodTransform, state);
|
|
678
|
+
}
|
|
679
|
+
if (zodTransform._def.openapi?.effectType === "input") {
|
|
680
|
+
return createSchemaObject(zodTransform._def.schema, state, [
|
|
681
|
+
"transform input"
|
|
682
|
+
]);
|
|
683
|
+
}
|
|
684
|
+
if (state.type === "output") {
|
|
685
|
+
return createManualTypeSchema(zodTransform, state);
|
|
686
|
+
}
|
|
687
|
+
state.effectType = "input";
|
|
688
|
+
return createSchemaObject(zodTransform._def.schema, state, [
|
|
689
|
+
"transform input"
|
|
690
|
+
]);
|
|
691
|
+
};
|
|
692
|
+
var throwTransformError = (zodType, state) => {
|
|
693
|
+
throw new Error(
|
|
694
|
+
`${JSON.stringify(zodType)} at ${state.path.join(
|
|
695
|
+
" > "
|
|
696
|
+
)} contains a transformation but is used in both an input and an output. This is likely a mistake. Set an \`effectType\`, wrap it in a ZodPipeline or assign it a manual type to resolve`
|
|
697
|
+
);
|
|
698
|
+
};
|
|
699
|
+
|
|
700
|
+
// src/create/schema/parsers/tuple.ts
|
|
701
|
+
var createTupleSchema = (zodTuple, state) => {
|
|
702
|
+
const items = zodTuple.items;
|
|
703
|
+
const rest = zodTuple._def.rest;
|
|
704
|
+
return {
|
|
705
|
+
type: "array",
|
|
706
|
+
...mapItemProperties(items, rest, state)
|
|
707
|
+
};
|
|
708
|
+
};
|
|
709
|
+
var mapPrefixItems = (items, state) => {
|
|
710
|
+
if (items.length) {
|
|
711
|
+
return items.map(
|
|
712
|
+
(item, index) => createSchemaObject(item, state, [`tuple item ${index}`])
|
|
713
|
+
);
|
|
714
|
+
}
|
|
715
|
+
return void 0;
|
|
716
|
+
};
|
|
717
|
+
var mapItemProperties = (items, rest, state) => {
|
|
718
|
+
const prefixItems = mapPrefixItems(items, state);
|
|
719
|
+
if (satisfiesVersion(state.components.openapi, "3.1.0")) {
|
|
720
|
+
if (!rest) {
|
|
721
|
+
return {
|
|
722
|
+
maxItems: items.length,
|
|
723
|
+
minItems: items.length,
|
|
724
|
+
...prefixItems && { prefixItems }
|
|
725
|
+
};
|
|
726
|
+
}
|
|
727
|
+
return {
|
|
728
|
+
items: createSchemaObject(rest, state, ["tuple items"]),
|
|
729
|
+
...prefixItems && { prefixItems }
|
|
730
|
+
};
|
|
731
|
+
}
|
|
732
|
+
if (!rest) {
|
|
733
|
+
return {
|
|
734
|
+
maxItems: items.length,
|
|
735
|
+
minItems: items.length,
|
|
736
|
+
...prefixItems && { items: { oneOf: prefixItems } }
|
|
737
|
+
};
|
|
738
|
+
}
|
|
739
|
+
return {
|
|
740
|
+
...prefixItems && {
|
|
741
|
+
items: {
|
|
742
|
+
oneOf: [
|
|
743
|
+
...prefixItems,
|
|
744
|
+
createSchemaObject(rest, state, ["tuple items"])
|
|
745
|
+
]
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
};
|
|
749
|
+
};
|
|
750
|
+
|
|
751
|
+
// src/create/schema/parsers/union.ts
|
|
752
|
+
var createUnionSchema = (zodUnion, state) => {
|
|
753
|
+
const schemas = zodUnion.options.map(
|
|
754
|
+
(option, index) => createSchemaObject(option, state, [`union option ${index}`])
|
|
755
|
+
);
|
|
756
|
+
if (zodUnion._def.openapi?.unionOneOf) {
|
|
757
|
+
return {
|
|
758
|
+
oneOf: schemas
|
|
759
|
+
};
|
|
760
|
+
}
|
|
761
|
+
return {
|
|
762
|
+
anyOf: schemas
|
|
763
|
+
};
|
|
27
764
|
};
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
765
|
+
|
|
766
|
+
// src/create/schema/parsers/unknown.ts
|
|
767
|
+
var createUnknownSchema = (_zodUnknown) => ({});
|
|
768
|
+
|
|
769
|
+
// src/create/schema/parsers/index.ts
|
|
770
|
+
var createSchemaSwitch = (zodSchema, state) => {
|
|
771
|
+
if (zodSchema._def.openapi?.type) {
|
|
772
|
+
return createManualTypeSchema(zodSchema, state);
|
|
773
|
+
}
|
|
774
|
+
if (isZodType(zodSchema, "ZodString")) {
|
|
775
|
+
return createStringSchema(zodSchema);
|
|
776
|
+
}
|
|
777
|
+
if (isZodType(zodSchema, "ZodNumber")) {
|
|
778
|
+
return createNumberSchema(zodSchema, state);
|
|
779
|
+
}
|
|
780
|
+
if (isZodType(zodSchema, "ZodBoolean")) {
|
|
781
|
+
return createBooleanSchema(zodSchema);
|
|
782
|
+
}
|
|
783
|
+
if (isZodType(zodSchema, "ZodEnum")) {
|
|
784
|
+
return createEnumSchema(zodSchema);
|
|
785
|
+
}
|
|
786
|
+
if (isZodType(zodSchema, "ZodLiteral")) {
|
|
787
|
+
return createLiteralSchema(zodSchema);
|
|
788
|
+
}
|
|
789
|
+
if (isZodType(zodSchema, "ZodNativeEnum")) {
|
|
790
|
+
return createNativeEnumSchema(zodSchema, state);
|
|
791
|
+
}
|
|
792
|
+
if (isZodType(zodSchema, "ZodArray")) {
|
|
793
|
+
return createArraySchema(zodSchema, state);
|
|
794
|
+
}
|
|
795
|
+
if (isZodType(zodSchema, "ZodObject")) {
|
|
796
|
+
return createObjectSchema(zodSchema, state);
|
|
797
|
+
}
|
|
798
|
+
if (isZodType(zodSchema, "ZodUnion")) {
|
|
799
|
+
return createUnionSchema(zodSchema, state);
|
|
800
|
+
}
|
|
801
|
+
if (isZodType(zodSchema, "ZodDiscriminatedUnion")) {
|
|
802
|
+
return createDiscriminatedUnionSchema(zodSchema, state);
|
|
803
|
+
}
|
|
804
|
+
if (isZodType(zodSchema, "ZodNull")) {
|
|
805
|
+
return createNullSchema(zodSchema);
|
|
806
|
+
}
|
|
807
|
+
if (isZodType(zodSchema, "ZodNullable")) {
|
|
808
|
+
return createNullableSchema(zodSchema, state);
|
|
809
|
+
}
|
|
810
|
+
if (isZodType(zodSchema, "ZodOptional")) {
|
|
811
|
+
return createOptionalSchema(zodSchema, state);
|
|
812
|
+
}
|
|
813
|
+
if (isZodType(zodSchema, "ZodReadonly")) {
|
|
814
|
+
return createReadonlySchema(zodSchema, state);
|
|
815
|
+
}
|
|
816
|
+
if (isZodType(zodSchema, "ZodDefault")) {
|
|
817
|
+
return createDefaultSchema(zodSchema, state);
|
|
818
|
+
}
|
|
819
|
+
if (isZodType(zodSchema, "ZodRecord")) {
|
|
820
|
+
return createRecordSchema(zodSchema, state);
|
|
821
|
+
}
|
|
822
|
+
if (isZodType(zodSchema, "ZodTuple")) {
|
|
823
|
+
return createTupleSchema(zodSchema, state);
|
|
824
|
+
}
|
|
825
|
+
if (isZodType(zodSchema, "ZodDate")) {
|
|
826
|
+
return createDateSchema(zodSchema);
|
|
827
|
+
}
|
|
828
|
+
if (isZodType(zodSchema, "ZodPipeline")) {
|
|
829
|
+
return createPipelineSchema(zodSchema, state);
|
|
830
|
+
}
|
|
831
|
+
if (isZodType(zodSchema, "ZodEffects") && zodSchema._def.effect.type === "transform") {
|
|
832
|
+
return createTransformSchema(zodSchema, state);
|
|
833
|
+
}
|
|
834
|
+
if (isZodType(zodSchema, "ZodEffects") && zodSchema._def.effect.type === "preprocess") {
|
|
835
|
+
return createPreprocessSchema(zodSchema, state);
|
|
836
|
+
}
|
|
837
|
+
if (isZodType(zodSchema, "ZodEffects") && zodSchema._def.effect.type === "refinement") {
|
|
838
|
+
return createRefineSchema(zodSchema, state);
|
|
839
|
+
}
|
|
840
|
+
if (isZodType(zodSchema, "ZodNativeEnum")) {
|
|
841
|
+
return createNativeEnumSchema(zodSchema, state);
|
|
842
|
+
}
|
|
843
|
+
if (isZodType(zodSchema, "ZodIntersection")) {
|
|
844
|
+
return createIntersectionSchema(zodSchema, state);
|
|
845
|
+
}
|
|
846
|
+
if (isZodType(zodSchema, "ZodCatch")) {
|
|
847
|
+
return createCatchSchema(zodSchema, state);
|
|
848
|
+
}
|
|
849
|
+
if (isZodType(zodSchema, "ZodUnknown") || isZodType(zodSchema, "ZodAny")) {
|
|
850
|
+
return createUnknownSchema(zodSchema);
|
|
851
|
+
}
|
|
852
|
+
if (isZodType(zodSchema, "ZodLazy")) {
|
|
853
|
+
return createLazySchema(zodSchema, state);
|
|
854
|
+
}
|
|
855
|
+
if (isZodType(zodSchema, "ZodBranded")) {
|
|
856
|
+
return createBrandedSchema(zodSchema, state);
|
|
857
|
+
}
|
|
858
|
+
if (isZodType(zodSchema, "ZodSet")) {
|
|
859
|
+
return createSetSchema(zodSchema, state);
|
|
860
|
+
}
|
|
861
|
+
return createManualTypeSchema(zodSchema, state);
|
|
862
|
+
};
|
|
863
|
+
|
|
864
|
+
// src/create/schema/index.ts
|
|
865
|
+
var newSchemaState = (state) => ({
|
|
866
|
+
type: state.type,
|
|
867
|
+
components: state.components,
|
|
868
|
+
path: [...state.path],
|
|
869
|
+
visited: new Set(state.visited)
|
|
870
|
+
});
|
|
871
|
+
var createNewSchema = (zodSchema, newState, subpath) => {
|
|
872
|
+
newState.path.push(...subpath);
|
|
873
|
+
if (newState.visited.has(zodSchema)) {
|
|
874
|
+
throw new Error(
|
|
875
|
+
`The schema at ${newState.path.join(
|
|
876
|
+
" > "
|
|
877
|
+
)} needs to be registered because it's circularly referenced`
|
|
878
|
+
);
|
|
879
|
+
}
|
|
880
|
+
newState.visited.add(zodSchema);
|
|
881
|
+
const {
|
|
882
|
+
effectType,
|
|
883
|
+
param,
|
|
884
|
+
header,
|
|
885
|
+
ref,
|
|
886
|
+
refType,
|
|
887
|
+
unionOneOf,
|
|
888
|
+
...additionalMetadata
|
|
889
|
+
} = zodSchema._def.openapi ?? {};
|
|
890
|
+
const schema = createSchemaSwitch(zodSchema, newState);
|
|
891
|
+
const description = zodSchema.description;
|
|
892
|
+
const schemaWithMetadata = enhanceWithMetadata(schema, {
|
|
893
|
+
...description && { description },
|
|
894
|
+
...additionalMetadata
|
|
895
|
+
});
|
|
896
|
+
return {
|
|
897
|
+
schema: schemaWithMetadata,
|
|
898
|
+
newState
|
|
899
|
+
};
|
|
900
|
+
};
|
|
901
|
+
var createNewRef = (ref, zodSchema, state, subpath) => {
|
|
902
|
+
state.components.schemas.set(zodSchema, {
|
|
903
|
+
type: "in-progress",
|
|
904
|
+
ref
|
|
905
|
+
});
|
|
906
|
+
const newSchema = createNewSchema(
|
|
907
|
+
zodSchema,
|
|
908
|
+
newSchemaState({ ...state, visited: /* @__PURE__ */ new Set() }),
|
|
909
|
+
subpath
|
|
910
|
+
);
|
|
911
|
+
state.components.schemas.set(zodSchema, {
|
|
912
|
+
type: "complete",
|
|
913
|
+
ref,
|
|
914
|
+
schemaObject: newSchema.schema,
|
|
915
|
+
creationType: newSchema.newState?.effectType
|
|
916
|
+
});
|
|
917
|
+
return {
|
|
918
|
+
schema: { $ref: createComponentSchemaRef(ref) },
|
|
919
|
+
newState: newSchema.newState
|
|
920
|
+
};
|
|
921
|
+
};
|
|
922
|
+
var createExistingRef = (zodSchema, component, state, subpath) => {
|
|
923
|
+
const newState = newSchemaState(state);
|
|
924
|
+
newState.path.push(...subpath);
|
|
925
|
+
if (component && component.type === "complete") {
|
|
926
|
+
if (component.creationType && component.creationType !== state.type) {
|
|
927
|
+
throwTransformError(zodSchema, newState);
|
|
928
|
+
}
|
|
929
|
+
return {
|
|
930
|
+
schema: { $ref: createComponentSchemaRef(component.ref) },
|
|
931
|
+
newState: {
|
|
932
|
+
...newState,
|
|
933
|
+
effectType: component.creationType
|
|
934
|
+
}
|
|
935
|
+
};
|
|
936
|
+
}
|
|
937
|
+
if (component && component.type === "in-progress") {
|
|
938
|
+
return {
|
|
939
|
+
schema: { $ref: createComponentSchemaRef(component.ref) },
|
|
940
|
+
newState
|
|
941
|
+
};
|
|
942
|
+
}
|
|
943
|
+
return;
|
|
944
|
+
};
|
|
945
|
+
var createSchemaOrRef = (zodSchema, state, subpath) => {
|
|
946
|
+
const component = state.components.schemas.get(zodSchema);
|
|
947
|
+
const existingRef = createExistingRef(zodSchema, component, state, subpath);
|
|
948
|
+
if (existingRef) {
|
|
949
|
+
return existingRef;
|
|
950
|
+
}
|
|
951
|
+
const ref = zodSchema._def.openapi?.ref ?? component?.ref;
|
|
952
|
+
if (ref) {
|
|
953
|
+
return createNewRef(ref, zodSchema, state, subpath);
|
|
954
|
+
}
|
|
955
|
+
return createNewSchema(zodSchema, newSchemaState(state), subpath);
|
|
956
|
+
};
|
|
957
|
+
var createSchemaObject = (zodSchema, state, subpath) => {
|
|
958
|
+
const { schema, newState } = createSchemaOrRef(zodSchema, state, subpath);
|
|
959
|
+
if (newState?.effectType) {
|
|
960
|
+
if (state.type !== newState?.effectType || state.effectType && newState.effectType !== state.effectType) {
|
|
961
|
+
throwTransformError(zodSchema, newState);
|
|
962
|
+
}
|
|
963
|
+
state.effectType = newState.effectType;
|
|
964
|
+
}
|
|
965
|
+
return schema;
|
|
966
|
+
};
|
|
967
|
+
|
|
968
|
+
// src/create/parameters.ts
|
|
969
|
+
var createComponentParamRef = (ref) => `#/components/parameters/${ref}`;
|
|
970
|
+
var createBaseParameter = (schema, components, subpath) => {
|
|
971
|
+
const { ref, ...rest } = schema._def.openapi?.param ?? {};
|
|
972
|
+
const state = newSchemaState({
|
|
973
|
+
components,
|
|
974
|
+
type: "input",
|
|
975
|
+
path: [],
|
|
976
|
+
visited: /* @__PURE__ */ new Set()
|
|
977
|
+
});
|
|
978
|
+
const schemaObject = createSchemaObject(schema, state, [
|
|
979
|
+
...subpath,
|
|
980
|
+
"schema"
|
|
981
|
+
]);
|
|
982
|
+
const required = !isOptionalSchema(schema, state);
|
|
983
|
+
return {
|
|
984
|
+
...rest,
|
|
985
|
+
...schema && { schema: schemaObject },
|
|
986
|
+
...required && { required }
|
|
987
|
+
};
|
|
988
|
+
};
|
|
989
|
+
var createParamOrRef = (zodSchema, components, subpath, type, name) => {
|
|
990
|
+
const component = components.parameters.get(zodSchema);
|
|
991
|
+
const paramType = zodSchema._def?.openapi?.param?.in ?? component?.in ?? type;
|
|
992
|
+
const paramName = zodSchema._def?.openapi?.param?.name ?? component?.name ?? name;
|
|
993
|
+
if (!paramType) {
|
|
994
|
+
throw new Error("Parameter type missing");
|
|
995
|
+
}
|
|
996
|
+
if (!paramName) {
|
|
997
|
+
throw new Error("Parameter name missing");
|
|
998
|
+
}
|
|
999
|
+
if (component && component.type === "complete") {
|
|
1000
|
+
if (!("$ref" in component.paramObject) && (component.in !== type || component.name !== name)) {
|
|
1001
|
+
throw new Error(`parameterRef "${component.ref}" is already registered`);
|
|
1002
|
+
}
|
|
1003
|
+
return {
|
|
1004
|
+
$ref: createComponentParamRef(component.ref)
|
|
1005
|
+
};
|
|
1006
|
+
}
|
|
1007
|
+
const baseParamOrRef = createBaseParameter(zodSchema, components, subpath);
|
|
1008
|
+
if ("$ref" in baseParamOrRef) {
|
|
1009
|
+
throw new Error("Unexpected Error: received a reference object");
|
|
1010
|
+
}
|
|
1011
|
+
const ref = zodSchema?._def?.openapi?.param?.ref ?? component?.ref;
|
|
1012
|
+
const paramObject = {
|
|
1013
|
+
in: paramType,
|
|
1014
|
+
name: paramName,
|
|
1015
|
+
...baseParamOrRef
|
|
1016
|
+
};
|
|
1017
|
+
if (ref) {
|
|
1018
|
+
components.parameters.set(zodSchema, {
|
|
1019
|
+
type: "complete",
|
|
1020
|
+
paramObject,
|
|
1021
|
+
ref,
|
|
1022
|
+
in: paramType,
|
|
1023
|
+
name: paramName
|
|
1024
|
+
});
|
|
1025
|
+
return {
|
|
1026
|
+
$ref: createComponentParamRef(ref)
|
|
1027
|
+
};
|
|
1028
|
+
}
|
|
1029
|
+
return paramObject;
|
|
1030
|
+
};
|
|
1031
|
+
var createParameters = (type, zodObject, components, subpath) => {
|
|
1032
|
+
if (!zodObject) {
|
|
1033
|
+
return [];
|
|
1034
|
+
}
|
|
1035
|
+
return Object.entries(zodObject.shape).map(
|
|
1036
|
+
([key, zodSchema]) => createParamOrRef(zodSchema, components, [...subpath, key], type, key)
|
|
1037
|
+
);
|
|
1038
|
+
};
|
|
1039
|
+
var createRequestParams = (requestParams, components, subpath) => {
|
|
1040
|
+
if (!requestParams) {
|
|
1041
|
+
return [];
|
|
1042
|
+
}
|
|
1043
|
+
const pathParams = createParameters("path", requestParams.path, components, [
|
|
1044
|
+
...subpath,
|
|
1045
|
+
"path"
|
|
1046
|
+
]);
|
|
1047
|
+
const queryParams = createParameters(
|
|
1048
|
+
"query",
|
|
1049
|
+
requestParams.query,
|
|
1050
|
+
components,
|
|
1051
|
+
[...subpath, "query"]
|
|
1052
|
+
);
|
|
1053
|
+
const cookieParams = createParameters(
|
|
1054
|
+
"cookie",
|
|
1055
|
+
requestParams.cookie,
|
|
1056
|
+
components,
|
|
1057
|
+
[...subpath, "cookie"]
|
|
1058
|
+
);
|
|
1059
|
+
const headerParams = createParameters(
|
|
1060
|
+
"header",
|
|
1061
|
+
requestParams.header,
|
|
1062
|
+
components,
|
|
1063
|
+
[...subpath, "header"]
|
|
1064
|
+
);
|
|
1065
|
+
return [...pathParams, ...queryParams, ...cookieParams, ...headerParams];
|
|
1066
|
+
};
|
|
1067
|
+
var createManualParameters = (parameters, components, subpath) => parameters?.map((param, index) => {
|
|
1068
|
+
if (isAnyZodType(param)) {
|
|
1069
|
+
return createParamOrRef(param, components, [
|
|
1070
|
+
...subpath,
|
|
1071
|
+
`param index ${index}`
|
|
1072
|
+
]);
|
|
1073
|
+
}
|
|
1074
|
+
return param;
|
|
1075
|
+
}) ?? [];
|
|
1076
|
+
var createParametersObject = (parameters, requestParams, components, subpath) => {
|
|
1077
|
+
const manualParameters = createManualParameters(
|
|
1078
|
+
parameters,
|
|
1079
|
+
components,
|
|
1080
|
+
subpath
|
|
1081
|
+
);
|
|
1082
|
+
const createdParams = createRequestParams(requestParams, components, subpath);
|
|
1083
|
+
const combinedParameters = [
|
|
1084
|
+
...manualParameters,
|
|
1085
|
+
...createdParams
|
|
1086
|
+
];
|
|
1087
|
+
return combinedParameters.length ? combinedParameters : void 0;
|
|
1088
|
+
};
|
|
1089
|
+
|
|
1090
|
+
// src/create/content.ts
|
|
1091
|
+
var createMediaTypeSchema = (schemaObject, components, type, subpath) => {
|
|
1092
|
+
if (!schemaObject) {
|
|
1093
|
+
return void 0;
|
|
1094
|
+
}
|
|
1095
|
+
if (!isAnyZodType(schemaObject)) {
|
|
1096
|
+
return schemaObject;
|
|
1097
|
+
}
|
|
1098
|
+
return createSchemaObject(
|
|
1099
|
+
schemaObject,
|
|
1100
|
+
newSchemaState({
|
|
1101
|
+
components,
|
|
1102
|
+
type,
|
|
1103
|
+
path: [],
|
|
1104
|
+
visited: /* @__PURE__ */ new Set()
|
|
1105
|
+
}),
|
|
1106
|
+
subpath
|
|
1107
|
+
);
|
|
1108
|
+
};
|
|
1109
|
+
var createMediaTypeObject = (mediaTypeObject, components, type, subpath) => {
|
|
1110
|
+
if (!mediaTypeObject) {
|
|
1111
|
+
return void 0;
|
|
1112
|
+
}
|
|
1113
|
+
return {
|
|
1114
|
+
...mediaTypeObject,
|
|
1115
|
+
schema: createMediaTypeSchema(mediaTypeObject.schema, components, type, [
|
|
1116
|
+
...subpath,
|
|
1117
|
+
"schema"
|
|
1118
|
+
])
|
|
1119
|
+
};
|
|
1120
|
+
};
|
|
1121
|
+
var createContent = (contentObject, components, type, subpath) => Object.entries(contentObject).reduce(
|
|
1122
|
+
(acc, [mediaType, zodOpenApiMediaTypeObject]) => {
|
|
1123
|
+
const mediaTypeObject = createMediaTypeObject(
|
|
1124
|
+
zodOpenApiMediaTypeObject,
|
|
1125
|
+
components,
|
|
1126
|
+
type,
|
|
1127
|
+
[...subpath, mediaType]
|
|
1128
|
+
);
|
|
1129
|
+
if (mediaTypeObject) {
|
|
1130
|
+
acc[mediaType] = mediaTypeObject;
|
|
1131
|
+
}
|
|
1132
|
+
return acc;
|
|
1133
|
+
},
|
|
1134
|
+
{}
|
|
1135
|
+
);
|
|
1136
|
+
|
|
1137
|
+
// src/create/specificationExtension.ts
|
|
1138
|
+
var isISpecificationExtension = (key) => key.startsWith("x-");
|
|
1139
|
+
|
|
1140
|
+
// src/create/responses.ts
|
|
1141
|
+
var createResponseHeaders = (responseHeaders, components) => {
|
|
1142
|
+
if (!responseHeaders) {
|
|
1143
|
+
return void 0;
|
|
1144
|
+
}
|
|
1145
|
+
if (isAnyZodType(responseHeaders)) {
|
|
1146
|
+
return Object.entries(responseHeaders.shape).reduce((acc, [key, zodSchema]) => {
|
|
1147
|
+
acc[key] = createHeaderOrRef(zodSchema, components);
|
|
1148
|
+
return acc;
|
|
1149
|
+
}, {});
|
|
1150
|
+
}
|
|
1151
|
+
return responseHeaders;
|
|
1152
|
+
};
|
|
1153
|
+
var createHeaderOrRef = (schema, components) => {
|
|
1154
|
+
const component = components.headers.get(schema);
|
|
1155
|
+
if (component && component.type === "complete") {
|
|
1156
|
+
return {
|
|
1157
|
+
$ref: createComponentHeaderRef(component.ref)
|
|
1158
|
+
};
|
|
1159
|
+
}
|
|
1160
|
+
const baseHeader = createBaseHeader(schema, components);
|
|
1161
|
+
if ("$ref" in baseHeader) {
|
|
1162
|
+
throw new Error("Unexpected Error: received a reference object");
|
|
1163
|
+
}
|
|
1164
|
+
const ref = schema._def?.openapi?.header?.ref ?? component?.ref;
|
|
1165
|
+
if (ref) {
|
|
1166
|
+
components.headers.set(schema, {
|
|
1167
|
+
type: "complete",
|
|
1168
|
+
headerObject: baseHeader,
|
|
1169
|
+
ref
|
|
1170
|
+
});
|
|
1171
|
+
return {
|
|
1172
|
+
$ref: createComponentHeaderRef(ref)
|
|
1173
|
+
};
|
|
1174
|
+
}
|
|
1175
|
+
return baseHeader;
|
|
1176
|
+
};
|
|
1177
|
+
var createBaseHeader = (schema, components) => {
|
|
1178
|
+
const { ref, ...rest } = schema._def.openapi?.header ?? {};
|
|
1179
|
+
const state = newSchemaState({
|
|
1180
|
+
components,
|
|
1181
|
+
type: "output",
|
|
1182
|
+
path: [],
|
|
1183
|
+
visited: /* @__PURE__ */ new Set()
|
|
1184
|
+
});
|
|
1185
|
+
const schemaObject = createSchemaObject(schema, state, ["header"]);
|
|
1186
|
+
const required = !isOptionalSchema(schema, state);
|
|
1187
|
+
return {
|
|
1188
|
+
...rest,
|
|
1189
|
+
...schema && { schema: schemaObject },
|
|
1190
|
+
...required && { required }
|
|
1191
|
+
};
|
|
1192
|
+
};
|
|
1193
|
+
var createComponentHeaderRef = (ref) => `#/components/headers/${ref}`;
|
|
1194
|
+
var createResponse = (responseObject, components, subpath) => {
|
|
1195
|
+
if ("$ref" in responseObject) {
|
|
1196
|
+
return responseObject;
|
|
1197
|
+
}
|
|
1198
|
+
const component = components.responses.get(responseObject);
|
|
1199
|
+
if (component && component.type === "complete") {
|
|
1200
|
+
return { $ref: createComponentResponseRef(component.ref) };
|
|
1201
|
+
}
|
|
1202
|
+
const { content, headers, ref, ...rest } = responseObject;
|
|
1203
|
+
const maybeHeaders = createResponseHeaders(headers, components);
|
|
1204
|
+
const response = {
|
|
1205
|
+
...rest,
|
|
1206
|
+
...maybeHeaders && { headers: maybeHeaders },
|
|
1207
|
+
...content && {
|
|
1208
|
+
content: createContent(content, components, "output", [
|
|
1209
|
+
...subpath,
|
|
1210
|
+
"content"
|
|
1211
|
+
])
|
|
1212
|
+
}
|
|
1213
|
+
};
|
|
1214
|
+
const responseRef = ref ?? component?.ref;
|
|
1215
|
+
if (responseRef) {
|
|
1216
|
+
components.responses.set(responseObject, {
|
|
1217
|
+
responseObject: response,
|
|
1218
|
+
ref: responseRef,
|
|
1219
|
+
type: "complete"
|
|
1220
|
+
});
|
|
1221
|
+
return {
|
|
1222
|
+
$ref: createComponentResponseRef(responseRef)
|
|
1223
|
+
};
|
|
1224
|
+
}
|
|
1225
|
+
return response;
|
|
1226
|
+
};
|
|
1227
|
+
var createResponses = (responsesObject, components, subpath) => Object.entries(responsesObject).reduce(
|
|
1228
|
+
(acc, [statusCode, responseObject]) => {
|
|
1229
|
+
if (isISpecificationExtension(statusCode)) {
|
|
1230
|
+
acc[statusCode] = responseObject;
|
|
1231
|
+
return acc;
|
|
1232
|
+
}
|
|
1233
|
+
acc[statusCode] = createResponse(responseObject, components, [
|
|
1234
|
+
...subpath,
|
|
1235
|
+
statusCode
|
|
1236
|
+
]);
|
|
1237
|
+
return acc;
|
|
1238
|
+
},
|
|
1239
|
+
{}
|
|
1240
|
+
);
|
|
1241
|
+
|
|
1242
|
+
// src/create/paths.ts
|
|
1243
|
+
var createRequestBody = (requestBodyObject, components, subpath) => {
|
|
1244
|
+
if (!requestBodyObject) {
|
|
1245
|
+
return void 0;
|
|
1246
|
+
}
|
|
1247
|
+
const component = components.requestBodies.get(requestBodyObject);
|
|
1248
|
+
if (component && component.type === "complete") {
|
|
1249
|
+
return {
|
|
1250
|
+
$ref: createComponentRequestBodyRef(component.ref)
|
|
1251
|
+
};
|
|
1252
|
+
}
|
|
1253
|
+
const ref = requestBodyObject.ref ?? component?.ref;
|
|
1254
|
+
const requestBody = {
|
|
1255
|
+
...requestBodyObject,
|
|
1256
|
+
content: createContent(requestBodyObject.content, components, "input", [
|
|
1257
|
+
...subpath,
|
|
1258
|
+
"content"
|
|
1259
|
+
])
|
|
1260
|
+
};
|
|
1261
|
+
if (ref) {
|
|
1262
|
+
components.requestBodies.set(requestBodyObject, {
|
|
1263
|
+
type: "complete",
|
|
1264
|
+
ref,
|
|
1265
|
+
requestBodyObject: requestBody
|
|
1266
|
+
});
|
|
1267
|
+
return {
|
|
1268
|
+
$ref: createComponentRequestBodyRef(ref)
|
|
1269
|
+
};
|
|
1270
|
+
}
|
|
1271
|
+
return requestBody;
|
|
1272
|
+
};
|
|
1273
|
+
var createOperation = (operationObject, components, subpath) => {
|
|
1274
|
+
const { parameters, requestParams, requestBody, responses, ...rest } = operationObject;
|
|
1275
|
+
const maybeParameters = createParametersObject(
|
|
1276
|
+
parameters,
|
|
1277
|
+
requestParams,
|
|
1278
|
+
components,
|
|
1279
|
+
[...subpath, "parameters"]
|
|
1280
|
+
);
|
|
1281
|
+
const maybeRequestBody = createRequestBody(
|
|
1282
|
+
operationObject.requestBody,
|
|
1283
|
+
components,
|
|
1284
|
+
[...subpath, "request body"]
|
|
1285
|
+
);
|
|
1286
|
+
const maybeResponses = createResponses(
|
|
1287
|
+
operationObject.responses,
|
|
1288
|
+
components,
|
|
1289
|
+
[...subpath, "responses"]
|
|
1290
|
+
);
|
|
1291
|
+
return {
|
|
1292
|
+
...rest,
|
|
1293
|
+
...maybeParameters && { parameters: maybeParameters },
|
|
1294
|
+
...maybeRequestBody && { requestBody: maybeRequestBody },
|
|
1295
|
+
...maybeResponses && { responses: maybeResponses }
|
|
1296
|
+
};
|
|
1297
|
+
};
|
|
1298
|
+
var createPathItem = (pathObject, components, path) => Object.entries(pathObject).reduce(
|
|
1299
|
+
(acc, [key, value]) => {
|
|
1300
|
+
if (!value) {
|
|
1301
|
+
return acc;
|
|
1302
|
+
}
|
|
1303
|
+
if (key === "get" || key === "put" || key === "post" || key === "delete" || key === "options" || key === "head" || key === "patch" || key === "trace") {
|
|
1304
|
+
acc[key] = createOperation(
|
|
1305
|
+
value,
|
|
1306
|
+
components,
|
|
1307
|
+
[path, key]
|
|
1308
|
+
);
|
|
1309
|
+
return acc;
|
|
1310
|
+
}
|
|
1311
|
+
acc[key] = value;
|
|
1312
|
+
return acc;
|
|
1313
|
+
},
|
|
1314
|
+
{}
|
|
1315
|
+
);
|
|
1316
|
+
var createPaths = (pathsObject, components) => {
|
|
1317
|
+
if (!pathsObject) {
|
|
1318
|
+
return void 0;
|
|
1319
|
+
}
|
|
1320
|
+
return Object.entries(pathsObject).reduce(
|
|
1321
|
+
(acc, [path, pathItemObject]) => {
|
|
1322
|
+
if (isISpecificationExtension(path)) {
|
|
1323
|
+
acc[path] = pathItemObject;
|
|
1324
|
+
return acc;
|
|
1325
|
+
}
|
|
1326
|
+
acc[path] = createPathItem(pathItemObject, components, path);
|
|
1327
|
+
return acc;
|
|
1328
|
+
},
|
|
1329
|
+
{}
|
|
1330
|
+
);
|
|
1331
|
+
};
|
|
1332
|
+
|
|
1333
|
+
// src/create/components.ts
|
|
1334
|
+
var getDefaultComponents = (componentsObject, openapi = "3.1.0") => {
|
|
1335
|
+
const defaultComponents = {
|
|
1336
|
+
schemas: /* @__PURE__ */ new Map(),
|
|
1337
|
+
parameters: /* @__PURE__ */ new Map(),
|
|
1338
|
+
headers: /* @__PURE__ */ new Map(),
|
|
1339
|
+
requestBodies: /* @__PURE__ */ new Map(),
|
|
1340
|
+
responses: /* @__PURE__ */ new Map(),
|
|
1341
|
+
openapi
|
|
1342
|
+
};
|
|
1343
|
+
if (!componentsObject) {
|
|
1344
|
+
return defaultComponents;
|
|
1345
|
+
}
|
|
1346
|
+
getSchemas(componentsObject.schemas, defaultComponents);
|
|
1347
|
+
getParameters(componentsObject.parameters, defaultComponents);
|
|
1348
|
+
getRequestBodies(componentsObject.requestBodies, defaultComponents);
|
|
1349
|
+
getHeaders(componentsObject.headers, defaultComponents);
|
|
1350
|
+
getResponses(componentsObject.responses, defaultComponents);
|
|
1351
|
+
return defaultComponents;
|
|
1352
|
+
};
|
|
1353
|
+
var getSchemas = (schemas, components) => {
|
|
1354
|
+
if (!schemas) {
|
|
1355
|
+
return;
|
|
1356
|
+
}
|
|
1357
|
+
Object.entries(schemas).forEach(([key, schema]) => {
|
|
1358
|
+
if (isAnyZodType(schema)) {
|
|
1359
|
+
if (components.schemas.has(schema)) {
|
|
1360
|
+
throw new Error(
|
|
1361
|
+
`Schema ${JSON.stringify(schema._def)} is already registered`
|
|
1362
|
+
);
|
|
1363
|
+
}
|
|
1364
|
+
const ref = schema._def.openapi?.ref ?? key;
|
|
1365
|
+
components.schemas.set(schema, {
|
|
1366
|
+
type: "manual",
|
|
1367
|
+
ref
|
|
1368
|
+
});
|
|
1369
|
+
}
|
|
1370
|
+
});
|
|
1371
|
+
};
|
|
1372
|
+
var getParameters = (parameters, components) => {
|
|
1373
|
+
if (!parameters) {
|
|
1374
|
+
return;
|
|
1375
|
+
}
|
|
1376
|
+
Object.entries(parameters).forEach(([key, schema]) => {
|
|
1377
|
+
if (isAnyZodType(schema)) {
|
|
1378
|
+
if (components.parameters.has(schema)) {
|
|
1379
|
+
throw new Error(
|
|
1380
|
+
`Parameter ${JSON.stringify(schema._def)} is already registered`
|
|
1381
|
+
);
|
|
1382
|
+
}
|
|
1383
|
+
const ref = schema._def.openapi?.param?.ref ?? key;
|
|
1384
|
+
const name = schema._def.openapi?.param?.name;
|
|
1385
|
+
const location = schema._def.openapi?.param?.in;
|
|
1386
|
+
if (!name || !location) {
|
|
1387
|
+
throw new Error("`name` or `in` missing in .openapi()");
|
|
1388
|
+
}
|
|
1389
|
+
components.parameters.set(schema, {
|
|
1390
|
+
type: "manual",
|
|
1391
|
+
ref,
|
|
1392
|
+
in: location,
|
|
1393
|
+
name
|
|
1394
|
+
});
|
|
1395
|
+
}
|
|
1396
|
+
});
|
|
1397
|
+
};
|
|
1398
|
+
var getHeaders = (responseHeaders, components) => {
|
|
1399
|
+
if (!responseHeaders) {
|
|
1400
|
+
return;
|
|
1401
|
+
}
|
|
1402
|
+
Object.entries(responseHeaders).forEach(([key, schema]) => {
|
|
1403
|
+
if (isAnyZodType(schema)) {
|
|
1404
|
+
if (components.parameters.has(schema)) {
|
|
1405
|
+
throw new Error(
|
|
1406
|
+
`Header ${JSON.stringify(schema._def)} is already registered`
|
|
1407
|
+
);
|
|
1408
|
+
}
|
|
1409
|
+
const ref = schema._def.openapi?.param?.ref ?? key;
|
|
1410
|
+
components.headers.set(schema, {
|
|
1411
|
+
type: "manual",
|
|
1412
|
+
ref
|
|
1413
|
+
});
|
|
1414
|
+
}
|
|
1415
|
+
});
|
|
1416
|
+
};
|
|
1417
|
+
var getResponses = (responses, components) => {
|
|
1418
|
+
if (!responses) {
|
|
1419
|
+
return;
|
|
1420
|
+
}
|
|
1421
|
+
Object.entries(responses).forEach(([key, responseObject]) => {
|
|
1422
|
+
if (components.responses.has(responseObject)) {
|
|
1423
|
+
throw new Error(
|
|
1424
|
+
`Header ${JSON.stringify(responseObject)} is already registered`
|
|
1425
|
+
);
|
|
1426
|
+
}
|
|
1427
|
+
const ref = responseObject?.ref ?? key;
|
|
1428
|
+
components.responses.set(responseObject, {
|
|
1429
|
+
type: "manual",
|
|
1430
|
+
ref
|
|
1431
|
+
});
|
|
1432
|
+
});
|
|
1433
|
+
};
|
|
1434
|
+
var getRequestBodies = (requestBodies, components) => {
|
|
1435
|
+
if (!requestBodies) {
|
|
1436
|
+
return;
|
|
1437
|
+
}
|
|
1438
|
+
Object.entries(requestBodies).forEach(([key, requestBody]) => {
|
|
1439
|
+
if (components.requestBodies.has(requestBody)) {
|
|
1440
|
+
throw new Error(
|
|
1441
|
+
`Header ${JSON.stringify(requestBody)} is already registered`
|
|
1442
|
+
);
|
|
1443
|
+
}
|
|
1444
|
+
const ref = requestBody?.ref ?? key;
|
|
1445
|
+
components.requestBodies.set(requestBody, {
|
|
1446
|
+
type: "manual",
|
|
1447
|
+
ref
|
|
1448
|
+
});
|
|
1449
|
+
});
|
|
1450
|
+
};
|
|
1451
|
+
var createComponentSchemaRef = (schemaRef) => `#/components/schemas/${schemaRef}`;
|
|
1452
|
+
var createComponentResponseRef = (responseRef) => `#/components/responses/${responseRef}`;
|
|
1453
|
+
var createComponentRequestBodyRef = (requestBodyRef) => `#/components/requestBodies/${requestBodyRef}`;
|
|
1454
|
+
var createComponents = (componentsObject, components) => {
|
|
1455
|
+
const combinedSchemas = createSchemaComponents(componentsObject, components);
|
|
1456
|
+
const combinedParameters = createParamComponents(
|
|
1457
|
+
componentsObject,
|
|
1458
|
+
components
|
|
1459
|
+
);
|
|
1460
|
+
const combinedHeaders = createHeaderComponents(componentsObject, components);
|
|
1461
|
+
const combinedResponses = createResponseComponents(components);
|
|
1462
|
+
const combinedRequestBodies = createRequestBodiesComponents(components);
|
|
1463
|
+
const { schemas, parameters, headers, responses, requestBodies, ...rest } = componentsObject;
|
|
1464
|
+
const finalComponents = {
|
|
1465
|
+
...rest,
|
|
1466
|
+
...combinedSchemas && { schemas: combinedSchemas },
|
|
1467
|
+
...combinedParameters && { parameters: combinedParameters },
|
|
1468
|
+
...combinedRequestBodies && { requestBodies: combinedRequestBodies },
|
|
1469
|
+
...combinedHeaders && { headers: combinedHeaders },
|
|
1470
|
+
...combinedResponses && { responses: combinedResponses }
|
|
1471
|
+
};
|
|
1472
|
+
return Object.keys(finalComponents).length ? finalComponents : void 0;
|
|
1473
|
+
};
|
|
1474
|
+
var createSchemaComponents = (componentsObject, components) => {
|
|
1475
|
+
Array.from(components.schemas).forEach(([schema, { type }], index) => {
|
|
1476
|
+
if (type === "manual") {
|
|
1477
|
+
const state = newSchemaState({
|
|
1478
|
+
components,
|
|
1479
|
+
type: schema._def.openapi?.refType ?? "output",
|
|
1480
|
+
path: [],
|
|
1481
|
+
visited: /* @__PURE__ */ new Set()
|
|
1482
|
+
});
|
|
1483
|
+
createSchemaObject(schema, state, [`component schema index ${index}`]);
|
|
1484
|
+
}
|
|
1485
|
+
});
|
|
1486
|
+
const customComponents = Object.entries(
|
|
1487
|
+
componentsObject.schemas ?? {}
|
|
1488
|
+
).reduce(
|
|
1489
|
+
(acc, [key, value]) => {
|
|
1490
|
+
if (isAnyZodType(value)) {
|
|
1491
|
+
return acc;
|
|
1492
|
+
}
|
|
1493
|
+
if (acc[key]) {
|
|
1494
|
+
throw new Error(`Schema "${key}" is already registered`);
|
|
1495
|
+
}
|
|
1496
|
+
acc[key] = value;
|
|
1497
|
+
return acc;
|
|
1498
|
+
},
|
|
1499
|
+
{}
|
|
1500
|
+
);
|
|
1501
|
+
const finalComponents = Array.from(components.schemas).reduce((acc, [_zodType, component]) => {
|
|
1502
|
+
if (component.type === "complete") {
|
|
1503
|
+
if (acc[component.ref]) {
|
|
1504
|
+
throw new Error(`Schema "${component.ref}" is already registered`);
|
|
1505
|
+
}
|
|
1506
|
+
acc[component.ref] = component.schemaObject;
|
|
1507
|
+
}
|
|
1508
|
+
return acc;
|
|
1509
|
+
}, customComponents);
|
|
1510
|
+
return Object.keys(finalComponents).length ? finalComponents : void 0;
|
|
1511
|
+
};
|
|
1512
|
+
var createParamComponents = (componentsObject, components) => {
|
|
1513
|
+
Array.from(components.parameters).forEach(([schema, component], index) => {
|
|
1514
|
+
if (component.type === "manual") {
|
|
1515
|
+
createParamOrRef(
|
|
1516
|
+
schema,
|
|
1517
|
+
components,
|
|
1518
|
+
[`component parameter index ${index}`],
|
|
1519
|
+
component.in,
|
|
1520
|
+
component.ref
|
|
1521
|
+
);
|
|
1522
|
+
}
|
|
1523
|
+
});
|
|
1524
|
+
const customComponents = Object.entries(
|
|
1525
|
+
componentsObject.parameters ?? {}
|
|
1526
|
+
).reduce(
|
|
1527
|
+
(acc, [key, value]) => {
|
|
1528
|
+
if (!isAnyZodType(value)) {
|
|
1529
|
+
if (acc[key]) {
|
|
1530
|
+
throw new Error(`Parameter "${key}" is already registered`);
|
|
1531
|
+
}
|
|
1532
|
+
acc[key] = value;
|
|
1533
|
+
}
|
|
1534
|
+
return acc;
|
|
1535
|
+
},
|
|
1536
|
+
{}
|
|
1537
|
+
);
|
|
1538
|
+
const finalComponents = Array.from(components.parameters).reduce((acc, [_zodType, component]) => {
|
|
1539
|
+
if (component.type === "complete") {
|
|
1540
|
+
if (acc[component.ref]) {
|
|
1541
|
+
throw new Error(`Parameter "${component.ref}" is already registered`);
|
|
1542
|
+
}
|
|
1543
|
+
acc[component.ref] = component.paramObject;
|
|
1544
|
+
}
|
|
1545
|
+
return acc;
|
|
1546
|
+
}, customComponents);
|
|
1547
|
+
return Object.keys(finalComponents).length ? finalComponents : void 0;
|
|
1548
|
+
};
|
|
1549
|
+
var createHeaderComponents = (componentsObject, components) => {
|
|
1550
|
+
Array.from(components.headers).forEach(([schema, component]) => {
|
|
1551
|
+
if (component.type === "manual") {
|
|
1552
|
+
createHeaderOrRef(schema, components);
|
|
1553
|
+
}
|
|
1554
|
+
});
|
|
1555
|
+
const headers = componentsObject.headers ?? {};
|
|
1556
|
+
const customComponents = Object.entries(headers).reduce((acc, [key, value]) => {
|
|
1557
|
+
if (!isAnyZodType(value)) {
|
|
1558
|
+
if (acc[key]) {
|
|
1559
|
+
throw new Error(`Header Ref "${key}" is already registered`);
|
|
1560
|
+
}
|
|
1561
|
+
acc[key] = value;
|
|
1562
|
+
}
|
|
1563
|
+
return acc;
|
|
1564
|
+
}, {});
|
|
1565
|
+
const finalComponents = Array.from(components.headers).reduce((acc, [_zodType, component]) => {
|
|
1566
|
+
if (component.type === "complete") {
|
|
1567
|
+
if (acc[component.ref]) {
|
|
1568
|
+
throw new Error(`Header "${component.ref}" is already registered`);
|
|
1569
|
+
}
|
|
1570
|
+
acc[component.ref] = component.headerObject;
|
|
1571
|
+
}
|
|
1572
|
+
return acc;
|
|
1573
|
+
}, customComponents);
|
|
1574
|
+
return Object.keys(finalComponents).length ? finalComponents : void 0;
|
|
1575
|
+
};
|
|
1576
|
+
var createResponseComponents = (components) => {
|
|
1577
|
+
Array.from(components.responses).forEach(([schema, component], index) => {
|
|
1578
|
+
if (component.type === "manual") {
|
|
1579
|
+
createResponse(schema, components, [`component response index ${index}`]);
|
|
1580
|
+
}
|
|
1581
|
+
});
|
|
1582
|
+
const finalComponents = Array.from(components.responses).reduce((acc, [_zodType, component]) => {
|
|
1583
|
+
if (component.type === "complete") {
|
|
1584
|
+
if (acc[component.ref]) {
|
|
1585
|
+
throw new Error(`Response "${component.ref}" is already registered`);
|
|
1586
|
+
}
|
|
1587
|
+
acc[component.ref] = component.responseObject;
|
|
1588
|
+
}
|
|
1589
|
+
return acc;
|
|
1590
|
+
}, {});
|
|
1591
|
+
return Object.keys(finalComponents).length ? finalComponents : void 0;
|
|
1592
|
+
};
|
|
1593
|
+
var createRequestBodiesComponents = (components) => {
|
|
1594
|
+
Array.from(components.requestBodies).forEach(([schema, component], index) => {
|
|
1595
|
+
if (component.type === "manual") {
|
|
1596
|
+
createRequestBody(schema, components, [
|
|
1597
|
+
`component request body ${index}`
|
|
1598
|
+
]);
|
|
1599
|
+
}
|
|
1600
|
+
});
|
|
1601
|
+
const finalComponents = Array.from(components.requestBodies).reduce((acc, [_zodType, component]) => {
|
|
1602
|
+
if (component.type === "complete") {
|
|
1603
|
+
if (acc[component.ref]) {
|
|
1604
|
+
throw new Error(`RequestBody "${component.ref}" is already registered`);
|
|
1605
|
+
}
|
|
1606
|
+
acc[component.ref] = component.requestBodyObject;
|
|
1607
|
+
}
|
|
1608
|
+
return acc;
|
|
1609
|
+
}, {});
|
|
1610
|
+
return Object.keys(finalComponents).length ? finalComponents : void 0;
|
|
1611
|
+
};
|
|
1612
|
+
|
|
1613
|
+
// src/create/document.ts
|
|
1614
|
+
var createDocument = (zodOpenApiObject) => {
|
|
1615
|
+
const { paths, webhooks, components = {}, ...rest } = zodOpenApiObject;
|
|
1616
|
+
const defaultComponents = getDefaultComponents(
|
|
1617
|
+
components,
|
|
1618
|
+
zodOpenApiObject.openapi
|
|
1619
|
+
);
|
|
1620
|
+
const createdPaths = createPaths(paths, defaultComponents);
|
|
1621
|
+
const createdWebhooks = createPaths(webhooks, defaultComponents);
|
|
1622
|
+
const createdComponents = createComponents(components, defaultComponents);
|
|
1623
|
+
return {
|
|
1624
|
+
...rest,
|
|
1625
|
+
...createdPaths && { paths: createdPaths },
|
|
1626
|
+
...createdWebhooks && { webhooks: createdWebhooks },
|
|
1627
|
+
...createdComponents && { components: createdComponents }
|
|
1628
|
+
};
|
|
1629
|
+
};
|
|
1630
|
+
|
|
1631
|
+
// src/extendZod.ts
|
|
1632
|
+
function extendZodWithOpenApi(zod) {
|
|
1633
|
+
if (typeof zod.ZodType.prototype.openapi !== "undefined") {
|
|
1634
|
+
return;
|
|
1635
|
+
}
|
|
1636
|
+
zod.ZodType.prototype.openapi = function(openapi) {
|
|
1637
|
+
const result = new this.constructor({
|
|
1638
|
+
...this._def,
|
|
1639
|
+
openapi
|
|
1640
|
+
});
|
|
1641
|
+
return result;
|
|
1642
|
+
};
|
|
1643
|
+
const zodObjectExtend = zod.ZodObject.prototype.extend;
|
|
1644
|
+
zod.ZodObject.prototype.extend = function(...args) {
|
|
1645
|
+
const extendResult = zodObjectExtend.apply(this, args);
|
|
1646
|
+
extendResult._def.extendMetadata = {
|
|
1647
|
+
extends: this
|
|
1648
|
+
};
|
|
1649
|
+
delete extendResult._def.openapi;
|
|
1650
|
+
return extendResult;
|
|
1651
|
+
};
|
|
1652
|
+
const zodObjectOmit = zod.ZodObject.prototype.omit;
|
|
1653
|
+
zod.ZodObject.prototype.omit = function(...args) {
|
|
1654
|
+
const omitResult = zodObjectOmit.apply(this, args);
|
|
1655
|
+
delete omitResult._def.extendMetadata;
|
|
1656
|
+
delete omitResult._def.openapi;
|
|
1657
|
+
return omitResult;
|
|
1658
|
+
};
|
|
1659
|
+
const zodObjectPick = zod.ZodObject.prototype.pick;
|
|
1660
|
+
zod.ZodObject.prototype.pick = function(...args) {
|
|
1661
|
+
const pickResult = zodObjectPick.apply(this, args);
|
|
1662
|
+
delete pickResult._def.extendMetadata;
|
|
1663
|
+
delete pickResult._def.openapi;
|
|
1664
|
+
return pickResult;
|
|
1665
|
+
};
|
|
1666
|
+
}
|
|
1667
|
+
|
|
1668
|
+
// src/openapi3-ts/dist/oas30.ts
|
|
1669
|
+
var oas30_exports = {};
|
|
1670
|
+
|
|
1671
|
+
// src/openapi3-ts/dist/oas31.ts
|
|
1672
|
+
var oas31_exports = {};
|
|
1673
|
+
|
|
1674
|
+
// src/api.ts
|
|
1675
|
+
var api_exports = {};
|
|
1676
|
+
__export(api_exports, {
|
|
1677
|
+
createComponents: () => createComponents,
|
|
1678
|
+
createMediaTypeSchema: () => createMediaTypeSchema,
|
|
1679
|
+
createParamOrRef: () => createParamOrRef,
|
|
1680
|
+
getDefaultComponents: () => getDefaultComponents
|
|
1681
|
+
});
|
|
1682
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
1683
|
+
0 && (module.exports = {
|
|
1684
|
+
api,
|
|
1685
|
+
createDocument,
|
|
1686
|
+
extendZodWithOpenApi,
|
|
1687
|
+
oas30,
|
|
1688
|
+
oas31
|
|
1689
|
+
});
|