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-extra

v1.0.9

Published

基于swagger.yaml的nodejs接口映射工具

Downloads

7

Readme

swagger-extra

基于swagger.yaml的nodejs接口映射工具,应用于KOA框架中。

相比于直接使用swagger的优点

  • 根据yaml,自动生成接口文件xx.js
  • 根据yaml,自动生成接口的路由
  • 根据yaml名称生成访问swagger的路由

只需要编写swagger,接口就能可用,与创建controller等烦躁的工作说再见。

使用

  • 第一步:准备swaggerxx.yaml
# ...
paths:
  /common/login:
    post:
      tags:
        - tagTest
      summary: 登录
      description: code必选;encryptedData,iv授权之后传
      parameters:
        - name: code
          type: string
          in: formData
          required: true
        - name: encryptedData
          type: string
          in: formData
        - name: iv
          type: string
          in: formData
      responses:
        200:
          description: successful
          schema:
            type: object
# ...
  • 第二步:创建SwaggerExtra实例
let SwaggerExtra = require('swagger-extra'),
    swaggerExtra = new SwaggerExtra(
        path.join(__dirname, 'config', 'controller'),
        path.join(__dirname, 'controller'),
        {
            //是否生成swagger相关路由,默认为是
            isAutoFixControllersOpen: true, 
            //是否将接口自动写入接口文件xx.js,默认为是
            isSwaggerOpen: true,
            //获取post参数对象的代码,默认为:ctx.request.body
            postBodyGetCode: 'ctx.request.body',
            //获取get参数对象的代码,默认为:ctx.param
            getParamsGetCode: 'ctx.params',
            //返回数据格式,示例:ctx.body = {code: 200, 'message': 'success', data: {}}
            controllerReturnCode: 'ctx.body = {code: 200,};'
        },
    );
  • 第三步:使用routes
koa.use(swaggerExtra.getRoutes());
  • 第四步,运行koa工程

此时可以通过ip:port/swagger文件名来访问swagger并可以通过swagger来访问接口。

注意

  • isAutoFixControllersOpen 为false时,不会自动创建controller文件
  • isSwaggerOpen 为false时,swagger不可访问。