@kubb/plugin-oas 3.0.0-alpha.15 → 3.0.0-alpha.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{OperationGenerator-BqfyX5-8.d.cts → OperationGenerator-B5CoXmjA.d.cts} +22 -20
- package/dist/{OperationGenerator-BqfyX5-8.d.ts → OperationGenerator-B5CoXmjA.d.ts} +22 -20
- package/dist/{Schema-BgpIlA79.d.cts → Schema-BsgDay0E.d.cts} +1 -12
- package/dist/{Schema-ExMIJOhD.d.ts → Schema-Dvt6i_X3.d.ts} +1 -12
- package/dist/{chunk-PZTS5YZV.js → chunk-AWRVBD2X.js} +15 -13
- package/dist/chunk-AWRVBD2X.js.map +1 -0
- package/dist/chunk-BEKIIF6H.cjs +1110 -0
- package/dist/chunk-BEKIIF6H.cjs.map +1 -0
- package/dist/chunk-CQMUJN22.cjs +42 -0
- package/dist/chunk-CQMUJN22.cjs.map +1 -0
- package/dist/chunk-FGOPORE3.js +38 -0
- package/dist/chunk-FGOPORE3.js.map +1 -0
- package/dist/{chunk-AAIEMW6V.cjs → chunk-FZABPEYO.cjs} +28 -798
- package/dist/chunk-FZABPEYO.cjs.map +1 -0
- package/dist/{chunk-M3V3WMCW.js → chunk-I6N3YPCE.js} +11 -773
- package/dist/chunk-I6N3YPCE.js.map +1 -0
- package/dist/chunk-IVSE3A7W.cjs +84 -0
- package/dist/chunk-IVSE3A7W.cjs.map +1 -0
- package/dist/chunk-VLHK3XAR.js +1098 -0
- package/dist/chunk-VLHK3XAR.js.map +1 -0
- package/dist/chunk-XIFGKAM3.js +76 -0
- package/dist/chunk-XIFGKAM3.js.map +1 -0
- package/dist/{chunk-I3ZL3GUQ.cjs → chunk-XON3FUPP.cjs} +19 -17
- package/dist/chunk-XON3FUPP.cjs.map +1 -0
- package/dist/components.cjs +5 -6
- package/dist/components.d.cts +2 -2
- package/dist/components.d.ts +2 -2
- package/dist/components.js +2 -3
- package/dist/generators.cjs +4 -5
- package/dist/generators.d.cts +1 -1
- package/dist/generators.d.ts +1 -1
- package/dist/generators.js +3 -4
- package/dist/hooks.cjs +102 -31
- package/dist/hooks.cjs.map +1 -1
- package/dist/hooks.d.cts +39 -6
- package/dist/hooks.d.ts +39 -6
- package/dist/hooks.js +92 -20
- package/dist/hooks.js.map +1 -1
- package/dist/index.cjs +25 -410
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +5 -400
- package/dist/index.js.map +1 -1
- package/dist/utils.cjs +12 -12
- package/dist/utils.cjs.map +1 -1
- package/dist/utils.d.cts +2 -2
- package/dist/utils.d.ts +2 -2
- package/dist/utils.js +2 -3
- package/dist/utils.js.map +1 -1
- package/package.json +10 -10
- package/src/OperationGenerator.ts +11 -15
- package/src/SchemaGenerator.ts +15 -6
- package/src/components/Schema.tsx +1 -98
- package/src/generator.tsx +22 -14
- package/src/generators/jsonGenerator.ts +4 -4
- package/src/hooks/index.ts +2 -0
- package/src/hooks/useOperationManager.ts +76 -29
- package/src/hooks/useSchemaManager.ts +76 -0
- package/src/utils/getParams.ts +1 -1
- package/dist/chunk-2PMRS7PB.cjs +0 -8
- package/dist/chunk-2PMRS7PB.cjs.map +0 -1
- package/dist/chunk-2V64GHQ4.js +0 -32
- package/dist/chunk-2V64GHQ4.js.map +0 -1
- package/dist/chunk-4JIFKRM5.js +0 -6
- package/dist/chunk-4JIFKRM5.js.map +0 -1
- package/dist/chunk-5ULY3TQB.cjs +0 -90
- package/dist/chunk-5ULY3TQB.cjs.map +0 -1
- package/dist/chunk-AAIEMW6V.cjs.map +0 -1
- package/dist/chunk-CFU6KMOB.cjs +0 -38
- package/dist/chunk-CFU6KMOB.cjs.map +0 -1
- package/dist/chunk-GAPWY2CE.js +0 -84
- package/dist/chunk-GAPWY2CE.js.map +0 -1
- package/dist/chunk-I3ZL3GUQ.cjs.map +0 -1
- package/dist/chunk-M3V3WMCW.js.map +0 -1
- package/dist/chunk-PZTS5YZV.js.map +0 -1
|
@@ -0,0 +1,1110 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunkIVSE3A7W_cjs = require('./chunk-IVSE3A7W.cjs');
|
|
4
|
+
var chunkXON3FUPP_cjs = require('./chunk-XON3FUPP.cjs');
|
|
5
|
+
var chunkCQMUJN22_cjs = require('./chunk-CQMUJN22.cjs');
|
|
6
|
+
var core = require('@kubb/core');
|
|
7
|
+
var transformers = require('@kubb/core/transformers');
|
|
8
|
+
var path = require('path');
|
|
9
|
+
var utils = require('@kubb/core/utils');
|
|
10
|
+
var oas = require('@kubb/oas');
|
|
11
|
+
var remeda = require('remeda');
|
|
12
|
+
|
|
13
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
14
|
+
|
|
15
|
+
var transformers__default = /*#__PURE__*/_interopDefault(transformers);
|
|
16
|
+
var path__default = /*#__PURE__*/_interopDefault(path);
|
|
17
|
+
|
|
18
|
+
// src/index.ts
|
|
19
|
+
chunkCQMUJN22_cjs.init_cjs_shims();
|
|
20
|
+
|
|
21
|
+
// src/OperationGenerator.ts
|
|
22
|
+
chunkCQMUJN22_cjs.init_cjs_shims();
|
|
23
|
+
var OperationGenerator = class extends core.BaseGenerator {
|
|
24
|
+
#operationsByMethod = {};
|
|
25
|
+
get operationsByMethod() {
|
|
26
|
+
return this.#operationsByMethod;
|
|
27
|
+
}
|
|
28
|
+
set operationsByMethod(paths) {
|
|
29
|
+
this.#operationsByMethod = paths;
|
|
30
|
+
}
|
|
31
|
+
#getOptions(operation, method) {
|
|
32
|
+
const { override = [] } = this.context;
|
|
33
|
+
return override.find(({ pattern, type }) => {
|
|
34
|
+
if (type === "tag") {
|
|
35
|
+
return !!operation.getTags()[0]?.name.match(pattern);
|
|
36
|
+
}
|
|
37
|
+
if (type === "operationId") {
|
|
38
|
+
return !!operation.getOperationId().match(pattern);
|
|
39
|
+
}
|
|
40
|
+
if (type === "path") {
|
|
41
|
+
return !!operation.path.match(pattern);
|
|
42
|
+
}
|
|
43
|
+
if (type === "method") {
|
|
44
|
+
return !!method.match(pattern);
|
|
45
|
+
}
|
|
46
|
+
return false;
|
|
47
|
+
})?.options || {};
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
*
|
|
51
|
+
* @deprecated
|
|
52
|
+
*/
|
|
53
|
+
#isExcluded(operation, method) {
|
|
54
|
+
const { exclude = [] } = this.context;
|
|
55
|
+
let matched = false;
|
|
56
|
+
exclude.forEach(({ pattern, type }) => {
|
|
57
|
+
if (type === "tag" && !matched) {
|
|
58
|
+
matched = !!operation.getTags()[0]?.name.match(pattern);
|
|
59
|
+
}
|
|
60
|
+
if (type === "operationId" && !matched) {
|
|
61
|
+
matched = !!operation.getOperationId().match(pattern);
|
|
62
|
+
}
|
|
63
|
+
if (type === "path" && !matched) {
|
|
64
|
+
matched = !!operation.path.match(pattern);
|
|
65
|
+
}
|
|
66
|
+
if (type === "method" && !matched) {
|
|
67
|
+
matched = !!method.match(pattern);
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
return matched;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
*
|
|
74
|
+
* @deprecated
|
|
75
|
+
*/
|
|
76
|
+
#isIncluded(operation, method) {
|
|
77
|
+
const { include = [] } = this.context;
|
|
78
|
+
let matched = false;
|
|
79
|
+
include.forEach(({ pattern, type }) => {
|
|
80
|
+
if (type === "tag" && !matched) {
|
|
81
|
+
matched = !!operation.getTags()[0]?.name.match(pattern);
|
|
82
|
+
}
|
|
83
|
+
if (type === "operationId" && !matched) {
|
|
84
|
+
matched = !!operation.getOperationId().match(pattern);
|
|
85
|
+
}
|
|
86
|
+
if (type === "path" && !matched) {
|
|
87
|
+
matched = !!operation.path.match(pattern);
|
|
88
|
+
}
|
|
89
|
+
if (type === "method" && !matched) {
|
|
90
|
+
matched = !!method.match(pattern);
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
return matched;
|
|
94
|
+
}
|
|
95
|
+
getSchemas(operation, {
|
|
96
|
+
resolveName = (name) => name
|
|
97
|
+
} = {}) {
|
|
98
|
+
const pathParamsSchema = this.context.oas.getParametersSchema(operation, "path");
|
|
99
|
+
const queryParamsSchema = this.context.oas.getParametersSchema(operation, "query");
|
|
100
|
+
const headerParamsSchema = this.context.oas.getParametersSchema(operation, "header");
|
|
101
|
+
const requestSchema = this.context.oas.getRequestSchema(operation);
|
|
102
|
+
const responseStatusCode = operation.schema.responses && Object.keys(operation.schema.responses).find((key) => key.startsWith("2")) || 200;
|
|
103
|
+
const responseSchema = this.context.oas.getResponseSchema(operation, responseStatusCode);
|
|
104
|
+
const statusCodes = operation.getResponseStatusCodes().map((statusCode) => {
|
|
105
|
+
let name = statusCode;
|
|
106
|
+
if (name === "default") {
|
|
107
|
+
name = "error";
|
|
108
|
+
}
|
|
109
|
+
const schema = this.context.oas.getResponseSchema(operation, statusCode);
|
|
110
|
+
return {
|
|
111
|
+
name: resolveName(transformers__default.default.pascalCase(`${operation.getOperationId()} ${name}`)),
|
|
112
|
+
description: operation.getResponseByStatusCode(statusCode)?.description,
|
|
113
|
+
schema,
|
|
114
|
+
operation,
|
|
115
|
+
operationName: transformers__default.default.pascalCase(`${operation.getOperationId()}`),
|
|
116
|
+
statusCode: name === "error" ? void 0 : Number(statusCode),
|
|
117
|
+
keys: schema?.properties ? Object.keys(schema.properties) : void 0
|
|
118
|
+
};
|
|
119
|
+
});
|
|
120
|
+
return {
|
|
121
|
+
pathParams: pathParamsSchema ? {
|
|
122
|
+
name: resolveName(transformers__default.default.pascalCase(`${operation.getOperationId()} PathParams`)),
|
|
123
|
+
operation,
|
|
124
|
+
operationName: transformers__default.default.pascalCase(`${operation.getOperationId()}`),
|
|
125
|
+
schema: pathParamsSchema,
|
|
126
|
+
keys: pathParamsSchema.properties ? Object.keys(pathParamsSchema.properties) : void 0
|
|
127
|
+
} : void 0,
|
|
128
|
+
queryParams: queryParamsSchema ? {
|
|
129
|
+
name: resolveName(transformers__default.default.pascalCase(`${operation.getOperationId()} QueryParams`)),
|
|
130
|
+
operation,
|
|
131
|
+
operationName: transformers__default.default.pascalCase(`${operation.getOperationId()}`),
|
|
132
|
+
schema: queryParamsSchema,
|
|
133
|
+
keys: queryParamsSchema.properties ? Object.keys(queryParamsSchema.properties) : []
|
|
134
|
+
} : void 0,
|
|
135
|
+
headerParams: headerParamsSchema ? {
|
|
136
|
+
name: resolveName(transformers__default.default.pascalCase(`${operation.getOperationId()} HeaderParams`)),
|
|
137
|
+
operation,
|
|
138
|
+
operationName: transformers__default.default.pascalCase(`${operation.getOperationId()}`),
|
|
139
|
+
schema: headerParamsSchema,
|
|
140
|
+
keys: headerParamsSchema.properties ? Object.keys(headerParamsSchema.properties) : void 0
|
|
141
|
+
} : void 0,
|
|
142
|
+
request: requestSchema ? {
|
|
143
|
+
name: resolveName(transformers__default.default.pascalCase(`${operation.getOperationId()} ${operation.method === "get" ? "queryRequest" : "mutationRequest"}`)),
|
|
144
|
+
description: operation.schema.requestBody?.description,
|
|
145
|
+
operation,
|
|
146
|
+
operationName: transformers__default.default.pascalCase(`${operation.getOperationId()}`),
|
|
147
|
+
schema: requestSchema,
|
|
148
|
+
keys: requestSchema.properties ? Object.keys(requestSchema.properties) : void 0,
|
|
149
|
+
keysToOmit: requestSchema.properties ? Object.keys(requestSchema.properties).filter((key) => {
|
|
150
|
+
const item = requestSchema.properties?.[key];
|
|
151
|
+
return item?.readOnly;
|
|
152
|
+
}) : void 0
|
|
153
|
+
} : void 0,
|
|
154
|
+
response: {
|
|
155
|
+
name: resolveName(transformers__default.default.pascalCase(`${operation.getOperationId()} ${operation.method === "get" ? "queryResponse" : "mutationResponse"}`)),
|
|
156
|
+
description: operation.getResponseAsJSONSchema(responseStatusCode)?.at(0)?.description,
|
|
157
|
+
operation,
|
|
158
|
+
operationName: transformers__default.default.pascalCase(`${operation.getOperationId()}`),
|
|
159
|
+
schema: responseSchema,
|
|
160
|
+
statusCode: Number(responseStatusCode),
|
|
161
|
+
keys: responseSchema?.properties ? Object.keys(responseSchema.properties) : void 0,
|
|
162
|
+
keysToOmit: responseSchema?.properties ? Object.keys(responseSchema.properties).filter((key) => {
|
|
163
|
+
const item = responseSchema.properties?.[key];
|
|
164
|
+
return item?.writeOnly;
|
|
165
|
+
}) : void 0
|
|
166
|
+
},
|
|
167
|
+
errors: statusCodes.filter((item) => item.statusCode?.toString().startsWith("4") || item.statusCode?.toString().startsWith("5")),
|
|
168
|
+
statusCodes
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
#methods = ["get", "post", "patch", "put", "delete"];
|
|
172
|
+
async build(...generators) {
|
|
173
|
+
const { oas } = this.context;
|
|
174
|
+
const paths = oas.getPaths();
|
|
175
|
+
this.operationsByMethod = Object.entries(paths).reduce((acc, [path2, method]) => {
|
|
176
|
+
const methods = Object.keys(method);
|
|
177
|
+
methods.forEach((method2) => {
|
|
178
|
+
const operation = oas.operation(path2, method2);
|
|
179
|
+
if (operation && [this.#methods].some((methods2) => method2 === operation.method)) {
|
|
180
|
+
const isExcluded = this.#isExcluded(operation, method2);
|
|
181
|
+
const isIncluded = this.context.include ? this.#isIncluded(operation, method2) : true;
|
|
182
|
+
if (isIncluded && !isExcluded) {
|
|
183
|
+
if (!acc[path2]) {
|
|
184
|
+
acc[path2] = {};
|
|
185
|
+
}
|
|
186
|
+
acc[path2] = {
|
|
187
|
+
...acc[path2],
|
|
188
|
+
[method2]: {
|
|
189
|
+
operation,
|
|
190
|
+
schemas: this.getSchemas(operation)
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
});
|
|
196
|
+
return acc;
|
|
197
|
+
}, {});
|
|
198
|
+
const promises = Object.keys(this.operationsByMethod).reduce((acc, path2) => {
|
|
199
|
+
const methods = this.operationsByMethod[path2] ? Object.keys(this.operationsByMethod[path2]) : [];
|
|
200
|
+
methods.forEach((method) => {
|
|
201
|
+
const { operation } = this.operationsByMethod[path2]?.[method];
|
|
202
|
+
const options = this.#getOptions(operation, method);
|
|
203
|
+
const methodToCall = this[method];
|
|
204
|
+
if (typeof methodToCall === "function") {
|
|
205
|
+
const promiseMethod = methodToCall?.call(this, operation, {
|
|
206
|
+
...this.options,
|
|
207
|
+
...options
|
|
208
|
+
});
|
|
209
|
+
if (promiseMethod) {
|
|
210
|
+
acc.push(promiseMethod);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
const promiseOperation = this.operation.call(this, operation, {
|
|
214
|
+
...this.options,
|
|
215
|
+
...options
|
|
216
|
+
});
|
|
217
|
+
if (promiseOperation) {
|
|
218
|
+
acc.push(promiseOperation);
|
|
219
|
+
}
|
|
220
|
+
generators?.forEach((generator) => {
|
|
221
|
+
const promise = generator.operation?.({
|
|
222
|
+
instance: this,
|
|
223
|
+
operation,
|
|
224
|
+
options: {
|
|
225
|
+
...this.options,
|
|
226
|
+
...options
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
if (promise) {
|
|
230
|
+
acc.push(promise);
|
|
231
|
+
}
|
|
232
|
+
});
|
|
233
|
+
});
|
|
234
|
+
return acc;
|
|
235
|
+
}, []);
|
|
236
|
+
const operations = Object.values(this.operationsByMethod).map((item) => Object.values(item).map((item2) => item2.operation));
|
|
237
|
+
promises.push(this.all(operations.flat().filter(Boolean), this.operationsByMethod));
|
|
238
|
+
generators?.forEach((generator) => {
|
|
239
|
+
const promise = generator.operations?.({
|
|
240
|
+
instance: this,
|
|
241
|
+
operations: operations.flat().filter(Boolean),
|
|
242
|
+
operationsByMethod: this.operationsByMethod,
|
|
243
|
+
options: this.options
|
|
244
|
+
});
|
|
245
|
+
if (promise) {
|
|
246
|
+
promises.push(promise);
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
const files = await Promise.all(promises);
|
|
250
|
+
return files.flat().filter(Boolean);
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Operation
|
|
254
|
+
*/
|
|
255
|
+
async operation(operation, options) {
|
|
256
|
+
return [];
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* GET
|
|
260
|
+
*/
|
|
261
|
+
async get(operation, options) {
|
|
262
|
+
return [];
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* POST
|
|
266
|
+
*/
|
|
267
|
+
async post(operation, options) {
|
|
268
|
+
return [];
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* PATCH
|
|
272
|
+
*/
|
|
273
|
+
async patch(operation, options) {
|
|
274
|
+
return [];
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* PUT
|
|
278
|
+
*/
|
|
279
|
+
async put(operation, options) {
|
|
280
|
+
return [];
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* DELETE
|
|
284
|
+
*/
|
|
285
|
+
async delete(operation, options) {
|
|
286
|
+
return [];
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Combination of GET, POST, PATCH, PUT, DELETE
|
|
290
|
+
*/
|
|
291
|
+
async all(operations, paths) {
|
|
292
|
+
return [];
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
// src/plugin.ts
|
|
297
|
+
chunkCQMUJN22_cjs.init_cjs_shims();
|
|
298
|
+
|
|
299
|
+
// src/SchemaGenerator.ts
|
|
300
|
+
chunkCQMUJN22_cjs.init_cjs_shims();
|
|
301
|
+
|
|
302
|
+
// src/SchemaMapper.ts
|
|
303
|
+
chunkCQMUJN22_cjs.init_cjs_shims();
|
|
304
|
+
var schemaKeywords = {
|
|
305
|
+
any: "any",
|
|
306
|
+
strict: "strict",
|
|
307
|
+
unknown: "unknown",
|
|
308
|
+
number: "number",
|
|
309
|
+
integer: "integer",
|
|
310
|
+
string: "string",
|
|
311
|
+
boolean: "boolean",
|
|
312
|
+
undefined: "undefined",
|
|
313
|
+
nullable: "nullable",
|
|
314
|
+
null: "null",
|
|
315
|
+
nullish: "nullish",
|
|
316
|
+
array: "array",
|
|
317
|
+
tuple: "tuple",
|
|
318
|
+
enum: "enum",
|
|
319
|
+
union: "union",
|
|
320
|
+
datetime: "datetime",
|
|
321
|
+
date: "date",
|
|
322
|
+
email: "email",
|
|
323
|
+
uuid: "uuid",
|
|
324
|
+
url: "url",
|
|
325
|
+
/* intersection */
|
|
326
|
+
default: "default",
|
|
327
|
+
const: "const",
|
|
328
|
+
and: "and",
|
|
329
|
+
describe: "describe",
|
|
330
|
+
min: "min",
|
|
331
|
+
max: "max",
|
|
332
|
+
optional: "optional",
|
|
333
|
+
readOnly: "readOnly",
|
|
334
|
+
// custom ones
|
|
335
|
+
object: "object",
|
|
336
|
+
ref: "ref",
|
|
337
|
+
matches: "matches",
|
|
338
|
+
firstName: "firstName",
|
|
339
|
+
lastName: "lastName",
|
|
340
|
+
password: "password",
|
|
341
|
+
phone: "phone",
|
|
342
|
+
blob: "blob",
|
|
343
|
+
deprecated: "deprecated",
|
|
344
|
+
example: "example",
|
|
345
|
+
schema: "schema",
|
|
346
|
+
catchall: "catchall",
|
|
347
|
+
time: "time",
|
|
348
|
+
name: "name"
|
|
349
|
+
};
|
|
350
|
+
function isKeyword(meta, keyword) {
|
|
351
|
+
return meta.keyword === keyword;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// src/SchemaGenerator.ts
|
|
355
|
+
var SchemaGenerator = class _SchemaGenerator extends core.BaseGenerator {
|
|
356
|
+
// Collect the types of all referenced schemas, so we can export them later
|
|
357
|
+
refs = {};
|
|
358
|
+
// Keep track of already used type aliases
|
|
359
|
+
#usedAliasNames = {};
|
|
360
|
+
/**
|
|
361
|
+
* Creates a type node from a given schema.
|
|
362
|
+
* Delegates to getBaseTypeFromSchema internally and
|
|
363
|
+
* optionally adds a union with null.
|
|
364
|
+
*/
|
|
365
|
+
parse(props) {
|
|
366
|
+
const options = this.#getOptions(props);
|
|
367
|
+
const defaultSchemas = this.#parseSchemaObject(props);
|
|
368
|
+
const schemas = options.transformers?.schema?.(props, defaultSchemas) || defaultSchemas || [];
|
|
369
|
+
return remeda.uniqueWith(schemas, remeda.isDeepEqual);
|
|
370
|
+
}
|
|
371
|
+
deepSearch(tree, keyword) {
|
|
372
|
+
return _SchemaGenerator.deepSearch(tree, keyword);
|
|
373
|
+
}
|
|
374
|
+
find(tree, keyword) {
|
|
375
|
+
return _SchemaGenerator.find(tree, keyword);
|
|
376
|
+
}
|
|
377
|
+
static deepSearch(tree, keyword) {
|
|
378
|
+
const foundItems = [];
|
|
379
|
+
tree?.forEach((schema) => {
|
|
380
|
+
if (schema.keyword === keyword) {
|
|
381
|
+
foundItems.push(schema);
|
|
382
|
+
}
|
|
383
|
+
if (schema.keyword === schemaKeywords.object) {
|
|
384
|
+
const subItem = schema;
|
|
385
|
+
Object.values(subItem.args?.properties || {}).forEach((entrySchema) => {
|
|
386
|
+
foundItems.push(..._SchemaGenerator.deepSearch(entrySchema, keyword));
|
|
387
|
+
});
|
|
388
|
+
Object.values(subItem.args?.additionalProperties || {}).forEach((entrySchema) => {
|
|
389
|
+
foundItems.push(..._SchemaGenerator.deepSearch([entrySchema], keyword));
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
if (schema.keyword === schemaKeywords.array) {
|
|
393
|
+
const subItem = schema;
|
|
394
|
+
subItem.args.items.forEach((entrySchema) => {
|
|
395
|
+
foundItems.push(..._SchemaGenerator.deepSearch([entrySchema], keyword));
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
if (schema.keyword === schemaKeywords.and) {
|
|
399
|
+
const subItem = schema;
|
|
400
|
+
subItem.args.forEach((entrySchema) => {
|
|
401
|
+
foundItems.push(..._SchemaGenerator.deepSearch([entrySchema], keyword));
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
if (schema.keyword === schemaKeywords.tuple) {
|
|
405
|
+
const subItem = schema;
|
|
406
|
+
subItem.args.items.forEach((entrySchema) => {
|
|
407
|
+
foundItems.push(..._SchemaGenerator.deepSearch([entrySchema], keyword));
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
if (schema.keyword === schemaKeywords.union) {
|
|
411
|
+
const subItem = schema;
|
|
412
|
+
subItem.args.forEach((entrySchema) => {
|
|
413
|
+
foundItems.push(..._SchemaGenerator.deepSearch([entrySchema], keyword));
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
});
|
|
417
|
+
return foundItems;
|
|
418
|
+
}
|
|
419
|
+
static findInObject(tree, keyword) {
|
|
420
|
+
let foundItem = void 0;
|
|
421
|
+
tree?.forEach((schema) => {
|
|
422
|
+
if (!foundItem && schema.keyword === keyword) {
|
|
423
|
+
foundItem = schema;
|
|
424
|
+
}
|
|
425
|
+
if (schema.keyword === schemaKeywords.object) {
|
|
426
|
+
const subItem = schema;
|
|
427
|
+
Object.values(subItem.args?.properties || {}).forEach((entrySchema) => {
|
|
428
|
+
if (!foundItem) {
|
|
429
|
+
foundItem = _SchemaGenerator.find(entrySchema, keyword);
|
|
430
|
+
}
|
|
431
|
+
});
|
|
432
|
+
Object.values(subItem.args?.additionalProperties || {}).forEach((entrySchema) => {
|
|
433
|
+
if (!foundItem) {
|
|
434
|
+
foundItem = _SchemaGenerator.find([entrySchema], keyword);
|
|
435
|
+
}
|
|
436
|
+
});
|
|
437
|
+
}
|
|
438
|
+
});
|
|
439
|
+
return foundItem;
|
|
440
|
+
}
|
|
441
|
+
static find(tree, keyword) {
|
|
442
|
+
let foundItem = void 0;
|
|
443
|
+
tree?.forEach((schema) => {
|
|
444
|
+
if (!foundItem && schema.keyword === keyword) {
|
|
445
|
+
foundItem = schema;
|
|
446
|
+
}
|
|
447
|
+
if (schema.keyword === schemaKeywords.array) {
|
|
448
|
+
const subItem = schema;
|
|
449
|
+
subItem.args.items.forEach((entrySchema) => {
|
|
450
|
+
if (!foundItem) {
|
|
451
|
+
foundItem = _SchemaGenerator.find([entrySchema], keyword);
|
|
452
|
+
}
|
|
453
|
+
});
|
|
454
|
+
}
|
|
455
|
+
if (schema.keyword === schemaKeywords.and) {
|
|
456
|
+
const subItem = schema;
|
|
457
|
+
subItem.args.forEach((entrySchema) => {
|
|
458
|
+
if (!foundItem) {
|
|
459
|
+
foundItem = _SchemaGenerator.find([entrySchema], keyword);
|
|
460
|
+
}
|
|
461
|
+
});
|
|
462
|
+
}
|
|
463
|
+
if (schema.keyword === schemaKeywords.tuple) {
|
|
464
|
+
const subItem = schema;
|
|
465
|
+
subItem.args.items.forEach((entrySchema) => {
|
|
466
|
+
if (!foundItem) {
|
|
467
|
+
foundItem = _SchemaGenerator.find([entrySchema], keyword);
|
|
468
|
+
}
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
if (schema.keyword === schemaKeywords.union) {
|
|
472
|
+
const subItem = schema;
|
|
473
|
+
subItem.args.forEach((entrySchema) => {
|
|
474
|
+
if (!foundItem) {
|
|
475
|
+
foundItem = _SchemaGenerator.find([entrySchema], keyword);
|
|
476
|
+
}
|
|
477
|
+
});
|
|
478
|
+
}
|
|
479
|
+
});
|
|
480
|
+
return foundItem;
|
|
481
|
+
}
|
|
482
|
+
#getUsedEnumNames(props) {
|
|
483
|
+
const options = this.#getOptions(props);
|
|
484
|
+
return options.usedEnumNames || {};
|
|
485
|
+
}
|
|
486
|
+
#getOptions({ name }) {
|
|
487
|
+
const { override = [] } = this.context;
|
|
488
|
+
return {
|
|
489
|
+
...this.options,
|
|
490
|
+
...override.find(({ pattern, type }) => {
|
|
491
|
+
if (name && type === "schemaName") {
|
|
492
|
+
return !!name.match(pattern);
|
|
493
|
+
}
|
|
494
|
+
return false;
|
|
495
|
+
})?.options || {}
|
|
496
|
+
};
|
|
497
|
+
}
|
|
498
|
+
#getUnknownReturn(props) {
|
|
499
|
+
const options = this.#getOptions(props);
|
|
500
|
+
if (options.unknownType === "any") {
|
|
501
|
+
return schemaKeywords.any;
|
|
502
|
+
}
|
|
503
|
+
return schemaKeywords.unknown;
|
|
504
|
+
}
|
|
505
|
+
/**
|
|
506
|
+
* Recursively creates a type literal with the given props.
|
|
507
|
+
*/
|
|
508
|
+
#parseProperties({ schema, name }) {
|
|
509
|
+
const properties = schema?.properties || {};
|
|
510
|
+
const additionalProperties = schema?.additionalProperties;
|
|
511
|
+
const required = schema?.required;
|
|
512
|
+
const propertiesSchemas = Object.keys(properties).map((propertyName) => {
|
|
513
|
+
const validationFunctions = [];
|
|
514
|
+
const propertySchema = properties[propertyName];
|
|
515
|
+
const isRequired = Array.isArray(required) ? required?.includes(propertyName) : !!required;
|
|
516
|
+
const nullable = propertySchema.nullable ?? propertySchema["x-nullable"] ?? false;
|
|
517
|
+
validationFunctions.push(...this.parse({ schema: propertySchema, name: propertyName, parentName: name }));
|
|
518
|
+
validationFunctions.push({
|
|
519
|
+
keyword: schemaKeywords.name,
|
|
520
|
+
args: propertyName
|
|
521
|
+
});
|
|
522
|
+
if (!isRequired && nullable) {
|
|
523
|
+
validationFunctions.push({ keyword: schemaKeywords.nullish });
|
|
524
|
+
} else if (!isRequired) {
|
|
525
|
+
validationFunctions.push({ keyword: schemaKeywords.optional });
|
|
526
|
+
}
|
|
527
|
+
return {
|
|
528
|
+
[propertyName]: validationFunctions
|
|
529
|
+
};
|
|
530
|
+
}).reduce((acc, curr) => ({ ...acc, ...curr }), {});
|
|
531
|
+
let additionalPropertiesSchemas = [];
|
|
532
|
+
if (additionalProperties) {
|
|
533
|
+
additionalPropertiesSchemas = additionalProperties === true ? [{ keyword: this.#getUnknownReturn({ schema, name }) }] : this.parse({ schema: additionalProperties, parentName: name });
|
|
534
|
+
}
|
|
535
|
+
return [
|
|
536
|
+
{
|
|
537
|
+
keyword: schemaKeywords.object,
|
|
538
|
+
args: {
|
|
539
|
+
properties: propertiesSchemas,
|
|
540
|
+
additionalProperties: additionalPropertiesSchemas
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
];
|
|
544
|
+
}
|
|
545
|
+
/**
|
|
546
|
+
* Create a type alias for the schema referenced by the given ReferenceObject
|
|
547
|
+
*/
|
|
548
|
+
#getRefAlias(obj) {
|
|
549
|
+
const { $ref } = obj;
|
|
550
|
+
let ref = this.refs[$ref];
|
|
551
|
+
const originalName = utils.getUniqueName($ref.replace(/.+\//, ""), this.#usedAliasNames);
|
|
552
|
+
const propertyName = this.context.pluginManager.resolveName({
|
|
553
|
+
name: originalName,
|
|
554
|
+
pluginKey: this.context.plugin.key,
|
|
555
|
+
type: "function"
|
|
556
|
+
});
|
|
557
|
+
if (ref) {
|
|
558
|
+
return [
|
|
559
|
+
{
|
|
560
|
+
keyword: schemaKeywords.ref,
|
|
561
|
+
args: { name: ref.propertyName, path: ref.path }
|
|
562
|
+
}
|
|
563
|
+
];
|
|
564
|
+
}
|
|
565
|
+
const fileName = this.context.pluginManager.resolveName({
|
|
566
|
+
name: originalName,
|
|
567
|
+
pluginKey: this.context.plugin.key,
|
|
568
|
+
type: "file"
|
|
569
|
+
});
|
|
570
|
+
const file = this.context.pluginManager.getFile({
|
|
571
|
+
name: fileName,
|
|
572
|
+
pluginKey: this.context.plugin.key,
|
|
573
|
+
extName: ".ts"
|
|
574
|
+
});
|
|
575
|
+
ref = this.refs[$ref] = {
|
|
576
|
+
propertyName,
|
|
577
|
+
originalName,
|
|
578
|
+
path: file.path
|
|
579
|
+
};
|
|
580
|
+
return [
|
|
581
|
+
{
|
|
582
|
+
keyword: schemaKeywords.ref,
|
|
583
|
+
args: { name: ref.propertyName, path: ref?.path }
|
|
584
|
+
}
|
|
585
|
+
];
|
|
586
|
+
}
|
|
587
|
+
#getParsedSchemaObject(schema) {
|
|
588
|
+
const parsedSchema = chunkIVSE3A7W_cjs.getSchemaFactory(this.context.oas)(schema);
|
|
589
|
+
return parsedSchema;
|
|
590
|
+
}
|
|
591
|
+
/**
|
|
592
|
+
* This is the very core of the OpenAPI to TS conversion - it takes a
|
|
593
|
+
* schema and returns the appropriate type.
|
|
594
|
+
*/
|
|
595
|
+
#parseSchemaObject({ schema: _schema, name, parentName }) {
|
|
596
|
+
const options = this.#getOptions({ schema: _schema, name });
|
|
597
|
+
const unknownReturn = this.#getUnknownReturn({ schema: _schema, name });
|
|
598
|
+
const { schema, version } = this.#getParsedSchemaObject(_schema);
|
|
599
|
+
if (!schema) {
|
|
600
|
+
return [{ keyword: unknownReturn }];
|
|
601
|
+
}
|
|
602
|
+
const baseItems = [
|
|
603
|
+
{
|
|
604
|
+
keyword: schemaKeywords.schema,
|
|
605
|
+
args: {
|
|
606
|
+
type: schema.type,
|
|
607
|
+
format: schema.format
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
];
|
|
611
|
+
const min = schema.minimum ?? schema.minLength ?? schema.minItems ?? void 0;
|
|
612
|
+
const max = schema.maximum ?? schema.maxLength ?? schema.maxItems ?? void 0;
|
|
613
|
+
const nullable = schema.nullable ?? schema["x-nullable"] ?? false;
|
|
614
|
+
if (schema.default !== void 0 && !Array.isArray(schema.default)) {
|
|
615
|
+
if (typeof schema.default === "string") {
|
|
616
|
+
baseItems.push({
|
|
617
|
+
keyword: schemaKeywords.default,
|
|
618
|
+
args: transformers__default.default.stringify(schema.default)
|
|
619
|
+
});
|
|
620
|
+
} else if (typeof schema.default === "boolean") {
|
|
621
|
+
baseItems.push({
|
|
622
|
+
keyword: schemaKeywords.default,
|
|
623
|
+
args: schema.default ?? false
|
|
624
|
+
});
|
|
625
|
+
} else {
|
|
626
|
+
baseItems.push({
|
|
627
|
+
keyword: schemaKeywords.default,
|
|
628
|
+
args: schema.default
|
|
629
|
+
});
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
if (schema.description) {
|
|
633
|
+
baseItems.push({
|
|
634
|
+
keyword: schemaKeywords.describe,
|
|
635
|
+
args: schema.description
|
|
636
|
+
});
|
|
637
|
+
}
|
|
638
|
+
if (schema.pattern) {
|
|
639
|
+
baseItems.unshift({
|
|
640
|
+
keyword: schemaKeywords.matches,
|
|
641
|
+
args: schema.pattern
|
|
642
|
+
});
|
|
643
|
+
}
|
|
644
|
+
if (max !== void 0) {
|
|
645
|
+
baseItems.unshift({ keyword: schemaKeywords.max, args: max });
|
|
646
|
+
}
|
|
647
|
+
if (min !== void 0) {
|
|
648
|
+
baseItems.unshift({ keyword: schemaKeywords.min, args: min });
|
|
649
|
+
}
|
|
650
|
+
if (nullable) {
|
|
651
|
+
baseItems.push({ keyword: schemaKeywords.nullable });
|
|
652
|
+
}
|
|
653
|
+
if (schema.type && Array.isArray(schema.type)) {
|
|
654
|
+
const [_schema2, nullable2] = schema.type;
|
|
655
|
+
if (nullable2 === "null") {
|
|
656
|
+
baseItems.push({ keyword: schemaKeywords.nullable });
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
if (schema.readOnly) {
|
|
660
|
+
baseItems.push({ keyword: schemaKeywords.readOnly });
|
|
661
|
+
}
|
|
662
|
+
if (oas.isReference(schema)) {
|
|
663
|
+
return [
|
|
664
|
+
...this.#getRefAlias(schema),
|
|
665
|
+
nullable && { keyword: schemaKeywords.nullable },
|
|
666
|
+
{
|
|
667
|
+
keyword: schemaKeywords.schema,
|
|
668
|
+
args: {
|
|
669
|
+
type: schema.type,
|
|
670
|
+
format: schema.format
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
].filter(Boolean);
|
|
674
|
+
}
|
|
675
|
+
if (schema.oneOf) {
|
|
676
|
+
const schemaWithoutOneOf = { ...schema, oneOf: void 0 };
|
|
677
|
+
const union = {
|
|
678
|
+
keyword: schemaKeywords.union,
|
|
679
|
+
args: schema.oneOf.map((item) => {
|
|
680
|
+
return item && this.parse({ schema: item, name, parentName })[0];
|
|
681
|
+
}).filter(Boolean).filter((item) => {
|
|
682
|
+
return item && item.keyword !== unknownReturn;
|
|
683
|
+
})
|
|
684
|
+
};
|
|
685
|
+
if (schemaWithoutOneOf.properties) {
|
|
686
|
+
return [...this.parse({ schema: schemaWithoutOneOf, name, parentName }), union, ...baseItems];
|
|
687
|
+
}
|
|
688
|
+
return [union, ...baseItems];
|
|
689
|
+
}
|
|
690
|
+
if (schema.anyOf) {
|
|
691
|
+
const schemaWithoutAnyOf = { ...schema, anyOf: void 0 };
|
|
692
|
+
const union = {
|
|
693
|
+
keyword: schemaKeywords.union,
|
|
694
|
+
args: schema.anyOf.map((item) => {
|
|
695
|
+
return item && this.parse({ schema: item, name, parentName })[0];
|
|
696
|
+
}).filter(Boolean).filter((item) => {
|
|
697
|
+
return item && item.keyword !== unknownReturn;
|
|
698
|
+
}).map((item) => {
|
|
699
|
+
if (isKeyword(item, schemaKeywords.object)) {
|
|
700
|
+
return {
|
|
701
|
+
...item,
|
|
702
|
+
args: {
|
|
703
|
+
...item.args,
|
|
704
|
+
strict: true
|
|
705
|
+
}
|
|
706
|
+
};
|
|
707
|
+
}
|
|
708
|
+
return item;
|
|
709
|
+
})
|
|
710
|
+
};
|
|
711
|
+
if (schemaWithoutAnyOf.properties) {
|
|
712
|
+
return [...this.parse({ schema: schemaWithoutAnyOf, name, parentName }), union, ...baseItems];
|
|
713
|
+
}
|
|
714
|
+
return [union, ...baseItems];
|
|
715
|
+
}
|
|
716
|
+
if (schema.allOf) {
|
|
717
|
+
const schemaWithoutAllOf = { ...schema, allOf: void 0 };
|
|
718
|
+
const and = {
|
|
719
|
+
keyword: schemaKeywords.and,
|
|
720
|
+
args: schema.allOf.map((item) => {
|
|
721
|
+
return item && this.parse({ schema: item, name, parentName })[0];
|
|
722
|
+
}).filter(Boolean).filter((item) => {
|
|
723
|
+
return item && item.keyword !== unknownReturn;
|
|
724
|
+
})
|
|
725
|
+
};
|
|
726
|
+
if (schemaWithoutAllOf.properties) {
|
|
727
|
+
return [
|
|
728
|
+
{
|
|
729
|
+
...and,
|
|
730
|
+
args: [...and.args || [], ...this.parse({ schema: schemaWithoutAllOf, name, parentName })]
|
|
731
|
+
},
|
|
732
|
+
...baseItems
|
|
733
|
+
];
|
|
734
|
+
}
|
|
735
|
+
return [and, ...baseItems];
|
|
736
|
+
}
|
|
737
|
+
if (schema.enum) {
|
|
738
|
+
const enumName = utils.getUniqueName(transformers.pascalCase([parentName, name, options.enumSuffix].join(" ")), this.#getUsedEnumNames({ schema, name }));
|
|
739
|
+
const typeName = this.context.pluginManager.resolveName({
|
|
740
|
+
name: enumName,
|
|
741
|
+
pluginKey: this.context.plugin.key,
|
|
742
|
+
type: "type"
|
|
743
|
+
});
|
|
744
|
+
const nullableEnum = schema.enum.includes(null);
|
|
745
|
+
if (nullableEnum) {
|
|
746
|
+
baseItems.push({ keyword: schemaKeywords.nullable });
|
|
747
|
+
}
|
|
748
|
+
const filteredValues = schema.enum.filter((value) => value !== null);
|
|
749
|
+
const extensionEnums = ["x-enumNames", "x-enum-varnames"].filter((extensionKey) => extensionKey in schema).map((extensionKey) => {
|
|
750
|
+
return [
|
|
751
|
+
{
|
|
752
|
+
keyword: schemaKeywords.enum,
|
|
753
|
+
args: {
|
|
754
|
+
name,
|
|
755
|
+
typeName,
|
|
756
|
+
asConst: false,
|
|
757
|
+
items: [...new Set(schema[extensionKey])].map((name2, index) => ({
|
|
758
|
+
name: transformers__default.default.stringify(name2),
|
|
759
|
+
value: schema.enum?.[index],
|
|
760
|
+
format: remeda.isNumber(schema.enum?.[index]) ? "number" : "string"
|
|
761
|
+
}))
|
|
762
|
+
}
|
|
763
|
+
},
|
|
764
|
+
...baseItems.filter(
|
|
765
|
+
(item) => item.keyword !== schemaKeywords.min && item.keyword !== schemaKeywords.max && item.keyword !== schemaKeywords.matches
|
|
766
|
+
)
|
|
767
|
+
];
|
|
768
|
+
});
|
|
769
|
+
if (schema.type === "number" || schema.type === "integer") {
|
|
770
|
+
const enumNames = extensionEnums[0]?.find((item) => isKeyword(item, schemaKeywords.enum));
|
|
771
|
+
return [
|
|
772
|
+
{
|
|
773
|
+
keyword: schemaKeywords.enum,
|
|
774
|
+
args: {
|
|
775
|
+
name: enumName,
|
|
776
|
+
typeName,
|
|
777
|
+
asConst: true,
|
|
778
|
+
items: enumNames?.args?.items ? [...new Set(enumNames.args.items)].map(({ name: name2, value }) => ({
|
|
779
|
+
name: name2,
|
|
780
|
+
value,
|
|
781
|
+
format: "number"
|
|
782
|
+
})) : [...new Set(filteredValues)].map((value) => {
|
|
783
|
+
return {
|
|
784
|
+
name: value,
|
|
785
|
+
value,
|
|
786
|
+
format: "number"
|
|
787
|
+
};
|
|
788
|
+
})
|
|
789
|
+
}
|
|
790
|
+
},
|
|
791
|
+
...baseItems.filter((item) => item.keyword !== schemaKeywords.min && item.keyword !== schemaKeywords.max && item.keyword !== schemaKeywords.matches)
|
|
792
|
+
];
|
|
793
|
+
}
|
|
794
|
+
if (extensionEnums.length > 0 && extensionEnums[0]) {
|
|
795
|
+
return extensionEnums[0];
|
|
796
|
+
}
|
|
797
|
+
return [
|
|
798
|
+
{
|
|
799
|
+
keyword: schemaKeywords.enum,
|
|
800
|
+
args: {
|
|
801
|
+
name: enumName,
|
|
802
|
+
typeName,
|
|
803
|
+
asConst: false,
|
|
804
|
+
items: [...new Set(filteredValues)].map((value) => ({
|
|
805
|
+
name: transformers__default.default.stringify(value),
|
|
806
|
+
value,
|
|
807
|
+
format: remeda.isNumber(value) ? "number" : "string"
|
|
808
|
+
}))
|
|
809
|
+
}
|
|
810
|
+
},
|
|
811
|
+
...baseItems.filter((item) => item.keyword !== schemaKeywords.min && item.keyword !== schemaKeywords.max && item.keyword !== schemaKeywords.matches)
|
|
812
|
+
];
|
|
813
|
+
}
|
|
814
|
+
if ("prefixItems" in schema) {
|
|
815
|
+
const prefixItems = schema.prefixItems;
|
|
816
|
+
const min2 = schema.minimum ?? schema.minLength ?? schema.minItems ?? void 0;
|
|
817
|
+
const max2 = schema.maximum ?? schema.maxLength ?? schema.maxItems ?? void 0;
|
|
818
|
+
return [
|
|
819
|
+
{
|
|
820
|
+
keyword: schemaKeywords.tuple,
|
|
821
|
+
args: {
|
|
822
|
+
min: min2,
|
|
823
|
+
max: max2,
|
|
824
|
+
items: prefixItems.map((item) => {
|
|
825
|
+
return this.parse({ schema: item, name, parentName })[0];
|
|
826
|
+
}).filter(Boolean)
|
|
827
|
+
}
|
|
828
|
+
},
|
|
829
|
+
...baseItems.filter((item) => item.keyword !== schemaKeywords.min && item.keyword !== schemaKeywords.max)
|
|
830
|
+
];
|
|
831
|
+
}
|
|
832
|
+
if (version === "3.1" && "const" in schema) {
|
|
833
|
+
if (schema["const"]) {
|
|
834
|
+
return [
|
|
835
|
+
{
|
|
836
|
+
keyword: schemaKeywords.const,
|
|
837
|
+
args: {
|
|
838
|
+
name: schema["const"],
|
|
839
|
+
format: typeof schema["const"] === "number" ? "number" : "string",
|
|
840
|
+
value: schema["const"]
|
|
841
|
+
}
|
|
842
|
+
},
|
|
843
|
+
...baseItems
|
|
844
|
+
];
|
|
845
|
+
}
|
|
846
|
+
return [{ keyword: schemaKeywords.null }];
|
|
847
|
+
}
|
|
848
|
+
if (schema.format) {
|
|
849
|
+
switch (schema.format) {
|
|
850
|
+
case "binary":
|
|
851
|
+
baseItems.push({ keyword: schemaKeywords.blob });
|
|
852
|
+
return baseItems;
|
|
853
|
+
case "date-time":
|
|
854
|
+
if (options.dateType) {
|
|
855
|
+
if (options.dateType === "date") {
|
|
856
|
+
baseItems.unshift({ keyword: schemaKeywords.date, args: { type: "date" } });
|
|
857
|
+
return baseItems;
|
|
858
|
+
}
|
|
859
|
+
if (options.dateType === "stringOffset") {
|
|
860
|
+
baseItems.unshift({ keyword: schemaKeywords.datetime, args: { offset: true } });
|
|
861
|
+
return baseItems;
|
|
862
|
+
}
|
|
863
|
+
if (options.dateType === "stringLocal") {
|
|
864
|
+
baseItems.unshift({ keyword: schemaKeywords.datetime, args: { local: true } });
|
|
865
|
+
return baseItems;
|
|
866
|
+
}
|
|
867
|
+
baseItems.unshift({ keyword: schemaKeywords.datetime, args: { offset: false } });
|
|
868
|
+
return baseItems;
|
|
869
|
+
}
|
|
870
|
+
break;
|
|
871
|
+
case "date":
|
|
872
|
+
if (options.dateType) {
|
|
873
|
+
if (options.dateType === "date") {
|
|
874
|
+
baseItems.unshift({ keyword: schemaKeywords.date, args: { type: "date" } });
|
|
875
|
+
return baseItems;
|
|
876
|
+
}
|
|
877
|
+
baseItems.unshift({ keyword: schemaKeywords.date, args: { type: "string" } });
|
|
878
|
+
return baseItems;
|
|
879
|
+
}
|
|
880
|
+
break;
|
|
881
|
+
case "time":
|
|
882
|
+
if (options.dateType) {
|
|
883
|
+
if (options.dateType === "date") {
|
|
884
|
+
baseItems.unshift({ keyword: schemaKeywords.time, args: { type: "date" } });
|
|
885
|
+
return baseItems;
|
|
886
|
+
}
|
|
887
|
+
baseItems.unshift({ keyword: schemaKeywords.time, args: { type: "string" } });
|
|
888
|
+
return baseItems;
|
|
889
|
+
}
|
|
890
|
+
break;
|
|
891
|
+
case "uuid":
|
|
892
|
+
baseItems.unshift({ keyword: schemaKeywords.uuid });
|
|
893
|
+
break;
|
|
894
|
+
case "email":
|
|
895
|
+
case "idn-email":
|
|
896
|
+
baseItems.unshift({ keyword: schemaKeywords.email });
|
|
897
|
+
break;
|
|
898
|
+
case "uri":
|
|
899
|
+
case "ipv4":
|
|
900
|
+
case "ipv6":
|
|
901
|
+
case "uri-reference":
|
|
902
|
+
case "hostname":
|
|
903
|
+
case "idn-hostname":
|
|
904
|
+
baseItems.unshift({ keyword: schemaKeywords.url });
|
|
905
|
+
break;
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
if ("items" in schema || schema.type === "array") {
|
|
909
|
+
const min2 = schema.minimum ?? schema.minLength ?? schema.minItems ?? void 0;
|
|
910
|
+
const max2 = schema.maximum ?? schema.maxLength ?? schema.maxItems ?? void 0;
|
|
911
|
+
const items = this.parse({ schema: "items" in schema ? schema.items : [], name, parentName });
|
|
912
|
+
return [
|
|
913
|
+
{
|
|
914
|
+
keyword: schemaKeywords.array,
|
|
915
|
+
args: {
|
|
916
|
+
items,
|
|
917
|
+
min: min2,
|
|
918
|
+
max: max2
|
|
919
|
+
}
|
|
920
|
+
},
|
|
921
|
+
...baseItems.filter((item) => item.keyword !== schemaKeywords.min && item.keyword !== schemaKeywords.max)
|
|
922
|
+
];
|
|
923
|
+
}
|
|
924
|
+
if (schema.properties || schema.additionalProperties) {
|
|
925
|
+
return [...this.#parseProperties({ schema, name }), ...baseItems];
|
|
926
|
+
}
|
|
927
|
+
if (schema.type) {
|
|
928
|
+
if (Array.isArray(schema.type)) {
|
|
929
|
+
const [type] = schema.type;
|
|
930
|
+
return [
|
|
931
|
+
...this.parse({
|
|
932
|
+
schema: {
|
|
933
|
+
...schema,
|
|
934
|
+
type
|
|
935
|
+
},
|
|
936
|
+
name,
|
|
937
|
+
parentName
|
|
938
|
+
}),
|
|
939
|
+
...baseItems
|
|
940
|
+
].filter(Boolean);
|
|
941
|
+
}
|
|
942
|
+
if (!["boolean", "object", "number", "string", "integer", "null"].includes(schema.type)) {
|
|
943
|
+
this.context.pluginManager.logger.emit("warning", `Schema type '${schema.type}' is not valid for schema ${parentName}.${name}`);
|
|
944
|
+
}
|
|
945
|
+
return [{ keyword: schema.type }, ...baseItems];
|
|
946
|
+
}
|
|
947
|
+
return [{ keyword: unknownReturn }];
|
|
948
|
+
}
|
|
949
|
+
async build(...generators) {
|
|
950
|
+
const { oas, contentType, include } = this.context;
|
|
951
|
+
const schemas = chunkIVSE3A7W_cjs.getSchemas({ oas, contentType, includes: include });
|
|
952
|
+
const promises = Object.entries(schemas).reduce((acc, [name, value]) => {
|
|
953
|
+
if (!value) {
|
|
954
|
+
return acc;
|
|
955
|
+
}
|
|
956
|
+
const options = this.#getOptions({ name });
|
|
957
|
+
const promiseOperation = this.schema.call(this, name, value, {
|
|
958
|
+
...this.options,
|
|
959
|
+
...options
|
|
960
|
+
});
|
|
961
|
+
if (promiseOperation) {
|
|
962
|
+
acc.push(promiseOperation);
|
|
963
|
+
}
|
|
964
|
+
generators?.forEach((generator) => {
|
|
965
|
+
const tree = this.parse({ schema: value, name });
|
|
966
|
+
const promise = generator.schema?.({
|
|
967
|
+
instance: this,
|
|
968
|
+
schema: {
|
|
969
|
+
name,
|
|
970
|
+
value,
|
|
971
|
+
tree
|
|
972
|
+
},
|
|
973
|
+
options: {
|
|
974
|
+
...this.options,
|
|
975
|
+
...options
|
|
976
|
+
}
|
|
977
|
+
});
|
|
978
|
+
if (promise) {
|
|
979
|
+
acc.push(promise);
|
|
980
|
+
}
|
|
981
|
+
});
|
|
982
|
+
return acc;
|
|
983
|
+
}, []);
|
|
984
|
+
const files = await Promise.all(promises);
|
|
985
|
+
return files.flat().filter(Boolean);
|
|
986
|
+
}
|
|
987
|
+
/**
|
|
988
|
+
* Schema
|
|
989
|
+
*/
|
|
990
|
+
async schema(name, object, options) {
|
|
991
|
+
return [];
|
|
992
|
+
}
|
|
993
|
+
};
|
|
994
|
+
|
|
995
|
+
// src/plugin.ts
|
|
996
|
+
var pluginOasName = "plugin-oas";
|
|
997
|
+
var pluginOas = core.createPlugin((options) => {
|
|
998
|
+
const {
|
|
999
|
+
output = {
|
|
1000
|
+
path: "schemas",
|
|
1001
|
+
export: false,
|
|
1002
|
+
exportType: "barrelNamed"
|
|
1003
|
+
},
|
|
1004
|
+
experimentalFilter: filter,
|
|
1005
|
+
experimentalSort: sort,
|
|
1006
|
+
validate = true,
|
|
1007
|
+
generators = [chunkXON3FUPP_cjs.jsonGenerator],
|
|
1008
|
+
serverIndex = 0,
|
|
1009
|
+
contentType,
|
|
1010
|
+
oasClass
|
|
1011
|
+
} = options;
|
|
1012
|
+
const getOas = async ({ config, logger, formatOptions }) => {
|
|
1013
|
+
try {
|
|
1014
|
+
const oas = await chunkIVSE3A7W_cjs.parseFromConfig(config, formatOptions, oasClass);
|
|
1015
|
+
if (validate) {
|
|
1016
|
+
await oas.valdiate();
|
|
1017
|
+
}
|
|
1018
|
+
return oas;
|
|
1019
|
+
} catch (e) {
|
|
1020
|
+
const error = e;
|
|
1021
|
+
logger.emit("warning", error?.message);
|
|
1022
|
+
return chunkIVSE3A7W_cjs.parseFromConfig(config, {}, oasClass);
|
|
1023
|
+
}
|
|
1024
|
+
};
|
|
1025
|
+
return {
|
|
1026
|
+
name: pluginOasName,
|
|
1027
|
+
output,
|
|
1028
|
+
options,
|
|
1029
|
+
context() {
|
|
1030
|
+
const { config, logger } = this;
|
|
1031
|
+
return {
|
|
1032
|
+
getOas(formatOptions) {
|
|
1033
|
+
return getOas({ config, logger, formatOptions });
|
|
1034
|
+
},
|
|
1035
|
+
async getSchemas({ includes } = {}) {
|
|
1036
|
+
const oas = await this.getOas();
|
|
1037
|
+
return chunkIVSE3A7W_cjs.getSchemas({ oas, contentType, includes });
|
|
1038
|
+
},
|
|
1039
|
+
async getBaseURL() {
|
|
1040
|
+
const oasInstance = await this.getOas();
|
|
1041
|
+
return oasInstance.api.servers?.at(serverIndex)?.url;
|
|
1042
|
+
},
|
|
1043
|
+
contentType
|
|
1044
|
+
};
|
|
1045
|
+
},
|
|
1046
|
+
resolvePath(baseName, pathMode, options2) {
|
|
1047
|
+
const root = path__default.default.resolve(this.config.root, this.config.output.path);
|
|
1048
|
+
const mode = pathMode ?? core.FileManager.getMode(path__default.default.resolve(root, output.path));
|
|
1049
|
+
if (mode === "single") {
|
|
1050
|
+
return path__default.default.resolve(root, output.path);
|
|
1051
|
+
}
|
|
1052
|
+
return path__default.default.resolve(root, output.path, baseName);
|
|
1053
|
+
},
|
|
1054
|
+
async buildStart() {
|
|
1055
|
+
if (!output) {
|
|
1056
|
+
return;
|
|
1057
|
+
}
|
|
1058
|
+
const oas = await getOas({
|
|
1059
|
+
config: this.config,
|
|
1060
|
+
logger: this.logger,
|
|
1061
|
+
formatOptions: {
|
|
1062
|
+
filterSet: filter,
|
|
1063
|
+
sortSet: sort
|
|
1064
|
+
}
|
|
1065
|
+
});
|
|
1066
|
+
await oas.dereference();
|
|
1067
|
+
const schemaGenerator = new SchemaGenerator(
|
|
1068
|
+
{
|
|
1069
|
+
unknownType: "unknown",
|
|
1070
|
+
dateType: "date",
|
|
1071
|
+
transformers: {},
|
|
1072
|
+
...this.plugin.options
|
|
1073
|
+
},
|
|
1074
|
+
{
|
|
1075
|
+
oas,
|
|
1076
|
+
pluginManager: this.pluginManager,
|
|
1077
|
+
plugin: this.plugin,
|
|
1078
|
+
contentType,
|
|
1079
|
+
include: void 0,
|
|
1080
|
+
override: void 0,
|
|
1081
|
+
mode: "split",
|
|
1082
|
+
output: output.path
|
|
1083
|
+
}
|
|
1084
|
+
);
|
|
1085
|
+
const schemaFiles = await schemaGenerator.build(...generators);
|
|
1086
|
+
await this.addFile(...schemaFiles);
|
|
1087
|
+
const operationGenerator = new OperationGenerator(this.plugin.options, {
|
|
1088
|
+
oas,
|
|
1089
|
+
pluginManager: this.pluginManager,
|
|
1090
|
+
plugin: this.plugin,
|
|
1091
|
+
contentType,
|
|
1092
|
+
exclude: void 0,
|
|
1093
|
+
include: void 0,
|
|
1094
|
+
override: void 0,
|
|
1095
|
+
mode: "split"
|
|
1096
|
+
});
|
|
1097
|
+
const operationFiles = await operationGenerator.build(...generators);
|
|
1098
|
+
await this.addFile(...operationFiles);
|
|
1099
|
+
}
|
|
1100
|
+
};
|
|
1101
|
+
});
|
|
1102
|
+
|
|
1103
|
+
exports.OperationGenerator = OperationGenerator;
|
|
1104
|
+
exports.SchemaGenerator = SchemaGenerator;
|
|
1105
|
+
exports.isKeyword = isKeyword;
|
|
1106
|
+
exports.pluginOas = pluginOas;
|
|
1107
|
+
exports.pluginOasName = pluginOasName;
|
|
1108
|
+
exports.schemaKeywords = schemaKeywords;
|
|
1109
|
+
//# sourceMappingURL=chunk-BEKIIF6H.cjs.map
|
|
1110
|
+
//# sourceMappingURL=chunk-BEKIIF6H.cjs.map
|