English | 简体中文
TypeScript request models for business APIs.
api-datamodel builds stable Services and business APIs on top of Fetch, Axios, UniApp, and other request implementations. Pages depend on business capabilities such as userApi and orderApi, rather than scattered URLs and request details.
- Organize endpoints by business module and manage service URLs and request paths centrally.
- Centralize defaults, authentication, response transformation, loading states, messages, errors, and cancellation.
- Integrate standard Fetch, UniApp, Taro, or a custom request library through adapters.
- Generate types and business APIs with the companion Codegen tool when OpenAPI is available.
Backend API → Service → Business API → Application Code
pnpm add api-datamodel axiosApplications install Axios themselves. If the runtime supports Fetch, you can install only api-datamodel and use the built-in fetchAdapter.
Create a Service:
// src/api/dataModel.ts
import axios from 'axios'
import { createService } from 'api-datamodel'
export const service = createService({
adapter: axios,
baseUrl: '/api',
})Define a business API:
// src/api/user.ts
import { service } from './dataModel'
interface User {
id: number
name: string
}
interface UserQuery {
keyword?: string
page: number
}
export const userApi = service.createApi('user', {
list(query: UserQuery) {
return this.$http.get<User[]>('list', query)
},
})Call it from application code:
const users = await userApi.list({ page: 1 })The request URL is /api/user/list. See the guide for complete coverage of paths, responses, feedback, and extensions.
- Build business APIs with
createService()andservice.createApi(). - Derive service prefixes under the same rules with
service.with(). - Unify the request environment with
defineConfig(), request interceptors, and response transformation. - Manage loading states, errors, and batched messages with global hooks.
- Cancel individual requests and abort all active requests with the standard
AbortSignal. - Upload, download, and extend common request capabilities through
Resource. - Connect different platforms through
fetchAdapter,buildAdapter, or a custom adapter.
Codegen converts Swagger/OpenAPI into TypeScript types and business APIs that continue to use your configured Service:
OpenAPI → API Codegen → TypeScript Types + Business APIs
api-datamodel-codegen sysSee the API Codegen guide for configuration, path mapping, method rules, and type rules. The generated directory is a projection of the OpenAPI document; keep business extensions outside it.
- Introduction
- Quick Start
- API Modeling
- Request Handling
- Request Adapters
- Request Extensions
- API Codegen
- Codegen Configuration
- Backend Conventions
import {
Http,
Resource,
buildAdapter,
createService,
defineConfig,
fetchAdapter,
setRequestHooks,
} from 'api-datamodel'Public API signatures and behavior are documented by responsibility in the relevant guide chapters instead of being duplicated in a separate API reference.