npm.io
0.0.12 • Published 3 years agoCLI

@crafts/mock-server

Licence
ISC
Version
0.0.12
Deps
15
Size
7.8 MB
Vulns
0
Weekly
0

Mock Server

Zero config, simple mock server! Keep the mocks close within your code.

Features

  1. Simple way to create mock. Example.
  2. Pass blob (mock files pattern) option for custom mock paths.
  3. Proxy to actual server using -p or --proxy. All unmatched requested will be sent to proxy.
  4. Out of the box watch method, if any new file is created/changed within the watch path, server will register them.
  5. Create mocks with dynamic URL for eg. /org/:orgId/projects/
  6. Custom status code and delay.
  7. Disable specific mock using disabled option in mock.json.

Usage

Install it

npm i @crafts/mock-server

Add files

Add JSON files ending with .mock.json, anywhere in repo as show in example

Run the server

npx @crafts/mock-server

Set the base URL

Point at http://localhost:4000

Options available
Option description default
blob Pattern to match the mocks /**/*.mock.json
proxy Original/actual server address --
root soruce dir for searching mocks process.cwd() Path from where script is ran
How to write mocks

Writing mocks is very easy, for real. Create a json file matching the search pattern (by default: /**/*.mock.json).

Example:

{
  "mocks": [
    {
      "url": "/a",
      "method": "PATCH",
      "response": { "a": 2 },
      "status": 201,
      "delay": 500,
      "disabled": false
    }
  ]
}
Options for mock
Props type Required
url string true
method string true
response object true
delay number false
status number false
disabled boolean false

UI

Mock server comes with UI (beta) for better experiance.

Screenshot 2023-02-05 at 10 23 36 PM Screenshot 2023-02-05 at 10 23 45 PM Screenshot 2023-02-05 at 10 23 59 PM

Coming soon

Note: Mock server is in beta, things will break but should not fall apart.

Please follow the board for live updates

https://github.com/users/mukuljainx/projects/5