# @bigcommerce/request-sender

> HTTP request client for browsers

Latest version **1.2.6** (published 2025-05-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install @bigcommerce/request-sender
pnpm add @bigcommerce/request-sender
yarn add @bigcommerce/request-sender
bun add @bigcommerce/request-sender
```

## Health

**Score 55/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 1.2.6 |
| Published | 2025-05-21 |
| First published | 2018-05-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 6 |
| Unpacked size | 40.4 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 3 |
| Author | BigCommerce |
| Maintainers | bcnpmuser, icatalina, chris.boulton, davidchin, pascal.zajac, chanceaclark, jairobc, jmwiese, jorgemoya, cilo, toma-r, leebigcommerce |

## Links

- npm: https://www.npmjs.com/package/@bigcommerce/request-sender
- Repository: https://github.com/bigcommerce/request-sender-js
- Issues: https://github.com/bigcommerce/request-sender-js/issues
- npm.io page: https://npm.io/package/@bigcommerce/request-sender

## Dependencies (6)

- [tslib](https://npm.io/package/tslib.md) ^2.8.1
- [js-cookie](https://npm.io/package/js-cookie.md) ^2.2.1
- [lodash.merge](https://npm.io/package/lodash.merge.md) ^4.6.2
- [query-string](https://npm.io/package/query-string.md) ^6.14.1
- [@types/js-cookie](https://npm.io/package/@types/js-cookie.md) ^2.2.7
- [@types/query-string](https://npm.io/package/@types/query-string.md) ^6.3.0

## Recent versions

- 1.2.6 (latest) — 2025-05-21
- 1.2.5 — 2025-05-01
- 1.2.4 — 2024-03-20
- 1.2.3 — 2024-03-05
- 1.2.2 — 2024-01-17
- 1.2.1 — 2022-06-28
- 1.2.0 — 2020-07-13
- 1.1.0 — 2020-06-10
- 1.0.3 — 2020-05-29
- 1.0.2 — 2020-05-14
- 1.0.1 — 2020-05-07
- 1.0.0 — 2020-05-06
- 0.5.1 — 2020-04-22
- 0.5.0 — 2020-02-06
- 0.4.0 — 2019-10-07
- … 9 more at https://npm.io/package/@bigcommerce/request-sender/versions

## README

# RequestSender

A simple library for sending HTTP requests.

## Usage

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

## 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<Response>
#### get(url: string, options: RequestOptions): Promise<Response>
#### post(url: string, options: RequestOptions): Promise<Response>
#### put(url: string, options: RequestOptions): Promise<Response>
#### patch(url: string, options: RequestOptions): Promise<Response>
#### delete(url: string, options: RequestOptions): Promise<Response>

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

```sh
# To test
npm test

# To lint
npm run lint

# To release
npm run release
```

For more commands, please see `package.json`

## License

MIT

---
_Source: https://npm.io/package/@bigcommerce/request-sender · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
