HTTP request client for browsers
npm install @bigcommerce/request-senderA simple library for sending HTTP requests.
To send a HTTP request.
``js
import { createRequestSender } from '@bigcommerce/request-sender';
const requestSender = createRequestSender();
// GET request
requestSender.get('/foobars')
.then(({ body }) => console.log(body));
// POST request
requestSender.post('/foobars', { body: { name: 'Foobar' } })
.then(({ body }) => console.log(body));
`
To cancel a pending request
`js
import { createRequestSender, createTimeout } from '@bigcommerce/request-sender';
const timeout = createTimeout(100);
const requestSender = createRequestSender();
requestSender.get('/foobars', { timeout })
.catch(({ status }) => console.log(status));
timeout.cancel();
`
To create a new instance of RequestSender.
To create a new instance of Timeout. If delay is defined, the instance will automatically timeout after the specified period. Otherwise, it remains inactive until complete() is called.
To submit a HTTP request using GET, POST, PUT, PATCH or DELETE method. Alternatively, you can call sendRequest and specify the request method as an argument.
To manually complete a timeout.
#### encodeParams: boolean?
URL encodes params.
Default:
true#### headers: Object?
Request headers.
Default:
{#### params: Object?
URL parameters. They get serialized as a query string.
Default:
null#### method: string?
Request method. It's ignored if calling one of the convenience methods (
get, post etc...).
Default: GET#### credentials: boolean?
Same as
XMLHttpRequest.withCredentials.
Default: true#### timeout: Timeout?
Define if wish to timeout a request.
Default:
null$3
#### body: any
Response body.
Default:
null#### headers: Object
Response headers.
Default:
{}#### status: number?
Response status code. Return
0 if the request is cancelled.
Default: undefined#### statusText: string?
Response status text.
Default:
undefinedDevelopment
Some useful commands
`sh
To test
npm testTo lint
npm run lintTo release
npm run release
`For more commands, please see
package.json`MIT