initapi v0.6.0
Features
- Code as Document
- Standardized, unified team style, easy to maintain
- Tool generates code to reduce coding and improve efficiency
- If the backend uses Swagger or Openapi3 specifications, it can be accessed at zero cost
Install
pnpm i initapi -D
Usage
configure
api.config.ts
orapi.config.js
import { defineConfig } from "initapi";
// you can use the `defineConfig` helper which should provide intellisense without the need for jsdoc annotations:
export default defineConfig({
// Path related configurations are all based on the working directory of the nodejs process
// Supports Openapi3 and Swagger specifications, with content created based on specified JSON
service: {
pets: {
url: "https://petstore.swagger.io/v2/swagger.json",
},
platform: {
url: "http://127.0.0.1:3000/api-json",
commonPrefix: "/api/v1",
},
// Can convert local JSON
local: {
url: "./assets/openapi.json",
// API address public prefix, used to generate class names and file names. When not configured, it will automatically attempt to find the public prefix
commonPrefix: "/mg/api",
},
},
outputDir: "./src/api",
outputType: "TypeScript",
// ...
});
// package.json
"scripts": {
"api": "initapi create"
}
pnpm run api
Generate API Workflows Example
Generate API content example
Generate Type content example
Configure
Detailed configuration items for defineConfig
Name | Description | Type | Required | Default |
---|---|---|---|---|
importRequest | Import axios or axios encapsulation | string | No | 'import axios from "axios";' |
useRequest | Using axios | string | No | 'axios.request' |
service | The JSON (swagger/openapi3 specification) file address corresponding to the API service | object | Yes | β |
outputDir | Output file storage location | string | No | './api' |
outputType | Output File Type - When there is no configuration, a command line interaction will pop up for selection | TypeScript or JavaScript | No | β |
definition | How to define types | class or interface | No | interface |
indexable | The advantage of using index signature is that it can add any number of attributes, making interface or class more flexible; The disadvantage is that it may cause uncertainty in the value type of the attribute | boolean | No | false |
enumMode | How to define enumeration (enum: generate enumeration type. type: generate type alias.) | enum or type | No | type |
multipleFiles | Enable multi file mode - single file only creates API files. Multiple files will split the API into all controllers and generate corresponding files | boolean | No | true |
Prettier
Before creating a file, by default, the data will be formatted using a prettier
When the project root directory has a. prettier file, the default configuration will be used for file formatting. If obtaining the file fails, the built-in prettier will be used to format the default configuration:
const defaultOptions: prettier.Options = {
parser: "typescript",
printWidth: 130,
tabWidth: 2,
useTabs: false,
semi: true,
singleQuote: false,
quoteProps: "as-needed",
jsxSingleQuote: false,
trailingComma: "all",
bracketSpacing: true,
bracketSameLine: true,
jsxBracketSameLine: true,
arrowParens: "avoid",
rangeStart: 0,
rangeEnd: Infinity,
requirePragma: false,
insertPragma: false,
proseWrap: "preserve",
htmlWhitespaceSensitivity: "ignore",
endOfLine: "auto",
}
Git commit
- π test: Adding missing tests
- πΈ feat: A new feature
- π fix: A bug fix
- π€ chore: Build process or auxiliary tool changes
- βοΈ docs: Documentation only changes
- π‘ refactor: A code change that neither fixes a bug or adds a feature
- π style: Markup, white-space, formatting, missing semi-colons...
- π‘ ci: CI related changes
- β‘οΈ perf: A code change that improves performance
Last
Welcome to provide feedback and contribute code.
License
MIT
4 months ago
5 months ago
6 months ago
11 months ago
11 months ago
10 months ago
10 months ago
12 months ago
10 months ago
12 months ago
12 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago