npm install @bigcommerce/request-sender

HTTP request client for browsers

About @bigcommerce/request-sender

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.

More from bigcommerce

bigcommerce npm packages

Find the best node modules for your project.

Search npm

@bigcommerce/request-sender

HTTP request client for...

Read more
,

@bigcommerce/script-loader

A library for loading JavaScript files...

Read more
,

node-bigcommerce

A node module for authentication and use with the BigCommerce...

Read more
,

@bigcommerce/checkout-sdk

BigCommerce Checkout JavaScript...

Read more
,

@bigcommerce/stencil-styles

Compiles SCSS for the Stencil...

Read more
,

@bigcommerce/stencil-cli

CLI tool to run BigCommerce Stores locally for theme development...

Read more
,

@bigcommerce/eslint-config

Default ESLint configuration used at...

Read more
,

@bigcommerce/eslint-plugin

This plugin contains a few custom eslint rules we use at BigCommerce...

Read more
,

@bigcommerce/bigpay-client

JavaScript client for...

Read more
,

@bigcommerce/stencil-paper-handlebars

A paper plugin to render pages using Handlebars...

Read more
,

@bigcommerce/citadel

A SASS Library based on Foundation with opinionated naming...

Read more
,

@bigcommerce/handlebars-v4

A simple wrapper for handlebars...

Read more

Dependencies

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

Documentation

A README file for the @bigcommerce/request-sender code repository. View Code

RequestSender

A simple library for sending HTTP requests.

Usage

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();

API

createRequestSender()

To create a new instance of RequestSender.

createTimeout(delay: number?)

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.

RequestSender

sendRequest(url: string, options: RequestOptions): Promise

get(url: string, options: RequestOptions): Promise

post(url: string, options: RequestOptions): Promise

put(url: string, options: RequestOptions): Promise

patch(url: string, options: RequestOptions): Promise

delete(url: string, options: RequestOptions): Promise

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.

Timeout

complete(): void;

To manually complete a timeout.

RequestOptions

body: any?

Request payload. Default: null

encodeParams: boolean?

URL encodes params. Default: true

headers: Object?

Request headers. Default: { 'Accept': 'application/json, text/plain, */*', 'Content-Type': 'application/json', }

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

Response

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: undefined

Development

Some useful commands

# To test
npm test

# To lint
npm run lint

# To release
npm run release

For more commands, please see package.json

License

MIT