0.3.5 • Published 2 years ago

axios-io-ts v0.3.5

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

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

0.3.5

2 years ago

0.3.2

2 years ago

0.3.4

2 years ago

0.3.3

2 years ago

0.3.0

3 years ago

0.3.1

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago