Skip to content

Commit

Permalink
Update docs, rename to experimental
Browse files Browse the repository at this point in the history
  • Loading branch information
tjosepo committed Jul 29, 2024
1 parent bdfa05c commit 7e1e6a1
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 16 deletions.
46 changes: 46 additions & 0 deletions docs/src/msw.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,52 @@ order: -6

# Mock Service Worker

## Type-safe Handlers

The [`experimental_openapiMSWPlugin` plugin](/using-plugins/#experimental_openapimswplugin) allows you to define MSW handlers in a type-safe way.

To use this client, you need to install the [`openapi-msw`](https://www.npmjs.com/package/openapi-msw) package:

+++ pnpm
```bash
pnpm add openapi-msw
```
+++ npm
```bash
npm install openapi-msw
```
+++ yarn
```bash
yarn add openapi-msw
```
+++

**Example usage:**

```ts #2,5 create-schemas.config.ts
import { defineConfig } from "@workleap/create-schemas";
import { experimental_openapiMSWPlugin } from "@workleap/create-schemas/plugins";

export default defineConfig({
plugins: [experimental_openapiMSWPlugin()]
input: "v1.yaml",
outdir: "codegen",
});
```

```ts #5-6
import { http } from "./codegen/openapi-msw.ts";

export const handlers = [
http.get("/good-vibes-points/{userId}", ({ response }) => {
return response(200).json({ pointx: 50 });
// ^^^^^^ Property "pointx" does not exist on type { points: number }
}),
];
```

## Auto-generated handlers

*Soon...*

See https://source.mswjs.io/
18 changes: 9 additions & 9 deletions docs/src/using-plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,11 @@ if (data?.point) {
}
```


### `unstable_openapiMSWPlugin`
### `experimental_openapiMSWPlugin`

!!!warning Warning

This plugin is currently marked as **unstable**. It may change at any time.
This plugin is currently marked as **experimental**. It may change at any time.

!!!

Expand Down Expand Up @@ -113,23 +112,24 @@ yarn add openapi-msw

```ts #2,5 create-schemas.config.ts
import { defineConfig } from "@workleap/create-schemas";
import { openapiMSWPlugin } from "@workleap/create-schemas/plugins";
import { experimental_openapiMSWPlugin } from "@workleap/create-schemas/plugins";

export default defineConfig({
plugins: [openapiMSWPlugin()]
plugins: [experimental_openapiMSWPlugin()]
input: "v1.yaml",
outdir: "codegen",
});
```

```ts
```ts #5-6
import { http } from "./codegen/openapi-msw.ts";

export const handlers = [
http.get("/good-vibes-points/{userId}", ({ response }) => {
return response(200).json({ points: 50 });
});
]
return response(200).json({ pointx: 50 });
// ^^^^^^ Property "pointx" does not exist on type { points: number }
}),
];
```


Expand Down
2 changes: 1 addition & 1 deletion packages/create-schemas/src/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export type { Plugin } from "./plugin.ts";
export { openapiFetchPlugin } from "./openapi-fetch-plugin.ts";
export { unstable_openapiMSWPlugin } from "./openapi-msw-plugin.ts";
export { experimental_openapiMSWPlugin } from "./openapi-msw-plugin.ts";
8 changes: 4 additions & 4 deletions packages/create-schemas/src/plugins/openapi-msw-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { Plugin } from "./plugin.ts";
import { getRelativeModuleResolutionExtension } from "../utils.ts";
import { openapiTypeScriptId } from "./openapi-typescript-plugin.ts";
import type { Plugin } from "./plugin.ts";

export function unstable_openapiMSWPlugin(): Plugin {
export function experimental_openapiMSWPlugin(): Plugin {
return {
name: "openapi-fetch-plugin",
name: "openapi-msw-plugin",
async transform({ id, emitFile }) {
if (id !== openapiTypeScriptId) {
return;
Expand All @@ -16,7 +16,7 @@ export function unstable_openapiMSWPlugin(): Plugin {
filename: "openapi-msw.ts",
code: [
`import type { paths } from "./types${importsFileExtension}";`,
"import { createOpenApiHttp } from \"openapi-msw\";",
"import { createOpenApiHttp } from \"openapi-msw\";\n",
"export const http = createOpenApiHttp<paths>();"
].join("\n")
});
Expand Down
5 changes: 3 additions & 2 deletions packages/create-schemas/tests/plugins.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { headerPlugin } from "../src/plugins/header-plugin.ts";
import { typesPlugin } from "../src/plugins/types-plugin.ts";
import { openapiTypeScriptId, openapiTypeScriptFilename } from "../src/plugins/openapi-typescript-plugin.ts";
import { resolveConfig } from "../src/config.ts";
import { unstable_openapiMSWPlugin } from "../src/plugins/openapi-msw-plugin.ts";
import { experimental_openapiMSWPlugin } from "../src/plugins/openapi-msw-plugin.ts";

describe.concurrent("plugins", () => {
test("headerPlugin", async({ expect }) => {
Expand Down Expand Up @@ -72,7 +72,7 @@ describe.concurrent("plugins", () => {
});

test("openapiMSWPlugin", async ({ expect }) => {
const plugin = unstable_openapiMSWPlugin();
const plugin = experimental_openapiMSWPlugin();

assert(plugin.transform);

Expand All @@ -95,6 +95,7 @@ describe.concurrent("plugins", () => {
expect(emittedFile.code).toMatchInlineSnapshot(`
"import type { paths } from "./types.ts";
import { createOpenApiHttp } from "openapi-msw";
export const http = createOpenApiHttp<paths>();"
`);
});
Expand Down

0 comments on commit 7e1e6a1

Please sign in to comment.