Aspida

1.14.0 · active · verified Wed Apr 22

Aspida is a TypeScript-friendly HTTP client wrapper designed for both browser and Node.js environments. It streamlines API client generation by enabling developers to define API endpoint types through a convention-over-configuration approach, leveraging a directory structure and `DefineMethods` type aliases. This approach significantly enhances type safety for API interactions at compile time, eliminating the need for manual client creation. Aspida supports integration with popular HTTP clients like Axios, Fetch, and Node-Fetch via official adapter packages. The current stable version is 1.14.0, and the project demonstrates an active release cadence with frequent updates. Its key differentiators include robust type inference for paths, query parameters, headers, request bodies, and responses, comprehensive support for `FormData` and `URLSearchParams`, and a unique workflow that generates a fully type-safe client based on a filesystem-defined API structure.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to install Aspida with the Axios adapter, define API endpoints, generate type-safe client code, and make various HTTP requests using the generated client.

import aspida from '@aspida/axios';
import api from '../api/$api'; // This file is generated by 'npm run api:build'

// Ensure this directory structure is created and 'aspida' command is run:
// api/
// ├── v1/
// │   ├── users/
// │   │   ├── index.ts
// │   │   └── _userId@number/
// │   │       └── index.ts
// package.json: { "scripts": { "api:build": "aspida" } }

// Example: api/v1/users/index.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; };
// }>;

(async () => {
  const userId = 0;
  const limit = 10;
  const client = api(aspida()); // Initialize the client with the Axios adapter

  try {
    // Example: POST /v1/users
    const newUser = await client.v1.users.post({ body: { name: 'aspida-user' } });
    console.log('Created user:', newUser.body);

    // Example: GET /v1/users?limit=10
    const users = await client.v1.users.get({ query: { limit } });
    console.log('Users list:', users.body);

    // Example: GET /v1/users/0
    const userById = await client.v1.users._userId(userId).$get();
    console.log('User by ID:', userById.body);

  } catch (error) {
    console.error('API request failed:', error);
  }
})();

view raw JSON →