UNPKG

auto-request

Version:

通过Yapi JSON Schema生成接口Axios或Taro接口

207 lines (188 loc) 5.27 kB
# 简介 通过`yapi`的 swagger json schema 生成 Taro 或 Axios 接口 非`yapi`的 swagger 可能会出现不兼容 ## 配置 scripts ```json "scripts": { "build:api": "node ./scripts/buildApi.js" }, ``` ## 脚本 ```javascript // apiPath(必填) api生成文件 // filename(必填) swagger schema 文件 // snapshotsPath(建议填写) = 增删文件日志,针对最终生成代码 // isTypeScript 是否ts模式 // loggerPath = 检查schema数据日志文件位置,针对schema文件 // logs 日志文件路径 // loggerFileName = 检查schema数据日志日志文件名 import { autoRequest } from 'auto-request'; import path from 'path'; import allApi from './../example/all-api.json'; const apiPath = path.join(__dirname, './../api/'); const logs = path.join(__dirname, './../logs/'); autoRequest(JSON.stringify(allApi) as any, apiPath, { filename: 'index', isTypeScript: false, jsdoc: { enable: true, }, snapshotsPath: './snapshots.md' // renderMethodCall() { // // 下面是默认的实现 // const args = renderMethodArgs([ // this.getMethodPrePath(), // this.getMethodPreParams(), // this.renderGetMethodData(), // this.getMethodOption(), // ]); // const requestArgs = renderMethodArgs([ // `url: \`${this.getUrl()}\``, // `method: \'${this.method}\'`, // `${this.renderOptionsStr.data}`, // `${this.renderOptionsStr.params}`, // `...options`, // ]); // return ` // /*** // * @description ${this.description} // **/ // export const ${this.getMethodsName()} = ( ${args} ) => { // return axios.request({${requestArgs}}) // }\n // `; // }, // renderHeaderTemplate: (instance) => { // // 下面是默认的实现 // return ` import axios from 'axios'; // }, // renderEndTemplate: (instance) => { // // 下面是默认的实现 // return `` // } // renderMethodJsDocCall: (instance) =>{ // // 下面是默认的实现 // const args = renderMethodArgs([ // instance.getMethodPrePath(), // instance.getJsDocMethodPreParams(), // instance.getJsDocMethodOption(), // ]); // const preInterface = renderMethodArgs(wapperMethodPreInteface(instance)); // const response = 'S'; // return ` // export type ${instance.getMethodsName()} = <${preInterface}>(${args})=> Promise<${response}>\n // `; // } }).then(({ write, json }: any) => { write(); }).catch((err) => { console.log(err) }); ``` 生成的文件 接口文件: {path}/index.ts 定义文件: {path}/index.define.ts ## 生成目录 ```bash ├── api │   ├── index.define.ts │   └── index.ts ├── assets │   └── api.json |── scripts | └── buildApi.js | ``` ## 使用jsdoc 生成接口文档 ```js /*** * @type {import("./index.types.ts").V7XXXXViewGet} * @description 获取应用权限列表 **/ export const V7XXXXViewGet = ( app_id, params, options = {} ) => { return axios.request({ baseURL: "/", url: `/v7/xxxx/xxx/${app_id}/xxxxx`, method: "get", params, ...options, }) } ``` ## 生成文件 ```ts import axios, { value AxiosRequestConfig, value AxiosResponse } from 'axios'; /*** * @description 获取实例的告警历史 **/ export const AlertalertHistoryGet = < S = AxiosResponse<AlertalertHistoryGetResponse> >( options: AxiosRequestConfig = {} ): Promise<S> => { return axios.request({ url: `/alert/alert_history`, method: 'get', ...options, }); }; ``` ```ts // index.define.ts * tslint:disable */ /** * This file was automatically generated by json-schema-to-typescript. * DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file, * and run json-schema-to-typescript to regenerate this file. */ export interface AlertalertInstanceGetRequsetParams { /** * page */ page: number; /** * page_size */ page_size: number; /** * 1.开始时间升序;2.告警次数升序;5.开始时间倒序;6.告警次数倒序 */ order: number; /** * (多个)严重程度 */ severity_list?: string; /** * (多个)firing:正在告警,normal:正常 用逗号间隔 */ status_list: string; /** * (多个)团队id,不传查全部 */ team_ids?: number; /** * (多个)负责人id,不查传全部 */ duty_user_list?: number; /** * (多个)规则来源模块,不查传全部 */ from_sre_list?: string; /** * 分组条件(单个): 1.Sre模块 2.规则 3.负责人 4.团队 5.业务实例 */ group_by?: number; /** * (多个)处理状态。如果不传,默认不包含失效的。0.未处理1.已确认2.维护3.失效 */ handle_status_list?: number; } export interface AlertalertInstanceGetResponse { groups?: AlertalertInstanceGetResponseGroups[]; page?: AlertalertInstanceGetResponsePage; } ```