Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

swagger-api-gen

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

swagger-api-gen

this tool generates fe api code by swagger api

  • 1.2.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

swagger-api-generator

swagger接口前端代码生成命令行工具

生成结果

TS

index.ts

import './indexTypes';
import funcRequest from './request';
import { RequestOptionsInit } from 'umi-request';
const request = funcRequest({ baseURL: '/mock' });
type CustomConfigProps = RequestOptionsInit; // 修改这里为自定义配置支持TS提示

/**
 * 示例接口
 * @param {string | number} id description: id | required: true | type: string
 * @returns name description: 名称 | type: string
 * @returns count description: 值 | type: integer
 * @returns type description: 类型 | type: string
 */
export const myHandsomeApiGET = (
  paramConfig: myHandsomeApiGETProps,
  customConfig: CustomConfigProps = {},
): Promise<myHandsomeApiGETResProps> =>
  request<myHandsomeApiGETResProps>({
    url: '/my/handsome/api',
    method: 'get',
    params: paramConfig,
    ...customConfig,
  });

indexTypes.ts

interface anyFields {
  [key: string]: any;
}
/**
 * 示例接口
 * @param {string | number} id description: id | required: true | type: string
 */
interface myHandsomeApiGETProps {
    id: string | number;
}
/**
* @param {string} name description: 名称 | type: string
* @param {number} count description: 值 | type: integer
* @param {string} type description: 类型 | type: string
*/
interface myHandsomeApiGETResProps {
    name: string;
    count: number;
    type: string;
}

JS

import funcRequest from './request';
const request = funcRequest({ baseURL: '/mock' });

/**
 * 示例接口
 * @param {string | number} id description: id | required: true | type: string
 * @returns name description: 名称 | type: string
 * @returns count description: 值 | type: integer
 * @returns type description: 类型 | type: string
 */
export const myHandsomeApiGET = (
  paramConfig,
  customConfig = {},
) =>
  request({
    url: '/my/handsome/api',
    method: 'get',
    params: paramConfig,
    ...customConfig,
  });

安装

npm install -D swagger-api-gen

用法

命令行

全局安装

api url=http://example.com/v2/api-docs

本地安装

npx api url=http://example.com/v2/api-docs

npm script使用

1.配置package.json

{
    "script": {
        "api": "api url=http://example.com/v2/api-docs"
    }
}

2.运行 npm run apiyarn api

完整示例

api url=http://example.com/v2/api-docs tarDir=./src/api fileName=index fileType=ts template='import request from "./request";' expandParams=true filter=pet client=true mock=true module=true

参数说明

参数必传说明默认示例
urlswagger api地址-url=http://example.com/v2/api-docs
tarDir目标目录当前目录tarDir=./src/api
fileName生成文件名,当module选项为true时不生效swagger-apifileName=index
fileType生成文件类型jststsfileType=js
template顶部自定义的代码段-template='import request from "./request";'
expandParams是否展开传参trueexpandParams=true
filter通过正则匹配接口path来筛选需要生成的接口-filter=pet
client是否生成请求客户端falseclient=true
mock是否生成mock请求falsemock=true
module是否分模块falsemodule=true

注意

使用 git for windows 终端时,参数首位的 / 会被解析为 $GIT_HOME/

解决方案如下

filter=//pet

MSYS_NO_PATHCONV=1 filter=/pet/find

filter=\\/pet/find

Keywords

FAQs

Package last updated on 07 Mar 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc