2.5.1 • Published 1 month ago

hapic v2.5.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month 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.1

1 month ago

2.5.0

4 months ago

2.4.0

5 months ago

2.3.0

8 months ago

2.0.0-alpha.11

11 months ago

2.2.0

10 months ago

2.0.0-alpha.10

12 months ago

2.0.0

11 months ago

2.0.0-alpha.7

1 year ago

2.0.0-alpha.8

1 year ago

2.0.0-alpha.9

1 year ago

2.0.0-alpha.3

1 year ago

2.0.0-alpha.4

1 year ago

2.0.0-alpha.5

1 year ago

2.0.0-alpha.6

1 year ago

1.2.0

1 year ago

1.6.0

1 year ago

1.4.0

1 year ago

1.3.0

1 year ago

1.2.1

1 year ago

2.0.0-alpha.0

1 year ago

2.0.0-alpha.1

1 year ago

2.0.0-alpha.2

1 year ago

1.1.0

1 year ago

1.0.0

1 year ago

0.1.0

2 years ago

0.0.3-alpha.22

2 years ago

0.0.3-alpha.23

2 years ago

0.3.0

1 year ago

0.1.2

2 years ago

0.2.0

1 year ago

0.1.1

2 years ago

0.0.3-alpha.21

2 years ago

0.1.3

1 year ago

0.0.3-alpha.24

2 years ago

0.0.3-alpha.19

2 years ago

0.0.3-alpha.17

2 years ago

0.0.3-alpha.18

2 years ago

0.0.3-alpha.15

2 years ago

0.0.3-alpha.16

2 years ago

0.0.3-alpha.14

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago

0.0.1-alpha.1

2 years ago

0.0.1-alpha.0

2 years ago