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 🙏

© 2026 – Pkg Stats / Ryan Hefner

sw2ts-generate

v0.5.9

Published

sw2ts-generate

Readme

sw2ts-generate


Install

$ npm install -g sw2ts-generate

or, install with --save-dev option to use in npm scripts.

Usage

Usage: sw2ts [options] [--] [filename]

Options:
  -o, --out <file>                output filename.
  -d, --dir <path>                output dirname.
  --url <url>                     input json schema from the url.
  -r, --required                  set property is required.
  -c, --config <file>             set configuration file path.
  -p, --proxy <path>              set proxy path.
  -n, --namespace <items...>      set namespace separated list.
  -m, --mode <mode>               set compilation mode (typescript | javascript).
  • filename should be swagger.json file.

Example

# 解析在线 swagger.json
$ sw2ts -o output --url https://petstore.swagger.io/v2/swagger.json -r

# 解析本地 swagger.json
$ sw2ts -o output -d schema swagger.json

# 通过 -r, 强制指定属性全部必传
$ sw2ts -o output -d schema -r swagger.json

# 通过 -n, 指定命名空间,生成部分API
$ sw2ts -o output --url https://petstore.swagger.io/v2/swagger.json -r -n /pet /user/login

Usage Tips

Use sw2ts in your script.

Code Sample:

import * as ts from 'typescript';

import * as sw2dts from '../index';

// @ts-ignore
const callback  = (data: any) => sw2dts.default()(data);

// @ts-ignore
const compilerOptions = { module: ts.ModuleKind.ES2015 };

// @ts-ignore
function fecth (url: string, code: string) {
	sw2dts.setConfSw2ts({
		proxy: 'api',
		file: '',
		namespace: []
	});

	if (code === 'typescript') {
		// generate typescript code
		sw2dts.readSchemaFromUrl(url).then(r => sw2dts.Generate2API(r, 'mock'));
	} else {
		sw2dts.readSchemaFromUrl(url).then(callback).then(r => {
			// generate javascript code
			sw2dts.createTransfer({ compilerOptions })(r.paths, 'release/index.js');
		});
	}
}

// ts-node --files  .\src\tests\index.ts
fecth('https://petstore.swagger.io/v2/swagger.json', 'typescript');

Format

prettierprettier-eslint

// 可以通过重写 .eslintrc.js  .prettierrc.js 实现自定义格式化效果
module.exports = {
	'singleQuote': true,
	'printWidth': 120,
	'prettier.tabWidth': 2,
	'prettier.semi': true,
	'proseWrap': 'never',
	'prettier.useTabs': false,
	'parser': 'babel-ts',
	'prettier.bracketSpacing': true,
}

Notes

/**
 * http://10.4.196.168:31621/v2/api-docs
 * path: /pet/{state.petId}
 * 本插件,示例如下:
 * 即将 body 中的参数,置于 payload 字段下,便于使用;
 * 由此可能导致请求参数命名不能出现 payload 名称,请注意;
 */
export function postPetPetId<T = any> (
	state: {
		payload?: FormData;
		petId: number;
	},
	config?: RequestConfig
) {
	return API.post<T>(`/api/pet/${state.petId}`, state.payload, config);
}

/**
 * 键名包含 id 的属性,通常是 number 类型,
 * 不好设置有效的默认值,故本插件统一重置为可选属性
 * 插件生成示例代码如下
 */
declare interface Order {
	id?: number; // int64
	petId?: number; // int64
	quantity: number; // int32
	shipDate: string; // date-time
	/* Order Status */
	status: OrderStatus;
	complete: boolean;
}

How to build

npm run build