2.5.2 • Published 7 months ago

hapic v2.5.2

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

hapic

main codecov Known Vulnerabilities npm version

This package contains a simple API-Client based on axios.

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:

Client

import {
    setConfig
} from "hapic";

setConfig({
    driver: {
        baseURL: 'http://localhost:3000/',
        withCredentials: true,
    },
}, 'key')

A configuration is always identified by a key, so multiple configurations can be registered. The default key is named: default

In the upper example, the driver property is used to pass information to the underneath driver (axios).

Singleton

A singleton instance associated for a given configuration key, can be acquired like described in the following:

Client

import {
    useClient
} from "hapic";

const client = useClient('key');
client.setAuthorizationHeader({
    type: 'bearer',
    token: 'xxx'
})

(async () => {
    const {data} = await client.get('users');
    console.log(data);
    // [{id: 1, name: 'Peter'}]
})();

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
2.5.2

7 months ago

2.5.1

1 year ago

2.5.0

1 year ago

2.4.0

2 years ago

2.3.0

2 years ago

2.0.0-alpha.11

2 years ago

2.2.0

2 years ago

2.0.0-alpha.10

2 years ago

2.0.0

2 years ago

2.0.0-alpha.7

2 years ago

2.0.0-alpha.8

2 years ago

2.0.0-alpha.9

2 years ago

2.0.0-alpha.3

2 years ago

2.0.0-alpha.4

2 years ago

2.0.0-alpha.5

2 years ago

2.0.0-alpha.6

2 years ago

1.2.0

2 years ago

1.6.0

2 years ago

1.4.0

2 years ago

1.3.0

2 years ago

1.2.1

2 years ago

2.0.0-alpha.0

2 years ago

2.0.0-alpha.1

2 years ago

2.0.0-alpha.2

2 years ago

1.1.0

2 years ago

1.0.0

2 years ago

0.1.0

3 years ago

0.0.3-alpha.22

3 years ago

0.0.3-alpha.23

3 years ago

0.3.0

2 years ago

0.1.2

3 years ago

0.2.0

3 years ago

0.1.1

3 years ago

0.0.3-alpha.21

3 years ago

0.1.3

3 years ago

0.0.3-alpha.24

3 years ago

0.0.3-alpha.19

3 years ago

0.0.3-alpha.17

3 years ago

0.0.3-alpha.18

3 years ago

0.0.3-alpha.15

3 years ago

0.0.3-alpha.16

3 years ago

0.0.3-alpha.14

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago

0.0.1-alpha.1

3 years ago

0.0.1-alpha.0

3 years ago