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

@ittlr/nest-common

v0.4.6

Published

NestJs一些公共的操作行为封装

Downloads

10

Readme

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 可以实现过期时间限制。