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

http-factory

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

http-factory

declarative, strongly-typed http clients and serial requests

  • 1.4.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

http-factory

Coverage Status Continuous Deployment Continuous Integration npm version License: MIT

http-factory lets you build declarative, strongly-typed http interfaces

const client = new HttpClient({ ...options })
  // transform the outbound request / inbound response
  .transforms({ request: fn, response: fn })
  // intercept the outbound request / inbound response or error
  .intercepts({ request: fn, response: fn, error: fn })
  // log the outbound request / inbound response
  .logs({ request: isDev, response: isDev })
  // set the base url for the client instance
  .setBaseUrl('...');

// make a serial request
const data = [];

for await (const rs of client.serialGet(urls)) {
  if (rs.status === 200) data.push({ data: rs.data });
...

By default, requests are sent with a Content-Type header of application/json. UTF-8 encoding is set by default, and all request bodies will be serialized.

To change this behavior, you can provide your own Axios options to the constructor.

Each request method accepts an optional callback to which the response or error will be piped. This affords the use of continuation-passing using callbacks:

...
client.intercepts({
  request: ({ data }) => ({ ok: true, data }),
  error: (err) => ({ ok: false, data: null, message: err.response.data.msg || 'something went wrong' }),
});

async function getData () {
  await client.getTheData({ url }, ({ ok, data }) => {
    if (ok) {
      // didn't have to do a bunch of response normalization in my component, yay
    } else {
      // handle
    }
  });
}

Continuations can likewise be passed to serial requests:

...
const callback = ({ data }) => results.push(data);

async function fetchAll () {
  try {
    for await (const _ of client.serialGet(urls, callback));
  } catch(ex) { ... }
}
...

See the API docs below for instantiating clients, dev logging, and making iterable requests.

For client options see Axios docs.

Installation

npm install http-factory

OR

yarn add http-factory

Supported Environments

http-factory currently supports UMD, CommonJS (node versions >= 10), and ESM build-targets

Commonjs:

const { HttpClient } = require('http-factory');

ESM:

import { HttpClient } from 'http-factory';

Documentation

Full documentation can be found here

Keywords

FAQs

Package last updated on 26 Dec 2021

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