@kubb/plugin-msw 4.5.1 → 4.5.3

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.
@@ -0,0 +1,511 @@
1
+ import { a as Output, c as ResolveNameParams, d as Oas, f as HttpMethod, h as contentType, i as Group, m as SchemaObject, n as PluginManager, o as Plugin, p as Operation, r as Config, s as PluginFactoryOptions, t as FileMetaBase, u as BaseGenerator } from "./index-DvEPuQDh.js";
2
+ import { Fabric } from "@kubb/react-fabric";
3
+ import { KubbNode } from "@kubb/react-fabric/types";
4
+ import { KubbFile } from "@kubb/fabric-core/types";
5
+
6
+ //#region ../plugin-oas/src/types.d.ts
7
+ type Context$2 = {
8
+ getOas(): Promise<Oas>;
9
+ getBaseURL(): Promise<string | undefined>;
10
+ };
11
+ declare global {
12
+ namespace Kubb {
13
+ interface PluginContext extends Context$2 {}
14
+ }
15
+ }
16
+ type ResolvePathOptions = {
17
+ pluginKey?: Plugin['key'];
18
+ group?: {
19
+ tag?: string;
20
+ path?: string;
21
+ };
22
+ type?: ResolveNameParams['type'];
23
+ };
24
+ /**
25
+ * `propertyName` is the ref name + resolved with the nameResolver
26
+ * @example import { Pet } from './Pet'
27
+ *
28
+ * `originalName` is the original name used(in PascalCase), only used to remove duplicates
29
+ *
30
+ * `pluginKey` can be used to override the current plugin being used, handy when you want to import a type/schema out of another plugin
31
+ * @example import a type(plugin-ts) for a mock file(swagger-faker)
32
+ */
33
+ type Ref = {
34
+ propertyName: string;
35
+ originalName: string;
36
+ path: KubbFile.Path;
37
+ pluginKey?: Plugin['key'];
38
+ };
39
+ type Refs = Record<string, Ref>;
40
+ type OperationSchema = {
41
+ /**
42
+ * Converted name, contains already `PathParams`, `QueryParams`, ...
43
+ */
44
+ name: string;
45
+ schema: SchemaObject;
46
+ operation?: Operation;
47
+ /**
48
+ * OperationName in PascalCase, only being used in OperationGenerator
49
+ */
50
+ operationName: string;
51
+ description?: string;
52
+ statusCode?: number;
53
+ keys?: string[];
54
+ keysToOmit?: string[];
55
+ withData?: boolean;
56
+ };
57
+ type OperationSchemas = {
58
+ pathParams?: OperationSchema & {
59
+ keysToOmit?: never;
60
+ };
61
+ queryParams?: OperationSchema & {
62
+ keysToOmit?: never;
63
+ };
64
+ headerParams?: OperationSchema & {
65
+ keysToOmit?: never;
66
+ };
67
+ request?: OperationSchema;
68
+ response: OperationSchema;
69
+ responses: Array<OperationSchema>;
70
+ statusCodes?: Array<OperationSchema>;
71
+ errors?: Array<OperationSchema>;
72
+ };
73
+ type ByTag = {
74
+ type: 'tag';
75
+ pattern: string | RegExp;
76
+ };
77
+ type ByOperationId = {
78
+ type: 'operationId';
79
+ pattern: string | RegExp;
80
+ };
81
+ type ByPath = {
82
+ type: 'path';
83
+ pattern: string | RegExp;
84
+ };
85
+ type ByMethod = {
86
+ type: 'method';
87
+ pattern: HttpMethod | RegExp;
88
+ };
89
+ type BySchemaName = {
90
+ type: 'schemaName';
91
+ pattern: string | RegExp;
92
+ };
93
+ type ByContentType = {
94
+ type: 'contentType';
95
+ pattern: string | RegExp;
96
+ };
97
+ type Exclude = ByTag | ByOperationId | ByPath | ByMethod | ByContentType;
98
+ type Include = ByTag | ByOperationId | ByPath | ByMethod | ByContentType;
99
+ type Override<TOptions> = (ByTag | ByOperationId | ByPath | ByMethod | BySchemaName | ByContentType) & {
100
+ options: Partial<TOptions>;
101
+ };
102
+ //#endregion
103
+ //#region ../plugin-oas/src/OperationGenerator.d.ts
104
+ type Context$1<TOptions, TPluginOptions extends PluginFactoryOptions> = {
105
+ fabric: Fabric;
106
+ oas: Oas;
107
+ exclude: Array<Exclude> | undefined;
108
+ include: Array<Include> | undefined;
109
+ override: Array<Override<TOptions>> | undefined;
110
+ contentType: contentType | undefined;
111
+ pluginManager: PluginManager;
112
+ /**
113
+ * Current plugin
114
+ */
115
+ plugin: Plugin<TPluginOptions>;
116
+ mode: KubbFile.Mode;
117
+ };
118
+ declare class OperationGenerator<TPluginOptions extends PluginFactoryOptions = PluginFactoryOptions, TFileMeta extends FileMetaBase = FileMetaBase> extends BaseGenerator<TPluginOptions['resolvedOptions'], Context$1<TPluginOptions['resolvedOptions'], TPluginOptions>> {
119
+ #private;
120
+ getSchemas(operation: Operation, {
121
+ resolveName
122
+ }?: {
123
+ resolveName?: (name: string) => string;
124
+ }): OperationSchemas;
125
+ getOperations(): Promise<Array<{
126
+ path: string;
127
+ method: HttpMethod;
128
+ operation: Operation;
129
+ }>>;
130
+ build(...generators: Array<Generator<TPluginOptions>>): Promise<Array<KubbFile.File<TFileMeta>>>;
131
+ }
132
+ //#endregion
133
+ //#region ../plugin-oas/src/SchemaMapper.d.ts
134
+ type SchemaKeywordMapper = {
135
+ object: {
136
+ keyword: 'object';
137
+ args: {
138
+ properties: {
139
+ [x: string]: Schema[];
140
+ };
141
+ additionalProperties: Schema[];
142
+ strict?: boolean;
143
+ };
144
+ };
145
+ url: {
146
+ keyword: 'url';
147
+ };
148
+ readOnly: {
149
+ keyword: 'readOnly';
150
+ };
151
+ writeOnly: {
152
+ keyword: 'writeOnly';
153
+ };
154
+ uuid: {
155
+ keyword: 'uuid';
156
+ };
157
+ email: {
158
+ keyword: 'email';
159
+ };
160
+ firstName: {
161
+ keyword: 'firstName';
162
+ };
163
+ lastName: {
164
+ keyword: 'lastName';
165
+ };
166
+ phone: {
167
+ keyword: 'phone';
168
+ };
169
+ password: {
170
+ keyword: 'password';
171
+ };
172
+ date: {
173
+ keyword: 'date';
174
+ args: {
175
+ type?: 'date' | 'string';
176
+ };
177
+ };
178
+ time: {
179
+ keyword: 'time';
180
+ args: {
181
+ type?: 'date' | 'string';
182
+ };
183
+ };
184
+ datetime: {
185
+ keyword: 'datetime';
186
+ args: {
187
+ offset?: boolean;
188
+ local?: boolean;
189
+ };
190
+ };
191
+ tuple: {
192
+ keyword: 'tuple';
193
+ args: {
194
+ items: Schema[];
195
+ min?: number;
196
+ max?: number;
197
+ rest?: Schema;
198
+ };
199
+ };
200
+ array: {
201
+ keyword: 'array';
202
+ args: {
203
+ items: Schema[];
204
+ min?: number;
205
+ max?: number;
206
+ unique?: boolean;
207
+ };
208
+ };
209
+ enum: {
210
+ keyword: 'enum';
211
+ args: {
212
+ name: string;
213
+ typeName: string;
214
+ asConst: boolean;
215
+ items: Array<{
216
+ name: string | number;
217
+ format: 'string' | 'number' | 'boolean';
218
+ value?: string | number | boolean;
219
+ }>;
220
+ };
221
+ };
222
+ and: {
223
+ keyword: 'and';
224
+ args: Schema[];
225
+ };
226
+ const: {
227
+ keyword: 'const';
228
+ args: {
229
+ name: string | number;
230
+ format: 'string' | 'number' | 'boolean';
231
+ value?: string | number | boolean;
232
+ };
233
+ };
234
+ union: {
235
+ keyword: 'union';
236
+ args: Schema[];
237
+ };
238
+ ref: {
239
+ keyword: 'ref';
240
+ args: {
241
+ name: string;
242
+ $ref: string;
243
+ /**
244
+ * Full qualified path.
245
+ */
246
+ path: KubbFile.Path;
247
+ /**
248
+ * When true `File.Import` will be used.
249
+ * When false a reference will be used inside the current file.
250
+ */
251
+ isImportable: boolean;
252
+ };
253
+ };
254
+ matches: {
255
+ keyword: 'matches';
256
+ args?: string;
257
+ };
258
+ boolean: {
259
+ keyword: 'boolean';
260
+ };
261
+ default: {
262
+ keyword: 'default';
263
+ args: string | number | boolean;
264
+ };
265
+ string: {
266
+ keyword: 'string';
267
+ };
268
+ integer: {
269
+ keyword: 'integer';
270
+ };
271
+ number: {
272
+ keyword: 'number';
273
+ };
274
+ max: {
275
+ keyword: 'max';
276
+ args: number;
277
+ };
278
+ min: {
279
+ keyword: 'min';
280
+ args: number;
281
+ };
282
+ exclusiveMaximum: {
283
+ keyword: 'exclusiveMaximum';
284
+ args: number;
285
+ };
286
+ exclusiveMinimum: {
287
+ keyword: 'exclusiveMinimum';
288
+ args: number;
289
+ };
290
+ describe: {
291
+ keyword: 'describe';
292
+ args: string;
293
+ };
294
+ example: {
295
+ keyword: 'example';
296
+ args: string;
297
+ };
298
+ deprecated: {
299
+ keyword: 'deprecated';
300
+ };
301
+ optional: {
302
+ keyword: 'optional';
303
+ };
304
+ undefined: {
305
+ keyword: 'undefined';
306
+ };
307
+ nullish: {
308
+ keyword: 'nullish';
309
+ };
310
+ nullable: {
311
+ keyword: 'nullable';
312
+ };
313
+ null: {
314
+ keyword: 'null';
315
+ };
316
+ any: {
317
+ keyword: 'any';
318
+ };
319
+ unknown: {
320
+ keyword: 'unknown';
321
+ };
322
+ void: {
323
+ keyword: 'void';
324
+ };
325
+ blob: {
326
+ keyword: 'blob';
327
+ };
328
+ schema: {
329
+ keyword: 'schema';
330
+ args: {
331
+ type: 'string' | 'number' | 'integer' | 'boolean' | 'array' | 'object';
332
+ format?: string;
333
+ };
334
+ };
335
+ name: {
336
+ keyword: 'name';
337
+ args: string;
338
+ };
339
+ catchall: {
340
+ keyword: 'catchall';
341
+ };
342
+ interface: {
343
+ keyword: 'interface';
344
+ };
345
+ };
346
+ type Schema = {
347
+ keyword: string;
348
+ } | SchemaKeywordMapper[keyof SchemaKeywordMapper];
349
+ //#endregion
350
+ //#region ../plugin-oas/src/SchemaGenerator.d.ts
351
+ type Context<TOptions, TPluginOptions extends PluginFactoryOptions> = {
352
+ fabric: Fabric;
353
+ oas: Oas;
354
+ pluginManager: PluginManager;
355
+ /**
356
+ * Current plugin
357
+ */
358
+ plugin: Plugin<TPluginOptions>;
359
+ mode: KubbFile.Mode;
360
+ include?: Array<'schemas' | 'responses' | 'requestBodies'>;
361
+ override: Array<Override<TOptions>> | undefined;
362
+ contentType?: contentType;
363
+ output?: string;
364
+ };
365
+ type SchemaGeneratorOptions = {
366
+ dateType: false | 'string' | 'stringOffset' | 'stringLocal' | 'date';
367
+ unknownType: 'any' | 'unknown' | 'void';
368
+ emptySchemaType: 'any' | 'unknown' | 'void';
369
+ enumType?: 'enum' | 'asConst' | 'asPascalConst' | 'constEnum' | 'literal';
370
+ enumSuffix?: string;
371
+ usedEnumNames?: Record<string, number>;
372
+ mapper?: Record<string, string>;
373
+ typed?: boolean;
374
+ transformers: {
375
+ /**
376
+ * Customize the names based on the type that is provided by the plugin.
377
+ */
378
+ name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string;
379
+ /**
380
+ * Receive schema and name(propertName) and return FakerMeta array
381
+ * TODO TODO add docs
382
+ * @beta
383
+ */
384
+ schema?: (schemaProps: SchemaProps$1, defaultSchemas: Schema[]) => Schema[] | undefined;
385
+ };
386
+ };
387
+ type SchemaProps$1 = {
388
+ schemaObject?: SchemaObject;
389
+ name?: string;
390
+ parentName?: string;
391
+ };
392
+ declare class SchemaGenerator<TOptions extends SchemaGeneratorOptions = SchemaGeneratorOptions, TPluginOptions extends PluginFactoryOptions = PluginFactoryOptions, TFileMeta extends FileMetaBase = FileMetaBase> extends BaseGenerator<TOptions, Context<TOptions, TPluginOptions>> {
393
+ #private;
394
+ refs: Refs;
395
+ /**
396
+ * Creates a type node from a given schema.
397
+ * Delegates to getBaseTypeFromSchema internally and
398
+ * optionally adds a union with null.
399
+ */
400
+ parse(props: SchemaProps$1): Schema[];
401
+ static deepSearch<T extends keyof SchemaKeywordMapper>(tree: Schema[] | undefined, keyword: T): Array<SchemaKeywordMapper[T]>;
402
+ static find<T extends keyof SchemaKeywordMapper>(tree: Schema[] | undefined, keyword: T): SchemaKeywordMapper[T] | undefined;
403
+ static combineObjects(tree: Schema[] | undefined): Schema[];
404
+ build(...generators: Array<Generator<TPluginOptions>>): Promise<Array<KubbFile.File<TFileMeta>>>;
405
+ }
406
+ //#endregion
407
+ //#region ../plugin-oas/src/generators/createGenerator.d.ts
408
+ type CoreGenerator<TOptions extends PluginFactoryOptions> = {
409
+ name: string;
410
+ type: 'core';
411
+ operations: (props: OperationsProps<TOptions>) => Promise<KubbFile.File[]>;
412
+ operation: (props: OperationProps<TOptions>) => Promise<KubbFile.File[]>;
413
+ schema: (props: SchemaProps<TOptions>) => Promise<KubbFile.File[]>;
414
+ };
415
+ //#endregion
416
+ //#region ../plugin-oas/src/generators/types.d.ts
417
+ type OperationsProps<TOptions extends PluginFactoryOptions> = {
418
+ config: Config;
419
+ generator: Omit<OperationGenerator<TOptions>, 'build'>;
420
+ plugin: Plugin<TOptions>;
421
+ operations: Array<Operation>;
422
+ };
423
+ type OperationProps<TOptions extends PluginFactoryOptions> = {
424
+ config: Config;
425
+ generator: Omit<OperationGenerator<TOptions>, 'build'>;
426
+ plugin: Plugin<TOptions>;
427
+ operation: Operation;
428
+ };
429
+ type SchemaProps<TOptions extends PluginFactoryOptions> = {
430
+ config: Config;
431
+ generator: Omit<SchemaGenerator<SchemaGeneratorOptions, TOptions>, 'build'>;
432
+ plugin: Plugin<TOptions>;
433
+ schema: {
434
+ name: string;
435
+ tree: Array<Schema>;
436
+ value: SchemaObject;
437
+ };
438
+ };
439
+ type Generator<TOptions extends PluginFactoryOptions> = CoreGenerator<TOptions> | ReactGenerator<TOptions>;
440
+ //#endregion
441
+ //#region ../plugin-oas/src/generators/createReactGenerator.d.ts
442
+ type ReactGenerator<TOptions extends PluginFactoryOptions> = {
443
+ name: string;
444
+ type: 'react';
445
+ Operations: (props: OperationsProps<TOptions>) => KubbNode;
446
+ Operation: (props: OperationProps<TOptions>) => KubbNode;
447
+ Schema: (props: SchemaProps<TOptions>) => KubbNode;
448
+ };
449
+ //#endregion
450
+ //#region src/types.d.ts
451
+ type Options$1 = {
452
+ /**
453
+ * Specify the export location for the files and define the behavior of the output
454
+ * @default { path: 'mocks', barrelType: 'named' }
455
+ */
456
+ output?: Output<Oas>;
457
+ /**
458
+ * Define which contentType should be used.
459
+ * By default, the first JSON valid mediaType will be used
460
+ */
461
+ contentType?: contentType;
462
+ baseURL?: string;
463
+ /**
464
+ * Group the MSW mocks based on the provided name.
465
+ */
466
+ group?: Group;
467
+ /**
468
+ * Array containing exclude parameters to exclude/skip tags/operations/methods/paths.
469
+ */
470
+ exclude?: Array<Exclude>;
471
+ /**
472
+ * Array containing include parameters to include tags/operations/methods/paths.
473
+ */
474
+ include?: Array<Include>;
475
+ /**
476
+ * Array containing override parameters to override `options` based on tags/operations/methods/paths.
477
+ */
478
+ override?: Array<Override<ResolvedOptions>>;
479
+ transformers?: {
480
+ /**
481
+ * Customize the names based on the type that is provided by the plugin.
482
+ */
483
+ name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string;
484
+ };
485
+ /**
486
+ * Create `handlers.ts` file with all handlers grouped by methods.
487
+ * @default false
488
+ */
489
+ handlers?: boolean;
490
+ /**
491
+ * Which parser should be used before returning the data to the `Response` of MSW.
492
+ * - `'faker'` will use `@kubb/plugin-faker` to generate the data for the response
493
+ * - `'data'` will use your custom data to generate the data for the response
494
+ * @default 'data'
495
+ */
496
+ parser?: 'data' | 'faker';
497
+ /**
498
+ * Define some generators next to the msw generators
499
+ */
500
+ generators?: Array<Generator<PluginMsw>>;
501
+ };
502
+ type ResolvedOptions = {
503
+ output: Output<Oas>;
504
+ group: Options$1['group'];
505
+ parser: NonNullable<Options$1['parser']>;
506
+ baseURL: Options$1['baseURL'] | undefined;
507
+ };
508
+ type PluginMsw = PluginFactoryOptions<'plugin-msw', Options$1, ResolvedOptions, never, ResolvePathOptions>;
509
+ //#endregion
510
+ export { PluginMsw as n, ReactGenerator as r, Options$1 as t };
511
+ //# sourceMappingURL=types-Y4PSmxId.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubb/plugin-msw",
3
- "version": "4.5.1",
3
+ "version": "4.5.3",
4
4
  "description": "Mock Service Worker (MSW) handlers generator plugin for Kubb, creating API mocks from OpenAPI specifications for frontend development and testing.",
5
5
  "keywords": [
6
6
  "msw",
@@ -73,16 +73,16 @@
73
73
  }
74
74
  ],
75
75
  "dependencies": {
76
- "@kubb/react-fabric": "0.2.14",
77
- "@kubb/core": "4.5.1",
78
- "@kubb/oas": "4.5.1",
79
- "@kubb/plugin-faker": "4.5.1",
80
- "@kubb/plugin-oas": "4.5.1",
81
- "@kubb/plugin-ts": "4.5.1"
76
+ "@kubb/react-fabric": "0.2.19",
77
+ "@kubb/core": "4.5.3",
78
+ "@kubb/oas": "4.5.3",
79
+ "@kubb/plugin-faker": "4.5.3",
80
+ "@kubb/plugin-oas": "4.5.3",
81
+ "@kubb/plugin-ts": "4.5.3"
82
82
  },
83
83
  "devDependencies": {},
84
84
  "peerDependencies": {
85
- "@kubb/react-fabric": "0.2.14"
85
+ "@kubb/react-fabric": "0.2.19"
86
86
  },
87
87
  "engines": {
88
88
  "node": ">=20"
package/src/plugin.ts CHANGED
@@ -1,9 +1,8 @@
1
1
  import path from 'node:path'
2
- import { createPlugin, type Group, getBarrelFiles, getMode, type Plugin, PluginManager } from '@kubb/core'
2
+ import { definePlugin, type Group, getBarrelFiles, getMode } from '@kubb/core'
3
3
  import { camelCase } from '@kubb/core/transformers'
4
4
 
5
5
  import { pluginFakerName } from '@kubb/plugin-faker'
6
- import type { PluginOas as SwaggerPluginOptions } from '@kubb/plugin-oas'
7
6
  import { OperationGenerator, pluginOasName } from '@kubb/plugin-oas'
8
7
  import { pluginTsName } from '@kubb/plugin-ts'
9
8
  import { handlersGenerator, mswGenerator } from './generators'
@@ -11,7 +10,7 @@ import type { PluginMsw } from './types.ts'
11
10
 
12
11
  export const pluginMswName = 'plugin-msw' satisfies PluginMsw['name']
13
12
 
14
- export const pluginMsw = createPlugin<PluginMsw>((options) => {
13
+ export const pluginMsw = definePlugin<PluginMsw>((options) => {
15
14
  const {
16
15
  output = { path: 'handlers', barrelType: 'named' },
17
16
  group,
@@ -81,12 +80,10 @@ export const pluginMsw = createPlugin<PluginMsw>((options) => {
81
80
 
82
81
  return resolvedName
83
82
  },
84
- async buildStart() {
85
- const [swaggerPlugin]: [Plugin<SwaggerPluginOptions>] = PluginManager.getDependedPlugins<SwaggerPluginOptions>(this.plugins, [pluginOasName])
86
-
87
- const oas = await swaggerPlugin.context.getOas()
83
+ async install() {
88
84
  const root = path.resolve(this.config.root, this.config.output.path)
89
85
  const mode = getMode(path.resolve(root, output.path))
86
+ const oas = await this.getOas()
90
87
 
91
88
  const operationGenerator = new OperationGenerator(this.plugin.options, {
92
89
  fabric: this.fabric,
@@ -103,7 +100,7 @@ export const pluginMsw = createPlugin<PluginMsw>((options) => {
103
100
  const files = await operationGenerator.build(...generators)
104
101
  await this.addFile(...files)
105
102
 
106
- const barrelFiles = await getBarrelFiles(this.fileManager.files, {
103
+ const barrelFiles = await getBarrelFiles(this.fabric.files, {
107
104
  type: output.barrelType ?? 'named',
108
105
  root,
109
106
  output,
@@ -1,13 +0,0 @@
1
- import * as OasTypes from "oas/types";
2
- import { HttpMethods as HttpMethod } from "oas/types";
3
- import { Operation as Operation$1 } from "oas/operation";
4
-
5
- //#region ../oas/src/types.d.ts
6
- type contentType = 'application/json' | (string & {});
7
- type SchemaObject$1 = OasTypes.SchemaObject & {
8
- 'x-nullable'?: boolean;
9
- $ref?: string;
10
- };
11
- //#endregion
12
- export { contentType as a, SchemaObject$1 as i, OasTypes as n, Operation$1 as r, HttpMethod as t };
13
- //# sourceMappingURL=types-8ZXwr93_.d.cts.map
@@ -1,13 +0,0 @@
1
- import * as OasTypes from "oas/types";
2
- import { HttpMethods as HttpMethod } from "oas/types";
3
- import { Operation as Operation$1 } from "oas/operation";
4
-
5
- //#region ../oas/src/types.d.ts
6
- type contentType = 'application/json' | (string & {});
7
- type SchemaObject$1 = OasTypes.SchemaObject & {
8
- 'x-nullable'?: boolean;
9
- $ref?: string;
10
- };
11
- //#endregion
12
- export { contentType as a, SchemaObject$1 as i, OasTypes as n, Operation$1 as r, HttpMethod as t };
13
- //# sourceMappingURL=types-D0JuaurR.d.ts.map