最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Nest.js參數(shù)校驗(yàn)和自定義返回數(shù)據(jù)格式詳解

 更新時間:2021年03月28日 11:55:14   作者:Jaxson Wang  
這篇文章主要給大家介紹了關(guān)于Nest.js參數(shù)校驗(yàn)和自定義返回數(shù)據(jù)格式的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

0x0 參數(shù)校驗(yàn)

參數(shù)校驗(yàn)大部分業(yè)務(wù)是使用 Nest.js 中的管道 方法實(shí)現(xiàn),具體可以查閱文檔 。不過編寫過程中遇到一些問題,雖然文檔講得比較晦澀。

在做個查詢接口,里面包含一些參數(shù),做成 dto 結(jié)構(gòu)數(shù)據(jù):

import { ApiProperty } from '@nestjs/swagger'

export class QueryUserDto {
 @ApiProperty({
 required: false,
 description: '頁碼'
 })
 readonly currentPage: number

 @ApiProperty({
 required: false,
 description: '條數(shù)'
 })
 readonly pageSize: number

 @ApiProperty({
 required: false,
 description: '用戶賬號'
 })
 readonly username?: string

 @ApiProperty({
 required: false,
 description: '用戶狀態(tài)'
 })
 readonly activeStatus: number

 @ApiProperty({
 required: false,
 description: '排序的方式: ASC, DESC'
 })
 readonly order: 'DESC' | 'ASC'
}
 TYPESCRIPT

在 @Query 請求傳入對應(yīng)的參數(shù),發(fā)現(xiàn)得到的數(shù)據(jù)類型都是 String ,然后查閱相關(guān)文檔才明白還需要 class-transformer 的 Type 進(jìn)行轉(zhuǎn)換:

import { ApiProperty } from '@nestjs/swagger'
import { Type } from 'class-transformer'

export class QueryUserDto {
 @ApiProperty({
 required: false,
 description: '頁碼'
 })
 @Type(() => Number)
 readonly currentPage: number = 1

 @ApiProperty({
 required: false,
 description: '條數(shù)'
 })
 @Type(() => Number)
 readonly pageSize: number = 10

 @ApiProperty({
 required: false,
 description: '用戶賬號'
 })
 readonly username?: string

 @ApiProperty({
 required: false,
 description: '用戶狀態(tài)'
 })
 @Type(() => Number)
 readonly activeStatus: number = 3

 @ApiProperty({
 required: false,
 description: '排序的方式: ASC, DESC'
 })
 readonly order: 'DESC' | 'ASC' = 'DESC'
}

然后在 ValidationPipe 管道方法里開啟 transform 選項:

app.useGlobalPipes(
 new ValidationPipe({
 transform: true
 })
)

或者在 app.modules.ts 注入:

import { ValidationPipe } from '@nestjs/common'
import { APP_PIPE } from '@nestjs/core'

@Module({
 imports: [
 // ...
 ],
 controllers: [AppController],
 providers: [
 {
  provide: APP_PIPE,
  useValue: new ValidationPipe({
  transform: true
  })
 }
 ]
})

倆者使用方法區(qū)別于程序的是否混合應(yīng)用類型。

我這邊為了省事直接寫在全局方法里,最終到 service 拿到的數(shù)據(jù)就是經(jīng)過管道業(yè)務(wù)處理過的數(shù)據(jù),不需要在 service 層進(jìn)行大量的數(shù)據(jù)類型判斷。

0x1 自定義返回數(shù)據(jù)格式

在 controller 返回的數(shù)據(jù)都是從數(shù)據(jù)庫表結(jié)構(gòu)而來:

{
 "id": "d8d5a56c-ee9f-4e41-be48-5414a7a5712c",
 "username": "Akeem.Cremin",
 "password": "$2b$10$kRcsmN6ewFC2GOs0TEg6TuvDbNzf1VGCbQf2fI1UeyPAiZCq9rMKm",
 "email": "Garrett87@hotmail.com",
 "nickname": "Wallace Nicolas",
 "role": "user",
 "isActive": true,
 "createdTime": "2021-03-24T15:24:26.806Z",
 "updatedTime": "2021-03-24T15:24:26.806Z"
}

如果需要定義最終返回接口的數(shù)據(jù)格式例如:

{
 "statusCode": 200,
 "message": "獲取成功",
 "data": {
  "id": "d8d5a56c-ee9f-4e41-be48-5414a7a5712c",
  "username": "Akeem.Cremin",
  "password": "$2b$10$kRcsmN6ewFC2GOs0TEg6TuvDbNzf1VGCbQf2fI1UeyPAiZCq9rMKm",
  "email": "Garrett87@hotmail.com",
  "nickname": "Wallace Nicolas",
  "role": "user",
  "isActive": true,
  "createdTime": "2021-03-24T15:24:26.806Z",
  "updatedTime": "2021-03-24T15:24:26.806Z"
 }
}

這里就需要做個自定義成功請求攔截器:

nest g in shared/interceptor/transform
import { CallHandler, ExecutionContext, Injectable, Logger, NestInterceptor } from '@nestjs/common'
import { Observable } from 'rxjs'
import { map } from 'rxjs/operators'
import { Request } from 'express'

interface Response<T> {
 data: T
}

@Injectable()
export class TransformInterceptor<T> implements NestInterceptor<T, Response<T>> {
 intercept(context: ExecutionContext, next: CallHandler<T>): Observable<any> {
 const request = context.switchToHttp().getRequest<Request>()
 Logger.log(request.url, '正常接口請求')

 return next.handle().pipe(
  map(data => {
  return {
   data: data,
   statusCode: 200,
   message: '請求成功'
  }
  })
 )
 }
}

然后在 app.module.ts 引入即可使用:

import { ValidationPipe } from '@nestjs/common'
import { APP_INTERCEPTOR } from '@nestjs/core'

import { TransformInterceptor } from '@/shared/interceptor/transform.interceptor'

@Module({
 imports: [
 // ...
 ],
 controllers: [AppController],
 providers: [
 {
  provide: APP_INTERCEPTOR,
  useClass: TransformInterceptor
 }
 ]
})

不過 APP_INTERCEPTOR 排序要注意,TransformInterceptor 最好放在第一個,否則會失效。

錯誤過濾器:

nest g f shared/filters/httpException
import { ArgumentsHost, Catch, ExceptionFilter, HttpException, Logger } from '@nestjs/common'
import { Response, Request } from 'express'

@Catch(HttpException)
export class HttpExceptionFilter implements ExceptionFilter {
 catch(exception: HttpException, host: ArgumentsHost) {
 const context = host.switchToHttp()
 const response = context.getResponse<Response>()
 const request = context.getRequest<Request>()
 const status = exception.getStatus()
 const message = exception.message

 Logger.log(`${request.url} - ${message}`, '非正常接口請求')

 response.status(status).json({
  statusCode: status,
  message: message,
  path: request.url,
  timestamp: new Date().toISOString()
 })
 }
}

然后在 app.module.ts 引入即可使用:

import { ValidationPipe } from '@nestjs/common'
import { APP_FILTER } from '@nestjs/core'

import { HttpExceptionFilter } from '@/shared/filters/http-exception.filter'

@Module({
 imports: [
 // ...
 ],
 controllers: [AppController],
 providers: [
 {
  provide: APP_FILTER,
  useClass: HttpExceptionFilter
 }
 ]
})

0x2 隱藏實(shí)體類中的某個字段

本來想使用 @Exclude 屬性來隱藏數(shù)據(jù)庫中一些敏感的字段,但發(fā)現(xiàn)無法滿足特殊的需求,如果是返回單條實(shí)例可以實(shí)現(xiàn)隱藏,但是我有個 findAll 就無法實(shí)現(xiàn)了,上面在 Serialization | NestJS - A progressive Node.js framework 文檔里說的非常詳細(xì),不過這里還有個辦法。首先在實(shí)力類敏感數(shù)據(jù)字段上添加屬性:

import { BaseEntity, Entity, Column, PrimaryGeneratedColumn } from 'typeorm'

@Entity('user')
export class UserEntity extends BaseEntity {
 @PrimaryGeneratedColumn('uuid', {
  comment: '用戶編號'
 })
 id: string

 @Column({
  type: 'varchar',
  length: 50,
  unique: true,
  comment: '登錄用戶'
 })
 username: string

 @Column({
  type: 'varchar',
  length: 200,
  select: false,
  comment: '密碼'
 })
 password: string

select: false 可以在返回查詢結(jié)果隱藏這個字段,但所有涉及到這個字段查詢必須添加這個字段,比如我在 user.service.ts 登錄查詢中:

const user = await getRepository(UserEntity)
   .createQueryBuilder('user')
   .where('user.username = :username', { username })
   .addSelect('user.password')
   .getOne()

.addSelect('user.password') 添加這個屬性查詢將會包括 password 這個字段,否則普通查詢的方法不會包括這個字段。

總結(jié)

到此這篇關(guān)于Nest.js參數(shù)校驗(yàn)和自定義返回數(shù)據(jù)格式的文章就介紹到這了,更多相關(guān)Nest.js參數(shù)校驗(yàn)和數(shù)據(jù)格式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Node.js 回調(diào)函數(shù)實(shí)例詳解

    Node.js 回調(diào)函數(shù)實(shí)例詳解

    這篇文章主要介紹了Node.js 回調(diào)函數(shù)實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • Node.js巧妙實(shí)現(xiàn)Web應(yīng)用代碼熱更新

    Node.js巧妙實(shí)現(xiàn)Web應(yīng)用代碼熱更新

    本文給大家講解的是Node.js的代碼熱更新的問題,其主要實(shí)現(xiàn)原理 是怎么對 module 對象做處理,也就是手工監(jiān)聽文件修改, 然后清楚模塊緩存, 重新掛載模塊,思路清晰考慮細(xì)致, 雖然有點(diǎn)冗余代碼,但還是推薦給大家
    2015-10-10
  • 詳解nodejs http請求相關(guān)總結(jié)

    詳解nodejs http請求相關(guān)總結(jié)

    這篇文章主要介紹了nodejs http請求相關(guān)總結(jié),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • pnpm實(shí)現(xiàn)依賴包共享和依賴包項目隔離的方法詳解

    pnpm實(shí)現(xiàn)依賴包共享和依賴包項目隔離的方法詳解

    pnpm是Node.js的包管理器,它是 npm 的直接替代品,相對于npm和yarn它的優(yōu)點(diǎn)就在于速度快和高效節(jié)省磁盤空間,本文主要講解pnpm相比于npm/yarn如何利用軟硬鏈接來節(jié)省磁盤空間,以及如何實(shí)現(xiàn)依賴包共享和依賴包項目隔離的,需要的朋友可以參考下
    2024-05-05
  • nodejs使用node-xlsx生成excel的方法示例

    nodejs使用node-xlsx生成excel的方法示例

    這篇文章主要介紹了nodejs使用node-xlsx生成excel,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • node連接MySQL數(shù)據(jù)庫的3種方式總結(jié)

    node連接MySQL數(shù)據(jù)庫的3種方式總結(jié)

    現(xiàn)在前端基本上都會用一些NodeJs,想必也想自己寫一些API或者個人博客的后臺系統(tǒng),這些就離不開連接數(shù)據(jù)庫的問題,下面這篇文章主要給大家介紹了關(guān)于node連接MySQL數(shù)據(jù)庫的3種方式,需要的朋友可以參考下
    2022-08-08
  • 詳解兩個Node.js進(jìn)程是如何通信

    詳解兩個Node.js進(jìn)程是如何通信

    進(jìn)程間通信是是Node.js的一個十分重要的部分,這篇文章主要給大家介紹了關(guān)于兩個Node.js進(jìn)程是如何通信的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2021-10-10
  • NodeJS Web應(yīng)用監(jiān)聽sock文件實(shí)例

    NodeJS Web應(yīng)用監(jiān)聽sock文件實(shí)例

    這篇文章主要介紹了NodeJS Web應(yīng)用監(jiān)聽sock文件實(shí)例,本文講解 NodeJS 的 TCP 和 HTTP 監(jiān)聽 Domain Socket 文件例子,需要的朋友可以參考下
    2015-02-02
  • 關(guān)于npm install報錯ERR code ETIMEDOUT的問題及解決

    關(guān)于npm install報錯ERR code ETIMEDOUT的問題及解決

    這篇文章主要介紹了關(guān)于npm install報錯ERR code ETIMEDOUT的問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-06-06
  • 詳解Node.js中的事件機(jī)制

    詳解Node.js中的事件機(jī)制

    Node.js能夠在眾多的后端JavaScript技術(shù)之中脫穎而出,正是因其基于事件的特點(diǎn)而受到歡迎,所以這篇文章小編給大家詳細(xì)介紹了Node.js中的事件機(jī)制,本文介紹的很詳細(xì),對大家的理解和學(xué)習(xí)很有幫助,下面來一起看看吧。
    2016-09-09

最新評論

汝南县| 安远县| 嘉定区| 青岛市| 息烽县| 县级市| 边坝县| 洱源县| 湖北省| 吉林省| 阜平县| 息烽县| 潜山县| 克拉玛依市| 偃师市| 和顺县| 湾仔区| 将乐县| 温宿县| 上犹县| 大姚县| 桦南县| 海口市| 新河县| 靖西县| 西青区| 岳阳县| 姜堰市| 乐昌市| 阳高县| 宿松县| 桃江县| 正镶白旗| 安康市| 台安县| 武定县| 沁源县| 尤溪县| 永州市| 北流市| 稻城县|