# fetch-stub

> This library wraps the fetch function and responds with a local json

Latest version **0.0.4** (published 2019-05-11) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install fetch-stub
pnpm add fetch-stub
yarn add fetch-stub
bun add fetch-stub
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.4 |
| Published | 2019-05-11 |
| First published | 2019-05-07 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 33 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | gianlucaparadise |
| Maintainers | gianlucaparadise |
| Keywords | fetch mock, stub, mock server, stub server, local server |

## Links

- npm: https://www.npmjs.com/package/fetch-stub
- Repository: https://github.com/gianlucaparadise/fetch-stub
- Homepage: https://github.com/gianlucaparadise/fetch-stub#readme
- Issues: https://github.com/gianlucaparadise/fetch-stub/issues
- npm.io page: https://npm.io/package/fetch-stub

## Dependencies (1)

- [url](https://npm.io/package/url.md) ^0.11.0

## Alternatives

- [pagerjs](https://npm.io/package/pagerjs.md) — 60 weekly downloads
- [whistle.savefor-mock](https://npm.io/package/whistle.savefor-mock.md) — 4 weekly downloads
- [@drietsch/sketch](https://npm.io/package/@drietsch/sketch.md) — 0 weekly downloads
- [@precisa-saude/fhir-rnds-sandbox](https://npm.io/package/@precisa-saude/fhir-rnds-sandbox.md) — 0 weekly downloads
- [@codegrenade/naija-faker-mcp](https://npm.io/package/@codegrenade/naija-faker-mcp.md) — 0 weekly downloads

## Recent versions

- 0.0.4 (latest) — 2019-05-11
- 0.0.3 — 2019-05-09
- 0.0.2 — 2019-05-09
- 0.0.1 — 2019-05-07

## README

# FetchStub

This library wraps the fetch function and responds with a local json.

## Purpose

This library is useful in the following conditions:

* Your backend is unavailable
* Your backend is not yet completed, but you have an interface agreement
* You don't have Internet access
* You need to test specific responses that are difficult to replicate with you server

## Installation

```shell
yarn add fetch-stub
```
or
```shell
npm i fetch-stub --save
```

## Basic Usage

To initialize and load FetchStub:
```js
const FetchStub = require('fetch-stub');
const mockConfig = require('../../config/mocks/mock.config.json'); // path to mock config file
FetchStub.load(mockConfig);
```
To unload FetchStub:
```js
FetchStub.unload();
```

### Config File Instructions

#### Config Example
Here you can see an example:
```js
{
  "$schema": "https://raw.githubusercontent.com/gianlucaparadise/fetch-stub/master/mock.schema.json",
  "forward": false,
  "requests": [
    {
      "method": "GET",
      "path": {
        "base": "/api/user",
        "queries": {
          "id": "7"
        }
      },
	  "responseJson": {
	    "user": "John Doe"
	  }
    },
    {
      "method": "POST",
      "path": {
        "base": "/api/changePassword"
      },
	  "responseJson": {
	    "result": "ok"
	  }
    },
    {
      "method": "POST",
      "path": {
        "base": "/api/registerUser"
      },
      "bodyPatterns": {
        "matches": "['\"]name['\"]\\s*:\\s*['\"]mario['\"]"
      },
	  "responseJson": {
		"result": "ok",
	    "user": "John Doe"
	  }
    }
  ]
}
```

##### Apply JSON Schema
You can write your configurations in a file named `*.mock.json`. For applying the JSON Schema validator in VS Code, add this configuration in your `settings.json` file:
```js
"json.schemas": [
  {
    "fileMatch": [
      "/*.mock.json"
    ],
    "url": "https://raw.githubusercontent.com/gianlucaparadise/fetch-stub/master/mock.schema.json"
  }
]
```

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