https://github.com/exbotanical/http-factory
Declarative http clients and serial requests
https://github.com/exbotanical/http-factory
axios factory rest rest-api rest-client
Last synced: 9 months ago
JSON representation
Declarative http clients and serial requests
- Host: GitHub
- URL: https://github.com/exbotanical/http-factory
- Owner: exbotanical
- License: mit
- Created: 2021-03-01T00:16:19.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-05-01T03:07:18.000Z (about 3 years ago)
- Last Synced: 2025-02-10T09:37:28.472Z (over 1 year ago)
- Topics: axios, factory, rest, rest-api, rest-client
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/http-factory
- Size: 587 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# http-factory
[](https://coveralls.io/github/MatthewZito/http-factory?branch=master)
[](https://github.com/MatthewZito/http-factory/actions/workflows/cd.yml)
[](https://github.com/MatthewZito/http-factory/actions/workflows/ci.yml)
[](https://badge.fury.io/js/http-factory)
[](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).
```bash
npm install http-factory
```
OR
```bash
yarn add http-factory
```
`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';
```
Full documentation can be found [here](https://matthewzito.github.io/http-factory/http-factory.html)