Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Vite plugin #3

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Major differences from the original:
- Improved and configurable logging.
- Helpers such as `defineMock` to assist IDE writing mocks.
- Typescript support.
- Builtin Vite Plugin.

## Usage

Expand Down Expand Up @@ -63,22 +64,14 @@ app.listen(3000);

```ts
import { defineConfig } from "vite";

/* setup mocks */
apimock.config([
{ url: "/api/foo", dir: "tests/mock/foo" },
{ url: "/api/bar", dir: "tests/mock/bar" },
]);
import { vitePlugin as apimockPlugin } from "@forsakringskassan/apimock-express";

export default defineConfig({
plugins: [
{
name: "apimock-plugin",
configureServer(server) {
/* install middleware */
server.middlewares.use("/", apimockExpress.mockRequest);
},
},
apimockPlugin([
{ url: "/api/foo", dir: "tests/mock/foo" },
{ url: "/api/bar", dir: "tests/mock/bar" },
]),
],
});
```
Expand Down
10 changes: 10 additions & 0 deletions main.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,13 @@ export function mockRequest(
response: Response,
next: NextFunction,
): void;

/**
* Vite plugin for apimock-express.
*
* @param mocks - Mock configuration
* @returns A Vite plugin instance.
*/
export function vitePlugin(mocks: MockEntry[]): {
name: "apimock-plugin";
};
24 changes: 23 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ const debug = require("debug")("apimock");
const Table = require("cli-table");
const { version } = require("./package.json");

/**
* @typedef {import("vite").Plugin} Plugin
*/

/**
* @typedef {import("./main").MockEntry} MockEntry
* @typedef {import("./main").MiddlewareConfiguration} MiddlewareConfiguration
Expand Down Expand Up @@ -51,7 +55,7 @@ function toArray(value) {
return Array.isArray(value) ? value : [value];
}

module.exports = {
const apimock = {
/**
* Configuration of the mock.
*
Expand Down Expand Up @@ -139,8 +143,26 @@ module.exports = {
respondData(res, response);
}
},

/**
* Vite plugin for apimock-express.
*
* @param {MockEntry[]} mocks - Mock configuration
* @returns {Plugin}
*/
vitePlugin(mocks) {
return {
name: "apimock-plugin",
configureServer(server) {
apimock.config(mocks);
server.middlewares.use("/", apimock.mockRequest);
},
};
},
};

module.exports = apimock;

/**
* Extracts filecontent for both js and json files
*
Expand Down
Loading