@kubb/plugin-msw 5.0.0-alpha.8 → 5.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/LICENSE +17 -10
- package/README.md +26 -7
- package/dist/components-BYvgvrY7.cjs +569 -0
- package/dist/components-BYvgvrY7.cjs.map +1 -0
- package/dist/components-Cm17DMTE.js +510 -0
- package/dist/components-Cm17DMTE.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-D9gvdP7Z.js +177 -0
- package/dist/generators-D9gvdP7Z.js.map +1 -0
- package/dist/generators-rZ99WaWQ.cjs +187 -0
- package/dist/generators-rZ99WaWQ.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 +62 -65
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.js +59 -65
- package/dist/index.js.map +1 -1
- package/dist/types-ItsHsMvC.d.ts +94 -0
- package/extension.yaml +233 -0
- package/package.json +57 -58
- package/src/components/Handlers.tsx +3 -3
- package/src/components/Mock.tsx +37 -28
- package/src/components/MockWithFaker.tsx +37 -24
- package/src/components/Response.tsx +23 -17
- package/src/generators/handlersGenerator.tsx +19 -19
- package/src/generators/mswGenerator.tsx +50 -60
- package/src/index.ts +1 -1
- package/src/plugin.ts +47 -86
- package/src/resolvers/resolverMsw.ts +28 -0
- package/src/types.ts +55 -27
- package/src/utils.ts +58 -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
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
<div align="center">
|
|
2
|
-
<h1
|
|
2
|
+
<h1>@kubb/plugin-msw</h1>
|
|
3
3
|
<a href="https://kubb.dev" target="_blank" rel="noopener noreferrer">
|
|
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
|
-
<a href="https://codesandbox.io/s/github/kubb-labs/
|
|
14
|
+
<a href="https://codesandbox.io/s/github/kubb-labs/plugins/tree/main/examples/msw" target="_blank">View Demo</a>
|
|
15
15
|
<span> · </span>
|
|
16
|
-
<a href="https://kubb.dev/" target="_blank">Documentation</a>
|
|
16
|
+
<a href="https://kubb.dev/plugins/msw" target="_blank">Documentation</a>
|
|
17
17
|
<span> · </span>
|
|
18
18
|
<a href="https://github.com/kubb-labs/kubb/issues/" target="_blank">Report Bug</a>
|
|
19
19
|
<span> · </span>
|
|
@@ -21,12 +21,32 @@
|
|
|
21
21
|
</h4>
|
|
22
22
|
</div>
|
|
23
23
|
|
|
24
|
-
|
|
24
|
+
`@kubb/plugin-msw` generates Mock Service Worker (MSW) request handlers from your OpenAPI specification. The handlers work in both browser and Node.js environments via MSW v2.
|
|
25
|
+
|
|
26
|
+
## Features
|
|
27
|
+
|
|
28
|
+
- Generates one handler per OpenAPI operation
|
|
29
|
+
- Infers request and response types from the spec
|
|
30
|
+
- Works in both browser (via service worker) and Node.js test environments
|
|
31
|
+
- Works with `@kubb/plugin-faker` to serve realistic response data
|
|
25
32
|
|
|
33
|
+
## Installation
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
bun add @kubb/plugin-msw
|
|
37
|
+
# or
|
|
38
|
+
pnpm add @kubb/plugin-msw
|
|
39
|
+
# or
|
|
40
|
+
npm install @kubb/plugin-msw
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Documentation
|
|
44
|
+
|
|
45
|
+
See the [full documentation](https://kubb.dev/plugins/msw) for configuration options and examples.
|
|
26
46
|
|
|
27
47
|
## Supporting Kubb
|
|
28
48
|
|
|
29
|
-
Kubb
|
|
49
|
+
Kubb is 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:
|
|
30
50
|
|
|
31
51
|
- [Become a Sponsor on GitHub](https://github.com/sponsors/stijnvanhulle)
|
|
32
52
|
|
|
@@ -36,7 +56,6 @@ Kubb uses an MIT-licensed open source project with its ongoing development made
|
|
|
36
56
|
</a>
|
|
37
57
|
</p>
|
|
38
58
|
|
|
39
|
-
|
|
40
59
|
<!-- Badges -->
|
|
41
60
|
|
|
42
61
|
[npm-version-src]: https://img.shields.io/npm/v/@kubb/plugin-msw?flat&colorA=18181B&colorB=f58517
|
|
@@ -0,0 +1,569 @@
|
|
|
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.toParamsObject();
|
|
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.toParamsObject()
|
|
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}').toParamsObject()
|
|
281
|
+
* // { petId: 'petId', tagId: 'tagId' }
|
|
282
|
+
* ```
|
|
283
|
+
*/
|
|
284
|
+
toParamsObject(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 ../../internals/shared/src/operation.ts
|
|
315
|
+
function getStatusCodeNumber(statusCode) {
|
|
316
|
+
const code = Number(statusCode);
|
|
317
|
+
return Number.isNaN(code) ? void 0 : code;
|
|
318
|
+
}
|
|
319
|
+
function isSuccessStatusCode(statusCode) {
|
|
320
|
+
const code = getStatusCodeNumber(statusCode);
|
|
321
|
+
return code !== void 0 && code >= 200 && code < 300;
|
|
322
|
+
}
|
|
323
|
+
function getSuccessResponses(responses) {
|
|
324
|
+
return responses.filter((response) => isSuccessStatusCode(response.statusCode));
|
|
325
|
+
}
|
|
326
|
+
function getOperationSuccessResponses(node) {
|
|
327
|
+
return getSuccessResponses(node.responses);
|
|
328
|
+
}
|
|
329
|
+
function getPrimarySuccessResponse(node) {
|
|
330
|
+
return getOperationSuccessResponses(node)[0];
|
|
331
|
+
}
|
|
332
|
+
function resolveResponseTypes(node, resolver) {
|
|
333
|
+
const types = [];
|
|
334
|
+
for (const response of node.responses) {
|
|
335
|
+
if (response.statusCode === "default") {
|
|
336
|
+
types.push(["default", resolver.resolveResponseName(node)]);
|
|
337
|
+
continue;
|
|
338
|
+
}
|
|
339
|
+
const code = getStatusCodeNumber(response.statusCode);
|
|
340
|
+
if (code === void 0) continue;
|
|
341
|
+
types.push([code, isSuccessStatusCode(code) ? resolver.resolveResponseName(node) : resolver.resolveResponseStatusName(node, response.statusCode)]);
|
|
342
|
+
}
|
|
343
|
+
return types;
|
|
344
|
+
}
|
|
345
|
+
//#endregion
|
|
346
|
+
//#region src/utils.ts
|
|
347
|
+
/**
|
|
348
|
+
* Gets the content type from a response, defaulting to 'application/json' if a schema exists.
|
|
349
|
+
*/
|
|
350
|
+
function getContentType(response) {
|
|
351
|
+
return getResponseContentType(response) ?? (hasResponseSchema(response) ? "application/json" : void 0);
|
|
352
|
+
}
|
|
353
|
+
/**
|
|
354
|
+
* Determines if a response has a schema that is not void or any.
|
|
355
|
+
*/
|
|
356
|
+
function hasResponseSchema(response) {
|
|
357
|
+
return !!getResponseContentType(response) || !!response?.schema && response.schema.type !== "void" && response.schema.type !== "any";
|
|
358
|
+
}
|
|
359
|
+
function getResponseContentType(response) {
|
|
360
|
+
const contentType = response;
|
|
361
|
+
const value = contentType?.mediaType ?? contentType?.contentType;
|
|
362
|
+
return typeof value === "string" && value.length > 0 ? value : void 0;
|
|
363
|
+
}
|
|
364
|
+
/**
|
|
365
|
+
* Converts an HTTP method to its lowercase MSW equivalent (e.g., 'POST' → 'post').
|
|
366
|
+
*/
|
|
367
|
+
function getMswMethod(node) {
|
|
368
|
+
return node.method.toLowerCase();
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* Converts an OpenAPI-style path to an Express/MSW-style path by replacing `{param}` with `:param`.
|
|
372
|
+
*/
|
|
373
|
+
function getMswUrl(node) {
|
|
374
|
+
return node.path.replaceAll("{", ":").replaceAll("}", "");
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
377
|
+
* Resolves faker metadata for an MSW operation, including response name and file path.
|
|
378
|
+
*/
|
|
379
|
+
function resolveFakerMeta(node, options) {
|
|
380
|
+
const { root, fakerResolver, fakerOutput, fakerGroup } = options;
|
|
381
|
+
const tag = node.tags[0] ?? "default";
|
|
382
|
+
return {
|
|
383
|
+
name: fakerResolver.resolveResponseName(node),
|
|
384
|
+
file: fakerResolver.resolveFile({
|
|
385
|
+
name: node.operationId,
|
|
386
|
+
extname: ".ts",
|
|
387
|
+
tag,
|
|
388
|
+
path: node.path
|
|
389
|
+
}, {
|
|
390
|
+
root,
|
|
391
|
+
output: fakerOutput,
|
|
392
|
+
group: fakerGroup
|
|
393
|
+
})
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
//#endregion
|
|
397
|
+
//#region src/components/Mock.tsx
|
|
398
|
+
const declarationPrinter$2 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
|
|
399
|
+
function Mock({ baseURL = "", name, typeName, requestTypeName, node }) {
|
|
400
|
+
const method = getMswMethod(node);
|
|
401
|
+
const successResponse = getPrimarySuccessResponse(node);
|
|
402
|
+
const statusCode = successResponse ? Number(successResponse.statusCode) : 200;
|
|
403
|
+
const contentType = getContentType(successResponse);
|
|
404
|
+
const url = new URLPath(getMswUrl(node)).toURLPath();
|
|
405
|
+
const headers = [contentType ? `'Content-Type': '${contentType}'` : void 0].filter(Boolean);
|
|
406
|
+
const dataType = hasResponseSchema(successResponse) ? typeName : "string | number | boolean | null | object";
|
|
407
|
+
const callbackType = requestTypeName ? `HttpResponseResolver<Record<string, string>, ${requestTypeName}, any>` : `((info: Parameters<Parameters<typeof http.${method}>[1]>[0]) => Response | Promise<Response>)`;
|
|
408
|
+
const params = declarationPrinter$2.print(_kubb_core.ast.createFunctionParameters({ params: [_kubb_core.ast.createFunctionParameter({
|
|
409
|
+
name: "data",
|
|
410
|
+
type: _kubb_core.ast.createParamsType({
|
|
411
|
+
variant: "reference",
|
|
412
|
+
name: `${dataType} | ${callbackType}`
|
|
413
|
+
}),
|
|
414
|
+
optional: true
|
|
415
|
+
})] }));
|
|
416
|
+
const httpCall = requestTypeName ? `http.${method}<Record<string, string>, ${requestTypeName}, any>` : `http.${method}`;
|
|
417
|
+
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Source, {
|
|
418
|
+
name,
|
|
419
|
+
isIndexable: true,
|
|
420
|
+
isExportable: true,
|
|
421
|
+
children: /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.Function, {
|
|
422
|
+
name,
|
|
423
|
+
export: true,
|
|
424
|
+
params: params ?? "",
|
|
425
|
+
children: `return ${httpCall}(\`${baseURL}${url.replace(/([^/]):/g, "$1\\\\:")}\`, function handler(info) {
|
|
426
|
+
if(typeof data === 'function') return data(info)
|
|
427
|
+
|
|
428
|
+
return new Response(JSON.stringify(data), {
|
|
429
|
+
status: ${statusCode},
|
|
430
|
+
${headers.length ? ` headers: {
|
|
431
|
+
${headers.join(", \n")}
|
|
432
|
+
},` : ""}
|
|
433
|
+
})
|
|
434
|
+
})`
|
|
435
|
+
})
|
|
436
|
+
});
|
|
437
|
+
}
|
|
438
|
+
//#endregion
|
|
439
|
+
//#region src/components/MockWithFaker.tsx
|
|
440
|
+
const declarationPrinter$1 = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
|
|
441
|
+
function MockWithFaker({ baseURL = "", name, fakerName, typeName, requestTypeName, node }) {
|
|
442
|
+
const method = getMswMethod(node);
|
|
443
|
+
const successResponse = getPrimarySuccessResponse(node);
|
|
444
|
+
const statusCode = successResponse ? Number(successResponse.statusCode) : 200;
|
|
445
|
+
const contentType = getContentType(successResponse);
|
|
446
|
+
const url = new URLPath(getMswUrl(node)).toURLPath();
|
|
447
|
+
const headers = [contentType ? `'Content-Type': '${contentType}'` : void 0].filter(Boolean);
|
|
448
|
+
const callbackType = requestTypeName ? `HttpResponseResolver<Record<string, string>, ${requestTypeName}, any>` : `((info: Parameters<Parameters<typeof http.${method}>[1]>[0]) => Response | Promise<Response>)`;
|
|
449
|
+
const params = declarationPrinter$1.print(_kubb_core.ast.createFunctionParameters({ params: [_kubb_core.ast.createFunctionParameter({
|
|
450
|
+
name: "data",
|
|
451
|
+
type: _kubb_core.ast.createParamsType({
|
|
452
|
+
variant: "reference",
|
|
453
|
+
name: `${typeName} | ${callbackType}`
|
|
454
|
+
}),
|
|
455
|
+
optional: true
|
|
456
|
+
})] }));
|
|
457
|
+
const httpCall = requestTypeName ? `http.${method}<Record<string, string>, ${requestTypeName}, any>` : `http.${method}`;
|
|
458
|
+
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Source, {
|
|
459
|
+
name,
|
|
460
|
+
isIndexable: true,
|
|
461
|
+
isExportable: true,
|
|
462
|
+
children: /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.Function, {
|
|
463
|
+
name,
|
|
464
|
+
export: true,
|
|
465
|
+
params: params ?? "",
|
|
466
|
+
children: `return ${httpCall}('${baseURL}${url.replace(/([^/]):/g, "$1\\\\:")}', function handler(info) {
|
|
467
|
+
if(typeof data === 'function') return data(info)
|
|
468
|
+
|
|
469
|
+
return new Response(JSON.stringify(data || ${fakerName}(data)), {
|
|
470
|
+
status: ${statusCode},
|
|
471
|
+
${headers.length ? ` headers: {
|
|
472
|
+
${headers.join(", \n")}
|
|
473
|
+
},` : ""}
|
|
474
|
+
})
|
|
475
|
+
})`
|
|
476
|
+
})
|
|
477
|
+
});
|
|
478
|
+
}
|
|
479
|
+
//#endregion
|
|
480
|
+
//#region src/components/Response.tsx
|
|
481
|
+
const declarationPrinter = (0, _kubb_plugin_ts.functionPrinter)({ mode: "declaration" });
|
|
482
|
+
function Response({ name, typeName, response }) {
|
|
483
|
+
const statusCode = Number(response.statusCode);
|
|
484
|
+
const contentType = getContentType(response);
|
|
485
|
+
const headers = [contentType ? `'Content-Type': '${contentType}'` : void 0].filter(Boolean);
|
|
486
|
+
const params = declarationPrinter.print(_kubb_core.ast.createFunctionParameters({ params: [_kubb_core.ast.createFunctionParameter({
|
|
487
|
+
name: "data",
|
|
488
|
+
type: _kubb_core.ast.createParamsType({
|
|
489
|
+
variant: "reference",
|
|
490
|
+
name: typeName
|
|
491
|
+
}),
|
|
492
|
+
optional: !hasResponseSchema(response)
|
|
493
|
+
})] }));
|
|
494
|
+
const responseName = `${name}Response${statusCode}`;
|
|
495
|
+
return /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Source, {
|
|
496
|
+
name: responseName,
|
|
497
|
+
isIndexable: true,
|
|
498
|
+
isExportable: true,
|
|
499
|
+
children: /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.Function, {
|
|
500
|
+
name: responseName,
|
|
501
|
+
export: true,
|
|
502
|
+
params: params ?? "",
|
|
503
|
+
children: `
|
|
504
|
+
return new Response(JSON.stringify(data), {
|
|
505
|
+
status: ${statusCode},
|
|
506
|
+
${headers.length ? ` headers: {
|
|
507
|
+
${headers.join(", \n")}
|
|
508
|
+
},` : ""}
|
|
509
|
+
})`
|
|
510
|
+
})
|
|
511
|
+
});
|
|
512
|
+
}
|
|
513
|
+
//#endregion
|
|
514
|
+
Object.defineProperty(exports, "Handlers", {
|
|
515
|
+
enumerable: true,
|
|
516
|
+
get: function() {
|
|
517
|
+
return Handlers;
|
|
518
|
+
}
|
|
519
|
+
});
|
|
520
|
+
Object.defineProperty(exports, "Mock", {
|
|
521
|
+
enumerable: true,
|
|
522
|
+
get: function() {
|
|
523
|
+
return Mock;
|
|
524
|
+
}
|
|
525
|
+
});
|
|
526
|
+
Object.defineProperty(exports, "MockWithFaker", {
|
|
527
|
+
enumerable: true,
|
|
528
|
+
get: function() {
|
|
529
|
+
return MockWithFaker;
|
|
530
|
+
}
|
|
531
|
+
});
|
|
532
|
+
Object.defineProperty(exports, "Response", {
|
|
533
|
+
enumerable: true,
|
|
534
|
+
get: function() {
|
|
535
|
+
return Response;
|
|
536
|
+
}
|
|
537
|
+
});
|
|
538
|
+
Object.defineProperty(exports, "__name", {
|
|
539
|
+
enumerable: true,
|
|
540
|
+
get: function() {
|
|
541
|
+
return __name;
|
|
542
|
+
}
|
|
543
|
+
});
|
|
544
|
+
Object.defineProperty(exports, "camelCase", {
|
|
545
|
+
enumerable: true,
|
|
546
|
+
get: function() {
|
|
547
|
+
return camelCase;
|
|
548
|
+
}
|
|
549
|
+
});
|
|
550
|
+
Object.defineProperty(exports, "getOperationSuccessResponses", {
|
|
551
|
+
enumerable: true,
|
|
552
|
+
get: function() {
|
|
553
|
+
return getOperationSuccessResponses;
|
|
554
|
+
}
|
|
555
|
+
});
|
|
556
|
+
Object.defineProperty(exports, "resolveFakerMeta", {
|
|
557
|
+
enumerable: true,
|
|
558
|
+
get: function() {
|
|
559
|
+
return resolveFakerMeta;
|
|
560
|
+
}
|
|
561
|
+
});
|
|
562
|
+
Object.defineProperty(exports, "resolveResponseTypes", {
|
|
563
|
+
enumerable: true,
|
|
564
|
+
get: function() {
|
|
565
|
+
return resolveResponseTypes;
|
|
566
|
+
}
|
|
567
|
+
});
|
|
568
|
+
|
|
569
|
+
//# sourceMappingURL=components-BYvgvrY7.cjs.map
|