Making api call with node-fetch, with middleware.
const Caller = require('node-callapi');
const caller = new Caller(setting);
caller.call(endpoint, options)
new Caller(options);
middlewares will be applied after every request finished.
a middleware is an async function, it recevied an object. and returns a object that has the shape same as received data.
async function middleware(data) {
const {
// fetch options
fetchOptions,
// the endpoint
url,
// response, if no response, it will be null
response,
// the request duration
duration,
// if any error occured, it will be an Error instance
error,
// the result data, it will be one of [array, object, string, null]
data,
} = data;
// do something
return result;
}
name | description |
---|---|
error.status |
the response status |
error.fetchOptions |
the fetch options |
error.duration |
the time of the fetch request processing time |
error.data |
the data from server, it will parsed even if request failed |
error.response |
the full response object |
error.url |
the fetch url |
url should use absolute url.
it will return the parsed result.
request method. should be one of ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'TRACE', 'HEAD', 'CONNECT'].
send data to server if use 'POST', 'PUT' method.
append middlewares. should be a valid middleware.
append headers. should be an key-value object.