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