Skip to content

Commit

Permalink
Merge pull request #3 from Forsakringskassan/feature/vite-plugin
Browse files Browse the repository at this point in the history
feat: add Vite plugin
  • Loading branch information
ext authored Mar 7, 2024
2 parents 685b376 + 8fe81ee commit 7c69e52
Show file tree
Hide file tree
Showing 5 changed files with 1,194 additions and 34 deletions.
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

0 comments on commit 7c69e52

Please sign in to comment.