| name | api-client-generator |
| version | 1.0.0 |
| description | Generates typed API client code from OpenAPI/Swagger specs. Use this when the user asks to create an API client, SDK, or typed HTTP wrapper from an API specification file. |
API Client Generator
Generate a fully typed API client from an OpenAPI specification.
When to Use
- User has an OpenAPI/Swagger spec (JSON or YAML) and wants a typed client
- User asks to "generate an API client", "create an SDK", or "make typed HTTP calls"
- User says "根据 API 文档生成客户端代码", "生成 SDK"
Steps
- Read the OpenAPI spec — Parse the spec file to extract endpoints, methods, request/response types
- Generate type definitions — Create TypeScript interfaces for all request/response schemas
- Generate client class — Create a client class with methods for each endpoint
- Write output files — Write
types.ts and client.ts to the generated/ directory
- Install dependencies — Ensure
axios or fetch wrapper is available
- Verify — Run TypeScript compiler to check generated code compiles
Implementation Notes
- Output directory:
generated/
- Use
axios for HTTP calls
- Generate JSDoc comments from spec descriptions
- Support both JSON and YAML spec formats
- Handle authentication schemes (Bearer, API Key, Basic)
Decision Tree
If spec has auth schemes:
→ Generate auth middleware
→ If Bearer token → Add token refresh support
→ If API Key → Add key injection
If spec has pagination:
→ Generate paginated iterator helpers
If spec has WebSocket endpoints:
→ Generate WebSocket client separately
Error Handling
- If spec is invalid, report validation errors with line numbers
- If spec uses unsupported features (callbacks, links), skip with warning
- If generated code has type errors, attempt to fix and retry once