New:Microsoft Teams Notifications Are Now Available in Socket.Learn more →
Get Started

hapic

Package Overview
Dependencies
Maintainers
1
Versions
54
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hapic

A http api client based on axios.

Source
npmnpm
Version
2.0.0-alpha.4
Version published
Weekly downloads
2.2K
0.05%
Maintainers
1
Weekly downloads
 
Created
Source

hapic 🚀

npm version main Known Vulnerabilities Conventional Commits

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({
    //...
});

// This will register the client for the key "default",
// which is also the default key.
setClient(client, 'default');

Access

import { useClient } from 'hapic';

const client = useClient('default');

// do something with the client

Request

DELETE

import client from 'hapic';

const { data } = await client.delete('users/1');
console.log(data);
// [{ id: 1, name: 'xxx' }]

GET

import client from 'hapic';

let { data } = await client.get('users');
console.log(data);
// [{ id: 2, name: 'Peter' }]

PATCH

import client from 'hapic';

let { data } = await client.patch('users/2', { name: 'Peter P.' });
console.log(data);
// [{ id: 2, name: 'Peter P.' }]

POST

import client from 'hapic';

let { data } = await client.post('users', { name: 'Hans' });
console.log(data);
// [{ id: 3, name: 'Hans' }]

PUT

import client from 'hapic';

let { data } = await client.put('users/3', { id: 3, name: 'Hans' });
console.log(data);
// [{ id: 3, name: 'Hans' }]

Header

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);
// {type: 'Basic', username: 'admin', password: 'start123'}

// -------------------------------------------------

let headerValue = stringifyAuthorizationHeader({
    type: 'Basic',
    username: 'admin',
    password: 'start123'
});
console.log(headerValue);
// Basic xxxxxxx

headerValue = stringifyAuthorizationHeader({
    type: 'Bearer',
    token: 'start123'
});
console.log(headerValue);
// Bearer start123

License

Made with 💚

Published under MIT License.

Keywords

query

FAQs

Package last updated on 18 Apr 2023

Related posts