0.0.4 • Published 5 years ago

fetch-stub v0.0.4

Weekly downloads
50
License
Apache-2.0
Repository
github
Last release
5 years ago

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

yarn add fetch-stub

or

npm i fetch-stub --save

Basic Usage

To initialize and load FetchStub:

const FetchStub = require('fetch-stub');
const mockConfig = require('../../config/mocks/mock.config.json'); // path to mock config file
FetchStub.load(mockConfig);

To unload FetchStub:

FetchStub.unload();

Config File Instructions

Config Example

Here you can see an example:

{
  "$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:

"json.schemas": [
  {
    "fileMatch": [
      "/*.mock.json"
    ],
    "url": "https://raw.githubusercontent.com/gianlucaparadise/fetch-stub/master/mock.schema.json"
  }
]
0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago

1.0.0

5 years ago