2.0.2 • Published 3 years ago

@janiscommerce/request v2.0.2

Weekly downloads
20
License
ISC
Repository
github
Last release
3 years ago

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

ParamTypeDefault
endpointstring
optionsRequestOptions{}

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

Kind: static method of Request

ParamTypeDefault
endpointstring
bodyany
optionsRequestOptions{}

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

Kind: static method of Request

ParamTypeDefault
endpointstring
bodyany
optionsRequestOptions{}

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

Kind: static method of Request

ParamTypeDefault
endpointstring
bodyany
optionsRequestOptions{}

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

Kind: static method of Request

ParamTypeDefault
endpointstring
optionsRequestOptions{}

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

Kind: static method of Request

ParamType
optionsCallOptions

PathTemplate : string

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

Kind: global typedef

CallOptions : object

Kind: global typedef Properties

NameTypeDescription
headersobjectCustom headers on request. Define as { headerName: headerValue }
pathParamsobjectReplace variables in path declared as "{variable}". Define structure as { variableNameInPath: valueForReplace }
queryParamsobjectQuery parameters / filters on request. Define structure as { queryVariableName: value }
pathPathTemplateThe request path
strictModebooleanWhen this flag is set as true, the request response content-type should be application/json or error will thrown
endpointstringThe request endpoint. Protocol and path are optionals. When no protocol specified, http goes by default. Supports *PathTemplate
methodstringThe request method. 'GET' is set by default
bodyanyThe request body (if request method accepts it). Can be a valid object, Array, string, or any serializable type.

RequestOptions : object

Kind: global typedef Properties

NameTypeDescription
headersobjectCustom headers on request. Define as { headerName: headerValue }
pathParamsobjectReplace variables in path declared as "{variable}". Define structure as { variableNameInPath: valueForReplace }
queryParamsobjectQuery parameters / filters on request. Define structure as { queryVariableName: value }
pathPathTemplateThe request path
strictModebooleanWhen 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

NameTypeDescription
completebooleanFlag that represents that if operation was completed
abortedbooleanFlag that represents that if operation was aborted
httpVersionstringString with http protocol version of the response sent
rawHeadersArray.<String>Request headers as array of srings
headersobjectResponse headers. Formatted as { headerName: headerValue }
statusCodenumberResponse status code
statusMessagestringResponse status message
bodyanyResponse body. Can be a valid object, Array, string, or any serializable type.
rawBodyArrayResponse body without serialization.
originRequestCallOptionsUsed 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');
2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago