From 7e1e6a17210c75460d3bfc57ecd9e1698e8da78e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommy=20Jos=C3=A9povic?= Date: Mon, 29 Jul 2024 11:57:17 -0400 Subject: [PATCH] Update docs, rename to experimental --- docs/src/msw.md | 46 +++++++++++++++++++ docs/src/using-plugins.md | 18 ++++---- packages/create-schemas/src/plugins/index.ts | 2 +- .../src/plugins/openapi-msw-plugin.ts | 8 ++-- packages/create-schemas/tests/plugins.test.ts | 5 +- 5 files changed, 63 insertions(+), 16 deletions(-) diff --git a/docs/src/msw.md b/docs/src/msw.md index 979179e..0d9ea95 100644 --- a/docs/src/msw.md +++ b/docs/src/msw.md @@ -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/ \ No newline at end of file diff --git a/docs/src/using-plugins.md b/docs/src/using-plugins.md index 977fca6..ee04aa2 100644 --- a/docs/src/using-plugins.md +++ b/docs/src/using-plugins.md @@ -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. !!! @@ -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 } + }), +]; ``` diff --git a/packages/create-schemas/src/plugins/index.ts b/packages/create-schemas/src/plugins/index.ts index 5cc44a4..a33911f 100644 --- a/packages/create-schemas/src/plugins/index.ts +++ b/packages/create-schemas/src/plugins/index.ts @@ -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"; diff --git a/packages/create-schemas/src/plugins/openapi-msw-plugin.ts b/packages/create-schemas/src/plugins/openapi-msw-plugin.ts index ac9e704..98095d7 100644 --- a/packages/create-schemas/src/plugins/openapi-msw-plugin.ts +++ b/packages/create-schemas/src/plugins/openapi-msw-plugin.ts @@ -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; @@ -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();" ].join("\n") }); diff --git a/packages/create-schemas/tests/plugins.test.ts b/packages/create-schemas/tests/plugins.test.ts index 70e20c8..a97722e 100644 --- a/packages/create-schemas/tests/plugins.test.ts +++ b/packages/create-schemas/tests/plugins.test.ts @@ -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 }) => { @@ -72,7 +72,7 @@ describe.concurrent("plugins", () => { }); test("openapiMSWPlugin", async ({ expect }) => { - const plugin = unstable_openapiMSWPlugin(); + const plugin = experimental_openapiMSWPlugin(); assert(plugin.transform); @@ -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();" `); });