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-api-gen

v1.2.2

Published

this tool generates fe api code by swagger api

Downloads

7

Readme

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

参数说明

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

注意

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

解决方案如下

filter=//pet

MSYS_NO_PATHCONV=1 filter=/pet/find

filter=\\/pet/find