1.2.2 • Published 30 days ago

@wx-sab/renkei v1.2.2

Weekly downloads
-
License
ISC
Repository
-
Last release
30 days ago

@wx-sab/renkei

解析 swagger 转为 ts 代码,生成可立即使用的 ts 代码,无需手动书写接口定义

日本动漫《游戏王》中的‘Renkei’,意为连携或协作。

Feature

  • 支持 swagger 解析
  • 支持 apifox 导出 swagger 解析
  • 支持多项目/多服务配置
  • 支持远程 apifox 远程 Mock
  • 多服务的 apifox 项目解析
  • 支持 swagger 3.0 解析
  • 支持自定义生成接口名
  • 支持只生成部分服务接口

Introduce

Install

项目发布在私有的 npm源,故需要更改 npm 源地址为:http://10.10.200.19:4873/(临时的)

  npm install @wx-sab/renkei --registry http://10.10.200.19:4873

注:可以使用 nrm 管理 npm 源

Configuration

import { GenerateApiParams, RawRouteInfo } from "swagger-typescript-api";

/**
 * 基础配置
 */
export type BasicConfig = {
  /**
   * 内置接口文档来源
   * swagger: `http://10.101.16.30:31809/${server}/v2/api-docs`
   * apifox: `http://127.0.0.1:4523/export/openapi?projectId=${projectId}&version=2.0`
   */
  sourceType?: "swagger" | "apifox";
  /**
   * 自定义接口文档来源,与 sourceType 互斥,优先级比 sourceType 高
   */
  source?: string;
  /**
   * 声明文件输出目录, 默认 src/services
   */
  output?: string;
  /**
   * 配置文件url, 默认读取项目根目录下的 renkei.config.js 或者 renkei.config.ts
   */
  configUrl?: string;
  /**
   * 数据模型的命名空间
   */
  modelNamespace?: string;
  /**
   * 接口服务名
   */
  serviceName: string;
  /**
   * 可以生成多个项目,会继承父配置项
   */
  projects?: GenerateConfig[];
  /**
   * 自定义 request 引入模板
   * 默认:import request from '${pkg.name}/lib/request'
   */
  requestTempalte?: string;
  /**
   * 自定义响应类型,默认为 AxiosResponse<T>
   */
  customResponseType?(type: string): string;
  /**
   * 自定义组装请求的url
   * 自定义组装 mock 的url
   */
  customRequestUrl?(routeData: RawRouteInfo): string | {
    /**
     * 接口请求路径
     */
    path: string;
    /**
     * mock请求完整 url
     */
    mockUrl?: string
  };
}
/**
 * 如果 sourceType 是 apifox,则适用该配置项
 */
export type ApifoxConfig = {
  /**
   * apifox 项目ID
   */
  apifoxProjectId?: string;
  /**
   * 自定义 mock 服务地址
   */
  customMockService?(config: GenerateConfig): string
};

/**
 * 配置项
 */
export type GenerateConfig = Partial<GenerateApiParams> &
  BasicConfig & ApifoxConfig;

Useage

import { defineConfig } from "@wx-sab/renkei";

export default defineConfig([
  // apifox 单服务项目
  {
    // 服务名
    serviceName: "dp-order",
    // 项目ID
    apifoxProjectId: "2963311",
  },
  // apifox 多服务项目
  {
    // 服务名
    serviceName: "dp-service",
    // 项目ID
    apifoxProjectId: "2954843",
    // 自定义 request 引入
    requestTempalte: `import { request } from '@/utils/http'`,
    // 自定义组装请求url
    customRequestUrl: (routeData) => {
      return (
        "/" +
        // @ts-ignore
        routeData["x-apifox-folder"].split("/")[0] +
        // @ts-ignore
        routeData.route
      );
    },
  },
  // swagger项目
  {
    sourceType: 'swagger',
    serviceName: 'dp-product',
  },
  // 自定义 swagger 源
  {
    source: 'https://swagger.com/xxx/xxx',
    serviceName: 'custom-swagger'
  }
]);

Examples

自定义接口的 mock 服务

import { request } from '@/utils/request'
import { DpService } from './services/dp-service'

const mockProxy = {
  "/dp-order": {
    mockService: "https://mock.apifox.cn/m1/2954843-0-6f0df579",
  },
  "/dp-product": {
    mockService: "https://mock.apifox.cn/m1/2954843-0-4d5b1ae0",
  },
};

generateApis({
  // 服务名
  serviceName: "dp-service",
  // 项目ID
  apifoxProjectId: "2954843",
  // 自定义 request 引入
  requestTempalte: `import request from "../../core/request";`,
  // 自定义组装请求url
  customRequestUrl: (routeData) => {
    const prefix = "/" + routeData["x-apifox-folder"].split("/")[0];
    const path = prefix + routeData.route;

    return {
      path,
      // @ts-ignore
      mockUrl: mockProxy[prefix]?.mockService ? mockProxy[prefix]?.mockService + routeData.route : undefined,
    };
  },
});
1.2.2

30 days ago

1.2.1

3 months ago

1.2.0

4 months ago

1.1.0

4 months ago

1.0.2

5 months ago

1.0.1

6 months ago

1.0.0

6 months ago

1.0.0-alpha.0

6 months ago

0.2.9

7 months ago

0.2.8

8 months ago

0.2.7

9 months ago

0.2.6

9 months ago