npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@wx-sab/renkei

v1.2.4

Published

解析swagger转为ts代码,日本动漫《游戏王》中的‘Renkei’,意为连携或协作。

Downloads

122

Readme

@wx-sab/renkei

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

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

Feature

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

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,
    };
  },
});