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