0.4.6 • Published 4 years ago

@ittlr/nest-common v0.4.6

Weekly downloads
-
License
MIT
Repository
-
Last release
4 years ago

NestCommon

基于 NestJS v7.x 的一些公共操作进行封装。

build

npm-v0.1.0

依赖项:

  • class-validator
  • class-transformer
  • think-helper

功能列表

  • pip
  • intercepter
  • log
  • decorator
  • AuthModule

pip

拦截请求器,请求进来时,会优先检测是否有定义的 DTO ,如果存在,自动和 dto 进行校验验证,验证不通过,返回 BadRequestException 异常,响应 信息如下:

{
    message: validateDatas[0].constraints[errorFields[0]], // 为 dto 错误消息的第一条
    code: 400
}

intercepter

拦截响应器,响应信息时,自动封装成统一的 API 格式,规则如下。

API 统一格式

{
    code: 0,
    message: '错误消息',
    data: {},
    info: {},
    xxxx: {}
}
  • code:0 成功;1 失败;
  • message: 提示文案;
  • data: 响应数据;
  • info: 拓展信息;
  • xxxx: 其他自定义信息

以上格式,每次都 return 也比较繁琐。。。

所以,为了不需要 return 以上信息,在响应器中进行了拦截修改处理,再次 return 时,可以按照下方进行简写。

返回错误信息

return 'this is err msg';

转换后:

{
    "code": 1,
    "message": "err msg"
}

返回成功信息

return [
    {
        detail: {
            id: 'this is id'
        }
    },
    'success msg'
];

转换后:

{
    "code": 0,
    "data": {
        "detail": {
            "id": "this is id"
        }
    },
    "message": "success msg"
}

规则: 返回失败,直接 string ,返回成功,必须为 数组,第一个元素为 data 中的 信息,第二个元素为 成功信息

AuthModule

基于 nest-jwt 的一套 jwt 鉴权策略,配合 Redis 可以实现过期时间限制。

0.4.6

4 years ago

0.4.5

4 years ago

0.4.3

4 years ago

0.4.2

4 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.0

4 years ago

0.1.0

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago