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

egg-router-auth

v1.3.4

Published

egg插件:egg-router-auth

Downloads

3

Readme

egg-router-auth

NPM version build status Test coverage David deps Known Vulnerabilities npm download

安装

npm i egg-router-auth --save

开启插件

// config/plugin.js
exports.auth = {
  enable: true,
  package: 'egg-router-auth',
};

使用场景

  • 在egg项目中验证路由表中是否包含请求的url,如果请求了路由表中未存在的路由,则会提示相应信息

  • 在egg项目中验证在许可的路由中是否存在用户的jwt登录

  • 验证路由的参数

配置

// config/config.default.js
config.auth = {
  enableJwtVerify: false, // true代表启用jwt验证,false代表不启用,默认为true
  jwtExclude: ['/api/login', '/api/public/verification'], // 验证用户登录需要跳过的路由
  errorCode: -2, // 错误的code,
  output: 'apidoc/output', // apidoc输出目录,必选
  template: 'apidoc/template' // apidoc模板,可选
}

配置成功后如果请求 /api/login/note 就会被跳过,如果请求 /api/test 就需要验证jwt是否存在

template 必须包含 api_data.json 文件,否则无法运行,因为 api_data.json 是整个插件的核心文件

如果修改了 template 的配置路径,建议先将 output 所处的文件目录删除后再进行编译操作

文件目录

建议文件目录需按此配置

project
├── app
│   ├── controller
│   │   └── home.js
│   └── router.js
├── apidoc
│   └── output
|       └── api_data.json
│   └── template
|       └── api_data.json
|
|...

在每次监听到 /app/controller 有文件内容发送改变后,会自动生成apidoc文档(存放路径在之前设置的output目录下),以便在中间件中验证参数的正确性

apiParam 使用说明

写法|说明|正确示例|错误示例 -|-|-|- string | 字符串 | xxx | - number | 数字 | 123 | 123xssx boolean | 布尔值 | truefalse | truexsa null | 空值 | null | "null" object | 对象 | {} | [] array | 数组 | [] | {} boolean[] | 只含有布尔类型的数组 | [true, false] | [true, "false"] string[] | 只含有string类型的数组 | ["xxx", "xxx1"] | - number[] | 只含有数值类型的数组 | [1, 3, 2] | [1, 3, "2"] object[] | 只含有对象类型的数组 | [{}, {}] | -

  • 可以使用 | 分割,用于表示一个变量可能有多个参数的情况

  • 可以使用 [] 包裹变量,用于表示变量是可选参数

使用

  // app/controller/home.js
  /**
  * @api {GET} /api/test 普通测试接口
  * @apiParam {string} user 用户名
  */
  async test() {
    const { ctx } = this;
    const res = '测试';
    ctx.body = res;
  }
  
  /**
  * @api {GET} /api/test 多参数类型测试接口
  * @apiParam {string|null} user 用户名
  */
  async test1() {
    const { ctx } = this;
    const res = '测试';
    ctx.body = res;
  }
  
  /**
  * @api {GET} /api/test 可选参数测试接口
  * @apiParam {string} [user] 用户名
  */
  async test2() {
    const { ctx } = this;
    const res = '测试';
    ctx.body = res;
  }
  
  /**
  * @api {GET} /api/test 复杂类型测试接口
  * @apiParam {object} user 用户
  * @apiParam {string} user.name 用户名
  */
  async test3() {
    const { ctx } = this;
    const res = '测试';
    ctx.body = res;
  }

apiParam 的参数类型可不区分大小写

插件会检测 url 为 /api/test 并且 method 为 GET 的请求,验证其参数是否正确

特别说明

  // 如果我们想给user.name.xxx检测string类型
  /**
  * @api {GET} /api/test 复杂类型测试接口
  * @apiParam {object} user 用户
  * @apiParam {object} user.name 用户名
  * @apiParam {string} name.xxx 参数 // 错误示例
  * @apiParam {string} user.name.xxx 参数 // 正确示例
  */

提问交流

请到 egg-router-auth issues 异步交流。

License

MIT