npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@janiscommerce/request

v2.0.2

Published

Simple and ligthweight module to handle external requests

Downloads

32

Readme

Request Module

Build Status Coverage Status npm version

📦 Installation

const { Request } = require('@janiscommerce/request');

| :warning: Migration :warning: | | :--: | | If you are using v1.x see the migration guide |

:hammer: Usage

Request example

Making a custom request.

:warning: When the response status code is greater than or equal to 400 throws an error.


try {

    const { statusCode, body } = await Request.get('https://reqres.in/api/users');

    console.log(`Status code: ${statusCode}`); // Status code: 200
    console.log(body); // { message: 'OK' }

} catch(error){

    console.log(error)

    /*
		{
			name: 'RequestError'
			message: 'Request failed: internal error',
			code: 1
		}
    */
}

RequestSafe example

Making a custom safe request

:warning: If you want to handle the error safely. NOT THROW

const { RequestSafe } = require('@janiscommerce/request');

const { statusCode, body } = await RequestSafe.get('https://reqres.in/api/users');

console.log(`Status code: ${statusCode}`); // Status code: 500
console.log(body); // { message: 'internal error' }

⚙️ Classes

📄 Structures

Request

Simple static class to make external request using http and http node core packages

Kind: global class

Request.lastRequest

Kind: static property of Request

Read only: true

Request.lastResponse

Kind: static property of Request

Read only: true

Request.get(endpoint, [options]) ⇒ Promise.<RequestResponse>

Kind: static method of Request

| Param | Type | Default | | --- | --- | --- | | endpoint | string | | | [options] | RequestOptions | {} |

Request.post(endpoint, body, [options]) ⇒ Promise.<RequestResponse>

Kind: static method of Request

| Param | Type | Default | | --- | --- | --- | | endpoint | string | | | body | any | | | [options] | RequestOptions | {} |

Request.put(endpoint, body, [options]) ⇒ Promise.<RequestResponse>

Kind: static method of Request

| Param | Type | Default | | --- | --- | --- | | endpoint | string | | | body | any | | | [options] | RequestOptions | {} |

Request.patch(endpoint, body, [options]) ⇒ Promise.<RequestResponse>

Kind: static method of Request

| Param | Type | Default | | --- | --- | --- | | endpoint | string | | | body | any | | | [options] | RequestOptions | {} |

Request.delete(endpoint, [options]) ⇒ Promise.<RequestResponse>

Kind: static method of Request

| Param | Type | Default | | --- | --- | --- | | endpoint | string | | | [options] | RequestOptions | {} |

Request.call(options) ⇒ Promise.<RequestResponse>

Kind: static method of Request

| Param | Type | | --- | --- | | options | CallOptions |

PathTemplate : string

A string path. Supports templating in "{variable}" format. IE: "/api/users/{userId}/contacts"

Kind: global typedef

CallOptions : object

Kind: global typedef Properties

| Name | Type | Description | | --- | --- | --- | | headers | object | Custom headers on request. Define as { [headerName]: headerValue } | | pathParams | object | Replace variables in path declared as "{variable}". Define structure as { [variableNameInPath]: valueForReplace } | | queryParams | object | Query parameters / filters on request. Define structure as { [queryVariableName]: value } | | path | PathTemplate | The request path | | strictMode | boolean | When this flag is set as true, the request response content-type should be application/json or error will thrown | | endpoint | string | The request endpoint. Protocol and path are optionals. When no protocol specified, http goes by default. Supports *PathTemplate | | method | string | The request method. 'GET' is set by default | | body | any | The request body (if request method accepts it). Can be a valid object, Array, string, or any serializable type. |

RequestOptions : object

Kind: global typedef Properties

| Name | Type | Description | | --- | --- | --- | | headers | object | Custom headers on request. Define as { [headerName]: headerValue } | | pathParams | object | Replace variables in path declared as "{variable}". Define structure as { [variableNameInPath]: valueForReplace } | | queryParams | object | Query parameters / filters on request. Define structure as { [queryVariableName]: value } | | path | PathTemplate | The request path | | strictMode | boolean | When this flag is set as true, the request response content-type should be application/json or error will thrown |

RequestResponse : object

Kind: global typedef Properties

| Name | Type | Description | | --- | --- | --- | | complete | boolean | Flag that represents that if operation was completed | | aborted | boolean | Flag that represents that if operation was aborted | | httpVersion | string | String with http protocol version of the response sent | | rawHeaders | Array.<String> | Request headers as array of srings | | headers | object | Response headers. Formatted as { [headerName]: headerValue } | | statusCode | number | Response status code | | statusMessage | string | Response status message | | body | any | Response body. Can be a valid object, Array, string, or any serializable type. | | rawBody | Array | Response body without serialization. | | originRequest | CallOptions | Used to make another request based on the origin request. Ie: For retry the same request |


:running: Migration

Migration from v1.x to v2

Now Request, in addition to being required in another way, throws an error if the response status code if >= 400

If you want to keep the functionality of v1.x must require and change RequestSafe to your old Request as follows

// old way to require the package
const Request = require('@janiscommerce/request');

// new way
const { RequestSafe } = require('@janiscommerce/request');