Aspida
TypeScript friendly HTTP client wrapper for the browser and node.js.
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
1. Export a Type alias named "Methods"
1. Call "aspida" with npm scripts
1. 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:
sh
$ npm install aspida @aspida/axios axios
- Using Yarn:
sh
$ yarn add aspida @aspida/axios axios
Create "api" directory
- ```sh
- $ mkdir api
- ```
Type helper DefineMethods
DefineMethods<> is helper type for defining Methods. In earlier aspida, user shuold define without checking for API definition.
If you already have aspida in your projects, you can easily use DefineMethods<> with surrounding your definition.
It is just an option whether to use DefineMethods<>.
Be aware of the limitation that DefineMethods<> can't detect extra meaningless properties.
Create an endpoint type definition file
- GET: /v1/users/?limit={number}
- POST: /v1/users
api/v1/users/index.ts
ts
import type { DefineMethods } from "aspida";
type User = {
id: number;
name: string;
};
export type Methods = DefineMethods<{
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"
ts
import type { DefineMethods } from "aspida";
type User = {
id: number;
name: string;
};
export type Methods = DefineMethods<{
get: {
resBody: User;
};
put: {
reqBody: {
name: string;
};
resBody: User;
};
}>;
Build type definition file
package.json
- ```json
- {
- "scripts": {
- "api:build": "aspida"
- }
- }
- ```
- ```sh
- $ npm run api:build
- > api/$api.ts was built successfully.
- ```
Make HTTP request from application
src/index.ts
- ```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
Option | Type | Default | Description |
---|---|---|---|
--config -c | string | "aspida.config.js" | Specify the path to the configuration file. |
--watch -w | Enable watch mode. Regenerate $api.ts according to the increase / decrease of the API endpoint file. | ||
--version -v | Print aspida version. |
Options of aspida.config.js
Option | Type | Default | Description |
---|---|---|---|
-------------------- | ------------------------------------ | ------- | ----------------------------------------------------- |
input | string | "api" | Specifies |
baseURL | string | "" | Specify |
trailingSlash | boolean | false | Append |
outputEachDir | boolean | false | Generate |
outputMode | "all" | "normalOnly" | "aliasOnly" |
Node.js API
- ```ts
- 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
- openapi2aspida - Convert OpenAPI 3.0 and Swagger 2.0 definitions
- aspida-mock - TypeScript friendly RESTful API mock
- frourio - Fast and type-safe full stack framework, for TypeScript TypeScript
- @aspida/react-query - React Query wrapper
- @aspida/swr - SWR (React Hooks) wrapper
- @aspida/swrv - SWRV (Vue Composition API) wrapper
- eslint-plugin-aspida - Support writing aspida api definition