dm-api-tool
v0.1.9
Published
Generate focused TypeScript API files from Swagger/OpenAPI specs.
Maintainers
Readme
dm-api-tool
Tiny npm CLI for generating focused TypeScript API files from Swagger/OpenAPI.
MVP scope
- Generate one API and its related data types.
- Generate all APIs and their related data types.
- Keep commands short.
Usage
npm i -g dm-api-toolInitialize config in a frontend project:
dm init https://example.com/swagger-ui/index.htmlOr write the built-in default config to the current directory:
dm setGenerate one API:
dm g POST /user/loginIf a path only has one method, the method can be omitted:
dm g /healthGenerate all APIs:
dm allGenerated API functions receive a request method as the first argument, so they can use your project's own HTTP layer instead of fetch:
import { update2 } from "./dm-api/update2";
const result = await update2(request.post, {
id: 1,
roleName: "Admin"
});
const resultWithHeaders = await update2(request.post, body, {
Authorization: "Bearer token"
});The request method only needs to match this shape:
type ApiRequestMethod<TResult = unknown> = (
url: string,
data?: any,
headers?: HeadersInit,
) => Promise<TResult>;Config
dm init creates dm.config.json:
{
"input": "https://example.com/swagger-ui/index.html",
"output": "dm-api",
"headers": {},
"translateNames": true
}input can be an OpenAPI JSON URL, a Swagger UI URL, or a local JSON file. Generated files are written to dm-api by default, relative to the directory where the command is executed. If dm.config.json is missing, the CLI uses its built-in default config.
translateNames defaults to true and translates Chinese API summaries/descriptions into English function and file names. Set it to false if your Swagger descriptions contain sensitive internal data and should not be sent to an online translation service.
Notes
This MVP focuses on JSON OpenAPI/Swagger specs. YAML support, custom request clients, auth helpers, and prettier formatting can be added after the core workflow is stable.
