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

swagger-ts-define

v1.0.3

Published

swagger-ts-define

Downloads

7

Readme

Swagger TypeScript 类型生成

目前只支持 swagger3.0.0

安装

npm i swagger-ts-define -D

or

yarn add swagger-ts-define -D

配置

根目录创建 swagger-ts-define.config.js,进行配置

const config = {
  /** swagger json schema 地址 */
  docUrl: 'http://example/docs-json',
  /** 类型输出目录,路径相对于根目录 */
  outputDir: 'api',
  /**
   * 对 api url 进行处理,该名称将会作为 interface 的名称前缀
   * @example /web/v1/user/list -> user/list -> interface UserListGetQuery {...}
   */
  getPath: (path) => path.match(/(?<=\/v1\/).+/)[0],
  /** api 调用方法方法来源,将会插入到文件首部 */
  requestFrom: 'import { request } from "src/utils/request";',
  /**
   * 针对 iso8601 时间格式字符串的自定义类型
   * @description 如果在 http 请求返回数据时统一做时间数据转换,比如统一转换为 Date 或者 Moment,那么可以在这里设置对应的类型
   */
  isoString: {
    typeName: 'string', // 'Moment'
    import: '', // 'import { Moment } from 'moment'
  },
};

module.exports = config;

运行

# 项目根目录执行
npx swagger-ts-define

类型说明

类型命名规则

url + method + (query/param/body/response) 得到字符串后进行驼峰命名处理

  • url 可通过配置文件中 getPath 方法进行处理,保留需要的信息
  • method 为请求方式,get/post/delete...
  • query 为 url 参数,例:/users?id=xxx
  • param 为 path 参数,例:/users/{id}
  • body 为请求体参数
  • response 为返回值
  1. 生成的类型分为四大类:UrlGetQuery,UrlGetParam,UrlPostBody,UrlGetResponse

  2. 可自行封装通用 http 请求函数来使用以上生成的类型,在配置文件中配置 requestFrom 即可自动生成接口调用文件

    request(config: { query?: UrlGetQuery, param?: UrlGetParam, body?: UrlPostBody, url: string, method: string, ... }): Promise<UrlGetResponse>

例子

  1. GET /v1/users/{role}?name=xx
export interface V1UsersByRoleGetParam {
  // ...
}

export interface V1UsersByRoleGetQuery {
  // ...
}

export interface V1UsersByRoleGetResponse {
  // ...
}