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

sm2tsservice

v3.4.2

Published

swagger or yapi mock to typescript services and models

Downloads

177

Readme


name: sm2tsservice route: /sm2tsservice

import { Props } from 'docz'; import { IGuardConfig, IYAPIConfig, ISwaggerParser, IJson2Service, IRequest, ISwaggerConfig } from './tests/Doc.tsx';

npm i -D sm2tsservice

根据 swagger 或者 yapi mock json 生成 typescript service 代码


1. CLI

1.1 配置文件

edit json2service.json,也可用 xxx.js,然后配置 -c xxx.js

1.1.1 CLI 配置

1.1.2 swaggerParser

1.1.3 guardConfig

1.2.4 yapiConfig

1.2.4 requestConfig

1.2.5 swaggerConfig

1.2 Example

{
  "url": "./api.json", // 文件路径
  "remoteUrl": "http://**", // url
  "requestConfig": {
    "url": "./api.json" // 文件路径或url
    // 以下所有 request 支持的参数
    // headers?: Headers;
    // baseUrl?: string;
    // callback?: RequestCallback;
    // jar?: CookieJar | boolean;
    // formData?: { [key: string]: any };
    // form?: { [key: string]: any } | string;
    // auth?: AuthOptions;
    // oauth?: OAuthOptions;
    // aws?: AWSOptions;
    // hawk?: HawkOptions;
    // qs?: any;
    // qsStringifyOptions?: any;
    // qsParseOptions?: any;
    // json?: any;
    // jsonReviver?: (key: string, value: any) => any;
    // jsonReplacer?: (key: string, value: any) => any;
    // multipart?: RequestPart[] | Multipart;
    // agent?: http.Agent | https.Agent;
    // agentOptions?: http.AgentOptions | https.AgentOptions;
    // agentClass?: any;
    // forever?: any;
    // host?: string;
    // port?: number;
    // method?: string;
    // body?: any;
    // family?: 4 | 6;
    // followRedirect?: boolean | ((response: http.IncomingMessage) => boolean);
    // followAllRedirects?: boolean;
    // followOriginalHttpMethod?: boolean;
    // maxRedirects?: number;
    // removeRefererHeader?: boolean;
    // encoding?: string | null;
    // pool?: any;
    // timeout?: number;
    // localAddress?: string;
    // proxy?: any;
    // tunnel?: boolean;
    // strictSSL?: boolean;
    // rejectUnauthorized?: boolean;
    // time?: boolean;
    // gzip?: boolean;
    // preambleCRLF?: boolean;
    // postambleCRLF?: boolean;
    // withCredentials?: boolean;
    // key?: Buffer;
    // cert?: Buffer;
    // passphrase?: string;
    // ca?: string | Buffer | string[] | Buffer[];
    // har?: HttpArchiveRequest;
    // useQuerystring?: boolean;
  },
  "type": "yapi",
  "yapiConfig": {
    "required": false,
    "bodyJsonRequired": false,
    "categoryMap": {
      "中文": "English" // yapi 接口分类中英文映射
    }
  },
  "swaggerParser": {
    "-o": "tmp/services"
  },
  "validateResponse": false, // 是否生成校验逻辑
  "guardConfig": {
    // + strict 严格模式
    //    - 校验 swagger tags【yapi 接口分类】是否是纯英文
    //    - 方法名使用 http method + url 驼峰形式
    //    - 新项目采用
    // + safe 安全模式
    //    - 方法名使用 http method + url 驼峰形式
    //    - 老项目升级,不会校验 tags,会生成方法调用替换映射表
    // + 默认
    //    - http method + url => operationId 映射锁定
    //    - 老项目维持现状
    "mode": "strict",
    // swagger 处理重复 operationId 逻辑有风险,因此需要锁定映射关系
    "methodUrl2OperationIdMap": {
      "get /api/xxx/xxx": "operationId"
    }
  }
}

1.3 生成 service 代码

命令行

  ./node_modules/.bin/sm2tsservice # 使用默认配置
  ./node_modules/.bin/sm2tsservice -c config.json # 指定配置文件
  ./node_modules/.bin/sm2tsservice --clear # 清空上次生成产物
  ./node_modules/.bin/sm2tsservice --quiet # 自动全量使用远程文档,不显示 diff & merge 页面
  ./node_modules/.bin/sm2tsservice --models # 仅生成 models
  ./node_modules/.bin/sm2tsservice --models --apis # 仅生成 models & apis
  ./node_modules/.bin/sm2tsservice --typeScriptDataFile TypeSciptData.json # 仅生成类型信息 json 到 TypeSciptData.json

npm run

写入 package.json,通过 npm run api 使用

{
  "scripts": {
    "api": "sm2tsservice --clear"
  }
}

2. ajax 接口

实现 ajax 类【如果使用的 axios,且后端返回数据结构遵循 { code?:number;message?:string;result: any }

详情 tkit-ajax

3. 增量更新

自 @3.1.* 版本起,支持增量更新

流程

  • 配置文件变更
    • url - 必须指向本地 swagger 文件【如文件不存在,在获取 remoteUrl 指定地址内容后,创建该文件
    • remoteUrl - url 地址,即之前的 url
  • 执行 CLI 生成代码
  • CLI 对本地 swagger 文件和远程 swagger 文件进行 DIFF
    • 有差异
      • 打开浏览器,人工选择需要同步的差异,点保存
      • CLI patch delta
    • 无差异
      • 直接下一步
  • CLI 生成 service 代码

4. CHANGELOG

import CHANGELOG from './CHANGELOG.md';