Skip to content

liaorg/nestjs-auth

Repository files navigation

目录结构

+-- bin // 自定义任务脚本
+-- dist // 源码构建目录
+-- public // 静态资源目录(web页面)
+-- src
|   +-- config // 公共全局配置
|   +-- entities // TypeORM Entities generated by `typeorm-model-generator` module
|   +-- auth // 鉴权模块
|   +-- common // 公共全局模块
|   |   +-- constants // 静态变量
|   |   +-- controllers // 控制器
|   |   +-- decorators // 装饰器
|   |   +-- dto // DTO (Data Transfer Object) Schema, Validation
|   |   +-- enums // 枚举变量
|   |   +-- exceptions // 自定义异常
|   |   +-- filters // 异常过滤器
|   |   +-- guards // 守卫
|   |   +-- interceptors // 拦截器
|   |   +-- interfaces // 接口
|   |   +-- middleware // 中间件
|   |   +-- pipes // 管道
|   |   +-- providers // 服务
|   +-- modules // 业务模块
|   |   +-- users // 通过命令 nest g res [模块名] 生成
|   |   |   +-- dto
|   |   |   +-- entities
|   |   |   +-- enums
|   |   |   +-- users.constant.ts
|   |   |   +-- users.controller.ts
|   |   |   +-- users.service.ts
|  |    |   +-- users.module.ts
|   |   |   +-- users.exception.ts
|   |   |   +-- users.*.ts
|   |   |   +-- index.ts
|   +-- * // 其他模块同以上结构
+-- test // Jest testing

Installation

vscode 扩展
在安装扩展输入框输入 @recommended 安装工作区推荐的扩展

Chinese:中文语言包
ashinzekene/NestJS Snippets
Abhijoy Basak/NestJS Files
Prettier:格式化代码-JavaScript / TypeScript / CSS
JavaScript (ES6) snippets:支持 JavaScript ES6 语法
Bookmarks

node-snippets
One Dark Pro
ESLint
Remote - WSL
Path Intellisense 路径补全
Outline Map 代码大纲

程序初始化

npm i -g @nestjs/cli


npm install

配置 lanunch.json 进行应用调试


Running the app

# development
$ npm run start

# watch mode
$ npm run start:dev

# production mode
$ npm run start:prod

Test

# unit tests
$ npm run test

# e2e tests
$ npm run test:e2e

# test coverage
$ npm run test:cov

License

Nest is MIT licensed.

配置 git

git config --global user.name "username"
git config --global user.email [email protected]

提交规范

git commit -m "type: subject"

1、type (必须) 用于说明 git commit 的类别,只允许使用下面的标识 feat: 新功能(feature) fix: 修复 bug docs: 文档(documentation) style: 格式(不影响代码运行的变动) refactor: 重构(既不是新增功能,也不是修改 bug 的代码变动) test: 增加测试 chore: 构建过程或辅助工具的变动 revert: 回滚到上一个版本 merge: 代码合并

build, ci, perf

i18n

# [https://github.com/toonvanstrijp/nestjs-i18n]
npm install --save nestjs-i18n
# nest-cli.json
{
  "collection": "@nestjs/schematics",
  "sourceRoot": "src",
  "compilerOptions": {
    "assets": [
      { "include": "i18n/**/*", "watchAssets": true }
    ]
  }
}
# app.module.ts
@Module({
  imports: [
    I18nModule.forRoot({
      fallbackLanguage: 'en',
      loaderOptions: {
        path: path.join(__dirname, '/i18n/'),
        watch: true,
      },
    }),
  ],
  controllers: [],
})

# users.service.ts / users.controller.ts
constructor(private readonly i18nService: I18nService) {}

npm install --save @nestjs/serve-static

curd

npm install mongodb --save
npm install @nestjsx/crud class-transformer class-validator
npm install @nestjs/typeorm typeorm --save
or
npm install --save @nestjs/mongoose mongoose

模块开发流程

  1. 生成一个 REST API 类 nest g res modules/role
  2. 定义表结构,创建文件夹 schemas 和 schema (Roles RolesSchema createRoleValidationSchema)
  3. 创建 dto 类
  4. 如果有 controller 操作接口,要在对应的 dto 类注入验证 schema 对象 @RequestValidationSchema(createRoleValidationSchema)
  5. 增加 i18n 文件 i18n/zh-CN/role.json
  6. 增加错误异常文件类 role.exception.ts
import { ApiException } from "@/common/exceptions";
import { ApiErrorObjectInterface } from "@/common/interfaces";

export class RolesException extends ApiException {
    constructor(objectOrError?: ApiErrorObjectInterface) {
        super(objectOrError);
    }
}
  1. 在文件 api-error-code.constant.ts 中增加错误对象RoleErros

  2. 在 module 中导入对应的数据库连接服务及其他必要服务

imports: [
    MongooseModule.forFeature([{ name: Role.name, schema: RoleSchema }]),
    AuthModule,
    // MenuModule
];

常用模块

1. npm install mkdirp --save 创建文件夹
2. npm install jimp --save 缩略图

About

nestjs 鉴权及权限管理 jwttoken

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published