@kubb/plugin-msw 5.2.0 → 5.3.0-canary.20261023T192639
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/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/docs/guides/mock-api.md +20 -0
- package/docs/options.md +12 -3
- package/docs/overview.md +1 -1
- package/package.json +5 -5
package/dist/index.cjs
CHANGED
package/dist/index.js
CHANGED
package/docs/guides/mock-api.md
CHANGED
|
@@ -83,3 +83,23 @@ export const worker = setupWorker(...handlers)
|
|
|
83
83
|
::
|
|
84
84
|
|
|
85
85
|
Call `server.listen()` in your test setup, or `await worker.start()` in your browser entry.
|
|
86
|
+
|
|
87
|
+
## Set the base URL at runtime
|
|
88
|
+
|
|
89
|
+
The `baseURL` option is fixed when Kubb generates the handlers. When the host is only known at runtime, such as from your API client's config, call `createHandlers` with it:
|
|
90
|
+
|
|
91
|
+
```typescript [test-server.ts]
|
|
92
|
+
import { setupServer } from 'msw/node'
|
|
93
|
+
import { createHandlers } from './src/gen/handlers/handlers'
|
|
94
|
+
|
|
95
|
+
export const server = setupServer(...createHandlers({ baseURL: getBaseURL() }))
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Each handler also takes `{ baseURL }` as its second argument, which overrides the configured one for that handler. Pass `undefined` as the first argument to keep the default response. This lets you mock two services that share a path:
|
|
99
|
+
|
|
100
|
+
```typescript [test-server.ts]
|
|
101
|
+
export const server = setupServer(
|
|
102
|
+
listPetsHandler(undefined, { baseURL: 'https://pets.example.com' }),
|
|
103
|
+
listPetsHandler(undefined, { baseURL: 'https://staging.example.com' }),
|
|
104
|
+
)
|
|
105
|
+
```
|
package/docs/options.md
CHANGED
|
@@ -21,7 +21,9 @@ Pass these options to `pluginMsw()`. Shared options link to [Shared plugin optio
|
|
|
21
21
|
|
|
22
22
|
### baseURL
|
|
23
23
|
|
|
24
|
-
Base URL prepended to every handler's request. When omitted, no host is prepended and each handler matches the operation's relative path from the spec. Set it to point at a different environment than the spec.
|
|
24
|
+
Base URL prepended to every handler's request. When omitted, no host is prepended and each handler matches the operation's relative path from the spec. Set it to point at a different environment than the spec. Leave off the trailing slash, because operation paths already start with `/`.
|
|
25
|
+
|
|
26
|
+
A value with a template expression, such as `'${process.env.API_URL}'`, is evaluated when the handler is created. To set the base URL at runtime instead, pass `{ baseURL }` to a handler or to [`createHandlers`](#handlers).
|
|
25
27
|
|
|
26
28
|
| | |
|
|
27
29
|
| -------- | -------- |
|
|
@@ -30,7 +32,10 @@ Base URL prepended to every handler's request. When omitted, no host is prepende
|
|
|
30
32
|
|
|
31
33
|
### handlers
|
|
32
34
|
|
|
33
|
-
Emits a `handlers.ts` file
|
|
35
|
+
Emits a `handlers.ts` file with every generated handler in operation order. It exports two things:
|
|
36
|
+
|
|
37
|
+
- `handlers`: the collection with the configured `baseURL`. Spread it into `setupServer(...handlers)` or `setupWorker(...handlers)`.
|
|
38
|
+
- `createHandlers(options?)`: builds the same collection with a `baseURL` you pass at runtime.
|
|
34
39
|
|
|
35
40
|
| | |
|
|
36
41
|
| -------- | --------- |
|
|
@@ -42,7 +47,11 @@ Emits a `handlers.ts` file that re-exports every generated handler in operation
|
|
|
42
47
|
import { getPetHandler } from './getPetHandler'
|
|
43
48
|
import { addPetHandler } from './addPetHandler'
|
|
44
49
|
|
|
45
|
-
export
|
|
50
|
+
export function createHandlers(options?: { baseURL?: string }) {
|
|
51
|
+
return [getPetHandler(undefined, options), addPetHandler(undefined, options)] as const
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export const handlers = createHandlers()
|
|
46
55
|
```
|
|
47
56
|
|
|
48
57
|
### parser
|
package/docs/overview.md
CHANGED
|
@@ -13,7 +13,7 @@ Each handler returns the data you pass to it. With `parser: 'faker'`, a handler
|
|
|
13
13
|
## Options worth knowing
|
|
14
14
|
|
|
15
15
|
- `parser` sets where the response body comes from: `data` (default) or `faker`.
|
|
16
|
-
- `handlers` writes a `handlers.ts` file that exports every handler, ready to spread into `setupServer` or `setupWorker
|
|
16
|
+
- `handlers` writes a `handlers.ts` file that exports every handler, ready to spread into `setupServer` or `setupWorker`, plus `createHandlers({ baseURL })` to set the base URL at runtime.
|
|
17
17
|
- `baseURL` is prepended to the path each handler matches.
|
|
18
18
|
|
|
19
19
|
See [Options](https://kubb.dev/extensions/kubb-labs/plugin-msw/options) for every option and its default.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/plugin-msw",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.3.0-canary.20261023T192639",
|
|
4
4
|
"description": "Generate Mock Service Worker (MSW) request handlers with Kubb. Intercept HTTP requests in the browser or Node.js for frontend development and testing.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"api-mocking",
|
|
@@ -85,12 +85,12 @@
|
|
|
85
85
|
"registry": "https://registry.npmjs.org/"
|
|
86
86
|
},
|
|
87
87
|
"dependencies": {
|
|
88
|
-
"@kubb/plugin-faker": "^5.
|
|
89
|
-
"@kubb/plugin-ts": "^5.0.
|
|
88
|
+
"@kubb/plugin-faker": "^5.2.0-canary.20261023T192638",
|
|
89
|
+
"@kubb/plugin-ts": "^5.1.0-canary.20261023T192638"
|
|
90
90
|
},
|
|
91
91
|
"devDependencies": {
|
|
92
|
-
"@internals/shared": "0.0.
|
|
93
|
-
"@internals/utils": "0.0.
|
|
92
|
+
"@internals/shared": "0.1.0-canary.20261023T192638",
|
|
93
|
+
"@internals/utils": "0.1.0-canary.20261023T192638",
|
|
94
94
|
"@typescript/typescript6": "^6.0.2",
|
|
95
95
|
"kubb": "^5.12.7",
|
|
96
96
|
"msw": "^2.15.0"
|