HTTP request client for browsers
The npm package "@bigcommerce/request-sender" is designed to serve as a robust HTTP request client specifically tailored for browser environments. This node module simplifies the process of sending HTTP requests from within web applications, making it an essential tool for developers working on e-commerce and other web-based projects. The primary benefit of using "@bigcommerce/request-sender" is its ability to streamline the network communication process, allowing developers to focus more on building features rather than dealing with the complexities of HTTP request handling. The package ensures that requests are managed efficiently, supporting better performance and reliability in web applications.
To start using "@bigcommerce/request-sender" in your project, simply run the command `npm install @bigcommerce/request-sender` in your project directory. This command installs the package and makes it available for import into your JavaScript files. Once installed, developers can leverage the `createRequestSender` function provided by the module to initiate and manage HTTP requests with ease. The function encapsulates a lot of the boilerplate code typically associated with AJAX requests, thereby reducing potential errors and increasing code readability. This npm package not only enhances the development speed but also improves the overall security and maintenance of the project by handling common pitfalls in HTTP communication effectively.
The "@bigcommerce/request-sender" module also comes with additional features such as error handling and response parsing, which further aid developers in creating seamless user experiences. By providing a straightforward and powerful approach to sending HTTP requests, this package plays a crucial role in the development of modern web applications, making it a popular choice among developers looking to implement reliable and efficient HTTP communications in their projects.
Core dependencies of this npm package and its dev dependencies.
@types/js-cookie, @types/query-string, js-cookie, lodash.merge, query-string, tslib, @bigcommerce/tslint-config, @bigcommerce/validate-commits, @types/jest, @types/lodash.merge, @types/node, @typescript-eslint/parser, eslint, jest, standard-version, ts-jest, ts-loader, tslint, typescript
A README file for the @bigcommerce/request-sender code repository. View Code
A simple library for sending HTTP requests.
To send a HTTP request.
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
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.
Request payload.
Default: null
URL encodes params.
Default: true
Request headers.
Default: { 'Accept': 'application/json, text/plain, */*', 'Content-Type': 'application/json', }
URL parameters. They get serialized as a query string.
Default: null
Request method. It's ignored if calling one of the convenience methods (get
, post
etc...).
Default: GET
Same as XMLHttpRequest.withCredentials
.
Default: true
Define if wish to timeout a request.
Default: null
Response body.
Default: null
Response headers.
Default: {}
Response status code. Return 0
if the request is cancelled.
Default: undefined
Response status text.
Default: undefined
Some useful commands
# To test
npm test
# To lint
npm run lint
# To release
npm run release
For more commands, please see package.json
MIT