@aircan/swapper
v1.0.2
Published
A CLI tool for generating TypeScript API functions and type definitions from Swagger/OpenAPI documents.
Readme
swapper
A CLI tool for generating TypeScript API functions and type definitions from Swagger/OpenAPI documents.
By default, the generated output is written into two files inside the target directory:
types.ts: type definitionsindex.ts: API request functions
Requirements
- Node.js
>= 18
Installation
For local development:
pnpm install
pnpm linkInstall it globally with:
npm install -g @aircan/swapperThen run the CLI with:
swapper --helpYou can also run the published package directly with:
npx @aircan/swapper --helpInstall the built-in skill for Codex or Claude Code:
swapper install-skillThe command opens an interactive selector so you can choose Codex or Claude Code with arrow keys.
When the skill is used by an agent, the expected execution path is to try the globally installed swapper command first. If availability is unclear, verify with swapper --help before falling back to a repo-local entrypoint.
Usage
swapper -u <swagger-url> -t <tags> -d <output-dir> -r <request-import> [options]An explicit subcommand form is also supported:
swapper generate -u <swagger-url> -t <tags> -d <output-dir> -r <request-import> [options]Basic Examples
Generate by controller:
swapper \
-u https://swagger-page.com/promotion/api/v2/api-docs \
--tag activity \
--dir ./api \
--prefix /promotion/api \
-r "import request from '@/utils/request';"Generate specific endpoints:
swapper \
-u https://swagger-page.com/promotion/api/v2/api-docs \
--tag GET-/calcProcessConfig,POST-/calcProcessConfig \
--dir ./api \
--prefix /promotion/api \
-r "import request from '@/utils/request';"Generate a mixed selection:
swapper \
-u https://petstore.swagger.io/v2/swagger.json \
--tag DyLkProductMapping,POST-/dyLkProductMapping \
--dir ./src/services \
-r "import { request } from 'umi';"Options
| Option | Short | Required | Default | Description |
| --- | --- | --- | --- | --- |
| --url | -u | Yes | None | Swagger document URL |
| --tag | -t | Yes | None | Interfaces to generate. Supports controller names or METHOD-/path, separated by commas |
| --dir | -d | Yes | None | Output directory |
| --request | -r | Yes | None | Import statement for the request function |
| --out-type | | No | ts | Output file type. Currently supports ts and js |
| --prefix | -p | No | None | Prefix appended to generated request URLs |
| --force | | No | false | Fully overwrite output files instead of incremental merge |
Install the Skill
Install the bundled generate-swagger-types skill into Codex or Claude Code on the current machine:
swapper install-skillThe command opens an interactive selector for Codex or Claude Code. If you choose Codex, it installs to:
${CODEX_HOME:-~/.codex}/skills/generate-swagger-typesIf you choose Claude Code, it installs to:
${CLAUDE_CONFIG_DIR:-~/.claude}/skills/generate-swagger-typesTo skip the prompt, pass --agent:
swapper install-skill --agent claude-codeOptional flags:
| Option | Required | Default | Description |
| --- | --- | --- | --- |
| --agent | No | Interactive selector in TTY; codex in non-interactive shells | Target agent. Supports codex, claude-code, and claude |
| --dest | No | Depends on --agent | Custom skill installation root |
| --force | No | true | Overwrite the destination if the skill already exists |
| --no-force | No | false | Fail when the destination skill already exists |
Examples:
swapper install-skill --dest ~/.codex/skills
swapper install-skill --agent claude-code
swapper install-skill --no-forceRestart Codex or Claude Code after installation so the new skill can be loaded.
Generation Behavior
Incremental merge is the default mode:
- Existing
types.tsandindex.tsfiles are read first - Newly generated types are merged into
types.ts - Newly generated functions are merged into
index.ts - Existing types and functions with the same name are replaced by the latest generated versions
When --force is used:
types.tsandindex.tsin the target directory are overwritten directly
Output Example
Running:
swapper \
-u https://swagger-page.com/promotion/api/v2/api-docs \
--tag activity \
--dir ./api \
--prefix /promotion/api \
-r "import request from '@/utils/request';"will generate:
api/
├── index.ts
└── types.tsWhere:
types.tscontains response, request body, and query parameter type definitionsindex.tscontains request functions andimport typestatements
Development
Show help:
node bin/swapper.js --helpRun directly:
node bin/swapper.js \
-u https://swagger-page.com/promotion/api/v2/api-docs \
--tag activity \
--dir ./api \
--prefix /promotion/api \
-r "import request from '@/utils/request';"