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

swg-router

v1.0.1

Published

load router and create swagger docs

Downloads

5

Readme

api写法

router/action.js


const {inpath, inquery, inform, resbody} = require('swg-router/swg-parameter')

module.exports = [{
    tags: ["活动"],
    summary: "添加活动记录",
    description: "添加活动记录",
    parameters: [
      inquery('type', '类型', true),
      inform('activity_id', '活动id', true),
    ],
    swagOps: {
      consumes: ['text/xml'], produces: ['text/xml'],
      responses: {
        200: resbody({
          return_code: '返回状态码',
          return_msg: '返回信息 (错误原因)',
        }, 'xml'),
      },
    },
    method: 'post',
    path: '/actions',
    handler: async(ctx)=>{
      return({docs: []})
    }
  }]

inpath: 地址栏的参数,第一个参数是字段名,第二个参数是描述,inpath默认required是true

inheader: header参数,第一个参数是字段名,第二个参数是描述,参数第三个是required,第四个为options参数。默认是字符串类型

inquery: query参数,第一个参数是字段名,第二个参数是描述,参数第三个是required,第四个为options参数。默认是字符串类型

inform: form参数,第一个参数是字段名,第二个参数是描述,参数第三个是required,第四个为options参数。默认是字符串类型

inbody: body参数,第一个参数是结构对象,第二个参数是类型,默认json格式,也可以传xml。

inbody的data按需要传入的参数格式设定。例如:

  inbody({
     field_name1: '描述内容',
     grade: {$type: 'integer', $desc: '年级'},
     addresses: [{phone: "手机号", road: "xx大道84号"}],
  })

swagOps: swagger的其他参数。按swagger文档进行设置。

resbody: 第一个参数是结构对象,第二个参数是类型,默认json格式,也可以传xml。

router/index.js

const Router = require('koa-router')
const swgApi = require('swg-router')

const router = new Router()

swgApi.Router(router, __dirname, {prefix: '', frame: 'koa'})

module.exports = router

swgApi.Router(router, __dirname, {prefix: '', frame: 'koa'}) 会加载当前目录和子目录中所有的js结尾的文件,并绑定里面的handler。

swagger 文档加载用法


const Router = require('koa-router')
const swgApi = require('swg-router')
const path = require('path')
const router = new Router({prefix: '/swagger'})

let routerPaths = [
  {path: path.resolve(__dirname, '../internal'), prefix: ''},
  {path: path.resolve(__dirname, '../mb/v1'), prefix: ''},
]

let swagOpts = {
  frame: 'koa', //如果是express,不传此字段
  title: 'api接口', 
  version: '3.0.0',
  description: '描述',
  routerPrefix: router.opts.prefix,
  routerPaths,
}

router.get('/', swgApi.Swagger(swagOpts))


module.exports = router

配置静态文件

const swgApi = require('swg-router')
swagOpts = {
  favicon: '图标url',
  swgUICss: '静态样式 url',
  bundleJS: 'js1 url',
  standaloneJS: 'js2 url',
}
swgApi.Swagger(swagOpts)

在public中存放了静态文件,可以放到自己的资源服务器上,然后更改配置。