# mock-restful-service

> Simple to use express middleware for creating mock REST services

Latest version **1.0.1** (published 2017-12-10) · ISC license · 0 weekly downloads

## Install

```sh
npm install mock-restful-service
pnpm add mock-restful-service
yarn add mock-restful-service
bun add mock-restful-service
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2017-12-10 |
| First published | 2017-12-09 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Jordan Nelson |
| Maintainers | jrnelson |
| Keywords | mock, restful, rest, http, api, express, middleware |

## Links

- npm: https://www.npmjs.com/package/mock-restful-service
- Repository: https://github.com/jrnelson333/mock-restful-service
- Homepage: https://github.com/jrnelson333/mock-restful-service#readme
- Issues: https://github.com/jrnelson333/mock-restful-service/issues
- npm.io page: https://npm.io/package/mock-restful-service

## Dependencies (2)

- [colors](https://npm.io/package/colors.md) ^1.1.2
- [deep-equal](https://npm.io/package/deep-equal.md) ^1.0.1

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 1.0.1 (latest) — 2017-12-10
- 1.0.0 — 2017-12-09

## README

# Mock Restful Service
Simple to use express middleware for creating mock REST services. Define routes (sets of requests and responses) as json objects and configure the express middleware to use the routes.

## Features
* Create custom responses for each route (request path and method)
* Create custom responses based on the requests body (headers coming soon!) as well as creating a default response
* URL (request path) pattern matching with Regex
* Create required fields for a request body and specify response status/body if fields are not met

* Specify '404 Not Found' response (status/body) OR pass through if route is not founc

## Sample Use
Below is the most basic use of mock-restful-service

```javascript
const express = require('express');
const mockRestfulSerice = require('mock-restful-service');

const app = express();

const routes = [
        {
            path: '/sample',
            method: 'get',
            defaultResponse: {
                status: 200,
                body: { message: 'Hello World!' }
            }
        },
    ]

app.use(mockRestfulSerice(routes));

app.listen(8002, () => {
    console.log('server now listening on 8002. Open http://localhost:8002');
});

```

## Route Definition

| key                    | Type(s)        | Required?   | Description                        |
| ---------------------- |:---------------|:-----------:|------------------------------------|
| path                   | string, regex  | true        | Partial URL of request             |
| regex                  | boolean        | false       | True if using regex for path param |
| method                 | string         | true        | HTTP method (get, post, put, delete, etc.) |
| requiredFields         | Array          | false       | An array of the required fields (not applicable to get method) |
| requiredFieldsResponse | Object         | false       | The status and the body if any of the required fields are missing |
| defaultResponse        | Object         | true        | The status and body if none of the responseMappings are matched (see below) |
| responseMappings       | Array          | false       | An array of request/response pairs. If the request is received, then the response will be returned |

### Example

```javascript
{
        path: '/user',
        regex: false,
        method: 'post', 
        requiredFields: ['username'],
        requiredFieldsResponse: {
            status: 400,
            body: { error: 'Required field validation'}
        },
        defaultResponse: {
            status: 201,
            body: { message: 'created'}
        },
        responseMappings: [
            {
                request: {
                    body: { username: 'jdoe' }
                },
                response: {
                    status: 400,
                    body: { error: 'this username already exists' }
                }
            }
        ]
    },
```

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