npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@technote-space/aspida

v1.7.4

Published

TypeScript friendly HTTP client wrapper for the browser and node.js

Downloads

1

Readme

aspida

Features

  • Path, URL query, header, body, and response can all specify the type
  • FormData / URLSearchParams content can also specify the type
  • HTTP client supports axios / fetch / node-fetch

Procedure

  1. Reproduce the endpoint directory structure in the "api" directory
  2. Export a Type alias named "Methods"
  3. Call "aspida" with npm scripts
  4. API type definition file "api/$api.ts" will be generated, so import the application and make an HTTP request

Getting Started

Installation (axios ver.)

  • Using npm:

    $ npm install @aspida/axios axios
  • Using Yarn:

    $ yarn add @aspida/axios axios

Create "api" directory

$ mkdir api

Create an endpoint type definition file

  • GET: /v1/users/?limit={number}

  • POST: /v1/users

    api/v1/users/index.ts

    type User = {
      id: number
      name: string
    }
    
    export type Methods = {
      get: {
        query?: {
          limit: number
        }
    
        resBody: User[]
      }
    
      post: {
        reqBody: {
          name: string
        }
    
        resBody: User
        /**
         * reqHeaders(?): ...
         * reqFormat: ...
         * status: ...
         * resHeaders(?): ...
         * polymorph: [...]
         */
      }
    }
  • GET: /v1/users/${userId}

  • PUT: /v1/users/${userId}

    api/v1/users/_userId@number/index.ts

    Specify the type of path variable “userId” starting with underscore with “@number”
    If not specified with @, the default path variable type is "number | string"

    type User = {
      id: number
      name: string
    }
    
    export type Methods = {
      get: {
        resBody: User
      }
    
      put: {
        reqBody: {
          name: string
        }
    
        resBody: User
      }
    }

Build type definition file

package.json

{
  "scripts": {
    "api:build": "aspida"
  }
}
$ npm run api:build

> api/$api.ts was built successfully.

Make HTTP request from application

src/index.ts

import aspida from "@aspida/axios"
import api from "../api/$api"
;(async () => {
  const userId = 0
  const limit = 10
  const client = api(aspida())

  await client.v1.users.post({ body: { name: "taro" } })

  const res = await client.v1.users.get({ query: { limit } })
  console.log(res)
  // req -> GET: /v1/users/?limit=10
  // res -> { status: 200, body: [{ id: 0, name: "taro" }], headers: {...} }

  const user = await client.v1.users._userId(userId).$get()
  console.log(user)
  // req -> GET: /v1/users/0
  // res -> { id: 0, name: "taro" }
})()

aspida official HTTP clients

Command Line Interface Options

Options of aspida.config.js

| Option | Type | Default | Description | | -------------------- | ------------------------------------ | ------------- | ----------------------------------------------------- | | input | string | "api" | Specifies the endpoint type definition root directory | | baseURL | string | "" | Specify baseURL of the request | | trailingSlash | boolean | false | Append / to the request URL | | outputEachDir | boolean | false | Generate $api.ts in each endpoint directory | | outputMode (>=1.6.0) | "all" | "normalOnly" | "aliasOnly" | "all" | Output either get or $get for compression |

Node.js API

import { version, build, watch } from "aspida/dist/commands"

console.log(version()) // 0.x.y

build()
build("./app/aspida.config.js")
build({ input: "api1" })
build([
  { baseURL: "https://example.com/v1" },
  {
    input: "api2",
    baseURL: "https://example.com/v2",
    trailingSlash: true,
    outputEachDir: true,
    outputMode: 'all'
  }
])

watch()
watch("./app/aspida.config.js")
watch({ input: "api1" })
watch([
  { baseURL: "https://example.com/v1" },
  {
    input: "api2",
    baseURL: "https://example.com/v2",
    trailingSlash: true,
    outputEachDir: true,
    outputMode: 'all'
  }
])

Ecosystem

Tips

  1. Change the directory where type definition file is placed to other than "api"
  2. Serialize GET parameters manually
  3. POST with FormData
  4. POST with URLSearchParams
  5. Receive response with ArrayBuffer
  6. Define polymorphic request
  7. Define endpoints that contain special characters
  8. Import only some endpoints
  9. Retrieve endpoint URL string
  10. Reflect TSDoc comments

Change the directory where type definition file is placed to other than "api"

Create a configuration file at the root of the project

aspida.config.js

module.exports = { input: "src" }

Specify baseURL in configuration file

module.exports = { baseURL: "https://example.com/api" }

If you want to define multiple API endpoints, specify them in an array

module.exports = [
  { input: "api1" },
  { input: "api2", baseURL: "https://example.com/api" }
]

Serialize GET parameters manually

aspida leaves GET parameter serialization to standard HTTP client behavior
If you want to serialize manually, you can use config object of HTTP client

src/index.ts

import axios from "axios"
import qs from "qs"
import aspida from "@aspida/axios"
import api from "../api/$api"
;(async () => {
  const client = api(
    aspida(axios, { paramsSerializer: params => qs.stringify(params, { indices: false }) })
  )

  const users = await client.v1.users.$get({
    // config: { paramsSerializer: (params) => qs.stringify(params, { indices: false }) },
    query: { ids: [1, 2, 3] }
  })
  console.log(users)
  // req -> GET: /v1/users/?ids=1&ids=2&ids=3
  // res -> [{ id: 1, name: "taro1" }, { id: 2, name: "taro2" }, { id: 3, name: "taro3" }]
})()

POST with FormData

api/v1/users/index.ts

import type { ReadStream } from 'fs'

export type Methods = {
  post: {
    reqFormat: FormData

    reqBody: {
      name: string
      icon: File | ReadStream
    }

    resBody: {
      id: number
      name: string
    }
  }
}

src/index.ts

import aspida from "@aspida/axios"
import api from "../api/$api"
;(async () => {
  const client = api(aspida())

  const user = await client.v1.users.$post({
    body: {
      name: "taro",
      icon: imageFile
    }
  })
  console.log(user)
  // req -> POST: h/v1/users
  // res -> { id: 0, name: "taro" }
})()

Post in Node.js (>=1.6.0)

src/index.ts

import fs from 'fs'
import aspida from "@aspida/axios"
import api from "../api/$api"
;(async () => {
  const client = api(aspida())
  const fileName = 'images/sample.jpg'
  const user = await client.v1.users.$post({
    body: {
      name: "taro",
      icon: fs.createReadStream(fileName)
    }
  })
  console.log(user)
  // req -> POST: h/v1/users
  // res -> { id: 0, name: "taro" }
})()

POST with URLSearchParams

api/v1/users/index.ts

export type Methods = {
  post: {
    reqFormat: URLSearchParams

    reqBody: {
      name: string
    }

    resBody: {
      id: number
      name: string
    }
  }
}

src/index.ts

import aspida from "@aspida/axios"
import api from "../api/$api"
;(async () => {
  const client = api(aspida())

  const user = await client.v1.users.$post({ body: { name: "taro" } })
  console.log(user)
  // req -> POST: /v1/users
  // res -> { id: 0, name: "taro" }
})()

Receive response with ArrayBuffer

api/v1/users/index.ts

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

    resBody: ArrayBuffer
  }
}

src/index.ts

import aspida from "@aspida/axios"
import api from "../api/$api"
;(async () => {
  const client = api(aspida())

  const user = await client.v1.users.$get({ query: { name: "taro" } })
  console.log(user)
  // req -> GET: /v1/users/?name=taro
  // res -> ArrayBuffer
})()

Define polymorphic request

api/users/index.ts

type User = {
  id: number
  name: string
}

export interface Methods {
  post: {
    // common properties
    reqFormat: FormData
    /**
     * query(?): ...
     * reqHeaders(?): ...
     * reqBody(?): ...
     * status: ...
     * resHeaders(?): ...
     * resBody(?): ...
     */
    polymorph: [
      // polymorphic types
      {
        reqBody: Omit<User, 'id'>
        resBody: User
        /**
         * query(?): ...
         * reqHeaders(?): ...
         * status: ...
         * resHeaders(?): ...
         */
      },
      {
        reqBody: Omit<User, 'id'>[]
        resBody: User[]
      }
    ]
  }
}

src/index.ts

import aspida from "@aspida/axios"
import api from "../api/$api"
;(async () => {
  const client = api(aspida())

  const user = await client.users.$post({ body: { name: "taro" } })
  console.log(user) // { id: 0, name: "taro" }

  const users = await client.users.$post({
    body: [{ name: "hanako" }, { name: "mario" }]
  })
  console.log(users) // [{ id: 1, name: "hanako" }, { id: 2, name: "mario" }]
})()

Define endpoints that contain special characters

Special characters are encoded as percent-encoding in the file name
Example ":" -> "%3A"

api/foo%3Abar/index.ts

export type Methods = {
  get: {
    resBody: string
  }
}

api/users/_userId@number%3Aread/index.ts

export type Methods = {
  get: {
    resBody: User
  }
}

With clients, "%3A" -> ":"

src/index.ts

import aspida from "@aspida/axios"
import api from "../api/$api"
;(async () => {
  const client = api(aspida())

  const message = await client.foo_bar.$get()
  console.log(message)
  // req -> GET: /foo:bar

  const user = await client.users._userId_read(1).$get()
  console.log(user)
  // req -> GET: /users/1:read
})()

Import only some endpoints

If you don't need to use all of api/$api.ts , you can split them up and import only part of them
outputEachDir option generates $api.ts in each endpoint directory
$api.ts will not be generated under the directory containing the path variable

aspida.config.js

module.exports = { outputEachDir: true }

Import only $api.ts of the endpoint you want to use and put it into Object

src/index.ts

import aspida from "@aspida/axios"
import api0 from "../api/v1/foo/$api"
import api1 from "../api/v2/bar/$api"
;(async () => {
  const aspidaClient = aspida()
  const client = {
    foo: api0(aspidaClient),
    bar: api1(aspidaClient)
  }

  const message = await client.bar._id(1).$get()
  // req -> GET: /v2/bar/1
})()

Retrieve endpoint URL string

src/index.ts

import aspida from "@aspida/axios"
import api from "../api/$api"
;(async () => {
  const client = api(aspida())

  console.log(client.v1.users.$path())
  // /v1/users

  console.log(client.vi.users.$path({ query: { limit: 10 } }))
  // /v1/users?limit=10

  console.log(client.vi.users.$path({
    method: 'post',
    query: { id: 1 }
  }))
  // /v1/users?id=1
})()

Reflect TSDoc comments

api/index.ts

/**
 * root comment
 * 
 * @remarks
 * root remarks comment
 */
export type Methods = {
  /**
   * post method comment
   * 
   * @remarks
   * post method remarks comment
   */
  post: {
    /** post query comment */
    query: { limit: number }

    /** post reqHeaders comment */
    reqHeaders: { token: string }

    reqFormat: FormData
    /** post reqBody comment */
    reqBody: UserCreation

    /**
     * post resBody comment1
     * post resBody comment2
     */
    resBody: User
  }
}
$ npm run api:build

api/$api.ts

/**
 * root comment
 * 
 * @remarks
 * root remarks comment
 */
const api = <T>({ baseURL, fetch }: AspidaClient<T>) => {
  return {
    /**
     * post method comment
     * 
     * @remarks
     * post method remarks comment
     * 
     * @param option.query - post query comment
     * @param option.headers - post reqHeaders comment
     * @param option.body - post reqBody comment
     * @returns post resBody comment1
     * post resBody comment2
     */
    $post: (option: { body: Methods0['post']['reqBody'], query: Methods0['post']['query'], headers: Methods0['post']['reqHeaders'], config?: T }) =>
      fetch<Methods0['post']['resBody']>(prefix, PATH0, POST, option).json().then(r => r.body)
  }
}

Support

License

aspida is licensed under a MIT License.