A tool to help developers in generating code for interfaces, classes, objects, form templates, and more.
MIT License
A tool to help developers in generating code for interfaces, classes, objects, form templates, and more.
English | 简体中文
A versatile tool library for building Web APIs and code templates
$ pnpm i @api-helper/cli
# or
$ npm i @api-helper/cli
# or
$ yarn add @api-helper/cli
$ npx apih -u https://api-documentation.com/demo/swagger-ui.html
# or
$ npx apih -u ./local-openapi.json
Usage: apih [options]
Options:
-u, --url <string> Interface document address [Can read local files when type is 'swagger', here can be a local file path]
-o, --output-path <path> Output path after code generation
--target <string> Generated target code type, default: typescript
--type <string> Interface document type, call the built-in parser according to the document type, default value: 'swagger'
--auth-token <string> Accessing documents may require authentication information, accessed through using token, yapi's validation token
Enter the following command to initialize the configuration file.
$ npx apih init
Command Description
Usage: apih init [options]
Options:
-c, --config-path <path> Custom configuration file path.
$ npx apih
Command Description
Usage: apih [options]
Options:
-c, --config-path <path> Custom configuration file path.
apih.config.ts
import type { Config } from '@api-helper/cli';
import { defineConfig } from '@api-helper/cli';
defineConfig accepts a Config object or Config[]. When you need to generate multiple API files, you can use the array method. The following document is a supplementary explanation of the Config object.
import { defineConfig } from '@api-helper/cli';
export default defineConfig({
// Use group function. After enabling this function, multi-file code will be generated according to the group
group: false,
// Whether to generate only the TypeScript type of interface request data and return data. If yes, the request file and request function will not be generated.
onlyTyping: false,
// The output path after code generation
outputPath: 'src/api/index.ts',
// The target type of the generated code. Default: typescript
target: 'typescript',
// File path of the request function.
requestFunctionFilePath: 'src/api/request.ts',
// Set all fields of the request data as required attributes, default: false
requiredRequestField: false,
// Set all fields of the response data as required attributes, default: true,默认:true
requiredResponseField: true,
// Interface document service configuration
documentServers: [{
// Interface Document address [When the type is 'swagger', you can read local files, here can be a local file path]
url: 'https://api-documentation.com/demo/swagger-ui.html',
// Interface document type, call the built-in parser according to the document type, default value: 'swagger' [Built-in yapi and swagger parser, other document types, add parserPlugins to implement document parsing by yourself]
type: 'swagger',
// The name of the current interface document service. When there is a value, the file output becomes -> path/current name
name: '',
// The key to get the response data, body[dataKey]
dataKey: '',
// Accessing the document may require authentication information, which can be done using HTTP auth type.
auth: {
username: '',
password: '',
},
// Accessing the document may require authentication information, which can be accessed using a token, such as Yapi's authentication token.
authToken: '',
// Request headers when fetching the interface document with AJAX.
headers: {},
// Hook events in the execution process.
events: {
// Event callback when generating interface name, the return value is used as the new InterfaceName, which is used to customize the InterfaceName.
// onRenderInterfaceName(api, options) {},
// Event callback when generating API name, the return value is used as the new RequestFunctionName, which is used to customize the RequestFunctionName.
// onRenderRequestFunctionName(api, options) {},
},
}],
// Parse extension plugins, used for custom parsing
parserPlugins: [],
});
Web services provide custom code generation based on interfaces and provide frontend operation pages to make code generation more convenient.
For Windows environment, please install pnpm first, and then double-click to run the run-web-server.bat file. The following are manual deployment steps.
git clone https://github.com/ztz2/api-helper.git
docker build -t api_helper/server .
docker compose build
docker run -d -p 3210:3210 api_helper/server
docker-compose up -d
pnpm run build
。pnpm run build
。pnpm run start:prod
。Thanks to JetBrains for supporting this project.