openapi-ts-request
Version:
Swagger2/OpenAPI3/Apifox to TypeScript/JavaScript, request client(support any client), request mock service, enum and enum translation, react-query/vue-query, type field label, JSON Schemas
88 lines (87 loc) • 2.85 kB
TypeScript
import type { GenerateServiceProps } from '../index';
import type { OpenAPIObject } from '../type';
import type { ControllerType, ISchemaItem, ITypeItem, TagAPIDataType } from './type';
export default class ServiceGenerator {
protected apiData: TagAPIDataType;
protected classNameList: ControllerType[];
protected config: GenerateServiceProps;
protected openAPIData: OpenAPIObject;
protected schemaList: ISchemaItem[];
protected interfaceTPConfigs: ITypeItem[];
protected typeModuleMap: Map<string, Set<string>>;
protected schemaKeyToTypeNameMap: Map<string, string>;
constructor(config: GenerateServiceProps, openAPIData: OpenAPIObject);
genFile(): void;
private getInterfaceTPConfigs;
private getServiceTPConfigs;
private genFileFromTemplate;
private getTemplate;
private getFunctionName;
private getType;
private getFunctionParamsTypeName;
private getBodyTP;
private getFileTP;
private resolveFileTP;
private getResponseTP;
/**
* 生成多状态码响应类型定义
* 将 OpenAPI 的 responses 对象转换为 TypeScript 类型定义
* 例如:{ 200: ResponseType, 400: unknown, 404: unknown }
*
* @param responses OpenAPI 响应对象
* @param functionName 函数名称,用于生成主响应类型名称
* @returns 多状态码响应类型定义字符串,如果没有响应则返回 null
*/
private getResponsesType;
/**
* 从响应内容中提取 TypeScript 类型
* 处理不同的媒体类型和 schema 类型
*
* @param response 响应对象
* @param components OpenAPI 组件对象
* @returns TypeScript 类型字符串
*/
private getResponseTypeFromContent;
private getParamsTP;
private resolveObject;
private resolveArray;
private resolveProperties;
private resolveEnumObject;
private resolveAllOfObject;
private getProps;
private resolveParameterRef;
private resolveRefObject;
log(message: string): void;
private generateInfoLog;
/**
* 校验规则
* @param regexp 正则数组
* @param data 数据
*/
private validateRegexp;
/**
*
* @param item 数组数据
* @param reg 规则
*/
private matches;
/**
* 记录类型被某个模块使用
* @param typeName 类型名称
* @param moduleName 模块名称
*/
private markTypeUsedByModule;
/**
* 获取模块需要导入的类型
* @param moduleTypes 模块类型列表
* @param commonTypes 公共类型列表
* @param enumTypes 枚举类型列表
* @returns 导入信息
*/
private getModuleImports;
/**
* 分组类型:按模块、公共类型和枚举
* @returns 分组后的类型
*/
private groupTypesByModule;
}