hapic 🚀

This package contains a HTTP-Client based on fetch.
IT provides a convenient way to make HTTP requests.
The client is extended by the Harbor,
Vault,
and OAuth2 clients,
which provide additional functionality specific to those services.
Table of Contents
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',
});
Singleton
A singleton instance can be associated to a key, to handle multiple singleton instances.
Registration
import { Client, setClient } from "hapic";
const client = new Client({
});
setClient(client, 'default');
Access
import { useClient } from 'hapic';
const client = useClient('default');
Request
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);
The main purpose of the header utils, is to parse and build HTTP headers.
AuthorizationHeader
import {
parseAuthorizationHeader,
stringifyAuthorizationHeader
} from "hapic";
const basicValue = Buffer
.from('admin:start123')
.toString('base64');
const value = parseAuthorizationHeader(`Basic ${basicValue}`);
console.log(value);
let headerValue = stringifyAuthorizationHeader({
type: 'Basic',
username: 'admin',
password: 'start123'
});
console.log(headerValue);
headerValue = stringifyAuthorizationHeader({
type: 'Bearer',
token: 'start123'
});
console.log(headerValue);
License
Made with 💚
Published under MIT License.