@kubb/plugin-faker 3.0.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Stijn Van Hulle
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,44 @@
1
+ <div align="center">
2
+
3
+ <!-- <img src="assets/logo.png" alt="logo" width="200" height="auto" /> -->
4
+ <h1>@kubb/plugin-faker</h1>
5
+
6
+ <p>
7
+ Swagger integration to create mock data based on Faker.js
8
+ </p>
9
+ <img src="https://raw.githubusercontent.com/kubb-labs/kubb/main/assets/banner.png" alt="logo" height="auto" />
10
+
11
+ [![npm version][npm-version-src]][npm-version-href]
12
+ [![npm downloads][npm-downloads-src]][npm-downloads-href]
13
+ [![Coverage][coverage-src]][coverage-href]
14
+ [![License][license-src]][license-href]
15
+
16
+ <!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
17
+ <!-- ALL-CONTRIBUTORS-BADGE:END -->
18
+ </p>
19
+
20
+ <h4>
21
+ <a href="https://codesandbox.io/s/github/kubb-labs/kubb/tree/alpha/examples/typescript" target="_blank">View Demo</a>
22
+ <span> · </span>
23
+ <a href="https://kubb.dev/" target="_blank">Documentation</a>
24
+ <span> · </span>
25
+ <a href="https://github.com/kubb-labs/kubb/issues/" target="_blank">Report Bug</a>
26
+ <span> · </span>
27
+ <a href="https://github.com/kubb-labs/kubb/issues/" target="_blank">Request Feature</a>
28
+ </h4>
29
+ </div>
30
+
31
+ <!-- Badges -->
32
+
33
+ [npm-version-src]: https://img.shields.io/npm/v/@kubb/plugin-faker?flat&colorA=18181B&colorB=f58517
34
+ [npm-version-href]: https://npmjs.com/package/@kubb/plugin-faker
35
+ [npm-downloads-src]: https://img.shields.io/npm/dm/@kubb/plugin-faker?flat&colorA=18181B&colorB=f58517
36
+ [npm-downloads-href]: https://npmjs.com/package/@kubb/plugin-faker
37
+ [license-src]: https://img.shields.io/github/license/kubb-labs/kubb.svg?flat&colorA=18181B&colorB=f58517
38
+ [license-href]: https://github.com/kubb-labs/kubb/blob/main/LICENSE
39
+ [build-src]: https://img.shields.io/github/actions/workflow/status/kubb-labs/kubb/ci.yaml?style=flat&colorA=18181B&colorB=f58517
40
+ [build-href]: https://www.npmjs.com/package/@kubb/plugin-faker
41
+ [minified-src]: https://img.shields.io/bundlephobia/min/@kubb/plugin-faker?style=flat&colorA=18181B&colorB=f58517
42
+ [minified-href]: https://www.npmjs.com/package/@kubb/plugin-faker
43
+ [coverage-src]: https://img.shields.io/codecov/c/github/kubb-labs/kubb?style=flat&colorA=18181B&colorB=f58517
44
+ [coverage-href]: https://www.npmjs.com/package/@kubb/plugin-faker
@@ -0,0 +1,577 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); 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; }// src/components/OperationSchema.tsx
2
+ var _components = require('@kubb/plugin-oas/components');
3
+ var _hooks = require('@kubb/plugin-oas/hooks');
4
+ var _react = require('@kubb/react');
5
+ var _plugints = require('@kubb/plugin-ts');
6
+
7
+ // src/SchemaGenerator.tsx
8
+ var _pluginoas = require('@kubb/plugin-oas');
9
+
10
+
11
+
12
+ // src/components/Schema.tsx
13
+
14
+
15
+
16
+ var _transformers = require('@kubb/core/transformers'); var _transformers2 = _interopRequireDefault(_transformers);
17
+
18
+
19
+
20
+ // src/parser/index.ts
21
+
22
+
23
+ var fakerKeywordMapper = {
24
+ any: () => "undefined",
25
+ unknown: () => "unknown",
26
+ number: (min, max) => {
27
+ if (max !== void 0 && min !== void 0) {
28
+ return `faker.number.float({ min: ${min}, max: ${max} })`;
29
+ }
30
+ if (min !== void 0) {
31
+ return `faker.number.float({ min: ${min} })`;
32
+ }
33
+ if (max !== void 0) {
34
+ return `faker.number.float({ max: ${max} })`;
35
+ }
36
+ return "faker.number.float()";
37
+ },
38
+ integer: (min, max) => {
39
+ if (max !== void 0 && min !== void 0) {
40
+ return `faker.number.int({ min: ${min}, max: ${max} })`;
41
+ }
42
+ if (min !== void 0) {
43
+ return `faker.number.int({ min: ${min} })`;
44
+ }
45
+ if (max !== void 0) {
46
+ return `faker.number.int({ max: ${max} })`;
47
+ }
48
+ return "faker.number.int()";
49
+ },
50
+ string: (min, max) => {
51
+ if (max !== void 0 && min !== void 0) {
52
+ return `faker.string.alpha({ length: { min: ${min}, max: ${max} } })`;
53
+ }
54
+ if (min !== void 0) {
55
+ return `faker.string.alpha({ length: { min: ${min} } })`;
56
+ }
57
+ if (max !== void 0) {
58
+ return `faker.string.alpha({ length: { max: ${max} } })`;
59
+ }
60
+ return "faker.string.alpha()";
61
+ },
62
+ boolean: () => "faker.datatype.boolean()",
63
+ undefined: () => "undefined",
64
+ null: () => "null",
65
+ array: (items = []) => `faker.helpers.arrayElements([${items.join(", ")}]) as any`,
66
+ tuple: (items = []) => `faker.helpers.arrayElements([${items.join(", ")}]) as any`,
67
+ enum: (items = []) => `faker.helpers.arrayElement<any>([${items.join(", ")}])`,
68
+ union: (items = []) => `faker.helpers.arrayElement<any>([${items.join(", ")}])`,
69
+ /**
70
+ * ISO 8601
71
+ */
72
+ datetime: () => "faker.date.anytime().toISOString()",
73
+ /**
74
+ * Type `'date'` Date
75
+ * Type `'string'` ISO date format (YYYY-MM-DD)
76
+ * @default ISO date format (YYYY-MM-DD)
77
+ */
78
+ date: (type = "string", parser) => {
79
+ if (type === "string") {
80
+ if (parser) {
81
+ return `${parser}(faker.date.anytime()).format("YYYY-MM-DD")`;
82
+ }
83
+ return "faker.date.anytime().toString()";
84
+ }
85
+ return "faker.date.anytime()";
86
+ },
87
+ /**
88
+ * Type `'date'` Date
89
+ * Type `'string'` ISO time format (HH:mm:ss[.SSSSSS])
90
+ * @default ISO time format (HH:mm:ss[.SSSSSS])
91
+ */
92
+ time: (type = "string", parser) => {
93
+ if (type === "string") {
94
+ if (parser) {
95
+ return `${parser}(faker.date.anytime()).format("HH:mm:ss")`;
96
+ }
97
+ return "faker.date.anytime().toString()";
98
+ }
99
+ return "faker.date.anytime()";
100
+ },
101
+ uuid: () => "faker.string.uuid()",
102
+ url: () => "faker.internet.url()",
103
+ and: (items = []) => `Object.assign({}, ${items.join(", ")})`,
104
+ object: () => "object",
105
+ ref: () => "ref",
106
+ matches: (value = "", regexGenerator = "faker") => {
107
+ if (regexGenerator === "randexp") {
108
+ return `${_transformers2.default.toRegExpString(value, "RandExp")}.gen()`;
109
+ }
110
+ return `faker.helpers.fromRegExp(${_transformers2.default.toRegExpString(value)})`;
111
+ },
112
+ email: () => "faker.internet.email()",
113
+ firstName: () => "faker.person.firstName()",
114
+ lastName: () => "faker.person.lastName()",
115
+ password: () => "faker.internet.password()",
116
+ phone: () => "faker.phone.number()",
117
+ blob: () => "faker.image.imageUrl() as unknown as Blob",
118
+ default: void 0,
119
+ describe: void 0,
120
+ const: (value) => _nullishCoalesce(value, () => ( "")),
121
+ max: void 0,
122
+ min: void 0,
123
+ nullable: void 0,
124
+ nullish: void 0,
125
+ optional: void 0,
126
+ readOnly: void 0,
127
+ strict: void 0,
128
+ deprecated: void 0,
129
+ example: void 0,
130
+ schema: void 0,
131
+ catchall: void 0,
132
+ name: void 0
133
+ };
134
+ function schemaKeywordsorter(a, b) {
135
+ if (b.keyword === "null") {
136
+ return -1;
137
+ }
138
+ return 0;
139
+ }
140
+ function joinItems(items) {
141
+ switch (items.length) {
142
+ case 0:
143
+ return "undefined";
144
+ case 1:
145
+ return items[0];
146
+ default:
147
+ return fakerKeywordMapper.union(items);
148
+ }
149
+ }
150
+ function parse(parent, current, options) {
151
+ const value = fakerKeywordMapper[current.keyword];
152
+ if (!value) {
153
+ return void 0;
154
+ }
155
+ if (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.union)) {
156
+ return fakerKeywordMapper.union(current.args.map((schema) => parse(current, schema, { ...options, withData: false })).filter(Boolean));
157
+ }
158
+ if (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.and)) {
159
+ return fakerKeywordMapper.and(current.args.map((schema) => parse(current, schema, { ...options, withData: false })).filter(Boolean));
160
+ }
161
+ if (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.array)) {
162
+ return fakerKeywordMapper.array(current.args.items.map((schema) => parse(current, schema, { ...options, withData: false })).filter(Boolean));
163
+ }
164
+ if (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.enum)) {
165
+ return fakerKeywordMapper.enum(
166
+ current.args.items.map((schema) => {
167
+ if (schema.format === "number") {
168
+ return schema.name;
169
+ }
170
+ return _transformers2.default.stringify(schema.name);
171
+ })
172
+ );
173
+ }
174
+ if (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.ref)) {
175
+ if (!_optionalChain([current, 'access', _ => _.args, 'optionalAccess', _2 => _2.name])) {
176
+ throw new Error(`Name not defined for keyword ${current.keyword}`);
177
+ }
178
+ if (options.withData) {
179
+ return `${current.args.name}(data)`;
180
+ }
181
+ return `${current.args.name}()`;
182
+ }
183
+ if (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.object)) {
184
+ const argsObject = Object.entries(_optionalChain([current, 'access', _3 => _3.args, 'optionalAccess', _4 => _4.properties]) || {}).filter((item) => {
185
+ const schema = item[1];
186
+ return schema && typeof schema.map === "function";
187
+ }).map(([name, schemas]) => {
188
+ const nameSchema = schemas.find((schema) => schema.keyword === _pluginoas.schemaKeywords.name);
189
+ const mappedName = _optionalChain([nameSchema, 'optionalAccess', _5 => _5.args]) || name;
190
+ if (_optionalChain([options, 'access', _6 => _6.mapper, 'optionalAccess', _7 => _7[mappedName]])) {
191
+ return `"${name}": ${_optionalChain([options, 'access', _8 => _8.mapper, 'optionalAccess', _9 => _9[mappedName]])}`;
192
+ }
193
+ return `"${name}": ${joinItems(
194
+ schemas.sort(schemaKeywordsorter).map((schema) => parse(current, schema, { ...options, withData: false })).filter(Boolean)
195
+ )}`;
196
+ }).join(",");
197
+ return `{${argsObject}}`;
198
+ }
199
+ if (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.tuple)) {
200
+ if (Array.isArray(current.args.items)) {
201
+ return fakerKeywordMapper.tuple(current.args.items.map((schema) => parse(current, schema, { ...options, withData: false })).filter(Boolean));
202
+ }
203
+ return parse(current, current.args.items, { ...options, withData: false });
204
+ }
205
+ if (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.const)) {
206
+ if (current.args.format === "number" && current.args.name !== void 0) {
207
+ return fakerKeywordMapper.const(_optionalChain([current, 'access', _10 => _10.args, 'access', _11 => _11.name, 'optionalAccess', _12 => _12.toString, 'call', _13 => _13()]));
208
+ }
209
+ return fakerKeywordMapper.const(_transformers2.default.stringify(current.args.value));
210
+ }
211
+ if (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.matches) && current.args) {
212
+ return fakerKeywordMapper.matches(current.args, options.regexGenerator);
213
+ }
214
+ if (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.null) || _pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.undefined) || _pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.any)) {
215
+ return value() || "";
216
+ }
217
+ if (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.string)) {
218
+ if (parent) {
219
+ const minSchema = _pluginoas.SchemaGenerator.find([parent], _pluginoas.schemaKeywords.min);
220
+ const maxSchema = _pluginoas.SchemaGenerator.find([parent], _pluginoas.schemaKeywords.max);
221
+ return fakerKeywordMapper.string(_optionalChain([minSchema, 'optionalAccess', _14 => _14.args]), _optionalChain([maxSchema, 'optionalAccess', _15 => _15.args]));
222
+ }
223
+ return fakerKeywordMapper.string();
224
+ }
225
+ if (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.number)) {
226
+ if (parent) {
227
+ const minSchema = _pluginoas.SchemaGenerator.find([parent], _pluginoas.schemaKeywords.min);
228
+ const maxSchema = _pluginoas.SchemaGenerator.find([parent], _pluginoas.schemaKeywords.max);
229
+ return fakerKeywordMapper.number(_optionalChain([minSchema, 'optionalAccess', _16 => _16.args]), _optionalChain([maxSchema, 'optionalAccess', _17 => _17.args]));
230
+ }
231
+ return fakerKeywordMapper.number();
232
+ }
233
+ if (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.integer)) {
234
+ if (parent) {
235
+ const minSchema = _pluginoas.SchemaGenerator.find([parent], _pluginoas.schemaKeywords.min);
236
+ const maxSchema = _pluginoas.SchemaGenerator.find([parent], _pluginoas.schemaKeywords.max);
237
+ return fakerKeywordMapper.integer(_optionalChain([minSchema, 'optionalAccess', _18 => _18.args]), _optionalChain([maxSchema, 'optionalAccess', _19 => _19.args]));
238
+ }
239
+ return fakerKeywordMapper.integer();
240
+ }
241
+ if (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.datetime)) {
242
+ return fakerKeywordMapper.datetime();
243
+ }
244
+ if (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.date)) {
245
+ return fakerKeywordMapper.date(current.args.type, options.dateParser);
246
+ }
247
+ if (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.time)) {
248
+ return fakerKeywordMapper.time(current.args.type, options.dateParser);
249
+ }
250
+ if (current.keyword in fakerKeywordMapper && "args" in current) {
251
+ const value2 = fakerKeywordMapper[current.keyword];
252
+ const options2 = JSON.stringify(current.args);
253
+ return value2(options2);
254
+ }
255
+ if (current.keyword in fakerKeywordMapper) {
256
+ return value();
257
+ }
258
+ return void 0;
259
+ }
260
+
261
+ // src/plugin.ts
262
+ var _path = require('path'); var _path2 = _interopRequireDefault(_path);
263
+ var _core = require('@kubb/core');
264
+
265
+ var _utils = require('@kubb/core/utils');
266
+
267
+ var _utils3 = require('@kubb/plugin-oas/utils');
268
+
269
+
270
+ // src/OperationGenerator.tsx
271
+
272
+
273
+
274
+ var _jsxruntime = require('@kubb/react/jsx-runtime');
275
+ var OperationGenerator = class extends _pluginoas.OperationGenerator {
276
+ async operation(operation, options) {
277
+ const { oas, pluginManager, plugin, mode } = this.context;
278
+ const root = _react.createRoot.call(void 0, {
279
+ logger: pluginManager.logger
280
+ });
281
+ root.render(
282
+ /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.App, { pluginManager, plugin: { ...plugin, options }, mode, children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _components.Oas, { oas, operations: [operation], generator: this, children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _components.Oas.Operation, { operation, children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, OperationSchema.File, {}) }) }) })
283
+ );
284
+ return root.files;
285
+ }
286
+ };
287
+
288
+ // src/plugin.ts
289
+ var pluginFakerName = "plugin-faker";
290
+ var pluginFaker = _core.createPlugin.call(void 0, (options) => {
291
+ const {
292
+ output = { path: "mocks" },
293
+ seed,
294
+ group,
295
+ exclude = [],
296
+ include,
297
+ override = [],
298
+ transformers: transformers3 = {},
299
+ mapper = {},
300
+ dateType = "string",
301
+ unknownType = "any",
302
+ dateParser,
303
+ regexGenerator = "faker"
304
+ } = options;
305
+ const template = _optionalChain([group, 'optionalAccess', _20 => _20.output]) ? group.output : `${output.path}/{{tag}}Controller`;
306
+ return {
307
+ name: pluginFakerName,
308
+ options: {
309
+ extName: output.extName,
310
+ transformers: transformers3,
311
+ dateType,
312
+ seed,
313
+ unknownType,
314
+ dateParser,
315
+ mapper,
316
+ override,
317
+ regexGenerator
318
+ },
319
+ pre: [_pluginoas.pluginOasName, _plugints.pluginTsName],
320
+ resolvePath(baseName, pathMode, options2) {
321
+ const root = _path2.default.resolve(this.config.root, this.config.output.path);
322
+ const mode = _nullishCoalesce(pathMode, () => ( _core.FileManager.getMode(_path2.default.resolve(root, output.path))));
323
+ if (mode === "single") {
324
+ return _path2.default.resolve(root, output.path);
325
+ }
326
+ if (_optionalChain([options2, 'optionalAccess', _21 => _21.tag]) && _optionalChain([group, 'optionalAccess', _22 => _22.type]) === "tag") {
327
+ const tag = _transformers.camelCase.call(void 0, options2.tag);
328
+ return _path2.default.resolve(root, _utils.renderTemplate.call(void 0, template, { tag }), baseName);
329
+ }
330
+ return _path2.default.resolve(root, output.path, baseName);
331
+ },
332
+ resolveName(name, type) {
333
+ const resolvedName = _transformers.camelCase.call(void 0, name, {
334
+ prefix: type ? "create" : void 0,
335
+ isFile: type === "file"
336
+ });
337
+ if (type) {
338
+ return _optionalChain([transformers3, 'optionalAccess', _23 => _23.name, 'optionalCall', _24 => _24(resolvedName, type)]) || resolvedName;
339
+ }
340
+ return resolvedName;
341
+ },
342
+ async writeFile(path2, source) {
343
+ if (!path2.endsWith(".ts") || !source) {
344
+ return;
345
+ }
346
+ return this.fileManager.write(path2, source, { sanity: false });
347
+ },
348
+ async buildStart() {
349
+ const [swaggerPlugin] = _core.PluginManager.getDependedPlugins(this.plugins, [_pluginoas.pluginOasName]);
350
+ const oas = await swaggerPlugin.api.getOas();
351
+ const root = _path2.default.resolve(this.config.root, this.config.output.path);
352
+ const mode = _core.FileManager.getMode(_path2.default.resolve(root, output.path));
353
+ const schemaGenerator = new SchemaGenerator2(this.plugin.options, {
354
+ oas,
355
+ pluginManager: this.pluginManager,
356
+ plugin: this.plugin,
357
+ contentType: swaggerPlugin.api.contentType,
358
+ include: void 0,
359
+ override,
360
+ mode,
361
+ output: output.path
362
+ });
363
+ const schemaFiles = await schemaGenerator.build();
364
+ await this.addFile(...schemaFiles);
365
+ const operationGenerator = new OperationGenerator(this.plugin.options, {
366
+ oas,
367
+ pluginManager: this.pluginManager,
368
+ plugin: this.plugin,
369
+ contentType: swaggerPlugin.api.contentType,
370
+ exclude,
371
+ include,
372
+ override,
373
+ mode
374
+ });
375
+ const operationFiles = await operationGenerator.build();
376
+ await this.addFile(...operationFiles);
377
+ },
378
+ async buildEnd() {
379
+ if (this.config.output.write === false) {
380
+ return;
381
+ }
382
+ const root = _path2.default.resolve(this.config.root, this.config.output.path);
383
+ if (_optionalChain([group, 'optionalAccess', _25 => _25.type]) === "tag") {
384
+ const rootFiles = await _utils3.getGroupedByTagFiles.call(void 0, {
385
+ logger: this.logger,
386
+ files: this.fileManager.files,
387
+ plugin: this.plugin,
388
+ template,
389
+ exportAs: group.exportAs || "{{tag}}Mocks",
390
+ root,
391
+ output
392
+ });
393
+ await this.addFile(...rootFiles);
394
+ }
395
+ await this.fileManager.addIndexes({
396
+ root,
397
+ output,
398
+ meta: { pluginKey: this.plugin.key },
399
+ logger: this.logger
400
+ });
401
+ }
402
+ };
403
+ });
404
+
405
+ // src/components/Schema.tsx
406
+
407
+ function Schema(props) {
408
+ const { withData, description } = props;
409
+ const { tree, name } = _hooks.useSchema.call(void 0, );
410
+ const {
411
+ pluginManager,
412
+ plugin: {
413
+ options: { dateParser, regexGenerator, mapper, seed }
414
+ }
415
+ } = _react.useApp.call(void 0, );
416
+ const resolvedName = pluginManager.resolveName({
417
+ name,
418
+ pluginKey: [pluginFakerName],
419
+ type: "function"
420
+ });
421
+ const typeName = pluginManager.resolveName({
422
+ name,
423
+ pluginKey: [_plugints.pluginTsName],
424
+ type: "type"
425
+ });
426
+ const fakerText = joinItems(
427
+ tree.map((schema) => parse(void 0, schema, { name: resolvedName, typeName, seed, regexGenerator, mapper, withData, dateParser })).filter(Boolean)
428
+ );
429
+ let fakerDefaultOverride = void 0;
430
+ let fakerTextWithOverride = fakerText;
431
+ if (withData && fakerText.startsWith("{")) {
432
+ fakerDefaultOverride = "{}";
433
+ fakerTextWithOverride = `{
434
+ ...${fakerText},
435
+ ...data
436
+ }`;
437
+ }
438
+ if (withData && fakerText.startsWith("faker.helpers.arrayElements")) {
439
+ fakerDefaultOverride = "[]";
440
+ fakerTextWithOverride = `[
441
+ ...${fakerText},
442
+ ...data
443
+ ]`;
444
+ }
445
+ const params = fakerDefaultOverride ? `data: NonNullable<Partial<${typeName}>> = ${fakerDefaultOverride}` : `data?: NonNullable<Partial<${typeName}>>`;
446
+ return /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _jsxruntime.Fragment, { children: [
447
+ /* @__PURE__ */ _jsxruntime.jsxs.call(void 0,
448
+ _react.Function,
449
+ {
450
+ export: true,
451
+ name: resolvedName,
452
+ JSDoc: { comments: [description ? `@description ${_transformers2.default.jsStringEscape(description)}` : void 0].filter(Boolean) },
453
+ params: withData ? params : "",
454
+ returnType: typeName ? `NonNullable<${typeName}>` : "",
455
+ children: [
456
+ seed ? `faker.seed(${JSON.stringify(seed)})` : "",
457
+ /* @__PURE__ */ _jsxruntime.jsx.call(void 0, "br", {}),
458
+ /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.Function.Return, { children: fakerTextWithOverride })
459
+ ]
460
+ }
461
+ ),
462
+ /* @__PURE__ */ _jsxruntime.jsx.call(void 0, "br", {})
463
+ ] });
464
+ }
465
+ Schema.File = function({}) {
466
+ const { pluginManager } = _react.useApp.call(void 0, );
467
+ const { tree, schema } = _hooks.useSchema.call(void 0, );
468
+ const withData = tree.some(
469
+ (schema2) => schema2.keyword === _pluginoas.schemaKeywords.array || schema2.keyword === _pluginoas.schemaKeywords.and || schema2.keyword === _pluginoas.schemaKeywords.object || schema2.keyword === _pluginoas.schemaKeywords.union || schema2.keyword === _pluginoas.schemaKeywords.tuple
470
+ );
471
+ return /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _components.Oas.Schema.File, { output: pluginManager.config.output.path, children: [
472
+ /* @__PURE__ */ _jsxruntime.jsx.call(void 0, Schema.Imports, {}),
473
+ /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.File.Source, { children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, Schema, { description: _optionalChain([schema, 'optionalAccess', _26 => _26.description]), withData }) })
474
+ ] });
475
+ };
476
+ Schema.Imports = () => {
477
+ const {
478
+ pluginManager,
479
+ plugin: {
480
+ options: { extName, dateParser, regexGenerator }
481
+ }
482
+ } = _react.useApp.call(void 0, );
483
+ const { path: root } = _react.useFile.call(void 0, );
484
+ const { name, tree, schema } = _hooks.useSchema.call(void 0, );
485
+ const typeName = pluginManager.resolveName({
486
+ name,
487
+ pluginKey: [_plugints.pluginTsName],
488
+ type: "type"
489
+ });
490
+ const typeFileName = pluginManager.resolveName({
491
+ name,
492
+ pluginKey: [_plugints.pluginTsName],
493
+ type: "file"
494
+ });
495
+ const typePath = pluginManager.resolvePath({
496
+ baseName: typeFileName,
497
+ pluginKey: [_plugints.pluginTsName]
498
+ });
499
+ return /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _jsxruntime.Fragment, { children: [
500
+ /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.File.Import, { name: ["faker"], path: "@faker-js/faker" }),
501
+ regexGenerator === "randexp" && /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.File.Import, { name: "RandExp", path: "randexp" }),
502
+ dateParser && /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.File.Import, { path: dateParser, name: dateParser }),
503
+ typeName && typePath && /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.File.Import, { extName, isTypeOnly: true, root, path: typePath, name: [typeName] })
504
+ ] });
505
+ };
506
+
507
+ // src/SchemaGenerator.tsx
508
+
509
+ var SchemaGenerator2 = class extends _pluginoas.SchemaGenerator {
510
+ async schema(name, schema, options) {
511
+ const { oas, pluginManager, plugin, mode, output } = this.context;
512
+ const root = _react.createRoot.call(void 0, {
513
+ logger: pluginManager.logger
514
+ });
515
+ const tree = this.parse({ schema, name });
516
+ root.render(
517
+ /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.App, { pluginManager, plugin: { ...plugin, options }, mode, children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _components.Oas, { oas, children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _components.Oas.Schema, { name, value: schema, tree, children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, Schema.File, {}) }) }) })
518
+ );
519
+ return root.files;
520
+ }
521
+ };
522
+
523
+ // src/components/OperationSchema.tsx
524
+
525
+ function OperationSchema({ description }) {
526
+ return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, Schema, { withData: false, description });
527
+ }
528
+ OperationSchema.File = function({}) {
529
+ const { plugin, pluginManager, mode } = _react.useApp.call(void 0, );
530
+ const oas = _hooks.useOas.call(void 0, );
531
+ const { getSchemas, getFile } = _hooks.useOperationManager.call(void 0, );
532
+ const operation = _hooks.useOperation.call(void 0, );
533
+ const file = getFile(operation);
534
+ const schemas = getSchemas(operation);
535
+ const generator = new SchemaGenerator2(plugin.options, {
536
+ oas,
537
+ plugin,
538
+ pluginManager,
539
+ mode,
540
+ override: plugin.options.override
541
+ });
542
+ const items = [schemas.pathParams, schemas.queryParams, schemas.headerParams, schemas.statusCodes, schemas.request, schemas.response].flat().filter(Boolean);
543
+ const mapItem = ({ name, schema, description, ...options }, i) => {
544
+ const typeName = pluginManager.resolveName({
545
+ name,
546
+ pluginKey: [_plugints.pluginTsName],
547
+ type: "type"
548
+ });
549
+ const typeFileName = pluginManager.resolveName({
550
+ name: options.operationName || name,
551
+ pluginKey: [_plugints.pluginTsName],
552
+ type: "file"
553
+ });
554
+ const typePath = pluginManager.resolvePath({
555
+ baseName: typeFileName,
556
+ pluginKey: [_plugints.pluginTsName],
557
+ options: { tag: _optionalChain([options, 'access', _27 => _27.operation, 'optionalAccess', _28 => _28.getTags, 'call', _29 => _29(), 'access', _30 => _30[0], 'optionalAccess', _31 => _31.name]) }
558
+ });
559
+ const tree = generator.parse({ schema, name });
560
+ return /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _components.Oas.Schema, { name, value: schema, tree, children: [
561
+ typeName && typePath && /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.File.Import, { extName: plugin.options.extName, isTypeOnly: true, root: file.path, path: typePath, name: [typeName] }),
562
+ plugin.options.dateParser && /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.File.Import, { path: plugin.options.dateParser, name: plugin.options.dateParser }),
563
+ mode === "split" && /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _components.Oas.Schema.Imports, { extName: plugin.options.extName }),
564
+ /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.File.Source, { children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, OperationSchema, { description }) })
565
+ ] }, i);
566
+ };
567
+ return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.Parser, { language: "typescript", children: /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _react.File, { baseName: file.baseName, path: file.path, meta: file.meta, children: [
568
+ /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.File.Import, { name: ["faker"], path: "@faker-js/faker" }),
569
+ plugin.options.regexGenerator === "randexp" && /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.File.Import, { name: "RandExp", path: "randexp" }),
570
+ items.map(mapItem)
571
+ ] }) });
572
+ };
573
+
574
+
575
+
576
+ exports.OperationSchema = OperationSchema; exports.Schema = Schema;
577
+ //# sourceMappingURL=components.cjs.map