2023.41.0-RELEASE • Published 2 years ago

@hexworks/cobalt-http v2023.41.0-RELEASE

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
2 years ago

HTTP Utility for Cobalt

This library wraps Axios with Functional Programming constructs, and also adds data validation and error handling.

Usage:

📘 Note that this library uses io-ts for data valiation.

First, you have to create an io-ts codec that represents the data that you'll receive:

import * as z from "zod";

const Events = t.array(
    t.strict({
        id: t.number,
        name: t.string,
    })
);

Then you can call the appropriate method with the url and the codec:

const getEvents = get("http://myapi.com/v1/events", Events);

const result = await getEvents();

Note, that get constructs a function that returns a promise, so you can call it multiple times:

const once = await getEvents();
const twice = await getEvents();

Custom Parameters

You can pass custom parameters such as headers or authentication information as well:

get(EVENTS_URL, Events, {
    headers: { hi: "hello" },
});

📘 Take a look at RequestConfig to see the full list of options.