@kubb/plugin-faker 3.0.0-alpha.3 → 3.0.0-alpha.31
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 +13 -4
- package/dist/chunk-HTZ2R6MK.js +305 -0
- package/dist/chunk-HTZ2R6MK.js.map +1 -0
- package/dist/chunk-KBSFZ5OP.cjs +311 -0
- package/dist/chunk-KBSFZ5OP.cjs.map +1 -0
- package/dist/chunk-QFJUM25Z.js +113 -0
- package/dist/chunk-QFJUM25Z.js.map +1 -0
- package/dist/chunk-U6LOSTOE.cjs +115 -0
- package/dist/chunk-U6LOSTOE.cjs.map +1 -0
- package/dist/components.cjs +6 -565
- package/dist/components.cjs.map +1 -1
- package/dist/components.d.cts +15 -18
- package/dist/components.d.ts +15 -18
- package/dist/components.js +2 -570
- package/dist/components.js.map +1 -1
- package/dist/generators.cjs +13 -0
- package/dist/generators.cjs.map +1 -0
- package/dist/generators.d.cts +8 -0
- package/dist/generators.d.ts +8 -0
- package/dist/generators.js +4 -0
- package/dist/generators.js.map +1 -0
- package/dist/index.cjs +48 -505
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -125
- package/dist/index.d.ts +4 -125
- package/dist/index.js +35 -499
- package/dist/index.js.map +1 -1
- package/dist/types-C1bqlbxh.d.cts +94 -0
- package/dist/types-C1bqlbxh.d.ts +94 -0
- package/package.json +20 -16
- package/src/components/Faker.tsx +80 -0
- package/src/components/index.ts +1 -2
- package/src/generators/__snapshots__/createPet.ts +26 -0
- package/src/generators/__snapshots__/createPetSeed.ts +30 -0
- package/src/generators/__snapshots__/createPetUnknownTypeAny.ts +26 -0
- package/src/generators/__snapshots__/deletePet.ts +3 -0
- package/src/generators/__snapshots__/enumNames.ts +5 -0
- package/src/generators/__snapshots__/enumVarNames.ts +5 -0
- package/src/generators/__snapshots__/getPets.ts +29 -0
- package/src/generators/__snapshots__/pet.ts +8 -0
- package/src/generators/__snapshots__/petWithDateString.ts +8 -0
- package/src/generators/__snapshots__/petWithDayjs.ts +9 -0
- package/src/generators/__snapshots__/petWithMapper.ts +8 -0
- package/src/generators/__snapshots__/petWithRandExp.ts +9 -0
- package/src/generators/__snapshots__/pets.ts +8 -0
- package/src/generators/__snapshots__/showPetById.ts +29 -0
- package/src/generators/fakerGenerator.tsx +140 -0
- package/src/generators/index.ts +1 -0
- package/src/parser/index.ts +25 -14
- package/src/plugin.ts +26 -43
- package/src/types.ts +28 -67
- package/src/OperationGenerator.tsx +0 -31
- package/src/SchemaGenerator.tsx +0 -31
- package/src/components/OperationSchema.tsx +0 -84
- package/src/components/Schema.tsx +0 -146
package/dist/index.js
CHANGED
|
@@ -1,503 +1,53 @@
|
|
|
1
|
-
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import
|
|
7
|
-
import {
|
|
8
|
-
import { pluginTsName
|
|
1
|
+
import { fakerGenerator } from './chunk-QFJUM25Z.js';
|
|
2
|
+
import './chunk-HTZ2R6MK.js';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { createPlugin, FileManager, PluginManager } from '@kubb/core';
|
|
5
|
+
import { camelCase } from '@kubb/core/transformers';
|
|
6
|
+
import '@kubb/core/utils';
|
|
7
|
+
import { pluginOasName, SchemaGenerator, OperationGenerator } from '@kubb/plugin-oas';
|
|
8
|
+
import { pluginTsName } from '@kubb/plugin-ts';
|
|
9
9
|
|
|
10
|
-
// src/OperationGenerator.tsx
|
|
11
|
-
import { OperationGenerator as Generator2 } from "@kubb/plugin-oas";
|
|
12
|
-
import { Oas as Oas4 } from "@kubb/plugin-oas/components";
|
|
13
|
-
import { App as App2, createRoot as createRoot2 } from "@kubb/react";
|
|
14
|
-
|
|
15
|
-
// src/components/OperationSchema.tsx
|
|
16
|
-
import { Oas as Oas3 } from "@kubb/plugin-oas/components";
|
|
17
|
-
import { useOas, useOperation, useOperationManager } from "@kubb/plugin-oas/hooks";
|
|
18
|
-
import { File as File2, Parser, useApp as useApp2 } from "@kubb/react";
|
|
19
|
-
import { pluginTsName as pluginTsName2 } from "@kubb/plugin-ts";
|
|
20
|
-
|
|
21
|
-
// src/SchemaGenerator.tsx
|
|
22
|
-
import { SchemaGenerator as Generator } from "@kubb/plugin-oas";
|
|
23
|
-
import { Oas as Oas2 } from "@kubb/plugin-oas/components";
|
|
24
|
-
import { App, createRoot } from "@kubb/react";
|
|
25
|
-
|
|
26
|
-
// src/components/Schema.tsx
|
|
27
|
-
import { Oas } from "@kubb/plugin-oas/components";
|
|
28
|
-
import { File, Function, useApp, useFile } from "@kubb/react";
|
|
29
|
-
import { pluginTsName } from "@kubb/plugin-ts";
|
|
30
|
-
import transformers2 from "@kubb/core/transformers";
|
|
31
|
-
import { schemaKeywords as schemaKeywords2 } from "@kubb/plugin-oas";
|
|
32
|
-
import { useSchema } from "@kubb/plugin-oas/hooks";
|
|
33
|
-
|
|
34
|
-
// src/parser/index.ts
|
|
35
|
-
import transformers from "@kubb/core/transformers";
|
|
36
|
-
import { SchemaGenerator, isKeyword, schemaKeywords } from "@kubb/plugin-oas";
|
|
37
|
-
var fakerKeywordMapper = {
|
|
38
|
-
any: () => "undefined",
|
|
39
|
-
unknown: () => "unknown",
|
|
40
|
-
number: (min, max) => {
|
|
41
|
-
if (max !== void 0 && min !== void 0) {
|
|
42
|
-
return `faker.number.float({ min: ${min}, max: ${max} })`;
|
|
43
|
-
}
|
|
44
|
-
if (min !== void 0) {
|
|
45
|
-
return `faker.number.float({ min: ${min} })`;
|
|
46
|
-
}
|
|
47
|
-
if (max !== void 0) {
|
|
48
|
-
return `faker.number.float({ max: ${max} })`;
|
|
49
|
-
}
|
|
50
|
-
return "faker.number.float()";
|
|
51
|
-
},
|
|
52
|
-
integer: (min, max) => {
|
|
53
|
-
if (max !== void 0 && min !== void 0) {
|
|
54
|
-
return `faker.number.int({ min: ${min}, max: ${max} })`;
|
|
55
|
-
}
|
|
56
|
-
if (min !== void 0) {
|
|
57
|
-
return `faker.number.int({ min: ${min} })`;
|
|
58
|
-
}
|
|
59
|
-
if (max !== void 0) {
|
|
60
|
-
return `faker.number.int({ max: ${max} })`;
|
|
61
|
-
}
|
|
62
|
-
return "faker.number.int()";
|
|
63
|
-
},
|
|
64
|
-
string: (min, max) => {
|
|
65
|
-
if (max !== void 0 && min !== void 0) {
|
|
66
|
-
return `faker.string.alpha({ length: { min: ${min}, max: ${max} } })`;
|
|
67
|
-
}
|
|
68
|
-
if (min !== void 0) {
|
|
69
|
-
return `faker.string.alpha({ length: { min: ${min} } })`;
|
|
70
|
-
}
|
|
71
|
-
if (max !== void 0) {
|
|
72
|
-
return `faker.string.alpha({ length: { max: ${max} } })`;
|
|
73
|
-
}
|
|
74
|
-
return "faker.string.alpha()";
|
|
75
|
-
},
|
|
76
|
-
boolean: () => "faker.datatype.boolean()",
|
|
77
|
-
undefined: () => "undefined",
|
|
78
|
-
null: () => "null",
|
|
79
|
-
array: (items = []) => `faker.helpers.arrayElements([${items.join(", ")}]) as any`,
|
|
80
|
-
tuple: (items = []) => `faker.helpers.arrayElements([${items.join(", ")}]) as any`,
|
|
81
|
-
enum: (items = []) => `faker.helpers.arrayElement<any>([${items.join(", ")}])`,
|
|
82
|
-
union: (items = []) => `faker.helpers.arrayElement<any>([${items.join(", ")}])`,
|
|
83
|
-
/**
|
|
84
|
-
* ISO 8601
|
|
85
|
-
*/
|
|
86
|
-
datetime: () => "faker.date.anytime().toISOString()",
|
|
87
|
-
/**
|
|
88
|
-
* Type `'date'` Date
|
|
89
|
-
* Type `'string'` ISO date format (YYYY-MM-DD)
|
|
90
|
-
* @default ISO date format (YYYY-MM-DD)
|
|
91
|
-
*/
|
|
92
|
-
date: (type = "string", parser) => {
|
|
93
|
-
if (type === "string") {
|
|
94
|
-
if (parser) {
|
|
95
|
-
return `${parser}(faker.date.anytime()).format("YYYY-MM-DD")`;
|
|
96
|
-
}
|
|
97
|
-
return "faker.date.anytime().toString()";
|
|
98
|
-
}
|
|
99
|
-
return "faker.date.anytime()";
|
|
100
|
-
},
|
|
101
|
-
/**
|
|
102
|
-
* Type `'date'` Date
|
|
103
|
-
* Type `'string'` ISO time format (HH:mm:ss[.SSSSSS])
|
|
104
|
-
* @default ISO time format (HH:mm:ss[.SSSSSS])
|
|
105
|
-
*/
|
|
106
|
-
time: (type = "string", parser) => {
|
|
107
|
-
if (type === "string") {
|
|
108
|
-
if (parser) {
|
|
109
|
-
return `${parser}(faker.date.anytime()).format("HH:mm:ss")`;
|
|
110
|
-
}
|
|
111
|
-
return "faker.date.anytime().toString()";
|
|
112
|
-
}
|
|
113
|
-
return "faker.date.anytime()";
|
|
114
|
-
},
|
|
115
|
-
uuid: () => "faker.string.uuid()",
|
|
116
|
-
url: () => "faker.internet.url()",
|
|
117
|
-
and: (items = []) => `Object.assign({}, ${items.join(", ")})`,
|
|
118
|
-
object: () => "object",
|
|
119
|
-
ref: () => "ref",
|
|
120
|
-
matches: (value = "", regexGenerator = "faker") => {
|
|
121
|
-
if (regexGenerator === "randexp") {
|
|
122
|
-
return `${transformers.toRegExpString(value, "RandExp")}.gen()`;
|
|
123
|
-
}
|
|
124
|
-
return `faker.helpers.fromRegExp(${transformers.toRegExpString(value)})`;
|
|
125
|
-
},
|
|
126
|
-
email: () => "faker.internet.email()",
|
|
127
|
-
firstName: () => "faker.person.firstName()",
|
|
128
|
-
lastName: () => "faker.person.lastName()",
|
|
129
|
-
password: () => "faker.internet.password()",
|
|
130
|
-
phone: () => "faker.phone.number()",
|
|
131
|
-
blob: () => "faker.image.imageUrl() as unknown as Blob",
|
|
132
|
-
default: void 0,
|
|
133
|
-
describe: void 0,
|
|
134
|
-
const: (value) => value ?? "",
|
|
135
|
-
max: void 0,
|
|
136
|
-
min: void 0,
|
|
137
|
-
nullable: void 0,
|
|
138
|
-
nullish: void 0,
|
|
139
|
-
optional: void 0,
|
|
140
|
-
readOnly: void 0,
|
|
141
|
-
strict: void 0,
|
|
142
|
-
deprecated: void 0,
|
|
143
|
-
example: void 0,
|
|
144
|
-
schema: void 0,
|
|
145
|
-
catchall: void 0,
|
|
146
|
-
name: void 0
|
|
147
|
-
};
|
|
148
|
-
function schemaKeywordsorter(a, b) {
|
|
149
|
-
if (b.keyword === "null") {
|
|
150
|
-
return -1;
|
|
151
|
-
}
|
|
152
|
-
return 0;
|
|
153
|
-
}
|
|
154
|
-
function joinItems(items) {
|
|
155
|
-
switch (items.length) {
|
|
156
|
-
case 0:
|
|
157
|
-
return "undefined";
|
|
158
|
-
case 1:
|
|
159
|
-
return items[0];
|
|
160
|
-
default:
|
|
161
|
-
return fakerKeywordMapper.union(items);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
function parse(parent, current, options) {
|
|
165
|
-
const value = fakerKeywordMapper[current.keyword];
|
|
166
|
-
if (!value) {
|
|
167
|
-
return void 0;
|
|
168
|
-
}
|
|
169
|
-
if (isKeyword(current, schemaKeywords.union)) {
|
|
170
|
-
return fakerKeywordMapper.union(current.args.map((schema) => parse(current, schema, { ...options, withData: false })).filter(Boolean));
|
|
171
|
-
}
|
|
172
|
-
if (isKeyword(current, schemaKeywords.and)) {
|
|
173
|
-
return fakerKeywordMapper.and(current.args.map((schema) => parse(current, schema, { ...options, withData: false })).filter(Boolean));
|
|
174
|
-
}
|
|
175
|
-
if (isKeyword(current, schemaKeywords.array)) {
|
|
176
|
-
return fakerKeywordMapper.array(current.args.items.map((schema) => parse(current, schema, { ...options, withData: false })).filter(Boolean));
|
|
177
|
-
}
|
|
178
|
-
if (isKeyword(current, schemaKeywords.enum)) {
|
|
179
|
-
return fakerKeywordMapper.enum(
|
|
180
|
-
current.args.items.map((schema) => {
|
|
181
|
-
if (schema.format === "number") {
|
|
182
|
-
return schema.name;
|
|
183
|
-
}
|
|
184
|
-
return transformers.stringify(schema.name);
|
|
185
|
-
})
|
|
186
|
-
);
|
|
187
|
-
}
|
|
188
|
-
if (isKeyword(current, schemaKeywords.ref)) {
|
|
189
|
-
if (!current.args?.name) {
|
|
190
|
-
throw new Error(`Name not defined for keyword ${current.keyword}`);
|
|
191
|
-
}
|
|
192
|
-
if (options.withData) {
|
|
193
|
-
return `${current.args.name}(data)`;
|
|
194
|
-
}
|
|
195
|
-
return `${current.args.name}()`;
|
|
196
|
-
}
|
|
197
|
-
if (isKeyword(current, schemaKeywords.object)) {
|
|
198
|
-
const argsObject = Object.entries(current.args?.properties || {}).filter((item) => {
|
|
199
|
-
const schema = item[1];
|
|
200
|
-
return schema && typeof schema.map === "function";
|
|
201
|
-
}).map(([name, schemas]) => {
|
|
202
|
-
const nameSchema = schemas.find((schema) => schema.keyword === schemaKeywords.name);
|
|
203
|
-
const mappedName = nameSchema?.args || name;
|
|
204
|
-
if (options.mapper?.[mappedName]) {
|
|
205
|
-
return `"${name}": ${options.mapper?.[mappedName]}`;
|
|
206
|
-
}
|
|
207
|
-
return `"${name}": ${joinItems(
|
|
208
|
-
schemas.sort(schemaKeywordsorter).map((schema) => parse(current, schema, { ...options, withData: false })).filter(Boolean)
|
|
209
|
-
)}`;
|
|
210
|
-
}).join(",");
|
|
211
|
-
return `{${argsObject}}`;
|
|
212
|
-
}
|
|
213
|
-
if (isKeyword(current, schemaKeywords.tuple)) {
|
|
214
|
-
if (Array.isArray(current.args.items)) {
|
|
215
|
-
return fakerKeywordMapper.tuple(current.args.items.map((schema) => parse(current, schema, { ...options, withData: false })).filter(Boolean));
|
|
216
|
-
}
|
|
217
|
-
return parse(current, current.args.items, { ...options, withData: false });
|
|
218
|
-
}
|
|
219
|
-
if (isKeyword(current, schemaKeywords.const)) {
|
|
220
|
-
if (current.args.format === "number" && current.args.name !== void 0) {
|
|
221
|
-
return fakerKeywordMapper.const(current.args.name?.toString());
|
|
222
|
-
}
|
|
223
|
-
return fakerKeywordMapper.const(transformers.stringify(current.args.value));
|
|
224
|
-
}
|
|
225
|
-
if (isKeyword(current, schemaKeywords.matches) && current.args) {
|
|
226
|
-
return fakerKeywordMapper.matches(current.args, options.regexGenerator);
|
|
227
|
-
}
|
|
228
|
-
if (isKeyword(current, schemaKeywords.null) || isKeyword(current, schemaKeywords.undefined) || isKeyword(current, schemaKeywords.any)) {
|
|
229
|
-
return value() || "";
|
|
230
|
-
}
|
|
231
|
-
if (isKeyword(current, schemaKeywords.string)) {
|
|
232
|
-
if (parent) {
|
|
233
|
-
const minSchema = SchemaGenerator.find([parent], schemaKeywords.min);
|
|
234
|
-
const maxSchema = SchemaGenerator.find([parent], schemaKeywords.max);
|
|
235
|
-
return fakerKeywordMapper.string(minSchema?.args, maxSchema?.args);
|
|
236
|
-
}
|
|
237
|
-
return fakerKeywordMapper.string();
|
|
238
|
-
}
|
|
239
|
-
if (isKeyword(current, schemaKeywords.number)) {
|
|
240
|
-
if (parent) {
|
|
241
|
-
const minSchema = SchemaGenerator.find([parent], schemaKeywords.min);
|
|
242
|
-
const maxSchema = SchemaGenerator.find([parent], schemaKeywords.max);
|
|
243
|
-
return fakerKeywordMapper.number(minSchema?.args, maxSchema?.args);
|
|
244
|
-
}
|
|
245
|
-
return fakerKeywordMapper.number();
|
|
246
|
-
}
|
|
247
|
-
if (isKeyword(current, schemaKeywords.integer)) {
|
|
248
|
-
if (parent) {
|
|
249
|
-
const minSchema = SchemaGenerator.find([parent], schemaKeywords.min);
|
|
250
|
-
const maxSchema = SchemaGenerator.find([parent], schemaKeywords.max);
|
|
251
|
-
return fakerKeywordMapper.integer(minSchema?.args, maxSchema?.args);
|
|
252
|
-
}
|
|
253
|
-
return fakerKeywordMapper.integer();
|
|
254
|
-
}
|
|
255
|
-
if (isKeyword(current, schemaKeywords.datetime)) {
|
|
256
|
-
return fakerKeywordMapper.datetime();
|
|
257
|
-
}
|
|
258
|
-
if (isKeyword(current, schemaKeywords.date)) {
|
|
259
|
-
return fakerKeywordMapper.date(current.args.type, options.dateParser);
|
|
260
|
-
}
|
|
261
|
-
if (isKeyword(current, schemaKeywords.time)) {
|
|
262
|
-
return fakerKeywordMapper.time(current.args.type, options.dateParser);
|
|
263
|
-
}
|
|
264
|
-
if (current.keyword in fakerKeywordMapper && "args" in current) {
|
|
265
|
-
const value2 = fakerKeywordMapper[current.keyword];
|
|
266
|
-
const options2 = JSON.stringify(current.args);
|
|
267
|
-
return value2(options2);
|
|
268
|
-
}
|
|
269
|
-
if (current.keyword in fakerKeywordMapper) {
|
|
270
|
-
return value();
|
|
271
|
-
}
|
|
272
|
-
return void 0;
|
|
273
|
-
}
|
|
274
|
-
|
|
275
|
-
// src/components/Schema.tsx
|
|
276
|
-
import { Fragment, jsx, jsxs } from "@kubb/react/jsx-runtime";
|
|
277
|
-
function Schema(props) {
|
|
278
|
-
const { withData, description } = props;
|
|
279
|
-
const { tree, name } = useSchema();
|
|
280
|
-
const {
|
|
281
|
-
pluginManager,
|
|
282
|
-
plugin: {
|
|
283
|
-
options: { dateParser, regexGenerator, mapper, seed }
|
|
284
|
-
}
|
|
285
|
-
} = useApp();
|
|
286
|
-
const resolvedName = pluginManager.resolveName({
|
|
287
|
-
name,
|
|
288
|
-
pluginKey: [pluginFakerName],
|
|
289
|
-
type: "function"
|
|
290
|
-
});
|
|
291
|
-
const typeName = pluginManager.resolveName({
|
|
292
|
-
name,
|
|
293
|
-
pluginKey: [pluginTsName],
|
|
294
|
-
type: "type"
|
|
295
|
-
});
|
|
296
|
-
const fakerText = joinItems(
|
|
297
|
-
tree.map((schema) => parse(void 0, schema, { name: resolvedName, typeName, seed, regexGenerator, mapper, withData, dateParser })).filter(Boolean)
|
|
298
|
-
);
|
|
299
|
-
let fakerDefaultOverride = void 0;
|
|
300
|
-
let fakerTextWithOverride = fakerText;
|
|
301
|
-
if (withData && fakerText.startsWith("{")) {
|
|
302
|
-
fakerDefaultOverride = "{}";
|
|
303
|
-
fakerTextWithOverride = `{
|
|
304
|
-
...${fakerText},
|
|
305
|
-
...data
|
|
306
|
-
}`;
|
|
307
|
-
}
|
|
308
|
-
if (withData && fakerText.startsWith("faker.helpers.arrayElements")) {
|
|
309
|
-
fakerDefaultOverride = "[]";
|
|
310
|
-
fakerTextWithOverride = `[
|
|
311
|
-
...${fakerText},
|
|
312
|
-
...data
|
|
313
|
-
]`;
|
|
314
|
-
}
|
|
315
|
-
const params = fakerDefaultOverride ? `data: NonNullable<Partial<${typeName}>> = ${fakerDefaultOverride}` : `data?: NonNullable<Partial<${typeName}>>`;
|
|
316
|
-
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
317
|
-
/* @__PURE__ */ jsxs(
|
|
318
|
-
Function,
|
|
319
|
-
{
|
|
320
|
-
export: true,
|
|
321
|
-
name: resolvedName,
|
|
322
|
-
JSDoc: { comments: [description ? `@description ${transformers2.jsStringEscape(description)}` : void 0].filter(Boolean) },
|
|
323
|
-
params: withData ? params : "",
|
|
324
|
-
returnType: typeName ? `NonNullable<${typeName}>` : "",
|
|
325
|
-
children: [
|
|
326
|
-
seed ? `faker.seed(${JSON.stringify(seed)})` : "",
|
|
327
|
-
/* @__PURE__ */ jsx("br", {}),
|
|
328
|
-
/* @__PURE__ */ jsx(Function.Return, { children: fakerTextWithOverride })
|
|
329
|
-
]
|
|
330
|
-
}
|
|
331
|
-
),
|
|
332
|
-
/* @__PURE__ */ jsx("br", {})
|
|
333
|
-
] });
|
|
334
|
-
}
|
|
335
|
-
Schema.File = function({}) {
|
|
336
|
-
const { pluginManager } = useApp();
|
|
337
|
-
const { tree, schema } = useSchema();
|
|
338
|
-
const withData = tree.some(
|
|
339
|
-
(schema2) => schema2.keyword === schemaKeywords2.array || schema2.keyword === schemaKeywords2.and || schema2.keyword === schemaKeywords2.object || schema2.keyword === schemaKeywords2.union || schema2.keyword === schemaKeywords2.tuple
|
|
340
|
-
);
|
|
341
|
-
return /* @__PURE__ */ jsxs(Oas.Schema.File, { output: pluginManager.config.output.path, children: [
|
|
342
|
-
/* @__PURE__ */ jsx(Schema.Imports, {}),
|
|
343
|
-
/* @__PURE__ */ jsx(File.Source, { children: /* @__PURE__ */ jsx(Schema, { description: schema?.description, withData }) })
|
|
344
|
-
] });
|
|
345
|
-
};
|
|
346
|
-
Schema.Imports = () => {
|
|
347
|
-
const {
|
|
348
|
-
pluginManager,
|
|
349
|
-
plugin: {
|
|
350
|
-
options: { extName, dateParser, regexGenerator }
|
|
351
|
-
}
|
|
352
|
-
} = useApp();
|
|
353
|
-
const { path: root } = useFile();
|
|
354
|
-
const { name, tree, schema } = useSchema();
|
|
355
|
-
const typeName = pluginManager.resolveName({
|
|
356
|
-
name,
|
|
357
|
-
pluginKey: [pluginTsName],
|
|
358
|
-
type: "type"
|
|
359
|
-
});
|
|
360
|
-
const typeFileName = pluginManager.resolveName({
|
|
361
|
-
name,
|
|
362
|
-
pluginKey: [pluginTsName],
|
|
363
|
-
type: "file"
|
|
364
|
-
});
|
|
365
|
-
const typePath = pluginManager.resolvePath({
|
|
366
|
-
baseName: typeFileName,
|
|
367
|
-
pluginKey: [pluginTsName]
|
|
368
|
-
});
|
|
369
|
-
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
370
|
-
/* @__PURE__ */ jsx(File.Import, { name: ["faker"], path: "@faker-js/faker" }),
|
|
371
|
-
regexGenerator === "randexp" && /* @__PURE__ */ jsx(File.Import, { name: "RandExp", path: "randexp" }),
|
|
372
|
-
dateParser && /* @__PURE__ */ jsx(File.Import, { path: dateParser, name: dateParser }),
|
|
373
|
-
typeName && typePath && /* @__PURE__ */ jsx(File.Import, { extName, isTypeOnly: true, root, path: typePath, name: [typeName] })
|
|
374
|
-
] });
|
|
375
|
-
};
|
|
376
|
-
|
|
377
|
-
// src/SchemaGenerator.tsx
|
|
378
|
-
import { jsx as jsx2 } from "@kubb/react/jsx-runtime";
|
|
379
|
-
var SchemaGenerator2 = class extends Generator {
|
|
380
|
-
async schema(name, schema, options) {
|
|
381
|
-
const { oas, pluginManager, plugin, mode, output } = this.context;
|
|
382
|
-
const root = createRoot({
|
|
383
|
-
logger: pluginManager.logger
|
|
384
|
-
});
|
|
385
|
-
const tree = this.parse({ schema, name });
|
|
386
|
-
root.render(
|
|
387
|
-
/* @__PURE__ */ jsx2(App, { pluginManager, plugin: { ...plugin, options }, mode, children: /* @__PURE__ */ jsx2(Oas2, { oas, children: /* @__PURE__ */ jsx2(Oas2.Schema, { name, value: schema, tree, children: /* @__PURE__ */ jsx2(Schema.File, {}) }) }) })
|
|
388
|
-
);
|
|
389
|
-
return root.files;
|
|
390
|
-
}
|
|
391
|
-
};
|
|
392
|
-
|
|
393
|
-
// src/components/OperationSchema.tsx
|
|
394
|
-
import { jsx as jsx3, jsxs as jsxs2 } from "@kubb/react/jsx-runtime";
|
|
395
|
-
function OperationSchema({ description }) {
|
|
396
|
-
return /* @__PURE__ */ jsx3(Schema, { withData: false, description });
|
|
397
|
-
}
|
|
398
|
-
OperationSchema.File = function({}) {
|
|
399
|
-
const { plugin, pluginManager, mode } = useApp2();
|
|
400
|
-
const oas = useOas();
|
|
401
|
-
const { getSchemas, getFile } = useOperationManager();
|
|
402
|
-
const operation = useOperation();
|
|
403
|
-
const file = getFile(operation);
|
|
404
|
-
const schemas = getSchemas(operation);
|
|
405
|
-
const generator = new SchemaGenerator2(plugin.options, {
|
|
406
|
-
oas,
|
|
407
|
-
plugin,
|
|
408
|
-
pluginManager,
|
|
409
|
-
mode,
|
|
410
|
-
override: plugin.options.override
|
|
411
|
-
});
|
|
412
|
-
const items = [schemas.pathParams, schemas.queryParams, schemas.headerParams, schemas.statusCodes, schemas.request, schemas.response].flat().filter(Boolean);
|
|
413
|
-
const mapItem = ({ name, schema, description, ...options }, i) => {
|
|
414
|
-
const typeName = pluginManager.resolveName({
|
|
415
|
-
name,
|
|
416
|
-
pluginKey: [pluginTsName2],
|
|
417
|
-
type: "type"
|
|
418
|
-
});
|
|
419
|
-
const typeFileName = pluginManager.resolveName({
|
|
420
|
-
name: options.operationName || name,
|
|
421
|
-
pluginKey: [pluginTsName2],
|
|
422
|
-
type: "file"
|
|
423
|
-
});
|
|
424
|
-
const typePath = pluginManager.resolvePath({
|
|
425
|
-
baseName: typeFileName,
|
|
426
|
-
pluginKey: [pluginTsName2],
|
|
427
|
-
options: { tag: options.operation?.getTags()[0]?.name }
|
|
428
|
-
});
|
|
429
|
-
const tree = generator.parse({ schema, name });
|
|
430
|
-
return /* @__PURE__ */ jsxs2(Oas3.Schema, { name, value: schema, tree, children: [
|
|
431
|
-
typeName && typePath && /* @__PURE__ */ jsx3(File2.Import, { extName: plugin.options.extName, isTypeOnly: true, root: file.path, path: typePath, name: [typeName] }),
|
|
432
|
-
plugin.options.dateParser && /* @__PURE__ */ jsx3(File2.Import, { path: plugin.options.dateParser, name: plugin.options.dateParser }),
|
|
433
|
-
mode === "split" && /* @__PURE__ */ jsx3(Oas3.Schema.Imports, { extName: plugin.options.extName }),
|
|
434
|
-
/* @__PURE__ */ jsx3(File2.Source, { children: /* @__PURE__ */ jsx3(OperationSchema, { description }) })
|
|
435
|
-
] }, i);
|
|
436
|
-
};
|
|
437
|
-
return /* @__PURE__ */ jsx3(Parser, { language: "typescript", children: /* @__PURE__ */ jsxs2(File2, { baseName: file.baseName, path: file.path, meta: file.meta, children: [
|
|
438
|
-
/* @__PURE__ */ jsx3(File2.Import, { name: ["faker"], path: "@faker-js/faker" }),
|
|
439
|
-
plugin.options.regexGenerator === "randexp" && /* @__PURE__ */ jsx3(File2.Import, { name: "RandExp", path: "randexp" }),
|
|
440
|
-
items.map(mapItem)
|
|
441
|
-
] }) });
|
|
442
|
-
};
|
|
443
|
-
|
|
444
|
-
// src/OperationGenerator.tsx
|
|
445
|
-
import { jsx as jsx4 } from "@kubb/react/jsx-runtime";
|
|
446
|
-
var OperationGenerator = class extends Generator2 {
|
|
447
|
-
async operation(operation, options) {
|
|
448
|
-
const { oas, pluginManager, plugin, mode } = this.context;
|
|
449
|
-
const root = createRoot2({
|
|
450
|
-
logger: pluginManager.logger
|
|
451
|
-
});
|
|
452
|
-
root.render(
|
|
453
|
-
/* @__PURE__ */ jsx4(App2, { pluginManager, plugin: { ...plugin, options }, mode, children: /* @__PURE__ */ jsx4(Oas4, { oas, operations: [operation], generator: this, children: /* @__PURE__ */ jsx4(Oas4.Operation, { operation, children: /* @__PURE__ */ jsx4(OperationSchema.File, {}) }) }) })
|
|
454
|
-
);
|
|
455
|
-
return root.files;
|
|
456
|
-
}
|
|
457
|
-
};
|
|
458
|
-
|
|
459
|
-
// src/plugin.ts
|
|
460
10
|
var pluginFakerName = "plugin-faker";
|
|
461
11
|
var pluginFaker = createPlugin((options) => {
|
|
462
12
|
const {
|
|
463
|
-
output = { path: "mocks" },
|
|
13
|
+
output = { path: "mocks", barrelType: "named" },
|
|
464
14
|
seed,
|
|
465
15
|
group,
|
|
466
16
|
exclude = [],
|
|
467
17
|
include,
|
|
468
18
|
override = [],
|
|
469
|
-
transformers
|
|
19
|
+
transformers = {},
|
|
470
20
|
mapper = {},
|
|
471
|
-
dateType = "string",
|
|
472
21
|
unknownType = "any",
|
|
473
|
-
|
|
22
|
+
dateType = "string",
|
|
23
|
+
dateParser = "faker",
|
|
24
|
+
generators = [fakerGenerator].filter(Boolean),
|
|
474
25
|
regexGenerator = "faker"
|
|
475
26
|
} = options;
|
|
476
|
-
const template = group?.output ? group.output : `${output.path}/{{tag}}Controller`;
|
|
477
27
|
return {
|
|
478
28
|
name: pluginFakerName,
|
|
479
29
|
options: {
|
|
480
|
-
|
|
481
|
-
transformers
|
|
482
|
-
dateType,
|
|
30
|
+
output,
|
|
31
|
+
transformers,
|
|
483
32
|
seed,
|
|
33
|
+
dateType,
|
|
484
34
|
unknownType,
|
|
485
35
|
dateParser,
|
|
486
36
|
mapper,
|
|
487
37
|
override,
|
|
488
38
|
regexGenerator
|
|
489
39
|
},
|
|
490
|
-
pre: [pluginOasName,
|
|
40
|
+
pre: [pluginOasName, pluginTsName],
|
|
491
41
|
resolvePath(baseName, pathMode, options2) {
|
|
492
42
|
const root = path.resolve(this.config.root, this.config.output.path);
|
|
493
43
|
const mode = pathMode ?? FileManager.getMode(path.resolve(root, output.path));
|
|
44
|
+
if (options2?.tag && group?.type === "tag") {
|
|
45
|
+
const groupName = group?.name ? group.name : (ctx) => `${ctx.group}Controller`;
|
|
46
|
+
return path.resolve(root, output.path, groupName({ group: camelCase(options2.tag) }), baseName);
|
|
47
|
+
}
|
|
494
48
|
if (mode === "single") {
|
|
495
49
|
return path.resolve(root, output.path);
|
|
496
50
|
}
|
|
497
|
-
if (options2?.tag && group?.type === "tag") {
|
|
498
|
-
const tag = camelCase(options2.tag);
|
|
499
|
-
return path.resolve(root, renderTemplate(template, { tag }), baseName);
|
|
500
|
-
}
|
|
501
51
|
return path.resolve(root, output.path, baseName);
|
|
502
52
|
},
|
|
503
53
|
resolveName(name, type) {
|
|
@@ -506,7 +56,7 @@ var pluginFaker = createPlugin((options) => {
|
|
|
506
56
|
isFile: type === "file"
|
|
507
57
|
});
|
|
508
58
|
if (type) {
|
|
509
|
-
return
|
|
59
|
+
return transformers?.name?.(resolvedName, type) || resolvedName;
|
|
510
60
|
}
|
|
511
61
|
return resolvedName;
|
|
512
62
|
},
|
|
@@ -515,7 +65,7 @@ var pluginFaker = createPlugin((options) => {
|
|
|
515
65
|
const oas = await swaggerPlugin.context.getOas();
|
|
516
66
|
const root = path.resolve(this.config.root, this.config.output.path);
|
|
517
67
|
const mode = FileManager.getMode(path.resolve(root, output.path));
|
|
518
|
-
const schemaGenerator = new
|
|
68
|
+
const schemaGenerator = new SchemaGenerator(this.plugin.options, {
|
|
519
69
|
oas,
|
|
520
70
|
pluginManager: this.pluginManager,
|
|
521
71
|
plugin: this.plugin,
|
|
@@ -525,7 +75,7 @@ var pluginFaker = createPlugin((options) => {
|
|
|
525
75
|
mode,
|
|
526
76
|
output: output.path
|
|
527
77
|
});
|
|
528
|
-
const schemaFiles = await schemaGenerator.build();
|
|
78
|
+
const schemaFiles = await schemaGenerator.build(...generators);
|
|
529
79
|
await this.addFile(...schemaFiles);
|
|
530
80
|
const operationGenerator = new OperationGenerator(this.plugin.options, {
|
|
531
81
|
oas,
|
|
@@ -537,37 +87,23 @@ var pluginFaker = createPlugin((options) => {
|
|
|
537
87
|
override,
|
|
538
88
|
mode
|
|
539
89
|
});
|
|
540
|
-
const operationFiles = await operationGenerator.build();
|
|
90
|
+
const operationFiles = await operationGenerator.build(...generators);
|
|
541
91
|
await this.addFile(...operationFiles);
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
if (this.config.output.write === false) {
|
|
545
|
-
return;
|
|
546
|
-
}
|
|
547
|
-
const root = path.resolve(this.config.root, this.config.output.path);
|
|
548
|
-
if (group?.type === "tag") {
|
|
549
|
-
const rootFiles = await getGroupedByTagFiles({
|
|
550
|
-
logger: this.logger,
|
|
551
|
-
files: this.fileManager.files,
|
|
552
|
-
plugin: this.plugin,
|
|
553
|
-
template,
|
|
554
|
-
exportAs: group.exportAs || "{{tag}}Mocks",
|
|
555
|
-
root,
|
|
556
|
-
output
|
|
557
|
-
});
|
|
558
|
-
await this.addFile(...rootFiles);
|
|
559
|
-
}
|
|
560
|
-
await this.fileManager.addIndexes({
|
|
92
|
+
const barrelFiles = await this.fileManager.getBarrelFiles({
|
|
93
|
+
type: output.barrelType ?? "named",
|
|
561
94
|
root,
|
|
562
95
|
output,
|
|
563
|
-
|
|
96
|
+
files: this.fileManager.files,
|
|
97
|
+
meta: {
|
|
98
|
+
pluginKey: this.plugin.key
|
|
99
|
+
},
|
|
564
100
|
logger: this.logger
|
|
565
101
|
});
|
|
102
|
+
await this.addFile(...barrelFiles);
|
|
566
103
|
}
|
|
567
104
|
};
|
|
568
105
|
});
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
};
|
|
106
|
+
|
|
107
|
+
export { pluginFaker, pluginFakerName };
|
|
108
|
+
//# sourceMappingURL=index.js.map
|
|
573
109
|
//# sourceMappingURL=index.js.map
|