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-to-request

v1.1.2

Published

swagger generate request code

Downloads

6

Readme

swagger-to-request

swagger 自动生成 service 相关请求文件

该工具的定位为辅助前端进行接口类型定义

所有的接口类型都会放在 serviceTypes 文件中

使用方式

// package.json文件
  "scripts":{
    "swa":"esno ./src/swagger.ts"
  }

// swagger.ts
  import { generateRequest } from 'swagger-to-request';

  generateRequest({
    //  url:'http://test:8081/test-boot/v2/request.json' json文件也可以
    url:'http://test:8081/test-boot/v2/api-docs' // 文档的请求地址
    output:'./src/service' // 代码会生成在'./src/service/swagger中'
  })

工具使用必传的参数为url,output

文档将会生成在 ${output}/swagger 文件夹中

所有的类型会生成在方法前与 serviceTypes.ts 文件中

  // 默认使用
  import { generateRequest } from 'swagger-to-request';

  generateRequest({
    url:'http://test:8081/test-boot/v2/api-docs' // 文档的请求地址
    output:'./src/service' // 代码会生成在'./src/service/swagger中'
  })

可自定义请求的引用requestStr

默认为 import {request} from '@umijs/max'

generateRequest({
  url: "http://test:8081/test-boot/v2/api-docs", // 文档的请求地址
  output: "./src/service", // 代码会生成在'./src/service/swagger中'
  requestStr: "import axios from 'axios'",
});

可自定义请求体

generateRequest({
  url: "http://test:8081/test-boot/v2/api-docs", // 文档的请求地址
  output: "./src/service", // 代码会生成在'./src/service/swagger中'
  requestStr: "import axios from 'axios'",
  customFunBody: ({
    queryStr, //get请求传参
    bodyStr, // post请求传参类型
    resType, // 数据返回类型
    requestUrl, // 请求地址
    serviceName, // 请求方法名称
    fetchMethod, // 请求类型
  }): string => {
    return `export const ${serviceName} = (${
      queryStr ? "params: " + queryStr : ""
    } ${
      bodyStr ? "data:" + bodyStr : ""
    }) => axios.${fetchMethod}<${resType}>(\`${requestUrl}\`, {${
      queryStr ? " params, " : " "
    }${bodyStr ? "data" : ""} })\n\n`;
  },
});

// 默认生成代码为
import { request } from "@umijs/max";

export const testBootApprovalApprovalByPost = (data: ApprovalDTO) =>
  request < R > (`/crm-boot/approval/approval`, { method: "post", data });

// 自定义配置之后生成代码为
import axios from "axios";

export const crmBootRegionListByPost = (data: RegionSearchDTO) =>
  axios.post < RListRegionVO > (`/test-boot/region/list`, { data });