# @crafts/mock-server

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

Latest version **0.0.12** (published 2023-02-19) · ISC license · 0 weekly downloads

## Install

```sh
npm install @crafts/mock-server
pnpm add @crafts/mock-server
yarn add @crafts/mock-server
bun add @crafts/mock-server
```

Provides the command `start`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.12 |
| Published | 2023-02-19 |
| First published | 2023-01-18 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 15 |
| Unpacked size | 7.8 MB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Mukul Jain |
| Maintainers | mukuljainx |

## Links

- npm: https://www.npmjs.com/package/@crafts/mock-server
- Repository: https://github.com/mukuljainx/mock-server
- Homepage: https://github.com/mukuljainx/mock-server#readme
- Issues: https://github.com/mukuljainx/mock-server/issues
- npm.io page: https://npm.io/package/@crafts/mock-server

## Dependencies (15)

- [cors](https://npm.io/package/cors.md) ^2.8.5
- [glob](https://npm.io/package/glob.md) ^8.1.0
- [chalk](https://npm.io/package/chalk.md) ^4.1.2
- [dotenv](https://npm.io/package/dotenv.md) ^16.0.3
- [lodash](https://npm.io/package/lodash.md) ^4.17.21
- [express](https://npm.io/package/express.md) ^4.18.2
- [shelljs](https://npm.io/package/shelljs.md) ^0.8.5
- [ts-node](https://npm.io/package/ts-node.md) ^10.9.1
- [chokidar](https://npm.io/package/chokidar.md) ^3.5.3
- [commander](https://npm.io/package/commander.md) ^10.0.0
- [socket.io](https://npm.io/package/socket.io.md) ^4.5.4
- [body-parser](https://npm.io/package/body-parser.md) ^1.20.1
- [path-to-regexp](https://npm.io/package/path-to-regexp.md) ^6.2.1
- [express-validator](https://npm.io/package/express-validator.md) ^6.14.3
- [express-http-proxy](https://npm.io/package/express-http-proxy.md) ^1.6.3

## Recent versions

- 0.0.12 (latest) — 2023-02-19
- 0.0.11 — 2023-02-19
- 0.0.10 — 2023-02-16
- 0.0.9 — 2023-02-14
- 0.0.8 — 2023-02-08
- 0.0.7 — 2023-02-05
- 0.0.6 — 2023-02-01
- 0.0.5 — 2023-01-18
- 0.0.4 — 2023-01-18
- 0.0.3 — 2023-01-18
- 0.0.2 — 2023-01-18
- 0.0.1 — 2023-01-18

## README

# Mock Server

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

## Features

1. Simple way to create mock. [Example](https://github.com/mukuljainx/mock-server/blob/master/example/example.mock.json "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](https://github.com/mukuljainx/mock-server/blob/master/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:

```json
{
  "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](https://user-images.githubusercontent.com/10228876/216833293-5fc21bfe-89d5-4ff0-aede-e8f528462777.png)
![Screenshot 2023-02-05 at 10 23 45 PM](https://user-images.githubusercontent.com/10228876/216833294-d9e40bd4-66be-4b33-93cf-681a88240449.png)
![Screenshot 2023-02-05 at 10 23 59 PM](https://user-images.githubusercontent.com/10228876/216833295-b9119dfc-6054-450b-b0da-4f9035393692.png)

### 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

---
_Source: https://npm.io/package/@crafts/mock-server · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
