Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

melchett

Package Overview
Dependencies
Maintainers
2
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

melchett

A plugin-based HTTP client for NodeJS

  • 3.1.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
2
Created
Source

Melchett

A plugin-based HTTP client for NodeJS.

HttpClient

The REST client, constructed using new HttpClient(config). See below for the structure of the config object.

Configuration

All properties are optional.

PropertyTypeDescriptionDefault
namestringName of the clienthttp
timeoutnumberTimeout in milliseconds1500
userAgentstringCustom user agent for the clientmelchett/{VERSION}
successPredicate(status: number) => booleanFunction to determine if a response is resolved or rejected(status) => status >= 200 && status < 400
cacheCacheObject specifying caching options and a reference to a caching engine. If undefined, caching is disabled. See caching for more informationundefined
circuitBreakerCircuitBreakerObject specifying circuit breaker options. If undefined, circuit breaker is disabled. See circuit breaker for more informationundefined
timingHeaderstringResponse header from which to try to parse the response timeundefined
agentOptionsobjectObject containing options that will be used to create a custom https agent. Currently only ca, cert and key are supportedundefined

Middleware

Additional client features are implemented as middleware. These can be opted-into by adding the relevent property for the feature to the client configuration object.

Caching

Cache the responses to idempotent requests that have a max-age directive. Provide an instance of Catbox as the value of cache.store.

store is the only mandatory property.

PropertyTypeDescriptionDefault
storeCatbox instanceCache engine to be usedundefined
cacheTtlnumberMaximum number of seconds to store responses in cache (max-age is preferred if its value is lower)7200
ignoreErrorsbooleanDo not reject the response if a cache error occurstrue
doNotVarystring[]Array of header names that should not be varied on[]
Circuit breaker

If requests begin to fail (status code >= 500) add a circuit breaker to prevent subsequent requests for a predefined time period. Reduced traffic can allow the upstream time to recover. Opossum is used under the hood to provide this functionality. Provide configuration options on the circuitBreaker property of the client configuration object.

Valid configuration options can be found in the Opossum documentation.

Requests

Once the HttpClient has been instantiated, the following methods are available to call

client.get(url [, headers]);
client.post(url, body [, headers]);
client.delete(url [, headers]);
Parameters
  • url - a string representing the full URL (including scheme, and any query or fragment parts)
  • headers - an object containing key/value pairs representing the request headers
  • body - a string or object representing the payload for POST requests

Responses

The request configuration of a given request is always returned (regardless of whether the response is resolved). The following example shows its structure;

{
    request: {
        url: 'https://www.bbc.co.uk',
        client: 'http',
        method: 'get',
        id: '89dce102-2040-40b9-80ae-0a72c5aaa3db'
    }
}

If the response completes successfully, the promise is resolved and the response property will contain the fields shown below:

{
    response: {
        body: 'Here is a response that will inform, educate, and entertain',
        headers: {},
        status: 200,
        duration: 123,
        melchettCached: true
    }
}

Response that are not served from cache and contain a header matching the value provided in timingHeader have the header value added under the duration property.

If no timingHeader is provided or it was absent in the response, melchett will fall back to a less accurate timing calculation for the duration property.

If an error occurs at some point in the request/response chain, the promise is rejected with an additional error field as shown:

{
    error: {
        name: 'ESTATUS500',
        message: 'Status code 500 received',
        details: 'Internal Server Error'
    }
}

Rejections will still include properties under the response field if they are available.

Contributing

To develop melchett as a dependency for another package, when inside the melchett directory run:

npm link

Then, switch into the consuming package directory and run:

npm link melchett

This will symlink melchett into the consumer's node_modules folder and allow you to make changes to melchett without having to commit or reinstall.

Be sure to read the contributing document beforehand.

Copyright © 2021 BBC.

FAQs

Package last updated on 07 Jul 2023

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc