Socket
Socket
Sign inDemoInstall

@bigcommerce/request-sender

Package Overview
Dependencies
10
Maintainers
15
Versions
22
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @bigcommerce/request-sender

HTTP request client for browsers


Version published
Weekly downloads
7.9K
increased by2.43%
Maintainers
15
Created
Weekly downloads
 

Changelog

Source

1.2.4 (2024-03-20)

Bug Fixes

  • request: CHECKOUT-8092 Fix typo (63e1040)
  • request: CHECKOUT-8092 Update test (c7cfe44)

Readme

Source

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

FAQs

Last updated on 20 Mar 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc