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