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

koa-required-decorator

v0.0.2

Published

Check Koa Context parameters exist or not.

Downloads

3

Readme

koa-required-decorator

一个校验 koa context 特定参数是否存在类方法装饰器 - Check Koa Context parameters exist or not.

安装

npm i koa-required-decorator --save

使用

koa-router 使用的示例,

import Required from "koa-required-decorator";

class Controller {
  @Required({
    query: ["id", "name"]
  })
  async getMethod(ctx, _) {
    ctx.body = "GET success";
  }

  @Required({
    "request.fields": ["id", "name"]
  })
  async postMethod(ctx, _) {
    ctx.body = "POST success";
  }
}

const transfer = async (ctx, next) => {
  ctx.request.fields = ctx.query;
  await next();
};

router.get("/demo1", ctrl.getMethod);
router.post("/demo2", transfer, ctrl.postMethod);

GET /demo1?id=1

输出结果:

{ "errCode": 400000, "errMsg": "parameter \"name\" should not be null." }

POST /demo2?name=1

输出结果:

{ "errCode": 400000, "errMsg": "parameter \"id\" should not be null." }

API

@Required(params: IParams)

interface IParams {
  [key: string]: string[]
}

根据传入的 KEY,查询 Koa.context 的特定字段是否含有 VALUE 里列举的属性。 校验对象路径过深,需 . 隔开, 如校验 ctx.request.fields.app.apple,可以这么配置,

@Required({
    'request.fields.app': ['apple']
})

setKoaRequiredOptions(params: IOptions)

interface IOptions {
  errMsgFunc?: (errors: string[]) => string;
  responseFunc?: (params: string) => any;
}

可以自定义报错文案和返回的数据格式

| 参数 | 说明 | 类型 | 默认 | | ------------ | -------------------- | ----------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------- | | errMsgFunc | 定义报错文案 | function(errors: string[]): string 传参 errors 为被校验为空的字段名集合,出参为定义的文案字符串 | errors => errors.map(k => 'parameter "${k}" should not be null.').join(' ') | | responseFunc | 定义报错返回数据格式 | function(params: string): any 其中 params 为 errMsgFunc 返回的字符串,出参为定义的数据格式 | errMsg => ({errCode: 400000, errMsg: params}) |

import { setKoaRequiredOptions } from "koa-required-decorator";

setKoaRequiredOptions({
  errMsgFunc: arr => arr.map(k => `参数 "${k}" 不能为空.`).join(" "),
  responseFunc: errMsg => {
    return {
      code: 400001,
      msg: errMsg
    };
  }
});

GET /demo1

输出结果:

{ "code": 400001, "msg": "参数 \"id\" 不能为空. 参数 \"name\" 不能为空." }