一、安装
-
安装插件
vscode
-
创建项目
PS E:\Code\Vues> mkdir apricot-pont
1、创建目录
-
全局安装
pont-engine
$ npm i -g pont-engine
1、安装
pont-engine
二、使用
-
配置模板
$ pont start
1、配置模板
-
安装依赖
$ npm i -D pont-engine
1、安装依赖
-
文件生成
$ pont check
1、生成文件
-
目录结构
三、自定义模板
-
参数地址
/api/apricot/{id}
import { Interface, BaseClass, Property, CodeGenerator, Surrounding } from 'pont-engine'; export default class MyGenerator extends CodeGenerator { getInterfaceContentInDeclaration(inter: Interface) { const requestParams = inter.getRequestParams(); const paramsCode = inter.getParamsCode('Params'); //自定义参数 let params = `params:${JSON.stringify(requestParams).replace(/"/g, '')}`; // 重组url参数 if (inter.parameters[0] && inter.parameters[0].in.indexOf('path') > -1) { params = `${inter.parameters[0].name}:${inter.parameters[0].dataType.typeName || "any"}`; } return ` export ${paramsCode} export type Response = ${inter.responseType} export const init: Response; export function request(${params}): Promise<Response>; `; } getBaseClassInDeclaration(base: BaseClass) { const originProps = base.properties; base.properties = base.properties.map(prop => { return new Property({ ...prop, required: false }); }); const result = super.getBaseClassInDeclaration(base); base.properties = originProps; return result; } getInterfaceContent(inter: Interface) { const method = inter.method.toUpperCase(); const requestParams = inter.getRequestParams(this.surrounding); const paramsCode = inter.getParamsCode('Params', this.surrounding); //自定义参数 let paramsName = "params"; let path = `"${inter.path}"` let params = `${JSON.stringify(requestParams).replace(/"/g, '')}` // 重组url参数 if (inter.parameters[0] && inter.parameters[0].in.indexOf('path') > -1) { paramsName = "{}"; params = `${inter.parameters[0].name}:${inter.parameters[0].dataType.typeName || "any"}` path = '`(0)${(1)}`'.replace('(0)', inter.path.replace(`{${inter.parameters[0].name}}`, '')).replace('(1)', inter.parameters[0].name) } return ` /** * @desc ${inter.description} */ import * as defs from '../../baseClass'; import { PontCore } from '../../pontCore'; export ${paramsCode} export const init = ${inter.response.getInitialValue()}; export function request(${params}) { return PontCore.fetch(PontCore.getUrl(${path}, ${paramsName}, "${inter.method}"), ${inter.getRequestContent()}); } `; } }
1、参数地址请求