Consumer - Faked HTTP
Introduction
The Faked HTTP consumer consumes the results generated from @aida/injector-faked-routes. Its purpose is to take the file that contains all endpoints with fake data generated by the injector, and to override the supported HTTP request mechanisms so requests return mocked data instead of making a request over the wire. Only XMLHttpRequest
and fetch
are currently supported and automatically mocked.
Use Cases
The main use case for this consumer is to mock APIs your JS-based application, so development can be done independently from the backend. It can be used with any framework and platform that supports either XMLHttpRequest
or fetch
APIs.
Example
import mockHttp from '@aida/consumer-faked-http';
import fakedEndpoints from './fakedEndpoints';
const baseUri = 'http://localhost:4000';
const options = { timing: 1500 };
mockHttp(fakedEndpoints, baseUri, options);
API
mockHttp(fakedEndpoints, baseUri[, options])
(default export)
fakedEndpoints
- output generated by @aida/injector-faked-httpbaseUri
- The base url to which your requests are directed at, such as http://localhost:3000 or https://example.com/apioptions
- An object of options
timing
- Delay before response in ms. Defaults to 0
.
Notes
Some points to consider when using the consumer are:
- The response comes from the status
200
if defined, otherwise it comes from the first response defined in the endpoints file. - If a route is not defined in the endpoints file, it is passed through. All other routes are mocked.