Skip to content

Commit

Permalink
[IDP-1768] Add unstable MSW plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
tjosepo committed Jul 24, 2024
1 parent 38b06df commit db8bade
Show file tree
Hide file tree
Showing 9 changed files with 676 additions and 285 deletions.
4 changes: 2 additions & 2 deletions debug/create-schemas.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { defineConfig } from "@workleap/create-schemas";
import { openapiFetchPlugin } from "@workleap/create-schemas/plugins";
import { openapiFetchPlugin, unstable_openapiMSWPlugin } from "@workleap/create-schemas/plugins";

export default defineConfig({
input: "v1.yaml",
outdir: "src/codegen/v1",
plugins: [openapiFetchPlugin()]
plugins: [openapiFetchPlugin(), unstable_openapiMSWPlugin()]
});
3 changes: 2 additions & 1 deletion debug/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
},
"dependencies": {
"@workleap/create-schemas": "workspace:*",
"openapi-fetch": "^0.10.2"
"openapi-fetch": "0.10.2",
"openapi-msw": "0.7.0"
}
}
3 changes: 1 addition & 2 deletions debug/src/codegen/v1/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/** Do not modify. This file has been generated by @workleap/create-schemas */
/** This file has been generated by @workleap/create-schemas (https://github.com/gsoft-inc/wl-openapi-typescript). Do not modify manually. */
import type { paths } from "./types.ts";
import _createClient from "openapi-fetch";

export const createClient = _createClient as typeof _createClient<paths, "application/json">;
4 changes: 4 additions & 0 deletions debug/src/codegen/v1/openapi-msw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** This file has been generated by @workleap/create-schemas (https://github.com/gsoft-inc/wl-openapi-typescript). Do not modify manually. */
import type { paths } from "./types.ts";
import { createOpenApiHttp } from "openapi-msw";
export const http = createOpenApiHttp<paths>();
2 changes: 1 addition & 1 deletion debug/src/codegen/v1/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** Do not modify. This file has been generated by @workleap/create-schemas */
/** This file has been generated by @workleap/create-schemas (https://github.com/gsoft-inc/wl-openapi-typescript). Do not modify manually. */
export interface paths {
"/good-vibes-points/{userId}": {
parameters: {
Expand Down
3 changes: 2 additions & 1 deletion packages/create-schemas/src/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export type { Plugin } from "./plugin.ts";
export { openapiFetchPlugin } from "./openapi-fetch-plugin.ts";
export { openapiFetchPlugin } from "./openapi-fetch-plugin.ts";
export { unstable_openapiMSWPlugin } from "./openapi-msw-plugin.ts";
25 changes: 25 additions & 0 deletions packages/create-schemas/src/plugins/openapi-msw-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { Plugin } from "./plugin.ts";
import { getRelativeModuleResolutionExtension } from "../utils.ts";
import { openapiTypeScriptId } from "./openapi-typescript-plugin.ts";

export function unstable_openapiMSWPlugin(): Plugin {
return {
name: "openapi-fetch-plugin",
async transform({ id, emitFile }) {
if (id !== openapiTypeScriptId) {
return;
}

const importsFileExtension = getRelativeModuleResolutionExtension();

emitFile({
filename: "openapi-msw.ts",
code: [
`import type { paths } from "./types${importsFileExtension}";`,
"import { createOpenApiHttp } from \"openapi-msw\";",
"export const http = createOpenApiHttp<paths>();"
].join("\n")
});
}
};
}
35 changes: 32 additions & 3 deletions packages/create-schemas/tests/plugins.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@

import { assert, describe, expect, test } from "vitest";
import { assert, describe, test } from "vitest";
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";

describe.concurrent("plugins", () => {
test("headerPlugin", async() => {
test("headerPlugin", async({ expect }) => {
const plugin = headerPlugin({ header: "This is a header" });

assert(plugin.transform);
Expand All @@ -27,7 +28,7 @@ describe.concurrent("plugins", () => {
`);
});

test("typesPlugin", async() => {
test("typesPlugin", async ({ expect }) => {
const plugin = typesPlugin();

assert(plugin.transform);
Expand Down Expand Up @@ -69,4 +70,32 @@ describe.concurrent("plugins", () => {
"
`);
});

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

assert(plugin.transform);

let emittedFile: { filename: string; code: string } | undefined;
function emitFile(file: { filename: string; code: string }) {
emittedFile = file;
}

await plugin.transform({
config: await resolveConfig({ input: "openapi.json" }),
id: openapiTypeScriptId,
code: "export interface paths {}",
filename: openapiTypeScriptFilename,
emitFile
});

assert(emittedFile);

expect(emittedFile.filename).toBe("openapi-msw.ts");
expect(emittedFile.code).toMatchInlineSnapshot(`
"import type { paths } from "./types.ts";
import { createOpenApiHttp } from "openapi-msw";
export const http = createOpenApiHttp<paths>();"
`);
});
});
Loading

0 comments on commit db8bade

Please sign in to comment.