@kubb/swagger-ts 2.0.0-canary.20231030T125204 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/chunk-D7MXSC5P.cjs +787 -0
- package/dist/chunk-D7MXSC5P.cjs.map +1 -0
- package/dist/chunk-HCQOGZHE.js +760 -0
- package/dist/chunk-HCQOGZHE.js.map +1 -0
- package/dist/components.cjs +20 -0
- package/dist/components.cjs.map +1 -0
- package/dist/components.d.cts +69 -0
- package/dist/components.d.ts +69 -0
- package/dist/components.js +6 -0
- package/dist/components.js.map +1 -0
- package/dist/index-sycg8owy.d.cts +392 -0
- package/dist/index-sycg8owy.d.ts +392 -0
- package/dist/index.cjs +14 -652
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +11 -78
- package/dist/index.d.ts +11 -78
- package/dist/index.js +3 -625
- package/dist/index.js.map +1 -1
- package/dist/oas.cjs +4 -0
- package/dist/oas.cjs.map +1 -0
- package/dist/oas.d.cts +6 -0
- package/dist/oas.d.ts +6 -0
- package/dist/oas.js +5 -0
- package/dist/oas.js.map +1 -0
- package/dist/types-IAThMYCO.d.cts +105 -0
- package/dist/types-IAThMYCO.d.ts +105 -0
- package/package.json +27 -18
- package/src/OperationGenerator.tsx +63 -0
- package/src/TypeBuilder.ts +58 -0
- package/src/TypeGenerator.ts +396 -0
- package/src/components/Mutation.tsx +138 -0
- package/src/components/Oas.tsx +84 -0
- package/src/components/Query.tsx +137 -0
- package/src/components/index.ts +3 -0
- package/src/index.ts +6 -0
- package/src/oas/index.ts +7 -0
- package/src/oas/infer.ts +58 -0
- package/src/oas/mappers.ts +93 -0
- package/src/oas/model.ts +38 -0
- package/src/oas/requestParams.ts +170 -0
- package/src/oas/response.ts +39 -0
- package/src/oas/security.ts +158 -0
- package/src/plugin.ts +177 -0
- package/src/types.ts +110 -0
- package/dist/hooks.cjs +0 -656
- package/dist/hooks.cjs.map +0 -1
- package/dist/hooks.d.cts +0 -6
- package/dist/hooks.d.ts +0 -6
- package/dist/hooks.js +0 -634
- package/dist/hooks.js.map +0 -1
@@ -0,0 +1,760 @@
|
|
1
|
+
import { createRequire } from 'module';
|
2
|
+
import transformers2, { camelCase, pascalCase } from '@kubb/core/transformers';
|
3
|
+
import { print } from '@kubb/parser';
|
4
|
+
import * as factory2 from '@kubb/parser/factory';
|
5
|
+
import { keywordTypeNodes } from '@kubb/parser/factory';
|
6
|
+
import { usePlugin, useFile, File, usePluginManager, createRoot, Type } from '@kubb/react';
|
7
|
+
import { useOas, useSchemas, useOperationFile, useOperationName, useOperation } from '@kubb/swagger/hooks';
|
8
|
+
import { pluginName as pluginName$1, OasBuilder, OperationGenerator as OperationGenerator$1 } from '@kubb/swagger';
|
9
|
+
import { refsSorter, isReference, getSchemaFactory } from '@kubb/swagger/utils';
|
10
|
+
import { createPlugin, FileManager, PluginManager, Generator } from '@kubb/core';
|
11
|
+
import { renderTemplate, getUniqueName } from '@kubb/core/utils';
|
12
|
+
import path from 'path';
|
13
|
+
import { jsx, jsxs, Fragment } from '@kubb/react/jsx-runtime';
|
14
|
+
|
15
|
+
createRequire(import.meta.url);
|
16
|
+
|
17
|
+
var __accessCheck = (obj, member, msg) => {
|
18
|
+
if (!member.has(obj))
|
19
|
+
throw TypeError("Cannot " + msg);
|
20
|
+
};
|
21
|
+
var __privateGet = (obj, member, getter) => {
|
22
|
+
__accessCheck(obj, member, "read from private field");
|
23
|
+
return getter ? getter.call(obj) : member.get(obj);
|
24
|
+
};
|
25
|
+
var __privateAdd = (obj, member, value) => {
|
26
|
+
if (member.has(obj))
|
27
|
+
throw TypeError("Cannot add the same private member more than once");
|
28
|
+
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
29
|
+
};
|
30
|
+
var __privateMethod = (obj, member, method) => {
|
31
|
+
__accessCheck(obj, member, "access private method");
|
32
|
+
return method;
|
33
|
+
};
|
34
|
+
function Template({
|
35
|
+
name,
|
36
|
+
typeName,
|
37
|
+
api
|
38
|
+
}) {
|
39
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
40
|
+
`export const ${name} = ${JSON.stringify(api, void 0, 2)} as const`,
|
41
|
+
/* @__PURE__ */ jsx("br", {}),
|
42
|
+
/* @__PURE__ */ jsx(Type, { name: typeName, export: true, children: `Infer<typeof ${name}>` })
|
43
|
+
] });
|
44
|
+
}
|
45
|
+
var defaultTemplates = { default: Template };
|
46
|
+
function Oas({
|
47
|
+
name,
|
48
|
+
typeName,
|
49
|
+
Template: Template2 = defaultTemplates.default
|
50
|
+
}) {
|
51
|
+
const oas = useOas();
|
52
|
+
return /* @__PURE__ */ jsx(Template2, { name, typeName, api: oas.api });
|
53
|
+
}
|
54
|
+
Oas.File = function({ name, typeName, templates = defaultTemplates }) {
|
55
|
+
const { key: pluginKey2 } = usePlugin();
|
56
|
+
const file = useFile({ name, pluginKey: pluginKey2 });
|
57
|
+
const Template2 = templates.default;
|
58
|
+
return /* @__PURE__ */ jsxs(
|
59
|
+
File,
|
60
|
+
{
|
61
|
+
baseName: file.baseName,
|
62
|
+
path: file.path,
|
63
|
+
meta: file.meta,
|
64
|
+
children: [
|
65
|
+
/* @__PURE__ */ jsx(File.Import, { name: ["Infer"], path: "@kubb/swagger-ts/oas", isTypeOnly: true }),
|
66
|
+
/* @__PURE__ */ jsx(File.Source, { children: /* @__PURE__ */ jsx(Oas, { Template: Template2, name, typeName }) })
|
67
|
+
]
|
68
|
+
}
|
69
|
+
);
|
70
|
+
};
|
71
|
+
Oas.templates = defaultTemplates;
|
72
|
+
function printCombinedSchema(name, operation, schemas) {
|
73
|
+
const properties = {
|
74
|
+
"response": factory2.createTypeReferenceNode(
|
75
|
+
factory2.createIdentifier(schemas.response.name),
|
76
|
+
void 0
|
77
|
+
)
|
78
|
+
};
|
79
|
+
if (schemas.request) {
|
80
|
+
properties["request"] = factory2.createTypeReferenceNode(
|
81
|
+
factory2.createIdentifier(schemas.request.name),
|
82
|
+
void 0
|
83
|
+
);
|
84
|
+
}
|
85
|
+
if (schemas.pathParams) {
|
86
|
+
properties["pathParams"] = factory2.createTypeReferenceNode(
|
87
|
+
factory2.createIdentifier(schemas.pathParams.name),
|
88
|
+
void 0
|
89
|
+
);
|
90
|
+
}
|
91
|
+
if (schemas.queryParams) {
|
92
|
+
properties["queryParams"] = factory2.createTypeReferenceNode(
|
93
|
+
factory2.createIdentifier(schemas.queryParams.name),
|
94
|
+
void 0
|
95
|
+
);
|
96
|
+
}
|
97
|
+
if (schemas.headerParams) {
|
98
|
+
properties["headerParams"] = factory2.createTypeReferenceNode(
|
99
|
+
factory2.createIdentifier(schemas.headerParams.name),
|
100
|
+
void 0
|
101
|
+
);
|
102
|
+
}
|
103
|
+
if (schemas.errors) {
|
104
|
+
properties["errors"] = factory2.createUnionDeclaration({
|
105
|
+
nodes: schemas.errors.map((error) => {
|
106
|
+
return factory2.createTypeReferenceNode(
|
107
|
+
factory2.createIdentifier(error.name),
|
108
|
+
void 0
|
109
|
+
);
|
110
|
+
})
|
111
|
+
});
|
112
|
+
}
|
113
|
+
const namespaceNode = factory2.createNamespaceDeclaration({
|
114
|
+
name: operation.method === "get" ? `${name}Query` : `${name}Mutation`,
|
115
|
+
statements: Object.keys(properties).map((key) => {
|
116
|
+
const type = properties[key];
|
117
|
+
if (!type) {
|
118
|
+
return void 0;
|
119
|
+
}
|
120
|
+
return factory2.createTypeAliasDeclaration({
|
121
|
+
modifiers: [factory2.modifiers.export],
|
122
|
+
name: transformers2.pascalCase(key),
|
123
|
+
type
|
124
|
+
});
|
125
|
+
}).filter(Boolean)
|
126
|
+
});
|
127
|
+
return print(namespaceNode);
|
128
|
+
}
|
129
|
+
function Query({
|
130
|
+
builder
|
131
|
+
}) {
|
132
|
+
const { source } = builder.build();
|
133
|
+
return /* @__PURE__ */ jsx(Fragment, { children: source });
|
134
|
+
}
|
135
|
+
Query.File = function({ mode }) {
|
136
|
+
const { options } = usePlugin();
|
137
|
+
const schemas = useSchemas();
|
138
|
+
const pluginManager = usePluginManager();
|
139
|
+
const oas = useOas();
|
140
|
+
const file = useOperationFile();
|
141
|
+
const factoryName = useOperationName({ type: "type" });
|
142
|
+
const operation = useOperation();
|
143
|
+
const builder = new TypeBuilder(options, { oas, pluginManager }).add(schemas.pathParams).add(schemas.queryParams).add(schemas.headerParams).add(schemas.response).add(schemas.errors);
|
144
|
+
const { source, imports } = builder.build();
|
145
|
+
return /* @__PURE__ */ jsx(Fragment, { children: /* @__PURE__ */ jsxs(
|
146
|
+
File,
|
147
|
+
{
|
148
|
+
baseName: file.baseName,
|
149
|
+
path: file.path,
|
150
|
+
meta: file.meta,
|
151
|
+
children: [
|
152
|
+
mode === "directory" && imports.map((item, index) => {
|
153
|
+
return /* @__PURE__ */ jsx(File.Import, { root: file.path, ...item }, index);
|
154
|
+
}),
|
155
|
+
/* @__PURE__ */ jsxs(File.Source, { children: [
|
156
|
+
source,
|
157
|
+
printCombinedSchema(factoryName, operation, schemas)
|
158
|
+
] })
|
159
|
+
]
|
160
|
+
}
|
161
|
+
) });
|
162
|
+
};
|
163
|
+
var OperationGenerator = class extends OperationGenerator$1 {
|
164
|
+
async all() {
|
165
|
+
const { oas, pluginManager, plugin } = this.context;
|
166
|
+
if (!plugin.options.oasType) {
|
167
|
+
return null;
|
168
|
+
}
|
169
|
+
const root = createRoot({ logger: pluginManager.logger });
|
170
|
+
root.render(
|
171
|
+
/* @__PURE__ */ jsx(Oas.File, { name: "oas", typeName: "Oas" }),
|
172
|
+
{ meta: { oas, pluginManager, plugin } }
|
173
|
+
);
|
174
|
+
return root.files;
|
175
|
+
}
|
176
|
+
async get(operation, schemas, options) {
|
177
|
+
const { oas, pluginManager, plugin, mode = "directory" } = this.context;
|
178
|
+
const root = createRoot({ logger: pluginManager.logger });
|
179
|
+
root.render(
|
180
|
+
/* @__PURE__ */ jsx(Query.File, { mode }),
|
181
|
+
{ meta: { oas, pluginManager, plugin: { ...plugin, options }, schemas, operation } }
|
182
|
+
);
|
183
|
+
return root.files;
|
184
|
+
}
|
185
|
+
async post(operation, schemas, options) {
|
186
|
+
const { oas, pluginManager, plugin, mode = "directory" } = this.context;
|
187
|
+
const root = createRoot({ logger: pluginManager.logger });
|
188
|
+
root.render(
|
189
|
+
/* @__PURE__ */ jsx(Mutation.File, { mode }),
|
190
|
+
{ meta: { oas, pluginManager, plugin: { ...plugin, options }, schemas, operation } }
|
191
|
+
);
|
192
|
+
return root.files;
|
193
|
+
}
|
194
|
+
async put(operation, schemas, options) {
|
195
|
+
return this.post(operation, schemas, options);
|
196
|
+
}
|
197
|
+
async patch(operation, schemas, options) {
|
198
|
+
return this.post(operation, schemas, options);
|
199
|
+
}
|
200
|
+
async delete(operation, schemas, options) {
|
201
|
+
return this.post(operation, schemas, options);
|
202
|
+
}
|
203
|
+
};
|
204
|
+
|
205
|
+
// src/plugin.ts
|
206
|
+
var pluginName = "swagger-ts";
|
207
|
+
var pluginKey = [pluginName];
|
208
|
+
var definePlugin = createPlugin((options) => {
|
209
|
+
const {
|
210
|
+
output = { path: "types" },
|
211
|
+
group,
|
212
|
+
exclude = [],
|
213
|
+
include,
|
214
|
+
override = [],
|
215
|
+
enumType = "asConst",
|
216
|
+
dateType = "string",
|
217
|
+
optionalType = "questionToken",
|
218
|
+
transformers: transformers5 = {},
|
219
|
+
oasType = false
|
220
|
+
} = options;
|
221
|
+
const template = group?.output ? group.output : `${output.path}/{{tag}}Controller`;
|
222
|
+
return {
|
223
|
+
name: pluginName,
|
224
|
+
options: {
|
225
|
+
transformers: transformers5,
|
226
|
+
dateType,
|
227
|
+
enumType,
|
228
|
+
optionalType,
|
229
|
+
oasType,
|
230
|
+
// keep the used enumnames between TypeBuilder and OperationGenerator per plugin(pluginKey)
|
231
|
+
usedEnumNames: {}
|
232
|
+
},
|
233
|
+
pre: [pluginName$1],
|
234
|
+
resolvePath(baseName, directory, options2) {
|
235
|
+
const root = path.resolve(this.config.root, this.config.output.path);
|
236
|
+
const mode = FileManager.getMode(path.resolve(root, output.path));
|
237
|
+
if (mode === "file") {
|
238
|
+
return path.resolve(root, output.path);
|
239
|
+
}
|
240
|
+
if (options2?.tag && group?.type === "tag") {
|
241
|
+
const tag = camelCase(options2.tag);
|
242
|
+
return path.resolve(root, renderTemplate(template, { tag }), baseName);
|
243
|
+
}
|
244
|
+
return path.resolve(root, output.path, baseName);
|
245
|
+
},
|
246
|
+
resolveName(name, type) {
|
247
|
+
const resolvedName = pascalCase(name);
|
248
|
+
if (type) {
|
249
|
+
return transformers5?.name?.(resolvedName, type) || resolvedName;
|
250
|
+
}
|
251
|
+
return resolvedName;
|
252
|
+
},
|
253
|
+
async writeFile(source, writePath) {
|
254
|
+
if (!writePath.endsWith(".ts") || !source) {
|
255
|
+
return;
|
256
|
+
}
|
257
|
+
return this.fileManager.write(source, writePath, { sanity: false });
|
258
|
+
},
|
259
|
+
async buildStart() {
|
260
|
+
const [swaggerPlugin] = PluginManager.getDependedPlugins(this.plugins, [pluginName$1]);
|
261
|
+
const oas = await swaggerPlugin.api.getOas();
|
262
|
+
const schemas = await swaggerPlugin.api.getSchemas();
|
263
|
+
const root = path.resolve(this.config.root, this.config.output.path);
|
264
|
+
const mode = FileManager.getMode(path.resolve(root, output.path));
|
265
|
+
const builder = new TypeBuilder(this.plugin.options, { oas, pluginManager: this.pluginManager });
|
266
|
+
builder.add(
|
267
|
+
Object.entries(schemas).map(([name, schema]) => ({ name, schema }))
|
268
|
+
);
|
269
|
+
if (mode === "directory") {
|
270
|
+
const mapFolderSchema = async ([name]) => {
|
271
|
+
const baseName = `${this.resolveName({ name, pluginKey: this.plugin.key, type: "file" })}.ts`;
|
272
|
+
const resolvedPath = this.resolvePath({ baseName, pluginKey: this.plugin.key });
|
273
|
+
const { source, imports } = builder.build(name);
|
274
|
+
if (!resolvedPath) {
|
275
|
+
return null;
|
276
|
+
}
|
277
|
+
return this.addFile({
|
278
|
+
path: resolvedPath,
|
279
|
+
baseName,
|
280
|
+
source,
|
281
|
+
imports: imports.map((item) => ({ ...item, root: resolvedPath })),
|
282
|
+
meta: {
|
283
|
+
pluginKey: this.plugin.key
|
284
|
+
}
|
285
|
+
});
|
286
|
+
};
|
287
|
+
const promises = Object.entries(schemas).map(mapFolderSchema);
|
288
|
+
await Promise.all(promises);
|
289
|
+
}
|
290
|
+
if (mode === "file") {
|
291
|
+
const resolvedPath = this.resolvePath({ baseName: "", pluginKey: this.plugin.key });
|
292
|
+
const { source } = builder.build();
|
293
|
+
if (!resolvedPath) {
|
294
|
+
return;
|
295
|
+
}
|
296
|
+
await this.addFile({
|
297
|
+
path: resolvedPath,
|
298
|
+
baseName: output.path,
|
299
|
+
source,
|
300
|
+
imports: [],
|
301
|
+
meta: {
|
302
|
+
pluginKey: this.plugin.key
|
303
|
+
}
|
304
|
+
});
|
305
|
+
}
|
306
|
+
const operationGenerator = new OperationGenerator(
|
307
|
+
this.plugin.options,
|
308
|
+
{
|
309
|
+
oas,
|
310
|
+
pluginManager: this.pluginManager,
|
311
|
+
plugin: this.plugin,
|
312
|
+
contentType: swaggerPlugin.api.contentType,
|
313
|
+
exclude,
|
314
|
+
include,
|
315
|
+
override,
|
316
|
+
mode
|
317
|
+
}
|
318
|
+
);
|
319
|
+
const files = await operationGenerator.build();
|
320
|
+
await this.addFile(...files);
|
321
|
+
},
|
322
|
+
async buildEnd() {
|
323
|
+
if (this.config.output.write === false) {
|
324
|
+
return;
|
325
|
+
}
|
326
|
+
const root = path.resolve(this.config.root, this.config.output.path);
|
327
|
+
const { exportType = "barrel" } = output;
|
328
|
+
if (exportType === "barrel") {
|
329
|
+
await this.fileManager.addIndexes({
|
330
|
+
root,
|
331
|
+
output,
|
332
|
+
meta: { pluginKey: this.plugin.key },
|
333
|
+
options: {
|
334
|
+
isTypeOnly: true
|
335
|
+
}
|
336
|
+
});
|
337
|
+
}
|
338
|
+
}
|
339
|
+
};
|
340
|
+
});
|
341
|
+
|
342
|
+
// src/TypeGenerator.ts
|
343
|
+
var _usedAliasNames, _getTypeFromProperties, getTypeFromProperties_fn, _getRefAlias, getRefAlias_fn, _getParsedSchema, getParsedSchema_fn, _getBaseTypeFromSchema, getBaseTypeFromSchema_fn;
|
344
|
+
var TypeGenerator = class extends Generator {
|
345
|
+
constructor() {
|
346
|
+
super(...arguments);
|
347
|
+
/**
|
348
|
+
* Recursively creates a type literal with the given props.
|
349
|
+
*/
|
350
|
+
__privateAdd(this, _getTypeFromProperties);
|
351
|
+
/**
|
352
|
+
* Create a type alias for the schema referenced by the given ReferenceObject
|
353
|
+
*/
|
354
|
+
__privateAdd(this, _getRefAlias);
|
355
|
+
__privateAdd(this, _getParsedSchema);
|
356
|
+
/**
|
357
|
+
* This is the very core of the OpenAPI to TS conversion - it takes a
|
358
|
+
* schema and returns the appropriate type.
|
359
|
+
*/
|
360
|
+
__privateAdd(this, _getBaseTypeFromSchema);
|
361
|
+
this.refs = {};
|
362
|
+
this.imports = [];
|
363
|
+
this.extraNodes = [];
|
364
|
+
this.aliases = [];
|
365
|
+
// Keep track of already used type aliases
|
366
|
+
__privateAdd(this, _usedAliasNames, {});
|
367
|
+
}
|
368
|
+
build({
|
369
|
+
schema,
|
370
|
+
baseName,
|
371
|
+
description,
|
372
|
+
keysToOmit
|
373
|
+
}) {
|
374
|
+
const nodes = [];
|
375
|
+
const type = this.getTypeFromSchema(schema, baseName);
|
376
|
+
if (!type) {
|
377
|
+
return this.extraNodes;
|
378
|
+
}
|
379
|
+
const node = factory2.createTypeAliasDeclaration({
|
380
|
+
modifiers: [factory2.modifiers.export],
|
381
|
+
name: this.context.pluginManager.resolveName({ name: baseName, pluginKey, type: "type" }),
|
382
|
+
type: keysToOmit?.length ? factory2.createOmitDeclaration({ keys: keysToOmit, type, nonNullable: true }) : type
|
383
|
+
});
|
384
|
+
if (description) {
|
385
|
+
nodes.push(
|
386
|
+
factory2.appendJSDocToNode({
|
387
|
+
node,
|
388
|
+
comments: [`@description ${transformers2.trim(description)}`]
|
389
|
+
})
|
390
|
+
);
|
391
|
+
} else {
|
392
|
+
nodes.push(node);
|
393
|
+
}
|
394
|
+
const filterdNodes = nodes.filter(
|
395
|
+
(node2) => !this.extraNodes.some(
|
396
|
+
(extraNode) => extraNode?.name?.escapedText === node2?.name?.escapedText
|
397
|
+
)
|
398
|
+
);
|
399
|
+
return [...this.extraNodes, ...filterdNodes];
|
400
|
+
}
|
401
|
+
/**
|
402
|
+
* Creates a type node from a given schema.
|
403
|
+
* Delegates to getBaseTypeFromSchema internally and
|
404
|
+
* optionally adds a union with null.
|
405
|
+
*/
|
406
|
+
getTypeFromSchema(schema, name) {
|
407
|
+
const type = __privateMethod(this, _getBaseTypeFromSchema, getBaseTypeFromSchema_fn).call(this, schema, name);
|
408
|
+
if (!type) {
|
409
|
+
return null;
|
410
|
+
}
|
411
|
+
if (schema && !schema.nullable) {
|
412
|
+
return type;
|
413
|
+
}
|
414
|
+
return factory2.createUnionDeclaration({ nodes: [type, factory2.keywordTypeNodes.null] });
|
415
|
+
}
|
416
|
+
};
|
417
|
+
_usedAliasNames = new WeakMap();
|
418
|
+
_getTypeFromProperties = new WeakSet();
|
419
|
+
getTypeFromProperties_fn = function(baseSchema, baseName) {
|
420
|
+
const { optionalType } = this.options;
|
421
|
+
const properties = baseSchema?.properties || {};
|
422
|
+
const required = baseSchema?.required;
|
423
|
+
const additionalProperties = baseSchema?.additionalProperties;
|
424
|
+
const members = Object.keys(properties).map((name) => {
|
425
|
+
const schema = properties[name];
|
426
|
+
const isRequired = Array.isArray(required) ? required.includes(name) : !!required;
|
427
|
+
let type = this.getTypeFromSchema(schema, this.context.pluginManager.resolveName({ name: `${baseName || ""} ${name}`, pluginKey, type: "type" }));
|
428
|
+
if (!type) {
|
429
|
+
return null;
|
430
|
+
}
|
431
|
+
if (!isRequired && ["undefined", "questionTokenAndUndefined"].includes(optionalType)) {
|
432
|
+
type = factory2.createUnionDeclaration({ nodes: [type, factory2.keywordTypeNodes.undefined] });
|
433
|
+
}
|
434
|
+
const propertySignature = factory2.createPropertySignature({
|
435
|
+
questionToken: ["questionToken", "questionTokenAndUndefined"].includes(optionalType) && !isRequired,
|
436
|
+
name,
|
437
|
+
type,
|
438
|
+
readOnly: schema.readOnly
|
439
|
+
});
|
440
|
+
return factory2.appendJSDocToNode({
|
441
|
+
node: propertySignature,
|
442
|
+
comments: [
|
443
|
+
schema.description ? `@description ${schema.description}` : void 0,
|
444
|
+
schema.type ? `@type ${schema.type?.toString()}${isRequired ? "" : " | undefined"} ${schema.format || ""}` : void 0,
|
445
|
+
schema.example ? `@example ${schema.example}` : void 0,
|
446
|
+
schema.deprecated ? `@deprecated` : void 0,
|
447
|
+
schema.default !== void 0 && typeof schema.default === "string" ? `@default '${schema.default}'` : void 0,
|
448
|
+
schema.default !== void 0 && typeof schema.default !== "string" ? `@default ${schema.default}` : void 0
|
449
|
+
].filter(Boolean)
|
450
|
+
});
|
451
|
+
});
|
452
|
+
if (additionalProperties) {
|
453
|
+
const type = additionalProperties === true ? factory2.keywordTypeNodes.any : this.getTypeFromSchema(additionalProperties);
|
454
|
+
if (type) {
|
455
|
+
members.push(factory2.createIndexSignature(type));
|
456
|
+
}
|
457
|
+
}
|
458
|
+
return factory2.createTypeLiteralNode(members.filter(Boolean));
|
459
|
+
};
|
460
|
+
_getRefAlias = new WeakSet();
|
461
|
+
getRefAlias_fn = function(obj, _baseName) {
|
462
|
+
const { $ref } = obj;
|
463
|
+
let ref = this.refs[$ref];
|
464
|
+
if (ref) {
|
465
|
+
return factory2.createTypeReferenceNode(ref.propertyName, void 0);
|
466
|
+
}
|
467
|
+
const originalName = getUniqueName($ref.replace(/.+\//, ""), __privateGet(this, _usedAliasNames));
|
468
|
+
const propertyName = this.context.pluginManager.resolveName({ name: originalName, pluginKey, type: "type" });
|
469
|
+
ref = this.refs[$ref] = {
|
470
|
+
propertyName,
|
471
|
+
originalName
|
472
|
+
};
|
473
|
+
const path2 = this.context.pluginManager.resolvePath({ baseName: propertyName, pluginKey });
|
474
|
+
this.imports.push({
|
475
|
+
ref,
|
476
|
+
path: path2 || "",
|
477
|
+
isTypeOnly: true
|
478
|
+
});
|
479
|
+
return factory2.createTypeReferenceNode(ref.propertyName, void 0);
|
480
|
+
};
|
481
|
+
_getParsedSchema = new WeakSet();
|
482
|
+
getParsedSchema_fn = function(schema) {
|
483
|
+
const parsedSchema = getSchemaFactory(this.context.oas)(schema);
|
484
|
+
return parsedSchema;
|
485
|
+
};
|
486
|
+
_getBaseTypeFromSchema = new WeakSet();
|
487
|
+
getBaseTypeFromSchema_fn = function(_schema, baseName) {
|
488
|
+
const { schema, version } = __privateMethod(this, _getParsedSchema, getParsedSchema_fn).call(this, _schema);
|
489
|
+
if (!schema) {
|
490
|
+
return factory2.keywordTypeNodes.any;
|
491
|
+
}
|
492
|
+
if (isReference(schema)) {
|
493
|
+
return __privateMethod(this, _getRefAlias, getRefAlias_fn).call(this, schema, baseName);
|
494
|
+
}
|
495
|
+
if (schema.oneOf) {
|
496
|
+
const schemaWithoutOneOf = { ...schema, oneOf: void 0 };
|
497
|
+
const union = factory2.createUnionDeclaration({
|
498
|
+
withParentheses: true,
|
499
|
+
nodes: schema.oneOf.map((item) => {
|
500
|
+
return item && this.getTypeFromSchema(item);
|
501
|
+
}).filter((item) => {
|
502
|
+
return item && item !== factory2.keywordTypeNodes.any;
|
503
|
+
})
|
504
|
+
});
|
505
|
+
if (schemaWithoutOneOf.properties) {
|
506
|
+
return factory2.createIntersectionDeclaration({
|
507
|
+
nodes: [this.getTypeFromSchema(schemaWithoutOneOf, baseName), union].filter(Boolean)
|
508
|
+
});
|
509
|
+
}
|
510
|
+
return union;
|
511
|
+
}
|
512
|
+
if (schema.anyOf) {
|
513
|
+
const schemaWithoutAnyOf = { ...schema, anyOf: void 0 };
|
514
|
+
const union = factory2.createUnionDeclaration({
|
515
|
+
withParentheses: true,
|
516
|
+
nodes: schema.anyOf.map((item) => {
|
517
|
+
return item && this.getTypeFromSchema(item);
|
518
|
+
}).filter((item) => {
|
519
|
+
return item && item !== factory2.keywordTypeNodes.any;
|
520
|
+
})
|
521
|
+
});
|
522
|
+
if (schemaWithoutAnyOf.properties) {
|
523
|
+
return factory2.createIntersectionDeclaration({
|
524
|
+
nodes: [this.getTypeFromSchema(schemaWithoutAnyOf, baseName), union].filter(Boolean)
|
525
|
+
});
|
526
|
+
}
|
527
|
+
return union;
|
528
|
+
}
|
529
|
+
if (schema.allOf) {
|
530
|
+
const schemaWithoutAllOf = { ...schema, allOf: void 0 };
|
531
|
+
const and = factory2.createIntersectionDeclaration({
|
532
|
+
withParentheses: true,
|
533
|
+
nodes: schema.allOf.map((item) => {
|
534
|
+
return item && this.getTypeFromSchema(item);
|
535
|
+
}).filter((item) => {
|
536
|
+
return item && item !== factory2.keywordTypeNodes.any;
|
537
|
+
})
|
538
|
+
});
|
539
|
+
if (schemaWithoutAllOf.properties) {
|
540
|
+
return factory2.createIntersectionDeclaration({
|
541
|
+
nodes: [this.getTypeFromSchema(schemaWithoutAllOf, baseName), and].filter(Boolean)
|
542
|
+
});
|
543
|
+
}
|
544
|
+
return and;
|
545
|
+
}
|
546
|
+
if (schema.enum && baseName) {
|
547
|
+
const enumName = getUniqueName(baseName, this.options.usedEnumNames);
|
548
|
+
let enums = [...new Set(schema.enum)].map((key) => [key, key]);
|
549
|
+
if ("x-enumNames" in schema) {
|
550
|
+
enums = [...new Set(schema["x-enumNames"])].map((key, index) => {
|
551
|
+
return [key, schema.enum?.[index]];
|
552
|
+
});
|
553
|
+
}
|
554
|
+
this.extraNodes.push(
|
555
|
+
...factory2.createEnumDeclaration({
|
556
|
+
name: transformers2.camelCase(enumName),
|
557
|
+
typeName: this.context.pluginManager.resolveName({ name: enumName, pluginKey, type: "type" }),
|
558
|
+
enums,
|
559
|
+
type: this.options.enumType
|
560
|
+
})
|
561
|
+
);
|
562
|
+
return factory2.createTypeReferenceNode(this.context.pluginManager.resolveName({ name: enumName, pluginKey, type: "type" }), void 0);
|
563
|
+
}
|
564
|
+
if (schema.enum) {
|
565
|
+
return factory2.createUnionDeclaration({
|
566
|
+
nodes: schema.enum.map((name) => {
|
567
|
+
return factory2.createLiteralTypeNode(typeof name === "number" ? factory2.createNumericLiteral(name) : factory2.createStringLiteral(`${name}`));
|
568
|
+
})
|
569
|
+
});
|
570
|
+
}
|
571
|
+
if ("items" in schema) {
|
572
|
+
const node = this.getTypeFromSchema(schema.items, baseName);
|
573
|
+
if (node) {
|
574
|
+
return factory2.createArrayTypeNode(node);
|
575
|
+
}
|
576
|
+
}
|
577
|
+
if ("prefixItems" in schema) {
|
578
|
+
const prefixItems = schema.prefixItems;
|
579
|
+
return factory2.createTupleDeclaration({
|
580
|
+
nodes: prefixItems.map((item) => {
|
581
|
+
return this.getTypeFromSchema(item, void 0);
|
582
|
+
})
|
583
|
+
});
|
584
|
+
}
|
585
|
+
if (schema.properties || schema.additionalProperties) {
|
586
|
+
return __privateMethod(this, _getTypeFromProperties, getTypeFromProperties_fn).call(this, schema, baseName);
|
587
|
+
}
|
588
|
+
if (version === "3.1" && "const" in schema) {
|
589
|
+
if (schema["const"]) {
|
590
|
+
if (typeof schema["const"] === "string") {
|
591
|
+
return factory2.createLiteralTypeNode(factory2.createStringLiteral(schema["const"]));
|
592
|
+
} else if (typeof schema["const"] === "number") {
|
593
|
+
return factory2.createLiteralTypeNode(factory2.createNumericLiteral(schema["const"]));
|
594
|
+
}
|
595
|
+
} else {
|
596
|
+
return keywordTypeNodes.null;
|
597
|
+
}
|
598
|
+
}
|
599
|
+
if (schema.type) {
|
600
|
+
if (Array.isArray(schema.type)) {
|
601
|
+
const [type, nullable] = schema.type;
|
602
|
+
return factory2.createUnionDeclaration({
|
603
|
+
nodes: [
|
604
|
+
this.getTypeFromSchema(
|
605
|
+
{
|
606
|
+
...schema,
|
607
|
+
type
|
608
|
+
},
|
609
|
+
baseName
|
610
|
+
),
|
611
|
+
nullable ? factory2.createLiteralTypeNode(factory2.createNull()) : void 0
|
612
|
+
].filter(Boolean)
|
613
|
+
});
|
614
|
+
}
|
615
|
+
if (this.options.dateType === "date" && ["date", "date-time"].some((item) => item === schema.format)) {
|
616
|
+
return factory2.createTypeReferenceNode(factory2.createIdentifier("Date"));
|
617
|
+
}
|
618
|
+
if (schema.type in factory2.keywordTypeNodes) {
|
619
|
+
return factory2.keywordTypeNodes[schema.type];
|
620
|
+
}
|
621
|
+
}
|
622
|
+
if (schema.format === "binary") {
|
623
|
+
return factory2.createTypeReferenceNode("Blob", []);
|
624
|
+
}
|
625
|
+
return factory2.keywordTypeNodes.any;
|
626
|
+
};
|
627
|
+
|
628
|
+
// src/TypeBuilder.ts
|
629
|
+
var TypeBuilder = class extends OasBuilder {
|
630
|
+
build(name) {
|
631
|
+
const importMeta = [];
|
632
|
+
const codes = [];
|
633
|
+
const generated = this.items.filter((operationSchema) => name ? operationSchema.name === name : true).sort(transformers2.nameSorter).map((operationSchema) => {
|
634
|
+
const generator = new TypeGenerator(this.options, this.context);
|
635
|
+
const sources = generator.build({
|
636
|
+
schema: operationSchema.schema,
|
637
|
+
baseName: operationSchema.name,
|
638
|
+
description: operationSchema.description,
|
639
|
+
keysToOmit: operationSchema.keysToOmit
|
640
|
+
});
|
641
|
+
importMeta.push(...generator.imports);
|
642
|
+
return {
|
643
|
+
import: {
|
644
|
+
refs: generator.refs,
|
645
|
+
name: operationSchema.name
|
646
|
+
},
|
647
|
+
sources
|
648
|
+
};
|
649
|
+
}).sort(refsSorter);
|
650
|
+
generated.forEach((item) => {
|
651
|
+
codes.push(print(item.sources));
|
652
|
+
});
|
653
|
+
const imports = importMeta.map((item) => {
|
654
|
+
return {
|
655
|
+
name: [item.ref.propertyName],
|
656
|
+
path: item.path,
|
657
|
+
isTypeOnly: item.isTypeOnly
|
658
|
+
};
|
659
|
+
});
|
660
|
+
return {
|
661
|
+
imports,
|
662
|
+
source: transformers2.combineCodes(codes)
|
663
|
+
};
|
664
|
+
}
|
665
|
+
};
|
666
|
+
function printCombinedSchema2(name, operation, schemas) {
|
667
|
+
const properties = {
|
668
|
+
"response": factory2.createTypeReferenceNode(
|
669
|
+
factory2.createIdentifier(schemas.response.name),
|
670
|
+
void 0
|
671
|
+
)
|
672
|
+
};
|
673
|
+
if (schemas.request) {
|
674
|
+
properties["request"] = factory2.createTypeReferenceNode(
|
675
|
+
factory2.createIdentifier(schemas.request.name),
|
676
|
+
void 0
|
677
|
+
);
|
678
|
+
}
|
679
|
+
if (schemas.pathParams) {
|
680
|
+
properties["pathParams"] = factory2.createTypeReferenceNode(
|
681
|
+
factory2.createIdentifier(schemas.pathParams.name),
|
682
|
+
void 0
|
683
|
+
);
|
684
|
+
}
|
685
|
+
if (schemas.queryParams) {
|
686
|
+
properties["queryParams"] = factory2.createTypeReferenceNode(
|
687
|
+
factory2.createIdentifier(schemas.queryParams.name),
|
688
|
+
void 0
|
689
|
+
);
|
690
|
+
}
|
691
|
+
if (schemas.headerParams) {
|
692
|
+
properties["headerParams"] = factory2.createTypeReferenceNode(
|
693
|
+
factory2.createIdentifier(schemas.headerParams.name),
|
694
|
+
void 0
|
695
|
+
);
|
696
|
+
}
|
697
|
+
if (schemas.errors) {
|
698
|
+
properties["errors"] = factory2.createUnionDeclaration({
|
699
|
+
nodes: schemas.errors.map((error) => {
|
700
|
+
return factory2.createTypeReferenceNode(
|
701
|
+
factory2.createIdentifier(error.name),
|
702
|
+
void 0
|
703
|
+
);
|
704
|
+
})
|
705
|
+
});
|
706
|
+
}
|
707
|
+
const namespaceNode = factory2.createNamespaceDeclaration({
|
708
|
+
name: operation.method === "get" ? `${name}Query` : `${name}Mutation`,
|
709
|
+
statements: Object.keys(properties).map((key) => {
|
710
|
+
const type = properties[key];
|
711
|
+
if (!type) {
|
712
|
+
return void 0;
|
713
|
+
}
|
714
|
+
return factory2.createTypeAliasDeclaration({
|
715
|
+
modifiers: [factory2.modifiers.export],
|
716
|
+
name: transformers2.pascalCase(key),
|
717
|
+
type
|
718
|
+
});
|
719
|
+
}).filter(Boolean)
|
720
|
+
});
|
721
|
+
return print(namespaceNode);
|
722
|
+
}
|
723
|
+
function Mutation({
|
724
|
+
builder
|
725
|
+
}) {
|
726
|
+
const { source } = builder.build();
|
727
|
+
return /* @__PURE__ */ jsx(Fragment, { children: source });
|
728
|
+
}
|
729
|
+
Mutation.File = function({ mode }) {
|
730
|
+
const { options } = usePlugin();
|
731
|
+
const schemas = useSchemas();
|
732
|
+
const pluginManager = usePluginManager();
|
733
|
+
const oas = useOas();
|
734
|
+
const file = useOperationFile();
|
735
|
+
const factoryName = useOperationName({ type: "type" });
|
736
|
+
const operation = useOperation();
|
737
|
+
const builder = new TypeBuilder(options, { oas, pluginManager }).add(schemas.pathParams).add(schemas.queryParams).add(schemas.headerParams).add(schemas.response).add(schemas.request).add(schemas.errors);
|
738
|
+
const { source, imports } = builder.build();
|
739
|
+
return /* @__PURE__ */ jsx(Fragment, { children: /* @__PURE__ */ jsxs(
|
740
|
+
File,
|
741
|
+
{
|
742
|
+
baseName: file.baseName,
|
743
|
+
path: file.path,
|
744
|
+
meta: file.meta,
|
745
|
+
children: [
|
746
|
+
mode === "directory" && imports.map((item, index) => {
|
747
|
+
return /* @__PURE__ */ jsx(File.Import, { root: file.path, ...item }, index);
|
748
|
+
}),
|
749
|
+
/* @__PURE__ */ jsxs(File.Source, { children: [
|
750
|
+
source,
|
751
|
+
printCombinedSchema2(factoryName, operation, schemas)
|
752
|
+
] })
|
753
|
+
]
|
754
|
+
}
|
755
|
+
) });
|
756
|
+
};
|
757
|
+
|
758
|
+
export { Mutation, Oas, Query, definePlugin, pluginKey, pluginName };
|
759
|
+
//# sourceMappingURL=out.js.map
|
760
|
+
//# sourceMappingURL=chunk-HCQOGZHE.js.map
|