hapic

A http api client based on axios.

MIT 53 个版本
安装
npm install hapic
yarn add hapic
pnpm add hapic
bun add hapic
README

HAPIC 🚀

npm version main Known Vulnerabilities Conventional Commits

"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 {
    /**
     * globalThis.RequestInit
     *
     * A Headers object, an object literal, or an array of two-item arrays to set request's headers.
     */
    headers: {
        'Authorization': 'Bearer foo'
    },
    /**
     * globalThis.RequestInit
     *
     * A string indicating whether credentials will be sent with the request always, never,
     * or only when sent to a same-origin URL. Sets request's credentials.
     */
    credentials: 'include',
    /**
     * A string to set request's method.
     */
    method: 'GET',
    /**
     * The base URL for the HTTP endpoints.
     */
    baseURL: 'https://example.com/',
    /**
     * The request body.
     */
    body: {
        foo: 'bar'
    },
    /**
     * The desired response type (json, .
     *
     * default: CONTENT_TYPE header
     */
    responseType: 'json',
    /**
     * A function or array of functions to transform the response data.
     */
    responseTransform: [
        (data) => {
            // transform the data
            return data;
        }
    ],
    /**
     * Query string parameters for the request.
     */
    params: {
        id: 123
    },
    /**
     * Activate or deactivate the use of proxies or enter customized options.
     * By default, https_proxy, http_proxy, HTTPS_PROXY, and HTTP_PROXY environment variables will be checked and used
     *
     * default: true (disable with false)
     */
    proxy: {
        url: 'http://localhost:9080', // overrite env variables
        noProxy: '.foo.bar'
    },
    /**
     * Query string parameters for the request.
     */
    query: {
        id: 123
    },
    /**
     * A function or array of functions to transform the request data.
     */
    transform: [
        (data) => {
            // transform the 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);
// [{ 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' }]

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) => {
    // Do something with the request options before the request is sent
    options.transform = (data, headers) => {
        headers.set(HeaderName.AUTHORIZATION, 'Bearer foo');

        return data;
    };

    return options;
});

client.on('requestError', (error: ClientError) => {
    // Do something with the request error
    throw error;
})

Response Hooks

import client, { ClientError, Response } from 'hapic';

client.on('response', (res: Response) => {
    // Any status code that is not in the range 400-599 triggers this function.
    // Do something with the response data

    return res;
})

import client, { ClientError, Response } from 'hapic';

client.on('responseError', (error: ClientError) => {
    // Any status code that is in the range 400-599 triggers this function.
    // Do something with the response error

    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.

版本列表
3.0.1 2026-06-21
3.0.0 2026-06-20
2.8.2 2026-02-25
2.8.1 2026-01-05
2.8.0 2025-06-30
2.7.0 2025-06-26
2.6.0 2025-05-13
2.5.2 2024-11-18
2.5.1 2024-03-27
2.5.0 2024-01-09
2.4.0 2023-12-11
2.3.0 2023-08-31
2.2.0 2023-07-11
2.0.0-alpha.11 2023-06-03
2.0.0-alpha.10 2023-05-26
2.0.0-alpha.9 2023-04-21
2.0.0-alpha.8 2023-04-18
2.0.0-alpha.7 2023-04-18
2.0.0-alpha.6 2023-04-18
2.0.0-alpha.5 2023-04-18
2.0.0-alpha.4 2023-04-18
2.0.0-alpha.3 2023-04-16
2.0.0-alpha.2 2023-04-08
2.0.0-alpha.1 2023-04-08
2.0.0-alpha.0 2023-04-07
2.0.0 2023-06-12
1.6.0 2023-04-03
1.4.0 2023-03-22
1.3.0 2023-03-17
1.2.1 2023-02-28
1.2.0 2023-02-15
1.1.0 2023-01-27
1.0.0 2022-12-31
0.3.0 2022-12-16
0.2.0 2022-11-23
0.1.3 2022-11-16
0.1.2 2022-10-26
0.1.1 2022-10-26
0.1.0 2022-10-15
0.0.3-alpha.24 2022-10-14
0.0.3-alpha.23 2022-10-13
0.0.3-alpha.22 2022-10-13
0.0.3-alpha.21 2022-10-13
0.0.3-alpha.19 2022-09-30
0.0.3-alpha.18 2022-09-30
0.0.3-alpha.17 2022-09-30
0.0.3-alpha.16 2022-09-30
0.0.3-alpha.15 2022-09-30
0.0.3-alpha.14 2022-09-13
0.0.2 2022-08-08
0.0.1-alpha.1 2022-08-04
0.0.1-alpha.0 2022-08-04
0.0.1 2022-08-08