"Aspida" is a tool to give types to HTTP clients
Tags: aspida, typescript
Published: 2020-05-01 11:10:57

## What is Aspida?

When you use an HTTP client, you often have a hard time because the type inference in the editor is not enough. If the API reference is changed or the URI is changed, the client has to modify the source code without type completion, which is very inconvenient.

Aspida, which is introduced in this article, is a tool that solves those problems. **aspida allows HTTP clients such as axios and fetch to be given types in TypeScript**. It can be used in a browser or Node.JS.

[GitHub](https://github.com/aspidajs/aspida/tree/master/packages/aspida)

---

## How to install

Aspida can be installed using either `npm or yarn`.

Supported HTTP clients are: axios, ky, ky-universal, fetch, and node-fetch.

In this article, we will use axios as an example to illustrate.

```
$ npm install @aspida/axios axios

$ yarn add @aspida/axios axios
```

---

## Preparation

First, let's create a directory to store the type definition file.

```
$ mkdir apis
```

Next, edit the `package.json`.

```json
{
  "scripts": {
    "api:build": "aspida --build"
  }
}
```

---

## How to use

### Example of GET

Let's use Hello, salut!(https://www.fourtonfish.com/hellosalut/hello/) This is a public API that returns a country's greeting when you send a language code. You can check the API reference on the official page.

First, create an API definition file. The file name should match the API path.

```typescript
// apis/hellosalut/index.ts

type Greeting = {
  code: string
  hello: string
}

export type Methods = {
  get: {
    query: {
      lang: string 
    }

    resBody: Greeting
  }
}
```

Next, execute the following command.

```bash
$ npm run api:build

> aspida@1.0.0 api:build /Users/shindex/js/aspida
> aspida --build

apis/$api.ts was built successfully.
```

Then, the `$api.ts` file will be generated in the `/apis` directory. This is the type definition file.

Now you're ready to use aspida. Let's feel the benefits of type completion.

Here is a sample.

```typescript
// src/index.ts

import aspida from "@aspida/axios"
import api from "../apis/$api"

(async () => {
  const client = api(aspida(undefined, { baseURL:'https://fourtonfish.com' }))
  const res = await client.hellosalut.$get({ query: { lang: 'ja' } })

  console.log('county code: ', res.code) // county code: ja
  console.log(
    'greeting   : ',
    res.hello.startsWith('&#')
      ? String.fromCharCode(
          ...res.hello
            .slice(2, -1)
            .split(';&#')
            .map((n) => +n)
        )
      : res.hello
  ) // greeting: こんにちは
})()
```

The following image shows how type completion works in VSCode.
You can see that the type inference has been done perfectly.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/vptkjkddzx3f79vtpcqo.gif)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/htg2r0cxkkfh8qflc6ym.gif)

Let's install a new module and run the above source code.

```bash
$ npm install ts-node typescript @types/node

$ yarn add ts-node typescript @types/node
```

Next, edit the `package.json`.

```json
{
  "scripts": {
    "api:build": "aspida --build",
    "start": "ts-node src/index.ts"
  }
}
```

Run it!

```bash
$ npm start

> aspida@1.0.0 start /Users/shindex/js/aspida
> ts-node src/index.ts

county code:  ja
greeting   :  こんにちは
```