Skip to content

Commit f2f35a7

Browse files
authored
[IDP-1768] Add experimental MSW plugin (#23)
* [IDP-1768] Add unstable MSW plugin * Add docs * Update docs, rename to experimental * update debug * PR comments * fix broken test
1 parent 3c6be50 commit f2f35a7

File tree

16 files changed

+846
-294
lines changed

16 files changed

+846
-294
lines changed

.changeset/dull-cougars-call.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@workleap/create-schemas": minor
3+
---
4+
5+
Add `experimental_openapiMSWPlugin`

.changeset/new-bears-sleep.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@workleap/create-schemas": minor
3+
---
4+
5+
[BREAKING] Rename `openapiFetchPlugin` to `experimental_openapiFetchPlugin`

debug/create-schemas.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { defineConfig } from "@workleap/create-schemas";
2-
import { openapiFetchPlugin } from "@workleap/create-schemas/plugins";
2+
import { experimental_openapiFetchPlugin, experimental_openapiMSWPlugin } from "@workleap/create-schemas/plugins";
33

44
export default defineConfig({
55
input: "v1.yaml",
66
outdir: "src/codegen/v1",
7-
plugins: [openapiFetchPlugin()]
7+
plugins: [experimental_openapiFetchPlugin(), experimental_openapiMSWPlugin()]
88
});

debug/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
},
88
"dependencies": {
99
"@workleap/create-schemas": "workspace:*",
10-
"openapi-fetch": "^0.10.2"
10+
"openapi-fetch": "0.10.2",
11+
"openapi-msw": "0.7.0"
1112
}
1213
}

debug/src/codegen/v1/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/** Do not modify. This file has been generated by @workleap/create-schemas */
1+
/** This file has been generated by @workleap/create-schemas (https://github.com/gsoft-inc/wl-openapi-typescript). Do not modify manually. */
22
import type { paths } from "./types.ts";
33
import _createClient from "openapi-fetch";
44

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/** This file has been generated by @workleap/create-schemas (https://github.com/gsoft-inc/wl-openapi-typescript). Do not modify manually. */
2+
import type { paths } from "./types.ts";
3+
import { createOpenApiHttp } from "openapi-msw";
4+
5+
export const http = createOpenApiHttp<paths>();

debug/src/codegen/v1/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/** Do not modify. This file has been generated by @workleap/create-schemas */
1+
/** This file has been generated by @workleap/create-schemas (https://github.com/gsoft-inc/wl-openapi-typescript). Do not modify manually. */
22
export interface paths {
33
"/good-vibes-points/{userId}": {
44
parameters: {

docs/src/msw.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,52 @@ order: -6
66

77
# Mock Service Worker
88

9+
## Type-safe Handlers
10+
11+
The [`experimental_openapiMSWPlugin` plugin](/using-plugins/#experimental_openapimswplugin) allows you to define MSW handlers in a type-safe way.
12+
13+
To use this client, you need to install the [`openapi-msw`](https://www.npmjs.com/package/openapi-msw) package:
14+
15+
+++ pnpm
16+
```bash
17+
pnpm add openapi-msw
18+
```
19+
+++ npm
20+
```bash
21+
npm install openapi-msw
22+
```
23+
+++ yarn
24+
```bash
25+
yarn add openapi-msw
26+
```
27+
+++
28+
29+
**Example usage:**
30+
31+
```ts #2,5 create-schemas.config.ts
32+
import { defineConfig } from "@workleap/create-schemas";
33+
import { experimental_openapiMSWPlugin } from "@workleap/create-schemas/plugins";
34+
35+
export default defineConfig({
36+
plugins: [experimental_openapiMSWPlugin()]
37+
input: "v1.yaml",
38+
outdir: "codegen",
39+
});
40+
```
41+
42+
```ts #5-6
43+
import { http } from "./codegen/openapi-msw.ts";
44+
45+
export const handlers = [
46+
http.get("/good-vibes-points/{userId}", ({ response }) => {
47+
return response(200).json({ pointx: 50 });
48+
// ^^^^^^ Property "pointx" does not exist on type { points: number }
49+
}),
50+
];
51+
```
52+
53+
## Auto-generated handlers
54+
955
*Soon...*
1056

1157
See https://source.mswjs.io/

docs/src/using-plugins.md

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export default defineConfig({
2727

2828
## Built-in Plugins
2929

30-
### `openapiFetchPlugin`
30+
### `experimental_openapiFetchPlugin`
3131

3232
**Description:**
3333

@@ -53,10 +53,10 @@ yarn add openapi-fetch
5353

5454
```ts #2,5 create-schemas.config.ts
5555
import { defineConfig } from "@workleap/create-schemas";
56-
import { openapiFetchPlugin } from "@workleap/create-schemas/plugins";
56+
import { experimental_openapiFetchPlugin } from "@workleap/create-schemas/plugins";
5757

5858
export default defineConfig({
59-
plugins: [openapiFetchPlugin()]
59+
plugins: [experimental_openapiFetchPlugin()]
6060
input: "v1.yaml",
6161
outdir: "codegen",
6262
});
@@ -80,6 +80,58 @@ if (data?.point) {
8080
}
8181
```
8282

83+
### `experimental_openapiMSWPlugin`
84+
85+
!!!warning Warning
86+
87+
This plugin is currently marked as **experimental**. It may change at any time.
88+
89+
!!!
90+
91+
**Description:**
92+
93+
This plugins outputs a very thin wrapper over [openapi-msw](https://www.npmjs.com/package/openapi-msw). This package lets your define typed MSW handlers.
94+
95+
To use this client, you need to install the `openapi-msw` package:
96+
97+
+++ pnpm
98+
```bash
99+
pnpm add openapi-msw
100+
```
101+
+++ npm
102+
```bash
103+
npm install openapi-msw
104+
```
105+
+++ yarn
106+
```bash
107+
yarn add openapi-msw
108+
```
109+
+++
110+
111+
**Example usage:**
112+
113+
```ts #2,5 create-schemas.config.ts
114+
import { defineConfig } from "@workleap/create-schemas";
115+
import { experimental_openapiMSWPlugin } from "@workleap/create-schemas/plugins";
116+
117+
export default defineConfig({
118+
plugins: [experimental_openapiMSWPlugin()]
119+
input: "v1.yaml",
120+
outdir: "codegen",
121+
});
122+
```
123+
124+
```ts #5-6
125+
import { http } from "./codegen/openapi-msw.ts";
126+
127+
export const handlers = [
128+
http.get("/good-vibes-points/{userId}", ({ response }) => {
129+
return response(200).json({ pointx: 50 });
130+
// ^^^^^^ Property "pointx" does not exist on type { points: number }
131+
}),
132+
];
133+
```
134+
83135

84136
## Creating a Plugin
85137

@@ -122,7 +174,7 @@ export default defineConfig({
122174
});
123175
```
124176

125-
## Build hooks
177+
### Build hooks
126178

127179
To interact with the code generation process, a plugin may include "hooks". Hooks are function that are are called at various stages of the generation. Hooks can affect how a build is run, add a file to the output, or modify a build once complete.
128180

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export type { Plugin } from "./plugin.ts";
2-
export { openapiFetchPlugin } from "./openapi-fetch-plugin.ts";
2+
export { experimental_openapiFetchPlugin } from "./openapi-fetch-plugin.ts";
3+
export { experimental_openapiMSWPlugin } from "./openapi-msw-plugin.ts";

0 commit comments

Comments
 (0)