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

swagger2api-v3

v1.1.12

Published

A command-line tool for generating TypeScript API interfaces from OpenAPI 3.0 documentation

Downloads

176

Readme

Swagger2API-v3

English | 中文

A powerful command-line tool for automatically generating TypeScript or JavaScript interface code from OpenAPI 3.0 documentation.

✨ Features

  • 🚀 Fast Generation - Quickly generate TypeScript interface code from OpenAPI 3.0 JSON or YAML
  • 📁 Smart Grouping - Support automatic file grouping by document tags
  • 📝 Detailed Comments - Automatically generate detailed comments including descriptions, parameters, and return values
  • 🎨 Code Formatting - Support custom formatting commands
  • ⚙️ Environment Adaptation - Automatically detect project environment and generate corresponding configuration files
  • 🔧 CLI Tool - Provide complete command-line tools

📦 Installation

# Global installation
npm install -g swagger2api-v3

# Project dependency
npm install swagger2api-v3

🚀 Quick Start

1. Initialize Configuration File

npx swagger2api-v3 init

2. Configuration File Description

The tool generates a .swagger.config.json configuration file:

{
  "$schema": "./node_modules/swagger2api-v3/dist/.swagger2api.schema.json",
  "input": "https://petstore3.swagger.io/api/v3/openapi.json",
  "output": "./src/api",
  "importTemplate": "import { request } from '@/utils/request';",
  "generator": "typescript",
  "requestStyle": "generic",
  "groupByTags": true,
  "multiTagStrategy": "first",
  "overwrite": true,
  "prefix": "",
  "lint": "prettier --write",
  "methodNameIgnorePrefix": [],
  "addMethodSuffix": true,
  "headerComment": "",
  "filter": {
    "include": {
      "tags": []
    },
    "exclude": {
      "tags": []
    }
  },
  "options": {
    "addComments": true
  }
}

3. Generate Interface Code

npx swagger2api-v3 generate

⚙️ Configuration Options

| Option | Type | Default | Description | | ------------------------ | --------------------- | -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | $schema | string | - | Local JSON Schema path for editor completion. The default points to node_modules/swagger2api-v3/dist/.swagger2api.schema.json | | input | string | - | OpenAPI 3.0 JSON/YAML file path or URL; local and remote external $ref references are bundled automatically | | output | string | './src/api' | Output directory for generated code | | generator | string | 'typescript' | Code generator type. Supports 'typescript' and 'javascript'. 'javascript' outputs .js files and skips type file generation | | groupByTags | boolean | true | Whether to group files by tags | | multiTagStrategy | 'first' | 'all' | 'first' | Grouping strategy for operations with multiple tags. first uses only the first tag, all combines all tags into one group name | | overwrite | boolean | true | Whether to overwrite existing files | | prefix | string | '' | Common prefix for API paths | | importTemplate | string | - | Import statement template for request function | | requestStyle | 'method' | 'generic' | 'generic' | Request call style: method uses request.get/post, generic uses request({ method }) | | lint | string | - | Code formatting command (optional) | | methodNameIgnorePrefix | string[] | [] | Array of prefixes to ignore when generating method names. For example, ['api', 'auth'] will transform apiGetName to getName and authUserInfo to userInfo | | addMethodSuffix | boolean | true | Whether to add HTTP method suffix to generated function names. true generates userListPost, false generates userList | | headerComment | string | - | Custom header comment for generated types, API, and index files | | filter.include.tags | string[] | [] | Only generate APIs whose tags match this list. Empty means include all | | filter.exclude.tags | string[] | [] | Skip APIs whose tags match this list. Exclude rules take priority over include rules | | options.addComments | boolean | true | Whether to add detailed comments |

📁 Generated File Structure

Grouped by Tags (Recommended)

src/api/
├── types.ts           # Data type definitions (TypeScript mode only)
├── user/              # User-related APIs
│   └── index.ts
├── auth/              # Auth-related APIs
│   └── index.ts
└── index.ts          # Entry file

JavaScript Output

When generator: 'javascript' is set:

  • Outputs .js files (index.js, api.js, user/index.js, etc.)
  • Does not generate a types.ts file
  • Removes TypeScript-specific syntax (types, import type, generics like <T>)

Example generated API function (method style):

export const codeAuth = (data, config) => {
  return request.post({ url: '/api/auth/codeAuth', data, ...config });
};

Example generated API function (generic style):

export const codeAuth = (data, config) => {
  return request({
    url: '/api/auth/codeAuth',
    method: 'POST',
    data,
    ...config
  });
};

Not Grouped

src/api/
├── types.ts       # Data type definitions
├── api.ts         # All API interfaces
└── index.ts       # Entry file

💡 Usage Examples

Generated Type Definitions

// types.ts
export interface LoginDto {
  /** Account */
  account: string;
  /** Password */
  password: string;
}

export interface UserInfo {
  /** User ID */
  id: string;
  /** Username */
  username: string;
}

Generated API Interfaces

// authController/index.ts
import { request } from '@/utils/request';
import type { LoginDto, LoginRespDto } from '../types';

/**
 * Login
 * @param data Login parameters
 * @param config Optional request configuration
 */
export const authControllerLoginPost = (data: LoginDto, config?: any) => {
  return request.post<LoginRespDto>({
    url: '/admin/auth/login',
    data,
    ...config
  });
};

// When requestStyle is set to 'generic':
export const authControllerLoginPost2 = (data: LoginDto, config?: any) => {
  return request<LoginRespDto>({
    url: '/admin/auth/login',
    data,
    method: 'POST',
    ...config
  });
};

🔧 CLI Commands

# Initialize configuration file
npx swagger2api-v3 init [--force]

# Generate interface code
npx swagger2api-v3 generate [--config <path>]

# Validate configuration file
npx swagger2api-v3 validate [--config <path>]

# Show help
npx swagger2api-v3 --help

📝 NPM Scripts

Add to package.json:

{
  "scripts": {
    "api:generate": "swagger2api-v3 generate",
    "api:init": "swagger2api-v3 init",
    "api:validate": "swagger2api-v3 validate"
  }
}

🎨 Code Formatting

Support automatic execution of formatting commands after generation:

{
  "lint": "prettier --write"
}

🤝 Contributing

If you encounter any problems or have suggestions, please feel free to submit an issue on GitHub. Pull Requests are also welcome!

📄 License

MIT License