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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@tzxhy/sw2ts-generate

v0.0.3

Published

@tzxhy/sw2ts-generate

Downloads

5

Readme

@tzxhy/sw2ts-generate

说明

通过swagger.json文件,得到对应的类型定义文件和 api 调用文件。

目前版本支持 swagger v2.0 JSON 格式文件的解析。

安装

# 作为全局依赖
$ npm install -g @tzxhy/sw2ts-generate
# 作为项目dev依赖
$ npm install @tzxhy/sw2ts-generate -D

使用说明

Usage: sw2ts [options] [--] [input_filename]

Options:
    --url <url>                     在线swagger.json地址(不使用 input_filename)
    -o, --out <file>                使用 url 单文件时,可指定输出文件名
    -d, --dir <path>                输出目录。默认src
    -r, --required                  是否默认所有属性均为必选属性

用例

# 单文件,使用url作为输入
$ sw2ts --url https://petstore.swagger.io/v2/swagger.json -o output -r
tree ./src
.
├── service
│   ├── api
│   │   └── output.ts
│   └── request.ts
└── typings
    └── swagger
        └── output.ts

# 单文件,使用本地json文件
$ sw2ts -d src swagger.json
tree ./src
.
├── service
│   ├── api
│   │   └── swagger.ts
│   └── request.ts
└── typings
    └── swagger
        └── swagger.ts

# 指定输入目录
$ sw2ts -d src -r swaggers/
tree ./swaggers
.
├── api1.swagger.json
└── api2.swagger.json

tree ./src
.
├── service
│   ├── api
│   │   ├── api1.swagger.ts
│   │   └── api2.swagger.ts
│   └── request.ts
└── typings
    └── swagger
        ├── api1.swagger.ts
        └── api2.swagger.ts

用例可以放入 package.jsonscripts 中执行。

其中,request.ts 文件为样板代码:

/* eslint-disable @typescript-eslint/no-unused-vars */
import { AxiosRequestConfig } from "axios";

export interface APIGenerate {
    head<T>(url: string, params: any, config?: AxiosRequestConfig): T;
    get<T>(url: string, params: any, config?: AxiosRequestConfig): T;
    patch<T>(url: string, params: any, config?: AxiosRequestConfig): T;
    post<T>(url: string, params: any, config?: AxiosRequestConfig): T;
    put<T>(url: string, params: any, config?: AxiosRequestConfig): T;
    delete<T>(url: string, params: any, config?: AxiosRequestConfig): T;
}

export class API implements APIGenerate {
    head<T>(url: string, params: any, config?: AxiosRequestConfig): T {
        throw new Error("Method not implemented.");
    }
    get<T>(url: string, params: any, config?: AxiosRequestConfig): T {
        throw new Error("Method not implemented.");
    }
    patch<T>(url: string, params: any, config?: AxiosRequestConfig): T {
        throw new Error("Method not implemented.");
    }
    post<T>(url: string, params: any, config?: AxiosRequestConfig): T {
        throw new Error("Method not implemented.");
    }
    put<T>(url: string, params: any, config?: AxiosRequestConfig): T {
        throw new Error("Method not implemented.");
    }
    delete<T>(url: string, params: any, config?: AxiosRequestConfig): T {
        throw new Error("Method not implemented.");
    }
}

export default new API();

自行实现相关方法即可。当存在该文件时,不会覆盖该文件。

bugs or features

请邮件反馈:[email protected]