You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
190 lines
9.7 KiB
190 lines
9.7 KiB
|
4 years ago
|
"use strict";
|
||
|
|
var __extends = (this && this.__extends) || (function () {
|
||
|
|
var extendStatics = function (d, b) {
|
||
|
|
extendStatics = Object.setPrototypeOf ||
|
||
|
|
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
||
|
|
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
||
|
|
return extendStatics(d, b);
|
||
|
|
};
|
||
|
|
return function (d, b) {
|
||
|
|
extendStatics(d, b);
|
||
|
|
function __() { this.constructor = d; }
|
||
|
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||
|
|
};
|
||
|
|
})();
|
||
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
|
var pont_engine_1 = require("pont-engine");
|
||
|
|
/**
|
||
|
|
* 生成代码
|
||
|
|
*/
|
||
|
|
var MyGenerator = /** @class */ (function (_super) {
|
||
|
|
__extends(MyGenerator, _super);
|
||
|
|
function MyGenerator() {
|
||
|
|
return _super !== null && _super.apply(this, arguments) || this;
|
||
|
|
}
|
||
|
|
/**
|
||
|
|
* 类型声明 api.d.ts
|
||
|
|
*/
|
||
|
|
/** 获取公共的类型定义代码 */
|
||
|
|
// getCommonDeclaration() {
|
||
|
|
// return `
|
||
|
|
// import { RequestOptionsInit } from 'umi-request';
|
||
|
|
// `;
|
||
|
|
// }
|
||
|
|
/** 获取所有基类的类型定义代码,一个 namespace */
|
||
|
|
MyGenerator.prototype.getBaseClassesInDeclaration = function () {
|
||
|
|
var _this = this;
|
||
|
|
var content = "namespace " + (this.dataSource.name || 'defs') + " {\n " + this.dataSource.baseClasses
|
||
|
|
.map(function (base) { return "\n export " + _this.getBaseClassInDeclaration(base) + "\n "; })
|
||
|
|
.join('\n') + "\n }\n ";
|
||
|
|
return content;
|
||
|
|
};
|
||
|
|
/** 获取接口内容的类型定义代码 */
|
||
|
|
MyGenerator.prototype.getInterfaceContentInDeclaration = function (inter) {
|
||
|
|
// 请求体(Body)类型参数列表
|
||
|
|
var bodyParams = inter.parameters.filter(function (param) { return param.in === 'body'; });
|
||
|
|
// 路径参数(PathVariable)类型参数列表
|
||
|
|
var pathParams = inter.parameters.filter(function (param) { return param.in === 'path'; });
|
||
|
|
// 查询字符串(QueryString)类型参数列表
|
||
|
|
var queryParams = inter.parameters.filter(function (param) { return param.in === 'query'; });
|
||
|
|
// 获取请求体类型参数类代码
|
||
|
|
var bodyParamsCode = inter.getBodyParamsCode();
|
||
|
|
// 获取查询字符串参数类代码
|
||
|
|
var paramsCode = inter.getParamsCode();
|
||
|
|
// 获取路径参数类代码
|
||
|
|
var pathParamsCode = this.getPathParamsCodeInDeclaration(pathParams);
|
||
|
|
// 接口方法参数列表
|
||
|
|
var requestArgs = [];
|
||
|
|
pathParams.length > 0 && requestArgs.push("pathParams: PathParams");
|
||
|
|
queryParams.length > 0 && requestArgs.push("params: Params");
|
||
|
|
bodyParams.length > 0 && requestArgs.push("bodyParams:" + (bodyParams.length > 0 ? bodyParamsCode : ''));
|
||
|
|
requestArgs.push('options?: WechatMiniprogram.RequestOption');
|
||
|
|
var requestParams = requestArgs.join(', ');
|
||
|
|
// 返回数据类型
|
||
|
|
var responseType = inter.responseType;
|
||
|
|
return "\n " + (queryParams.length > 0 ? paramsCode : '') + "\n " + (pathParams.length > 0 ? pathParamsCode : '') + "\n\n export type ResponseType = Promise<" + responseType + ">\n\n export const URL:string;\n\n export function request(" + requestParams + "): ResponseType;\n ";
|
||
|
|
};
|
||
|
|
/**
|
||
|
|
* 全局入口文件 index.ts
|
||
|
|
*/
|
||
|
|
MyGenerator.prototype.getIndex = function () {
|
||
|
|
// let conclusion = `
|
||
|
|
// export * from './mods/';
|
||
|
|
// `;
|
||
|
|
// dataSource name means multiple dataSource
|
||
|
|
// if (this.dataSource.name) {
|
||
|
|
// conclusion = `
|
||
|
|
// export { ${this.dataSource.name} } from './mods/';
|
||
|
|
// `;
|
||
|
|
// }
|
||
|
|
return "\n import * as defs from './baseClass';\n import API from './mods/index';\n\n export default {\n defs: defs,\n API: API\n }\n ";
|
||
|
|
};
|
||
|
|
/**
|
||
|
|
* 模块入口文件 /mods/index.ts
|
||
|
|
*/
|
||
|
|
MyGenerator.prototype.getModsIndex = function () {
|
||
|
|
var conclusion = "\n export default {\n " + this.dataSource.mods.map(function (mod) { return mod.name; }).join(', \n') + "\n };\n ";
|
||
|
|
// let conclusion = `
|
||
|
|
// export const API = {
|
||
|
|
// ${this.dataSource.mods.map(mod => mod.name).join(', \n')}
|
||
|
|
// };
|
||
|
|
// `;
|
||
|
|
// dataSource name means multiple dataSource
|
||
|
|
// if (this.dataSource.name) {
|
||
|
|
// conclusion = `
|
||
|
|
// export const ${this.dataSource.name} = {
|
||
|
|
// ${this.dataSource.mods.map(mod => mod.name).join(', \n')}
|
||
|
|
// };
|
||
|
|
// `;
|
||
|
|
// }
|
||
|
|
return "\n " + this.dataSource.mods
|
||
|
|
.map(function (mod) {
|
||
|
|
return "import * as " + mod.name + " from './" + mod.name + "/index';";
|
||
|
|
})
|
||
|
|
.join('\n') + "\n " + conclusion + "\n ";
|
||
|
|
};
|
||
|
|
// getModIndex(mod: Mod) {
|
||
|
|
// return `/**
|
||
|
|
// * @description ${mod.description}
|
||
|
|
// */
|
||
|
|
// ${ (mod.interfaces || []).map((inter) => {
|
||
|
|
// return `import * as ${ inter.name } from './${ inter.name }/index';`
|
||
|
|
// }) }
|
||
|
|
// export {
|
||
|
|
// ${
|
||
|
|
// (mod.interfaces || []).map((inter) => {
|
||
|
|
// return inter.name
|
||
|
|
// }).join(',')
|
||
|
|
// }
|
||
|
|
// };
|
||
|
|
// `;
|
||
|
|
// }
|
||
|
|
/**
|
||
|
|
* 接口实现 /mods/<module_name>/<method_name>.ts
|
||
|
|
*/
|
||
|
|
/** 获取接口实现内容的代码 */
|
||
|
|
MyGenerator.prototype.getInterfaceContent = function (inter) {
|
||
|
|
// 接口请求类型
|
||
|
|
var method = inter.method.toUpperCase();
|
||
|
|
// 请求体(Body)类型参数列表
|
||
|
|
var bodyParams = inter.parameters.filter(function (param) { return param.in === 'body'; });
|
||
|
|
// 路径参数(PathVariable)类型参数列表
|
||
|
|
var pathParams = inter.parameters.filter(function (param) { return param.in === 'path'; });
|
||
|
|
// 查询字符串(QueryString)类型参数列表
|
||
|
|
var queryParams = inter.parameters.filter(function (param) { return param.in === 'query'; });
|
||
|
|
// 获取请求体类型参数类代码
|
||
|
|
var bodyParamsCode = inter.getBodyParamsCode();
|
||
|
|
// 获取查询字符串参数类代码
|
||
|
|
var paramsCode = this.getParamsCode(queryParams);
|
||
|
|
// 获取路径参数类代码
|
||
|
|
var pathParamsCode = this.getPathParamsCode(pathParams);
|
||
|
|
// 返回数据类型
|
||
|
|
var responseType = inter.responseType;
|
||
|
|
// 请求的 MIME 类型
|
||
|
|
var contentType = inter.consumes && inter.consumes.length ? inter.consumes[0] : 'application/json';
|
||
|
|
// 接口方法参数列表
|
||
|
|
var requestArgs = [];
|
||
|
|
pathParams.length > 0 && requestArgs.push("pathParams: PathParams");
|
||
|
|
queryParams.length > 0 && requestArgs.push("params: Params");
|
||
|
|
bodyParams.length > 0 && requestArgs.push("bodyParams:" + (bodyParams.length > 0 ? bodyParamsCode : ''));
|
||
|
|
requestArgs.push('options?: WechatMiniprogram.RequestOption');
|
||
|
|
var requestParams = requestArgs.join(', ');
|
||
|
|
// 数据源前缀
|
||
|
|
var dataSourcePrefix = '';
|
||
|
|
if (this.dataSource.name) {
|
||
|
|
dataSourcePrefix = "/" + this.dataSource.name;
|
||
|
|
}
|
||
|
|
var pathParamsHandleCode = '';
|
||
|
|
if (pathParams.length > 0) {
|
||
|
|
pathParamsHandleCode = "\n for (let key in pathParams) {\n url.replace('{' + key + '}', pathParams[key]);\n }\n ";
|
||
|
|
}
|
||
|
|
return "\n import { fetch } from '../../../utils/util';\n\n " + (pathParams.length > 0 ? "export " + pathParamsCode : '') + "\n " + (queryParams.length > 0 ? "export " + paramsCode : '') + "\n\n export type ResponseType = Promise<" + responseType + ">;\n\n export const URL = '" + dataSourcePrefix + inter.path + "';\n\n /**\n * @desc " + inter.description + "\n */\n export function request (" + requestParams + "):ResponseType {\n let url = '" + dataSourcePrefix + inter.path + "';\n\n " + pathParamsHandleCode + "\n\n const fetchOption = Object.assign ({\n url: url,\n method: '" + method + "',\n headers: {\n 'Content-Type': '" + contentType + "'\n },\n " + (queryParams.length > 0 ? 'params: params,' : '') + "\n " + (bodyParamsCode ? 'data: bodyParams' : '') + "\n },\n options)\n return fetch(fetchOption);\n }\n ";
|
||
|
|
};
|
||
|
|
// 生成参数类(Params)
|
||
|
|
MyGenerator.prototype.getParamsCode = function (params) {
|
||
|
|
if (!Array.isArray(params) || params.length === 0)
|
||
|
|
return '';
|
||
|
|
return "\n export class Params {\n " + params.map(function (param) {
|
||
|
|
return "" + param.toPropertyCode().replace(":", "?:");
|
||
|
|
}).join('\n') + "\n }\n ";
|
||
|
|
};
|
||
|
|
// 生成路径参数类(PathParams)
|
||
|
|
MyGenerator.prototype.getPathParamsCode = function (pathParams) {
|
||
|
|
if (!Array.isArray(pathParams) || pathParams.length === 0)
|
||
|
|
return '';
|
||
|
|
return "\n class PathParams {\n " + pathParams.map(function (param) {
|
||
|
|
return param.toPropertyCodeWithInitValue("") + ";";
|
||
|
|
}).join('\n') + "\n\n [key:string]: any\n }\n ";
|
||
|
|
};
|
||
|
|
// 生成路径参数类(PathParams)
|
||
|
|
MyGenerator.prototype.getPathParamsCodeInDeclaration = function (pathParams) {
|
||
|
|
if (!Array.isArray(pathParams) || pathParams.length === 0)
|
||
|
|
return '';
|
||
|
|
return "\n class PathParams {\n " + pathParams.map(function (param) {
|
||
|
|
return "" + param.toPropertyCode(pont_engine_1.Surrounding.typeScript, param.required);
|
||
|
|
}).join('\n') + "\n\n [key:string]: any\n }\n ";
|
||
|
|
};
|
||
|
|
return MyGenerator;
|
||
|
|
}(pont_engine_1.CodeGenerator));
|
||
|
|
exports.default = MyGenerator;
|