axios-io-ts
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

axios-io-ts

Axios with io-ts validation.

ci status codecov status npm version npm version

Table of Contents

Getting started

NPM install

npm install axios-io-ts

Features

Requests

HTTP request functions can be imported individually e.g.

import { httpPost } from "axios-io-ts"

const promise = httpPost({
    url: "/test",
    data: {
        foo: "bar",
    },
})

as part of the default export e.g.

import axios from "axios-io-ts"

const promise = axios.get({
    url: "/test",
    data: {
        foo: "bar",
    },
})

or, with the client factory e.g.

import { httpClient } from "axios-io-ts"

const client = httpClient({ baseURL: "baseURL" }) // OR axios.create({ baseURL: "baseURL" })
const promise = client.post({
    url: "/test",
    data: {
        foo: "bar",
    },
})

⇧ back to top

Data validation

Axios response data can be validated by providing an io-ts decoder to your request

import { httpGet } from "axios-io-ts"
import * as t from "io-ts"

const promise = httpGet({
    url: "/test",
    decoder: t.type({
        foo: t.string,
    }),
}).then((response) => response.data.foo) // strongly typed foo

⇧ back to top

Error handling

If data validation fails, a DecodeError is thrown. You can catch this with onDecodeError()

import { httpGet, onDecodeError } from "axios-io-ts"
import * as t from "io-ts"

const promise = httpGet({
    url: "/test",
    decoder: t.type({
        foo: t.string,
    }),
})
    .then((response) => response.data.foo)
    .catch(onDecodeError((err) => handle(err)))
    .catch((other) => null)

⇧ back to top

Readme

Keywords

Package Sidebar

Install

npm i axios-io-ts

Weekly Downloads

7

Version

1.0.1

License

ISC

Unpacked Size

27.4 kB

Total Files

26

Last publish

Collaborators

  • johnyherangi