UNPKG

cis-api-tool

Version:

根据 swagger/yapi/apifox 的接口定义生成 TypeScript/JavaScript 的接口类型及其请求函数代码。

173 lines (171 loc) 7.23 kB
/// <reference types="lodash" /> import { OneOrMore } from "./type-CDnRd9vn.mjs"; import { ChangeCase, ExtendedInterface, Interface, Method, PropDefinitions } from "./types-B8Xpfzsb.mjs"; import * as lodash0 from "lodash"; import prettier from "prettier"; import { JSONSchema4, JSONSchema4TypeName } from "json-schema"; //#region src/utils.d.ts /** * @description 抛出错误。 * @param msg 错误信息 */ declare function throwError(...msg: string[]): never; /** * @description 将路径统一为 unix 风格的路径。 * @param path 路径 * @returns unix 风格的路径 */ declare function toUnixPath(path: string): string; /** * @description 获得规范化的相对路径。 * @param from 来源路径 * @param to 去向路径 * @returns 相对路径 */ declare function getNormalizedRelativePath(from: string, to: string): string; /** * @description 原地遍历 JSONSchema。 */ declare function traverseJsonSchema(jsonSchema: JSONSchema4, cb: (jsonSchema: JSONSchema4, currentPath: Array<string | number>) => JSONSchema4, currentPath?: Array<string | number>): JSONSchema4; /** * @description 原地处理 JSONSchema。 * @param jsonSchema 待处理的 JSONSchema * @returns 处理后的 JSONSchema */ declare function processJsonSchema(jsonSchema: JSONSchema4, customTypeMapping: Record<string, JSONSchema4TypeName>): JSONSchema4; /** * @description 获取适用于 JSTT 的 JSONSchema。 * @param jsonSchema 待处理的 JSONSchema * @returns 适用于 JSTT 的 JSONSchema */ declare function jsonSchemaToJSTTJsonSchema(jsonSchema: JSONSchema4, typeName: string): JSONSchema4; /** * @description 将 JSONSchema 字符串转为 JSONSchema 对象。 *jsonSchemaStringToJsonSchema @param str 要转换的 JSONSchema 字符串 * @returns 转换后的 JSONSchema 对象 */ declare function jsonSchemaStringToJsonSchema(str: string, customTypeMapping: Record<string, JSONSchema4TypeName>): JSONSchema4; /** * @description 获得 JSON 数据的 JSONSchema 对象。 * @param json JSON 数据 * @returns JSONSchema 对象 */ declare function jsonToJsonSchema(json: object, customTypeMapping: Record<string, JSONSchema4TypeName>): JSONSchema4; /** * @description 获得 mockjs 模板的 JSONSchema 对象。 * @param template mockjs 模板 * @returns JSONSchema 对象 */ declare function mockjsTemplateToJsonSchema(template: object, customTypeMapping: Record<string, JSONSchema4TypeName>): JSONSchema4; /** * @description 获得属性定义列表的 JSONSchema 对象。 * @param propDefinitions 属性定义列表 * @returns JSONSchema 对象 */ declare function propDefinitionsToJsonSchema(propDefinitions: PropDefinitions, customTypeMapping: Record<string, JSONSchema4TypeName>): JSONSchema4; /** * @description 根据 JSONSchema 对象生产 TypeScript 类型定义。 * @param jsonSchema JSONSchema 对象 * @param typeName 类型名称 * @returns TypeScript 类型定义 */ declare function jsonSchemaToType(jsonSchema: JSONSchema4, typeName: string): Promise<string>; declare function getRequestDataJsonSchema(interfaceInfo: Interface, customTypeMapping: Record<string, JSONSchema4TypeName>): JSONSchema4; /** * @description 获得响应数据 JSONSchema 对象。 * @param interfaceInfo 接口信息 * @param customTypeMapping 自定义类型映射 * @param dataKey 数据键 * @returns 响应数据 JSONSchema 对象 */ declare function getResponseDataJsonSchema(interfaceInfo: Interface, customTypeMapping: Record<string, JSONSchema4TypeName>, dataKey?: OneOrMore<string>): JSONSchema4; /** * @description 获取 JSONSchema 对象的指定路径。 * @param jsonSchema JSONSchema 对象 * @param path 路径 * @returns 指定路径的 JSONSchema 对象 */ declare function reachJsonSchema(jsonSchema: JSONSchema4, path: OneOrMore<string>): JSONSchema4; /** * @description 根据权重排序。 * @param list 列表 * @returns 排序后的列表 */ declare function sortByWeights<T extends { weights: number[]; }>(list: T[]): T[]; /** * @description 判断是否为 GET 类请求。 * @param method 请求方式 * @returns 是否为 GET 类请求 */ declare function isGetLikeMethod(method: Method): boolean; /** * @description 判断是否为 POST 类请求。 * @param method 请求方式 * @returns 是否为 POST 类请求 */ declare function isPostLikeMethod(method: Method): boolean; /** * @description 获取 prettier 配置。 * @param cwd 当前工作目录 * @returns prettier 配置 */ declare function getPrettier(cwd: string): Promise<typeof prettier>; /** * @description 获取 prettier 配置。 * @returns prettier 配置 */ declare function getPrettierOptions(): Promise<prettier.Options>; /** * @description 获取缓存的 prettier 配置。 * @returns prettier 配置 */ declare const getCachedPrettierOptions: typeof getPrettierOptions & lodash0.MemoizedFunction; /** * @description 获取 HTTP 请求。 * @param url 请求 URL * @param query 请求参数 * @returns 请求结果 */ declare function httpGet<T>(url: string, query?: Record<string, any>): Promise<T>; /** * @description 生成请求函数名称 * @param interfaceInfo 接口信息 * @param changeCase 大小写转换函数 * @returns 请求函数名称 */ declare function getRequestFunctionName(interfaceInfo: ExtendedInterface, changeCase: ChangeCase): string; /** * @description 生成请求数据类型名称 * @param interfaceInfo 接口信息 * @param changeCase 大小写转换函数 * @returns 请求数据类型名称 */ declare function getRequestDataTypeName(interfaceInfo: ExtendedInterface, changeCase: ChangeCase): string; /** * @description 生成响应数据类型名称 * @param interfaceInfo 接口信息 * @param changeCase 大小写转换函数 * @returns 响应数据类型名称 */ declare function getReponseDataTypeName(interfaceInfo: ExtendedInterface, changeCase: ChangeCase): string; declare function getOutputFilePath(interfaceInfo: Interface, changeCase: ChangeCase, outputDir?: string): string; declare function transformPaths(pathsArray: string[], outputDir?: string): string[]; /** * 将相对路径转换为 alias 路径 * @param relativePath 相对路径 * @param outputDir 输出目录 * @returns alias 路径 */ declare function getAliasPath(relativePath: string, outputDir?: string): string; /** * 获取规范化的相对路径,支持 alias 路径 * @param from 源文件路径 * @param to 目标文件路径 * @param outputDir 输出目录 * @returns 规范化的路径 */ declare function getNormalizedPathWithAlias(from: string, to: string, outputDir?: string): string; //#endregion export { getAliasPath, getCachedPrettierOptions, getNormalizedPathWithAlias, getNormalizedRelativePath, getOutputFilePath, getPrettier, getPrettierOptions, getReponseDataTypeName, getRequestDataJsonSchema, getRequestDataTypeName, getRequestFunctionName, getResponseDataJsonSchema, httpGet, isGetLikeMethod, isPostLikeMethod, jsonSchemaStringToJsonSchema, jsonSchemaToJSTTJsonSchema, jsonSchemaToType, jsonToJsonSchema, mockjsTemplateToJsonSchema, processJsonSchema, propDefinitionsToJsonSchema, reachJsonSchema, sortByWeights, throwError, toUnixPath, transformPaths, traverseJsonSchema };