@kubb/plugin-msw 5.0.0-alpha.8 → 5.0.0-beta.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.
- package/LICENSE +17 -10
- package/README.md +1 -3
- package/dist/components-CLQ77DVn.cjs +584 -0
- package/dist/components-CLQ77DVn.cjs.map +1 -0
- package/dist/components-vO0FIb2i.js +519 -0
- package/dist/components-vO0FIb2i.js.map +1 -0
- package/dist/components.cjs +1 -1
- package/dist/components.d.ts +17 -21
- package/dist/components.js +1 -1
- package/dist/generators-BPJCs1x1.js +176 -0
- package/dist/generators-BPJCs1x1.js.map +1 -0
- package/dist/generators-CrmMwWE4.cjs +186 -0
- package/dist/generators-CrmMwWE4.cjs.map +1 -0
- package/dist/generators.cjs +1 -1
- package/dist/generators.d.ts +4 -471
- package/dist/generators.js +1 -1
- package/dist/index.cjs +54 -65
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.js +51 -65
- package/dist/index.js.map +1 -1
- package/dist/types-Dxu0KMQ4.d.ts +89 -0
- package/package.json +59 -57
- package/src/components/Handlers.tsx +3 -3
- package/src/components/Mock.tsx +36 -28
- package/src/components/MockWithFaker.tsx +36 -24
- package/src/components/Response.tsx +23 -17
- package/src/generators/handlersGenerator.tsx +18 -18
- package/src/generators/mswGenerator.tsx +49 -60
- package/src/index.ts +1 -1
- package/src/plugin.ts +48 -85
- package/src/resolvers/resolverMsw.ts +19 -0
- package/src/types.ts +45 -22
- package/src/utils.ts +109 -0
- package/dist/components-8XBwMbFa.cjs +0 -343
- package/dist/components-8XBwMbFa.cjs.map +0 -1
- package/dist/components-DgtTZkWX.js +0 -277
- package/dist/components-DgtTZkWX.js.map +0 -1
- package/dist/generators-CY1SNd5X.cjs +0 -171
- package/dist/generators-CY1SNd5X.cjs.map +0 -1
- package/dist/generators-CvyZTxOm.js +0 -161
- package/dist/generators-CvyZTxOm.js.map +0 -1
- package/dist/types-MdHRNpgi.d.ts +0 -68
package/LICENSE
CHANGED
|
@@ -1,14 +1,21 @@
|
|
|
1
|
-
|
|
1
|
+
MIT License
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Copyright (c) 2026 Kubb Labs
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
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:
|
|
7
11
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
— see licenses/LICENSE-AGPL-3.0 for the full license text:
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
11
14
|
|
|
12
|
-
|
|
13
|
-
|
|
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
CHANGED
|
@@ -4,12 +4,12 @@
|
|
|
4
4
|
<img width="180" src="https://raw.githubusercontent.com/kubb-labs/kubb/main/assets/logo.png" alt="Kubb logo">
|
|
5
5
|
</a>
|
|
6
6
|
|
|
7
|
-
|
|
8
7
|
[![npm version][npm-version-src]][npm-version-href]
|
|
9
8
|
[![npm downloads][npm-downloads-src]][npm-downloads-href]
|
|
10
9
|
[![Coverage][coverage-src]][coverage-href]
|
|
11
10
|
[![License][license-src]][license-href]
|
|
12
11
|
[![Sponsors][sponsors-src]][sponsors-href]
|
|
12
|
+
|
|
13
13
|
<h4>
|
|
14
14
|
<a href="https://codesandbox.io/s/github/kubb-labs/kubb/tree/main//examples/typescript" target="_blank">View Demo</a>
|
|
15
15
|
<span> · </span>
|
|
@@ -23,7 +23,6 @@
|
|
|
23
23
|
|
|
24
24
|
Swagger integration to create mock data based on MSW
|
|
25
25
|
|
|
26
|
-
|
|
27
26
|
## Supporting Kubb
|
|
28
27
|
|
|
29
28
|
Kubb uses an MIT-licensed open source project with its ongoing development made possible entirely by the support of Sponsors. If you would like to become a sponsor, please consider:
|
|
@@ -36,7 +35,6 @@ Kubb uses an MIT-licensed open source project with its ongoing development made
|
|
|
36
35
|
</a>
|
|
37
36
|
</p>
|
|
38
37
|
|
|
39
|
-
|
|
40
38
|
<!-- Badges -->
|
|
41
39
|
|
|
42
40
|
[npm-version-src]: https://img.shields.io/npm/v/@kubb/plugin-msw?flat&colorA=18181B&colorB=f58517
|
|
@@ -0,0 +1,584 @@
|
|
|
1
|
+
//#region \0rolldown/runtime.js
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __name = (target, value) => __defProp(target, "name", {
|
|
4
|
+
value,
|
|
5
|
+
configurable: true
|
|
6
|
+
});
|
|
7
|
+
//#endregion
|
|
8
|
+
let _kubb_core = require("@kubb/core");
|
|
9
|
+
let _kubb_plugin_ts = require("@kubb/plugin-ts");
|
|
10
|
+
let _kubb_renderer_jsx = require("@kubb/renderer-jsx");
|
|
11
|
+
let _kubb_renderer_jsx_jsx_runtime = require("@kubb/renderer-jsx/jsx-runtime");
|
|
12
|
+
//#region ../../internals/utils/src/casing.ts
|
|
13
|
+
/**
|
|
14
|
+
* Shared implementation for camelCase and PascalCase conversion.
|
|
15
|
+
* Splits on common word boundaries (spaces, hyphens, underscores, dots, slashes, colons)
|
|
16
|
+
* and capitalizes each word according to `pascal`.
|
|
17
|
+
*
|
|
18
|
+
* When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are.
|
|
19
|
+
*/
|
|
20
|
+
function toCamelOrPascal(text, pascal) {
|
|
21
|
+
return text.trim().replace(/([a-z\d])([A-Z])/g, "$1 $2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2").replace(/(\d)([a-z])/g, "$1 $2").split(/[\s\-_./\\:]+/).filter(Boolean).map((word, i) => {
|
|
22
|
+
if (word.length > 1 && word === word.toUpperCase()) return word;
|
|
23
|
+
if (i === 0 && !pascal) return word.charAt(0).toLowerCase() + word.slice(1);
|
|
24
|
+
return word.charAt(0).toUpperCase() + word.slice(1);
|
|
25
|
+
}).join("").replace(/[^a-zA-Z0-9]/g, "");
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Splits `text` on `.` and applies `transformPart` to each segment.
|
|
29
|
+
* The last segment receives `isLast = true`, all earlier segments receive `false`.
|
|
30
|
+
* Segments are joined with `/` to form a file path.
|
|
31
|
+
*
|
|
32
|
+
* Only splits on dots followed by a letter so that version numbers
|
|
33
|
+
* embedded in operationIds (e.g. `v2025.0`) are kept intact.
|
|
34
|
+
*/
|
|
35
|
+
function applyToFileParts(text, transformPart) {
|
|
36
|
+
const parts = text.split(/\.(?=[a-zA-Z])/);
|
|
37
|
+
return parts.map((part, i) => transformPart(part, i === parts.length - 1)).join("/");
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Converts `text` to camelCase.
|
|
41
|
+
* When `isFile` is `true`, dot-separated segments are each cased independently and joined with `/`.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* camelCase('hello-world') // 'helloWorld'
|
|
45
|
+
* camelCase('pet.petId', { isFile: true }) // 'pet/petId'
|
|
46
|
+
*/
|
|
47
|
+
function camelCase(text, { isFile, prefix = "", suffix = "" } = {}) {
|
|
48
|
+
if (isFile) return applyToFileParts(text, (part, isLast) => camelCase(part, isLast ? {
|
|
49
|
+
prefix,
|
|
50
|
+
suffix
|
|
51
|
+
} : {}));
|
|
52
|
+
return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false);
|
|
53
|
+
}
|
|
54
|
+
//#endregion
|
|
55
|
+
//#region ../../internals/utils/src/reserved.ts
|
|
56
|
+
/**
|
|
57
|
+
* JavaScript and Java reserved words.
|
|
58
|
+
* @link https://github.com/jonschlinkert/reserved/blob/master/index.js
|
|
59
|
+
*/
|
|
60
|
+
const reservedWords = new Set([
|
|
61
|
+
"abstract",
|
|
62
|
+
"arguments",
|
|
63
|
+
"boolean",
|
|
64
|
+
"break",
|
|
65
|
+
"byte",
|
|
66
|
+
"case",
|
|
67
|
+
"catch",
|
|
68
|
+
"char",
|
|
69
|
+
"class",
|
|
70
|
+
"const",
|
|
71
|
+
"continue",
|
|
72
|
+
"debugger",
|
|
73
|
+
"default",
|
|
74
|
+
"delete",
|
|
75
|
+
"do",
|
|
76
|
+
"double",
|
|
77
|
+
"else",
|
|
78
|
+
"enum",
|
|
79
|
+
"eval",
|
|
80
|
+
"export",
|
|
81
|
+
"extends",
|
|
82
|
+
"false",
|
|
83
|
+
"final",
|
|
84
|
+
"finally",
|
|
85
|
+
"float",
|
|
86
|
+
"for",
|
|
87
|
+
"function",
|
|
88
|
+
"goto",
|
|
89
|
+
"if",
|
|
90
|
+
"implements",
|
|
91
|
+
"import",
|
|
92
|
+
"in",
|
|
93
|
+
"instanceof",
|
|
94
|
+
"int",
|
|
95
|
+
"interface",
|
|
96
|
+
"let",
|
|
97
|
+
"long",
|
|
98
|
+
"native",
|
|
99
|
+
"new",
|
|
100
|
+
"null",
|
|
101
|
+
"package",
|
|
102
|
+
"private",
|
|
103
|
+
"protected",
|
|
104
|
+
"public",
|
|
105
|
+
"return",
|
|
106
|
+
"short",
|
|
107
|
+
"static",
|
|
108
|
+
"super",
|
|
109
|
+
"switch",
|
|
110
|
+
"synchronized",
|
|
111
|
+
"this",
|
|
112
|
+
"throw",
|
|
113
|
+
"throws",
|
|
114
|
+
"transient",
|
|
115
|
+
"true",
|
|
116
|
+
"try",
|
|
117
|
+
"typeof",
|
|
118
|
+
"var",
|
|
119
|
+
"void",
|
|
120
|
+
"volatile",
|
|
121
|
+
"while",
|
|
122
|
+
"with",
|
|
123
|
+
"yield",
|
|
124
|
+
"Array",
|
|
125
|
+
"Date",
|
|
126
|
+
"hasOwnProperty",
|
|
127
|
+
"Infinity",
|
|
128
|
+
"isFinite",
|
|
129
|
+
"isNaN",
|
|
130
|
+
"isPrototypeOf",
|
|
131
|
+
"length",
|
|
132
|
+
"Math",
|
|
133
|
+
"name",
|
|
134
|
+
"NaN",
|
|
135
|
+
"Number",
|
|
136
|
+
"Object",
|
|
137
|
+
"prototype",
|
|
138
|
+
"String",
|
|
139
|
+
"toString",
|
|
140
|
+
"undefined",
|
|
141
|
+
"valueOf"
|
|
142
|
+
]);
|
|
143
|
+
/**
|
|
144
|
+
* Returns `true` when `name` is a syntactically valid JavaScript variable name.
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* ```ts
|
|
148
|
+
* isValidVarName('status') // true
|
|
149
|
+
* isValidVarName('class') // false (reserved word)
|
|
150
|
+
* isValidVarName('42foo') // false (starts with digit)
|
|
151
|
+
* ```
|
|
152
|
+
*/
|
|
153
|
+
function isValidVarName(name) {
|
|
154
|
+
if (!name || reservedWords.has(name)) return false;
|
|
155
|
+
return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name);
|
|
156
|
+
}
|
|
157
|
+
//#endregion
|
|
158
|
+
//#region ../../internals/utils/src/urlPath.ts
|
|
159
|
+
/**
|
|
160
|
+
* Parses and transforms an OpenAPI/Swagger path string into various URL formats.
|
|
161
|
+
*
|
|
162
|
+
* @example
|
|
163
|
+
* const p = new URLPath('/pet/{petId}')
|
|
164
|
+
* p.URL // '/pet/:petId'
|
|
165
|
+
* p.template // '`/pet/${petId}`'
|
|
166
|
+
*/
|
|
167
|
+
var URLPath = class {
|
|
168
|
+
/**
|
|
169
|
+
* The raw OpenAPI/Swagger path string, e.g. `/pet/{petId}`.
|
|
170
|
+
*/
|
|
171
|
+
path;
|
|
172
|
+
#options;
|
|
173
|
+
constructor(path, options = {}) {
|
|
174
|
+
this.path = path;
|
|
175
|
+
this.#options = options;
|
|
176
|
+
}
|
|
177
|
+
/** Converts the OpenAPI path to Express-style colon syntax, e.g. `/pet/{petId}` → `/pet/:petId`.
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* ```ts
|
|
181
|
+
* new URLPath('/pet/{petId}').URL // '/pet/:petId'
|
|
182
|
+
* ```
|
|
183
|
+
*/
|
|
184
|
+
get URL() {
|
|
185
|
+
return this.toURLPath();
|
|
186
|
+
}
|
|
187
|
+
/** Returns `true` when `path` is a fully-qualified URL (e.g. starts with `https://`).
|
|
188
|
+
*
|
|
189
|
+
* @example
|
|
190
|
+
* ```ts
|
|
191
|
+
* new URLPath('https://petstore.swagger.io/v2/pet').isURL // true
|
|
192
|
+
* new URLPath('/pet/{petId}').isURL // false
|
|
193
|
+
* ```
|
|
194
|
+
*/
|
|
195
|
+
get isURL() {
|
|
196
|
+
try {
|
|
197
|
+
return !!new URL(this.path).href;
|
|
198
|
+
} catch {
|
|
199
|
+
return false;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Converts the OpenAPI path to a TypeScript template literal string.
|
|
204
|
+
*
|
|
205
|
+
* @example
|
|
206
|
+
* new URLPath('/pet/{petId}').template // '`/pet/${petId}`'
|
|
207
|
+
* new URLPath('/account/monetary-accountID').template // '`/account/${monetaryAccountId}`'
|
|
208
|
+
*/
|
|
209
|
+
get template() {
|
|
210
|
+
return this.toTemplateString();
|
|
211
|
+
}
|
|
212
|
+
/** Returns the path and its extracted params as a structured `URLObject`, or as a stringified expression when `stringify` is set.
|
|
213
|
+
*
|
|
214
|
+
* @example
|
|
215
|
+
* ```ts
|
|
216
|
+
* new URLPath('/pet/{petId}').object
|
|
217
|
+
* // { url: '/pet/:petId', params: { petId: 'petId' } }
|
|
218
|
+
* ```
|
|
219
|
+
*/
|
|
220
|
+
get object() {
|
|
221
|
+
return this.toObject();
|
|
222
|
+
}
|
|
223
|
+
/** Returns a map of path parameter names, or `undefined` when the path has no parameters.
|
|
224
|
+
*
|
|
225
|
+
* @example
|
|
226
|
+
* ```ts
|
|
227
|
+
* new URLPath('/pet/{petId}').params // { petId: 'petId' }
|
|
228
|
+
* new URLPath('/pet').params // undefined
|
|
229
|
+
* ```
|
|
230
|
+
*/
|
|
231
|
+
get params() {
|
|
232
|
+
return this.getParams();
|
|
233
|
+
}
|
|
234
|
+
#transformParam(raw) {
|
|
235
|
+
const param = isValidVarName(raw) ? raw : camelCase(raw);
|
|
236
|
+
return this.#options.casing === "camelcase" ? camelCase(param) : param;
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Iterates over every `{param}` token in `path`, calling `fn` with the raw token and transformed name.
|
|
240
|
+
*/
|
|
241
|
+
#eachParam(fn) {
|
|
242
|
+
for (const match of this.path.matchAll(/\{([^}]+)\}/g)) {
|
|
243
|
+
const raw = match[1];
|
|
244
|
+
fn(raw, this.#transformParam(raw));
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
toObject({ type = "path", replacer, stringify } = {}) {
|
|
248
|
+
const object = {
|
|
249
|
+
url: type === "path" ? this.toURLPath() : this.toTemplateString({ replacer }),
|
|
250
|
+
params: this.getParams()
|
|
251
|
+
};
|
|
252
|
+
if (stringify) {
|
|
253
|
+
if (type === "template") return JSON.stringify(object).replaceAll("'", "").replaceAll(`"`, "");
|
|
254
|
+
if (object.params) return `{ url: '${object.url}', params: ${JSON.stringify(object.params).replaceAll("'", "").replaceAll(`"`, "")} }`;
|
|
255
|
+
return `{ url: '${object.url}' }`;
|
|
256
|
+
}
|
|
257
|
+
return object;
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Converts the OpenAPI path to a TypeScript template literal string.
|
|
261
|
+
* An optional `replacer` can transform each extracted parameter name before interpolation.
|
|
262
|
+
*
|
|
263
|
+
* @example
|
|
264
|
+
* new URLPath('/pet/{petId}').toTemplateString() // '`/pet/${petId}`'
|
|
265
|
+
*/
|
|
266
|
+
toTemplateString({ prefix = "", replacer } = {}) {
|
|
267
|
+
return `\`${prefix}${this.path.split(/\{([^}]+)\}/).map((part, i) => {
|
|
268
|
+
if (i % 2 === 0) return part;
|
|
269
|
+
const param = this.#transformParam(part);
|
|
270
|
+
return `\${${replacer ? replacer(param) : param}}`;
|
|
271
|
+
}).join("")}\``;
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Extracts all `{param}` segments from the path and returns them as a key-value map.
|
|
275
|
+
* An optional `replacer` transforms each parameter name in both key and value positions.
|
|
276
|
+
* Returns `undefined` when no path parameters are found.
|
|
277
|
+
*
|
|
278
|
+
* @example
|
|
279
|
+
* ```ts
|
|
280
|
+
* new URLPath('/pet/{petId}/tag/{tagId}').getParams()
|
|
281
|
+
* // { petId: 'petId', tagId: 'tagId' }
|
|
282
|
+
* ```
|
|
283
|
+
*/
|
|
284
|
+
getParams(replacer) {
|
|
285
|
+
const params = {};
|
|
286
|
+
this.#eachParam((_raw, param) => {
|
|
287
|
+
const key = replacer ? replacer(param) : param;
|
|
288
|
+
params[key] = key;
|
|
289
|
+
});
|
|
290
|
+
return Object.keys(params).length > 0 ? params : void 0;
|
|
291
|
+
}
|
|
292
|
+
/** Converts the OpenAPI path to Express-style colon syntax.
|
|
293
|
+
*
|
|
294
|
+
* @example
|
|
295
|
+
* ```ts
|
|
296
|
+
* new URLPath('/pet/{petId}').toURLPath() // '/pet/:petId'
|
|
297
|
+
* ```
|
|
298
|
+
*/
|
|
299
|
+
toURLPath() {
|
|
300
|
+
return this.path.replace(/\{([^}]+)\}/g, ":$1");
|
|
301
|
+
}
|
|
302
|
+
};
|
|
303
|
+
//#endregion
|
|
304
|
+
//#region src/components/Handlers.tsx
|
|
305
|
+
function Handlers({ name, handlers }) {
|
|
306
|
+
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Source, {
|
|
307
|
+
name,
|
|
308
|
+
isIndexable: true,
|
|
309
|
+
isExportable: true,
|
|
310
|
+
children: `export const ${name} = ${JSON.stringify(handlers).replaceAll(`"`, "")} as const`
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
//#endregion
|
|
314
|
+
//#region src/utils.ts
|
|
315
|
+
/**
|
|
316
|
+
* Applies a name transformer function to a name if configured, otherwise returns it unchanged.
|
|
317
|
+
*/
|
|
318
|
+
function transformName(name, type, transformers) {
|
|
319
|
+
return transformers?.name?.(name, type) || name;
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Filters responses to only those with 2xx status codes.
|
|
323
|
+
*/
|
|
324
|
+
function getSuccessResponses(node) {
|
|
325
|
+
return node.responses.filter((response) => {
|
|
326
|
+
const code = Number.parseInt(response.statusCode, 10);
|
|
327
|
+
return !Number.isNaN(code) && code >= 200 && code < 300;
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Returns the first 2xx response for an operation, if any.
|
|
332
|
+
*/
|
|
333
|
+
function getPrimarySuccessResponse(node) {
|
|
334
|
+
return getSuccessResponses(node)[0];
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Gets the content type from a response, defaulting to 'application/json' if a schema exists.
|
|
338
|
+
*/
|
|
339
|
+
function getContentType(response) {
|
|
340
|
+
return getResponseContentType(response) ?? (hasResponseSchema(response) ? "application/json" : void 0);
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* Determines if a response has a schema that is not void or any.
|
|
344
|
+
*/
|
|
345
|
+
function hasResponseSchema(response) {
|
|
346
|
+
return !!getResponseContentType(response) || !!response?.schema && response.schema.type !== "void" && response.schema.type !== "any";
|
|
347
|
+
}
|
|
348
|
+
function getResponseContentType(response) {
|
|
349
|
+
const contentType = response;
|
|
350
|
+
const value = contentType?.mediaType ?? contentType?.contentType;
|
|
351
|
+
return typeof value === "string" && value.length > 0 ? value : void 0;
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* Maps all operation responses to their type names, including status code or 'default' for default responses.
|
|
355
|
+
*/
|
|
356
|
+
function getResponseTypes(node, tsResolver) {
|
|
357
|
+
const types = [];
|
|
358
|
+
for (const response of node.responses) {
|
|
359
|
+
if (response.statusCode === "default") {
|
|
360
|
+
types.push(["default", tsResolver.resolveResponseName(node)]);
|
|
361
|
+
continue;
|
|
362
|
+
}
|
|
363
|
+
const code = Number.parseInt(response.statusCode, 10);
|
|
364
|
+
if (Number.isNaN(code)) continue;
|
|
365
|
+
if (code >= 200 && code < 300) {
|
|
366
|
+
types.push([code, tsResolver.resolveResponseName(node)]);
|
|
367
|
+
continue;
|
|
368
|
+
}
|
|
369
|
+
types.push([code, tsResolver.resolveResponseStatusName(node, response.statusCode)]);
|
|
370
|
+
}
|
|
371
|
+
return types;
|
|
372
|
+
}
|
|
373
|
+
/**
|
|
374
|
+
* Converts an HTTP method to its lowercase MSW equivalent (e.g., 'POST' → 'post').
|
|
375
|
+
*/
|
|
376
|
+
function getMswMethod(node) {
|
|
377
|
+
return node.method.toLowerCase();
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* Converts an OpenAPI-style path to an Express/MSW-style path by replacing `{param}` with `:param`.
|
|
381
|
+
*/
|
|
382
|
+
function getMswUrl(node) {
|
|
383
|
+
return node.path.replaceAll("{", ":").replaceAll("}", "");
|
|
384
|
+
}
|
|
385
|
+
/**
|
|
386
|
+
* Resolves faker metadata for an MSW operation, including response name and file path.
|
|
387
|
+
*/
|
|
388
|
+
function resolveFakerMeta(node, options) {
|
|
389
|
+
const { root, fakerResolver, fakerOutput, fakerGroup } = options;
|
|
390
|
+
const tag = node.tags[0] ?? "default";
|
|
391
|
+
return {
|
|
392
|
+
name: fakerResolver.resolveResponseName(node),
|
|
393
|
+
file: fakerResolver.resolveFile({
|
|
394
|
+
name: node.operationId,
|
|
395
|
+
extname: ".ts",
|
|
396
|
+
tag,
|
|
397
|
+
path: node.path
|
|
398
|
+
}, {
|
|
399
|
+
root,
|
|
400
|
+
output: fakerOutput,
|
|
401
|
+
group: fakerGroup
|
|
402
|
+
})
|
|
403
|
+
};
|
|
404
|
+
}
|
|
405
|
+
//#endregion
|
|
406
|
+
//#region src/components/Mock.tsx
|
|
407
|
+
const declarationPrinter$2 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
|
|
408
|
+
function Mock({ baseURL = "", name, typeName, requestTypeName, node }) {
|
|
409
|
+
const method = getMswMethod(node);
|
|
410
|
+
const successResponse = getPrimarySuccessResponse(node);
|
|
411
|
+
const statusCode = successResponse ? Number(successResponse.statusCode) : 200;
|
|
412
|
+
const contentType = getContentType(successResponse);
|
|
413
|
+
const url = new URLPath(getMswUrl(node)).toURLPath();
|
|
414
|
+
const headers = [contentType ? `'Content-Type': '${contentType}'` : void 0].filter(Boolean);
|
|
415
|
+
const dataType = hasResponseSchema(successResponse) ? typeName : "string | number | boolean | null | object";
|
|
416
|
+
const callbackType = requestTypeName ? `HttpResponseResolver<Record<string, string>, ${requestTypeName}, any>` : `((info: Parameters<Parameters<typeof http.${method}>[1]>[0]) => Response | Promise<Response>)`;
|
|
417
|
+
const params = declarationPrinter$2.print(_kubb_core.ast.createFunctionParameters({ params: [_kubb_core.ast.createFunctionParameter({
|
|
418
|
+
name: "data",
|
|
419
|
+
type: _kubb_core.ast.createParamsType({
|
|
420
|
+
variant: "reference",
|
|
421
|
+
name: `${dataType} | ${callbackType}`
|
|
422
|
+
}),
|
|
423
|
+
optional: true
|
|
424
|
+
})] }));
|
|
425
|
+
const httpCall = requestTypeName ? `http.${method}<Record<string, string>, ${requestTypeName}, any>` : `http.${method}`;
|
|
426
|
+
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Source, {
|
|
427
|
+
name,
|
|
428
|
+
isIndexable: true,
|
|
429
|
+
isExportable: true,
|
|
430
|
+
children: /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.Function, {
|
|
431
|
+
name,
|
|
432
|
+
export: true,
|
|
433
|
+
params: params ?? "",
|
|
434
|
+
children: `return ${httpCall}(\`${baseURL}${url.replace(/([^/]):/g, "$1\\\\:")}\`, function handler(info) {
|
|
435
|
+
if(typeof data === 'function') return data(info)
|
|
436
|
+
|
|
437
|
+
return new Response(JSON.stringify(data), {
|
|
438
|
+
status: ${statusCode},
|
|
439
|
+
${headers.length ? ` headers: {
|
|
440
|
+
${headers.join(", \n")}
|
|
441
|
+
},` : ""}
|
|
442
|
+
})
|
|
443
|
+
})`
|
|
444
|
+
})
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
//#endregion
|
|
448
|
+
//#region src/components/MockWithFaker.tsx
|
|
449
|
+
const declarationPrinter$1 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
|
|
450
|
+
function MockWithFaker({ baseURL = "", name, fakerName, typeName, requestTypeName, node }) {
|
|
451
|
+
const method = getMswMethod(node);
|
|
452
|
+
const successResponse = getPrimarySuccessResponse(node);
|
|
453
|
+
const statusCode = successResponse ? Number(successResponse.statusCode) : 200;
|
|
454
|
+
const contentType = getContentType(successResponse);
|
|
455
|
+
const url = new URLPath(getMswUrl(node)).toURLPath();
|
|
456
|
+
const headers = [contentType ? `'Content-Type': '${contentType}'` : void 0].filter(Boolean);
|
|
457
|
+
const callbackType = requestTypeName ? `HttpResponseResolver<Record<string, string>, ${requestTypeName}, any>` : `((info: Parameters<Parameters<typeof http.${method}>[1]>[0]) => Response | Promise<Response>)`;
|
|
458
|
+
const params = declarationPrinter$1.print(_kubb_core.ast.createFunctionParameters({ params: [_kubb_core.ast.createFunctionParameter({
|
|
459
|
+
name: "data",
|
|
460
|
+
type: _kubb_core.ast.createParamsType({
|
|
461
|
+
variant: "reference",
|
|
462
|
+
name: `${typeName} | ${callbackType}`
|
|
463
|
+
}),
|
|
464
|
+
optional: true
|
|
465
|
+
})] }));
|
|
466
|
+
const httpCall = requestTypeName ? `http.${method}<Record<string, string>, ${requestTypeName}, any>` : `http.${method}`;
|
|
467
|
+
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Source, {
|
|
468
|
+
name,
|
|
469
|
+
isIndexable: true,
|
|
470
|
+
isExportable: true,
|
|
471
|
+
children: /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.Function, {
|
|
472
|
+
name,
|
|
473
|
+
export: true,
|
|
474
|
+
params: params ?? "",
|
|
475
|
+
children: `return ${httpCall}('${baseURL}${url.replace(/([^/]):/g, "$1\\\\:")}', function handler(info) {
|
|
476
|
+
if(typeof data === 'function') return data(info)
|
|
477
|
+
|
|
478
|
+
return new Response(JSON.stringify(data || ${fakerName}(data)), {
|
|
479
|
+
status: ${statusCode},
|
|
480
|
+
${headers.length ? ` headers: {
|
|
481
|
+
${headers.join(", \n")}
|
|
482
|
+
},` : ""}
|
|
483
|
+
})
|
|
484
|
+
})`
|
|
485
|
+
})
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
//#endregion
|
|
489
|
+
//#region src/components/Response.tsx
|
|
490
|
+
const declarationPrinter = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
|
|
491
|
+
function Response({ name, typeName, response }) {
|
|
492
|
+
const statusCode = Number(response.statusCode);
|
|
493
|
+
const contentType = getContentType(response);
|
|
494
|
+
const headers = [contentType ? `'Content-Type': '${contentType}'` : void 0].filter(Boolean);
|
|
495
|
+
const params = declarationPrinter.print(_kubb_core.ast.createFunctionParameters({ params: [_kubb_core.ast.createFunctionParameter({
|
|
496
|
+
name: "data",
|
|
497
|
+
type: _kubb_core.ast.createParamsType({
|
|
498
|
+
variant: "reference",
|
|
499
|
+
name: typeName
|
|
500
|
+
}),
|
|
501
|
+
optional: !hasResponseSchema(response)
|
|
502
|
+
})] }));
|
|
503
|
+
const responseName = `${name}Response${statusCode}`;
|
|
504
|
+
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Source, {
|
|
505
|
+
name: responseName,
|
|
506
|
+
isIndexable: true,
|
|
507
|
+
isExportable: true,
|
|
508
|
+
children: /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.Function, {
|
|
509
|
+
name: responseName,
|
|
510
|
+
export: true,
|
|
511
|
+
params: params ?? "",
|
|
512
|
+
children: `
|
|
513
|
+
return new Response(JSON.stringify(data), {
|
|
514
|
+
status: ${statusCode},
|
|
515
|
+
${headers.length ? ` headers: {
|
|
516
|
+
${headers.join(", \n")}
|
|
517
|
+
},` : ""}
|
|
518
|
+
})`
|
|
519
|
+
})
|
|
520
|
+
});
|
|
521
|
+
}
|
|
522
|
+
//#endregion
|
|
523
|
+
Object.defineProperty(exports, "Handlers", {
|
|
524
|
+
enumerable: true,
|
|
525
|
+
get: function() {
|
|
526
|
+
return Handlers;
|
|
527
|
+
}
|
|
528
|
+
});
|
|
529
|
+
Object.defineProperty(exports, "Mock", {
|
|
530
|
+
enumerable: true,
|
|
531
|
+
get: function() {
|
|
532
|
+
return Mock;
|
|
533
|
+
}
|
|
534
|
+
});
|
|
535
|
+
Object.defineProperty(exports, "MockWithFaker", {
|
|
536
|
+
enumerable: true,
|
|
537
|
+
get: function() {
|
|
538
|
+
return MockWithFaker;
|
|
539
|
+
}
|
|
540
|
+
});
|
|
541
|
+
Object.defineProperty(exports, "Response", {
|
|
542
|
+
enumerable: true,
|
|
543
|
+
get: function() {
|
|
544
|
+
return Response;
|
|
545
|
+
}
|
|
546
|
+
});
|
|
547
|
+
Object.defineProperty(exports, "__name", {
|
|
548
|
+
enumerable: true,
|
|
549
|
+
get: function() {
|
|
550
|
+
return __name;
|
|
551
|
+
}
|
|
552
|
+
});
|
|
553
|
+
Object.defineProperty(exports, "camelCase", {
|
|
554
|
+
enumerable: true,
|
|
555
|
+
get: function() {
|
|
556
|
+
return camelCase;
|
|
557
|
+
}
|
|
558
|
+
});
|
|
559
|
+
Object.defineProperty(exports, "getResponseTypes", {
|
|
560
|
+
enumerable: true,
|
|
561
|
+
get: function() {
|
|
562
|
+
return getResponseTypes;
|
|
563
|
+
}
|
|
564
|
+
});
|
|
565
|
+
Object.defineProperty(exports, "getSuccessResponses", {
|
|
566
|
+
enumerable: true,
|
|
567
|
+
get: function() {
|
|
568
|
+
return getSuccessResponses;
|
|
569
|
+
}
|
|
570
|
+
});
|
|
571
|
+
Object.defineProperty(exports, "resolveFakerMeta", {
|
|
572
|
+
enumerable: true,
|
|
573
|
+
get: function() {
|
|
574
|
+
return resolveFakerMeta;
|
|
575
|
+
}
|
|
576
|
+
});
|
|
577
|
+
Object.defineProperty(exports, "transformName", {
|
|
578
|
+
enumerable: true,
|
|
579
|
+
get: function() {
|
|
580
|
+
return transformName;
|
|
581
|
+
}
|
|
582
|
+
});
|
|
583
|
+
|
|
584
|
+
//# sourceMappingURL=components-CLQ77DVn.cjs.map
|