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
- 🎉️ Supports openapi2, openapi3.0, openapi3.1, yapi interface documents, and custom interface document parsing
- 💪 Built with Typescript
- ⚙️ Unit testing
- 💻 Local deployment
$ 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.jsonUsage: 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 tokenEnter the following command to initialize the configuration file.
$ npx apih initCommand Description
Usage: apih init [options]
Options:
-c, --config-path <path> Custom configuration file path.- Open the apih.config.js or apih.config.ts file for configuration.
- After completing the configuration, enter the following command to generate the API.
$ 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({
// 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',
// Use category output. After this function is enabled, multi-file code is output based on interface classification
outputByCategory: false,
// The target type of the generated code. Default: typescript
target: 'typescript',
// File path of the request function.
requestFunctionFilePath: 'src/api/request.ts',
// Custom type conversion
// Example: { long: 'number' } — converts the 'long' type to 'number'
transformType: Record<string, string | ((type: string, format?: string | 'int32' | 'int64' | 'float' | 'double' | 'byte' | 'binary' | 'date' | 'date-time' | 'password', emptyType?: string) => string)>,
// 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,
// Generate an interface that contains only these categories
includeCategory: [],
// Exclude interfaces for these categories
excludeCategory: [],
// Generate specified interface
// Example:['/v1/login', ['/v1/login', 'get'], ['/v1/login', '(get|post|put)'], ['/v1/login', '*']],The * in the example represents all the request modes
// More syntax reference for matching rules:https://github.com/micromatch/micromatch
includeAPI: [],
// Exclude specified interface
excludeAPI: [],
// 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.
- The application scenario of WEB services is repetitive form or table pages, generating unified code templates according to the interface.
- The WEB service function is essentially a product of the B/S architecture and needs to be deployed locally.
- The browser-side code is under the web package.
- The server-side code is under the server package.
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.gitdocker build -t api_helper/server .docker compose builddocker run -d -p 3210:3210 api_helper/serverdocker-compose up -d- Build the front-end service by navigating to the web package and entering the command:
pnpm run build。 - Build the back-end service by navigating to the server package and entering the command:
pnpm run build。 - Start the service by navigating to the server package and entering the command:
pnpm run start:prod。 - Access address after the service starts:http://localhost:3210。
Thanks to JetBrains for supporting this project.