HAPIC 🚀

"HTTP API Client" is a tiny & simple fetch based http client.
It provides a convenient way to make HTTP requests.
Table of Contents
Features
- ✨ Simple API
- 🔄 Transform request payload & headers
- 🛑 Hooks to intercept request and response
- 🌐 Works in Node.Js, browser & workers environment
- ❌ Throws an error on responses with a non 2xx status code
- 🚀 Method shortcuts for GET, POST, PUT, ...
- ⚙️ Extended options (e.g. baseURL)
- 🎭 Proxy support
Documentation
To read the docs, visit https://hapic.tada5hi.net
Installation
npm install hapic --save
Usage
Config
To create a configuration for the Client, a configuration must be specified,
like described in the following:
import { Client } from "hapic";
const client = new Client({
baseURL: 'http://localhost:3000/',
credentials: 'include',
proxy: true
});
These are the available configuration options for making requests and creating a client:
The type for the configuration object can be inspected under src/request/type.ts.
[!NOTE]
The configuration object extends the RequestInit type of the globalThis object.
This means that these options are also available.
export default {
headers: {
'Authorization': 'Bearer foo'
},
credentials: 'include',
method: 'GET',
baseURL: 'https://example.com/',
body: {
foo: 'bar'
},
responseType: 'json',
responseTransform: [
(data) => {
return data;
}
],
params: {
id: 123
},
proxy: {
url: 'http://localhost:9080',
noProxy: '.foo.bar'
},
query: {
id: 123
},
transform: [
(data) => {
return data;
}
],
};
Request
For the sake of simplicity, alias names have been provided for all supported request methods.
DELETE
import client from 'hapic';
const { data } = await client.delete('users/1');
console.log(data);
GET
import client from 'hapic';
let { data } = await client.get('users');
console.log(data);
PATCH
import client from 'hapic';
let { data } = await client.patch('users/2', { name: 'Peter P.' });
console.log(data);
POST
import client from 'hapic';
let { data } = await client.post('users', { name: 'Hans' });
console.log(data);
PUT
import client from 'hapic';
let { data } = await client.put('users/3', { id: 3, name: 'Hans' });
console.log(data);
Hooks
It is possible to intercept requests or responses before and after they are processed.
Request Hooks
import client, { ClientError, RequestOptions } from 'hapic';
client.on('request', (options: RequestOptions) => {
options.transform = (data, headers) => {
headers.set(HeaderName.AUTHORIZATION, 'Bearer foo');
return data;
};
return options;
});
client.on('requestError', (error: ClientError) => {
throw error;
})
Response Hooks
import client, { ClientError, Response } from 'hapic';
client.on('response', (res: Response) => {
return res;
})
import client, { ClientError, Response } from 'hapic';
client.on('responseError', (error: ClientError) => {
return res;
})
Unsubscribe
The hook can also be removed in the following way:
import client from 'hapic';
const hook = client.on('xxx', () => {
});
client.off(hook);
License
Made with 💚
Published under MIT License.