# restify-mock

> A mock server for Restful API powered by restify (add OPTIONS method to all routes automatically to support Cross-origin resource sharing) ### Get start ```sh cd /path/to/project npm init -y npm i restify-mock ``` ### Usage - *serve* ```js const {MockServ

Latest version **0.1.0-alpha.8** (published 2020-02-16) · MIT license · 0 weekly downloads

## Install

```sh
npm install restify-mock
pnpm add restify-mock
yarn add restify-mock
bun add restify-mock
```

## 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.1.0-alpha.8 |
| Published | 2020-02-16 |
| First published | 2020-02-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 54.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | tuke |
| Maintainers | tuke |
| Keywords | js, nodejs, mock, restful |

## Links

- npm: https://www.npmjs.com/package/restify-mock
- npm.io page: https://npm.io/package/restify-mock

## Dependencies (1)

- [restify](https://npm.io/package/restify.md) ^8.5.1

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 0.1.0-alpha.8 (latest) — 2020-02-16
- 0.1.0-alpha.7 — 2020-02-14
- 0.1.0-alpha.6 — 2020-02-14
- 0.1.0-alpha.5 — 2020-02-14
- 0.1.0-alpha.4 — 2020-02-14
- 0.1.0-alpha.3 — 2020-02-14
- 0.1.0-alpha.2 — 2020-02-14
- 0.1.0-alpha.1 — 2020-02-14

## README

# restify-mock
A mock server for Restful API powered by restify
(add OPTIONS method to all routes automatically to support Cross-origin resource sharing)
### Get start
```sh
cd /path/to/project
npm init -y
npm i restify-mock
```
### Usage
- *serve*
```js
const {MockServer} = require('restify-mock');
// or verbose mode, pass true to the first argument
const server = new MockServer();

// #loadAndServe(), load a json file
server.loadAndServe('/path/to/routes.json');
// or #serve(), load from js object
server.serve(routes);
```
- *config*
```js
const {MockServer, LoggingLevel} = require('restify-mock');
// not change #loggingLevel or #bodyFormatter after invoking the constructor
const server = new MockServer(LoggingLevel.HEADERS,
        (statusCode, message, requestStruct, data) => {
    if (statusCode >= 200 && statusCode < 300){
        return {
            success: true,
            message: message,
            data: data,
        };
    } else {
        return {
            success: false,
            message: message,
        };
    }
});   
server.baseUrl = '/api/v1';
// just a example, use addHttpBasicInterceptor(username, password) instead of
server.addRoutedInterceptor((requestStruct, responseAction) => {
    let token = Buffer.from(`Basic ${username}:${password}`).toString('base64');
    let authorization = requestStruct.headers['authorization'];
    if (!authorization) authorization = requestStruct.headers['Authorization'];
    let authorized = authorization === token;
    if (authorized) return;
    responseAction.header('WWW-Authenticate', 'Basic realm="Restricted"');
    return {
        code: 401,
        message: "HTTP Basic Authorization Required",
        data: undefined,
    };
});
```
**routes.json**
```json
[
  {
    "path": "/mock",
    "method": "put",
    "params": {
      "s1": "params, query or request",
      "n1": 0,
      "b1": false
    },
    "response": {
      "ps1": "this is a description",
      "pn1": "0; field type is number"
    }
  },
  {
    "path": "/",
    "method": "post",
    "query": {
      "s1": "",
      "n1": 0,
      "b1": false
    },
    "request": {
      "qs1": "",
      "qn1": "0;number type"
    },
    "response": {
      "ps1": "response string",
      "pn1": "0;this is a description",
      "po1": {
        "po1s1": "mock server",
        "po1n1": "1;node",
        "po1n2": -1
      },
      "pa1": [
        {
          "pa11o1": {},
          "pa11n1": 2,
          "pa11n2": "2",
          "pa11b1": false
        },
        {
          "pa12n1": 1024
        },
        "1024;"
      ]
    }
  }
]
```

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