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

@nicecode/mofish

v3.38.0

Published

根据 YApi 的接口定义生成 TypeScript/JavaScript 的接口类型及其请求函数代码。

Downloads

1

Readme

接口代码自动生成

mofish 是一个代码生成工具,其可根据 YApiSwagger 的接口定义生成 TypeScript 或 JavaScript 的接口类型及其请求函数代码。

特性

  • 支持多服务器、多项目、多分类
  • 支持预处理接口信息
  • 可自定义类型或函数名称
  • 完整的注释
  • 支持生成 React Hooks 的请求代码
  • 支持参数路径
  • 支持上传文件
  • 支持生成 JavaScript 代码
  • 支持 Swagger

环境要求

首先得有 Node.js,并确保其版本 >= 10.19.0。同时:

  • 对于基于 YApi 的项目,要求 YApi 的版本必须 >= 1.5.12
  • 对于基于 Swagger 的项目,仅支持 Swagger 2OpenAPI 3

安装

选择你常用的包管理器将 @nicecode/mofish 加入项目依赖即可:

# npm
npm i @nicecode/mofish

# yarn
yarn add @nicecode/mofish

# pnpm
pnpm add @nicecode/mofish

使用

注意在swagger模式下无法分离api文件和按需引入接口,每个项目都在一个接口文件中,yapi模式可以通过 ID 筛选分类

生成配置文件

npx mofish init
// mofish.config.ts
import { defineConfig } from '@nicecode/mofish'

export default defineConfig([
  {
    serverUrl: 'http://localhost:3000', // 接口服务地址,支持swagger
    typesOnly: false,
    target: 'typescript',
    reactHooks: {
      enabled: false,
    },
    devEnvName: 'dev',
    prodEnvName: 'prod',
    outputFilePath: interfaceInfo => `api/${interfaceInfo._category.name}.ts`,   // 接口输出文件路径
    requestFunctionFilePath: 'utils/request.ts',   // 请求函数路径
    dataKey: 'data',
    comment: {
      category: false,
      tag: false,
      requestHeader: false
    },
    projects: [ // 项目列表,对应单个项目swagger文档链接
      {
        token: 'ce517e50453e14f9b00039538235720a23337590f107455eaa6fb65a6bda7f22',  // yapi token,在对应项目中获取,swagger模式下可不传
        categories: [
          {
            id: [272, 308, 425],   // 接口分类id,对应yapi链接:/api/cat_${id}  0为全部
            getRequestFunctionName(interfaceInfo, changeCase) {
              // 以接口全路径生成请求函数名
              return changeCase.camelCase(interfaceInfo.path)
            },
          },
        ],
      },
    ],
  },
])

生成代码

使用以下命令生成代码:

npx mofish

如果要使用自定义的配置文件:

npx mofish -c config/mofish.ts