Export Swagger JSON to Typescript interface
Each interface generates a namespace (for grouping and avoiding duplicate names), including Params, Response, and each DTO can generate an independent interface.
| Name | Description | Type | Default |
|---|---|---|---|
| swaggerToTypes.swaggerJsonUrl | Swagger API list | SwaggerJsonUrlItem[] | [] |
| swaggerToTypes.swaggerJsonHeaders | Append request headers (global) | object | {} |
| swaggerToTypes.savePath | .d.ts interface file save path |
string | 'types/swagger-interfaces' |
| swaggerToTypes.showStatusbarItem | Show status bar button | boolean | true |
| swaggerToTypes.compareChanges | Whether to compare changes when updating the interface (no change, no update) | boolean | true |
| swaggerToTypes.reloadWhenSettingsChanged | Reload data when user settings change. (Need to be turned off in some cases of frequent refresh settings) | boolean | true |
| swaggerToTypes.maxReferenceCount | The maximum number of times to reference the same interface | number | 5000 |
| Attribute | Description | Type | Required |
|---|---|---|---|
| title | Project title | string | * |
| url | swagger json url | string | * |
| link | Open external link in browser | string | |
| basePath | basePath | string | |
| headers | Custom request header information (such as authentication Token, etc.) | object |
- Search interface list: alt + shift + F
The following are optional configurations:
Add the @ignore tag to the header comment of the .d.ts file to ignore the current file when updating the local interface with one click.
/**
* @name Example interface
* @path /demo/demo-api
* @method POST
* @update 10/19/2020, 11:22:53 AM
* @ignore
*/Used to customize the output content format in some special scenarios.
| Method name | Parameters | Return value |
|---|---|---|
| namespace | TreeInterface | string |
| params | TreeInterface | string |
| paramsItem | TreeInterfacePropertiesItem, TreeInterface | string |
| response | TreeInterface | string |
| responseItem | TreeInterfacePropertiesItem, TreeInterface | string |
| copyRequest | FileHeaderInfo | string | string[] |
For detailed types, refer to TemplateBaseType
Edit the .vscode/swagger-to-types.template.cjs file.
⚠️ ⚠️ ⚠️ Note: If thepackage.jsonof the project has the"type": "module",field, the template configuration file must have the.cjssuffix, otherwise the plugin will not work properly.
function namespace(params) {
return `${params.groupName.replace(/[\-\n\s\/\\]/g, '_')}_${params.pathName}`
}
module.exports = { namespace }Edit .vscode/swagger-to-types.template.js file
/**
* Capitalize the first letter
* @param {String} str
*/
function toUp(str) {
if (typeof str !== 'string') return ''
return str.slice(0, 1).toUpperCase() + str.slice(1)
}
function paramsItem(item, params) {
// Project title (swaggerToTypes.swaggerJsonUrl[number].title) is demo-1 Ignore custom solutions when
if (params.groupName === 'demo-1') return
return `${toUp(item.name)}${item.required ? ':' : '?:'} ${item.type}`
}
module.exports = { paramsItem }Configure a request function template for quick copying
Edit the .vscode/swagger-to-types.template.js file
If the copyRequest function is exported, this function can be used
Related buttons will appear in the following locations:
- Local interface list operation button
.d.tsfile title bar operation button.d.tsfile code line first text button
Here is an example:
/**
* Request function template
*
* @param {{
* fileName: string
* ext: string
* filePath: string
* name?: string
* namespace?: string
* path?: string
* method?: string
* update?: string
* ignore?: boolean
* savePath?: string
* }} fileInfo
* @returns
*/
function copyRequest(fileInfo) {
return [
`/** ${fileInfo.name} */`,
`export async function unnamed(params?: ${fileInfo.namespace}.Params, options?: RequestOptions) {`,
` return $api`,
` .request<${fileInfo.namespace}.Response>('${fileInfo.path}', params, {`,
` method: ${fileInfo.method},`,
` ...options,`,
` })`,
` .then((res) => res.content || {})`,
`}`,
]
}
module.exports = {
// ...
copyRequest,
}Development preview debugging: Press F5 in vscode.
- Support swagger v2 API
- Support openapi 3.0.0 (new in 1.1.4)
- Please do not directly assign values to the parameters of the template processing function, which may have destructive effects.
