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

@umajs/arg-decorator

v2.0.5

Published

uma arg-decorator

Downloads

28

Readme

内置参数装饰器(ArgDecorator)

UMajs 提供了 createArgDecorator 可以很方便的创建自定义参数装饰器,并且框架还提供了如下装饰器直接使用。

使用

安装 npm install -S @umajs/arg-decorator

示例

import { Query, Body } from '@umajs/arg-decorator';

// url参数类型修饰校验
@Path('/saveUser')
saveUser(@Query.ToNumber('age') age :number) {
    return Result.send(`This router queryParms is ${userId} ${age}`);
}

// POST参数类型修饰校验
@Path({value:'/post',method:RequestMethod.POST})
saveUser(@Body.ToNumber('age') age: number){
    return Result.send(`This Post body info is ${JSON.stringify(userInfo)}`);
}

当接口访问localhost:port//saveUser?age=str时,controller方法将终止执行,并默认返回客户端提示信息

{
    "code": 0,
    "msg": "age 参数必须为数据类型。入参值str"
}

校验参数装饰器

| 修饰器 | 使用说明 | ---|--- @Param(id:string) | 动态路由参数修饰 @Query(id:string) | url参数修饰器 @Body(id?:string or Function or string[] or class) | POST请求参数修饰器 @Body() body:class or @Body('id') id:any or @Body(['name','age']) user: {name:any,age:any} @Require(id: string,message?:string) | url参数修饰并做必填校验 @ToNumber(id: string,message?: string) | 参数修饰并类型转换为number类型 类型转换失败则会终止函数执行并返回提示内容 @ToBoolean(id: string,message?: string) |参数修饰并类型转换布尔类型 类型转换失败则会终止函数执行并返回提示内容 @ToArray(id: string, split?:string ,message?: string) |参数修饰并类型转换数组 类型转换失败则会终止函数执行并返回提示内容 @ToDate(id: string,message?: string) | 参数修饰并类型转换为date类型 类型转换失败则会终止函数执行并返回提示内容 备注:参数接受如果为数字也会按照时间强制转换为时间格式。 @Equals(id: string,comparison?: any) | 参数修饰并做值对比校验 @NotNull(id: string,message?: string) | 限制必须不为null @AssertFalse(id: string,message?: string) | 限制必须为false @AssertTrue(id: string,message?: string) | 限制必须为true @DecimalMax(id: string,value: number,message?: string) | 限制必须为一个不大于指定值的数字 @DecimalMin(id: string,value: number,message?: string) | 限制必须为一个不小于指定值的数字 @Future(id: string,message?: string) | 限制必须是一个将来的日期 @Max(id: string,value: number,message?: string) | 限制必须为一个不大于指定值的数字 @Min(id: string,value: number,message?: string) | 限制必须为一个不小于指定值的数字 @Past(id: string,message?: string) | 限制必须是一个过去的日期 @Pattern(id: string,pattern: RegExp,message?: string) | 限制必须符合指定的正则表达式 @Size(id: string,max: number,min: number,message?: string) | 限制字符长度必须在min到max之间 @NotEmpty(id: string,message?: string) | 验证注解的元素值不为null且不为空(字符串长度不为0、集合大小不为0) @NotBlank(id: string,message?: string) | 验证注解的元素值不为空(不为null、去除首位空格后长度为0),不同于@NotEmpty,@NotBlank只应用于字符串且在比较时会去除字符串的空格 @Email(id: string,message?: string) | 验证注解的元素值是Email @Phone(id: string,message?: string) | 验证元素值是手机号 具体格式参考https://github.com/validatorjs/validator.js/blob/master/src/lib/isMobilePhone.js

非校验参数修饰器

| 修饰器 | 使用说明 | | --------------------------- | ---------------------------------------------------------------------------------------------------------------------- | | @Cookies(id:string) | cookies 参数修饰器 | | @Headers(id:string) | headers 请求头参数修饰器 | | @RequestParam(id:string) | GET/POST 通用参数修饰器,POST 请求类型时参数获取优先级为:Body < Query 相同参数 url 携带参数覆盖 body 请求体中的属性值 | | @RequestFile(field: string) | 文件上传时文件参数修饰器 |

高阶

Body参数装饰器接受class类

当Body传递为class类时,会将请求参数中获取到的数据作为参数调用类构造函数,同时也会进行属性类型校验,校验成功则返回实例化对象。更多class的使用请参考@umajs/class-validator

// 定义class
import { Type, Required, Min, Model } from '@umajs/class-validator';
export default class UserInfo extends Model {
    constructor({ id, name, age }: UserInfo, isValid: boolean) {
        super(isValid);
        this.id = id;
        this.name = name;
        this.age = age;
    }

    @Type('number')
    id: number = 123;

    @Required()
    name?: string;

    @Min(0)
    age?: number;
}

// 参数装饰器使用
@Path({ value: '/post', method: RequestMethod.POST })
model(@Body(UserInfo) userInfo: user) {
    return Result.send(`This Post body info is ${JSON.stringify(userInfo)}`);

    // >> {"code":0,"msg":{"validate":{"id":["id must be of type number."],"name":["name is required."],"age":["age must be greater than 0."]},"parms":{"id":"1","age":-10}}}
}

自定义校验提示内容

框架默认修饰器提示信息可以通过配置文件src/config/argDecorator.config.ts进行覆盖。通过Result模块用户可以自定义校验失败时返回的数据格式或者方式,比如json,或者状态码。

import { Result } from '@umajs/core';

export default {
    Require: {
        err({key, ctx, tip, val}) {
            return Result.send(tip || `请求${key} 参数不能为空。入参值为${val}`,403);
        },
    },
    ToNumber: {
        err({key, ctx, tip, val}) {
            return Result.json({
                code: 0,
                msg: tip || `请求${key} 参数必须为数字类型。入参值为${val}`,
            });
        },
    },
};