# http-factory

> declarative, strongly-typed http clients and serial requests

Latest version **1.4.1** (published 2021-12-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install http-factory
pnpm add http-factory
yarn add http-factory
bun add http-factory
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.4.1 |
| Published | 2021-12-26 |
| First published | 2021-03-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >= 10 |
| Dependencies | 0 |
| Unpacked size | 226 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Matthew T Zito |
| Maintainers | magister_zito |
| Keywords | http, http client, axios, http requests, response normalization |

## Links

- npm: https://www.npmjs.com/package/http-factory
- Repository: https://github.com/MatthewZito/http-factory
- Homepage: https://github.com/MatthewZito/http-factory#readme
- Issues: https://github.com/MatthewZito/http-factory/issues
- npm.io page: https://npm.io/package/http-factory

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 1.4.1 (latest) — 2021-12-26
- 1.4.0 — 2021-12-10
- 1.3.0 — 2021-05-07
- 1.2.1 — 2021-04-25
- 1.2.0 — 2021-04-09
- 1.1.0 — 2021-03-23
- 1.0.1 — 2021-03-18
- 1.0.0 — 2021-03-07

## README

# http-factory

[![Coverage Status](https://coveralls.io/repos/github/MatthewZito/http-factory/badge.svg?branch=master)](https://coveralls.io/github/MatthewZito/http-factory?branch=master)
[![Continuous Deployment](https://github.com/MatthewZito/http-factory/actions/workflows/cd.yml/badge.svg)](https://github.com/MatthewZito/http-factory/actions/workflows/cd.yml)
[![Continuous Integration](https://github.com/MatthewZito/http-factory/actions/workflows/ci.yml/badge.svg)](https://github.com/MatthewZito/http-factory/actions/workflows/ci.yml)
[![npm version](https://badge.fury.io/js/http-factory.svg)](https://badge.fury.io/js/http-factory)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

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

- [Install](#install)
- [Supported Environments](#support)
- [Documentation / API](#docs)

```ts
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:

```ts
...
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:

```ts
...
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](https://github.com/axios/axios).

## <a name="install"></a> Installation

```bash
npm install http-factory
```

OR

```bash
yarn add http-factory
```

## <a name="support"></a>  Supported Environments

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

Commonjs:

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

ESM:

```ts
import { HttpClient } from 'http-factory';
```

## <a name="docs"></a> Documentation

Full documentation can be found [here](https://matthewzito.github.io/http-factory/http-factory.html)

---
_Source: https://npm.io/package/http-factory · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
