GithubHelp home page GithubHelp logo

aspida's Introduction

🇺🇸English | 🇯🇵日本語

aspida

aspida

npm version CircleCI Codecov Language grade: JavaScript Dependabot Status License

Type safe HTTP client for the browser and node.js.

vscode

Fetures

  • CLI that generates a TS file that can define types for path, URL query, header, body, and response.
  • The return value is an axios response object
  • baseURL and common header can be set with axios

Procedure

  1. Reproduce the endpoint directory structure in the apis directory
  2. Export Methods interface with TS file
  3. Call 'aspida --build' with npm scripts
  4. API type definition file 'apis/$api.ts' will be generated, so import the application and make an HTTP request

Getting Started

Installation

  • Using npm:

    $ npm install axios
    $ npm install aspida --save-dev
  • Using Yarn:

    $ yarn add axios
    $ yarn add aspida --dev

Create apis directory

$ mkdir apis

Create an endpoint type definition file

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

  • POST: /v1/users

    apis/v1/users/index.ts

    interface User {
      id: number
      name: string
    }
    
    export interface Methods {
      get: {
        params?: {
          limit: number
        }
    
        response: User[]
      }
    
      post: {
        data: {
          name: string
        }
    
        response: User
      }
    }
  • GET: /v1/users/${userId}

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

    apis/v1/users/[email protected]

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

    interface User {
      id: number
      name: string
    }
    
    export interface Methods {
      get: {
        response: User
      }
    
      put: {
        data: {
          name: string
        }
    
        response: User
      }
    }

Build type definition file

package.json

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

> apis/$api.ts was built successfully.

Make HTTP request from application

src/index.ts

import api from "../apis/$api"
;(async () => {
  const userId = 0
  const limit = 10

  await api().v1.users.post({ name: "mario" })

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

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

Examples

See examples for source code.

Tips

Set baseURL

src/index.ts

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

axios.defaults.baseURL = "http://localhost:8080"
;(async () => {
  const limit = 10

  await api().v1.users.post({ name: "mario" })

  const res = await api().v1.users.$get({ params: { limit } })
  console.log(res)
  // req -> GET: http://localhost:8080/v1/users/?limit=10
  // res -> [{ id: 0, name: "mario" }]
})()

Request with token added to common header

src/index.ts

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

axios.defaults.headers.common["X-Auth-Token"] = "YOUR TOKEN"
;(async () => {
  const userId = 0
  const limit = 10

  await api().v1.users.post({ name: "mario" })

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

Request using axios Instance

src/index.ts

import axios from "axios"
import api from "../apis/$api"
;(async () => {
  const limit = 10

  // using axios instance
  const client = axios.create({ baseURL: "http://localhost:10000" })
  const $api = api(client)

  await $api.v1.users.post({ name: "mario" })

  const res = await $api.v1.users.$get({ params: { limit } })
  console.log(res)
  // req -> GET: http://localhost:10000/v1/users/?limit=10
  // res -> [{ id: 0, name: "mario" }]
})()

Contribution

Build

npm install
npm run build
node ./bin/index.js --build

if you want to watch file changes and rebuild automatically, you can use --watch instead of --build

License

Aspida is licensed under a MIT License.

aspida's People

Contributors

dependabot-preview[bot] avatar solufa avatar tooppoo avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.