当前位置: 首页 > news >正文

可以在电脑做公务员题的网站360网站收录提交入口

可以在电脑做公务员题的网站,360网站收录提交入口,手机兼职快递录单员,高端猎头公司排名NestJS的Pipe是一个用于数据转换和验证的特殊装饰器。Pipe可以应用于控制器(Controller)的处理方法(Handler)和中间件(Middleware),用于处理传入的数据。它可以用来转换和验证数据,确…

NestJS的Pipe是一个用于数据转换和验证的特殊装饰器。Pipe可以应用于控制器(Controller)的处理方法(Handler)和中间件(Middleware),用于处理传入的数据。它可以用来转换和验证数据,确保数据的有效性和一致性。

内置的 Pipe 是指在使用 NestJS 框架时,已经预定义好的一些管道,用于验证和转换传入的数据。

  • ValidationPipe(验证管道):用于验证传入的数据是否符合指定的规则,可以使用 class-validator 和 class-transformer 库来实现验证和转换。
  • ParseIntPipe(整数转换管道):用于将传入的字符串转换为整数。
  • ParseBoolPipe(布尔值转换管道):用于将传入的字符串转换为布尔值。
  • ParseArrayPipe(数组转换管道):用于将传入的字符串转换为数组。
  • ParseUUIDPipe(UUID 转换管道):用于将传入的字符串转换为 UUID。
  • DefaultValuePipe(默认值管道):用于在传入的参数不存在时,给参数设置一个默认值。
  • ParseEnumPipe(枚举转换管道):用于将传入的字符串转换为枚举类型。
  • ParseFloatPipe(浮点数转换管道):用于将传入的字符串转换为浮点数。
  • ParseFilePipe(文件转换管道):用于将传入的文件转换为上传的文件对象。

接下来我们测试一下内置 Pipe 的功能:
创建项目

nest new pipe -p npm

1719120039619.png
修改 app.controller.ts 可以看到这个接口默认接收test的字符串参数

import { Controller, Get, Query } from '@nestjs/common';
import { AppService } from './app.service';@Controller()
export class AppController {constructor(private readonly appService: AppService) { }@Get()getHello(@Query('test') test: string): string {return test;}
}

1、ParseIntPipe

我们可以使用ParseIntPipe 将接收的字符串参数转为整数:

import { Controller, Get, ParseIntPipe, Query } from '@nestjs/common';
import { AppService } from './app.service';@Controller()
export class AppController {constructor(private readonly appService: AppService) { }@Get()getHello(@Query('test', ParseIntPipe) test: string): string {return test+10;}
}

访问接口 http://localhost:3000/?test=100 可以看到参数test传入 100 变成了 110
1719120853656.png
且当我们传入的参数不能转为init时 则会报错 例如下面 访问 http://localhost:3000/?test=ww
1719120993724.png
这个返回的报错 我们也可以自定义 但是这种方式需要使用new XxxPipe 的方式 例如指定状态码为404

import { Controller, Get, HttpStatus, ParseIntPipe, Query } from '@nestjs/common';
import { AppService } from './app.service';@Controller()
export class AppController {constructor(private readonly appService: AppService) { }@Get()getHello(@Query('test', new ParseIntPipe({errorHttpStatusCode: HttpStatus.NOT_FOUND,})) test: string): string {return test + 10;}
}

1719121317498.png
同时也可以抛出异常再进行处理

import { Controller, Get, HttpException, HttpStatus, ParseIntPipe, Query } from '@nestjs/common';
import { AppService } from './app.service';@Controller()
export class AppController {constructor(private readonly appService: AppService) { }@Get()getHello(@Query('test', new ParseIntPipe({errorHttpStatusCode: HttpStatus.NOT_FOUND,exceptionFactory(error) {console.log(error);throw new HttpException('测试报错' + error, HttpStatus.NOT_IMPLEMENTED)},})) test: string): string {return test + 10;}
}

1719121515372.png

2、ParseFloatPipe

ParseFloatPipe 的作用是将参数转为float 类型
我们新建一个接口: test

import { Controller, Get, HttpException, HttpStatus, ParseFloatPipe, ParseIntPipe, Query } from '@nestjs/common';
import { AppService } from './app.service';@Controller()
export class AppController {constructor(private readonly appService: AppService) { }@Get()getHello(@Query('test', new ParseIntPipe({errorHttpStatusCode: HttpStatus.NOT_FOUND,exceptionFactory(error) {console.log(error);throw new HttpException('测试报错' + error, HttpStatus.NOT_IMPLEMENTED)},})) test: string): string {return test + 10;}@Get('test')getTest(@Query('test', ParseFloatPipe) test: number) {return test + 10;}
}

访问 http://localhost:3000/test?test=10.4
1719122081715.png
同样可以new ParseFloatPipe 的形式,传入 errorHttpStatusCode 和 exceptionFactory

3、ParseBoolPipe

用于将传入的字符串转换为布尔值。

@Get('boolean')getBoolean(@Query('test') test: boolean) {return test;}

访问 http://localhost:3000/boolean?test=false
1719122405754.png

4、ParseArrayPipe

用于将传入的字符串转换为数组。

@Get('array')getArray(@Query('test', ParseArrayPipe) test: number[]) {return test.reduce((pre, cur) => pre + cur, 0);}

保存代码可以发现报错了 提示需要class-validator 这个包
1719122693022.pngclass-class-class-validator 允许使用基于装饰器和非装饰器的验证。 内部使用 validator.js 来执行验证。

pnpm install class-validator

安装之后启动 发现还是报错 缺少 class-transformer
class-transformer 允许您将普通对象转换为类的某个实例,反之亦然。 此外,它还允许根据条件序列化/反序列化对象。 这个工具在前端和后端都非常有用。
1719123624634.png

pnpm install class-transformer 

访问 http://localhost:3000/array?test=1,2,3,4
1719123834101.png
可以发现 每一项都提取出来了但是类型还是string 没有转换成number类型
此时需要 new XxxPipe 的方式传入参数:

@Get('array')getArray(@Query('test', new ParseArrayPipe({items: Number,})) test: number[]) {return test.reduce((pre, cur) => pre + cur, 0);}

再次访问 http://localhost:3000/array?test=1,2,3,4
1719123963798.png
同时还可以指定传入时的分割符: separator: ‘/’,

@Get('array')getArray(@Query('test', new ParseArrayPipe({items: Number,separator: '/',})) test: number[]) {return test.reduce((pre, cur) => pre + cur, 0);}

访问 http://localhost:3000/array?test=1/2/3/4
1719124196570.png
当我们没有传入参数的时候会报错:
image.png
我们可以通过设置 optional 来实现不传参数不报错

  @Get('array')getArray(@Query('test', new ParseArrayPipe({items: Number,separator: '/',optional: true,})) test: number[]) {return test}

1719124324325.png

5、ParseEnumPipe

用于将传入的字符串转换为枚举类型。

import { Controller, Get, HttpException, HttpStatus, ParseArrayPipe, ParseEnumPipe, ParseFloatPipe, ParseIntPipe, Query } from '@nestjs/common';
import { AppService } from './app.service';enum TestEnum {A = 'A111',B = 'B111',
}@Controller()
export class AppController {constructor(private readonly appService: AppService) { }@Get()getHello(@Query('test', new ParseIntPipe({errorHttpStatusCode: HttpStatus.NOT_FOUND,exceptionFactory(error) {console.log(error);throw new HttpException('测试报错' + error, HttpStatus.NOT_IMPLEMENTED)},})) test: string): string {return test + 10;}@Get('test')getTest(@Query('test', ParseFloatPipe) test: number) {return test + 10;}@Get('boolean')getBoolean(@Query('test') test: boolean) {return test;}@Get('array')getArray(@Query('test', new ParseArrayPipe({items: Number,separator: '/',optional: true,})) test: number[]) {return test}@Get('enum/:test')getEnum(@Query('test', new ParseEnumPipe(TestEnum)) test: TestEnum) {return test}
}

访问 http://localhost:3000/enum/111
1719126381961.png
当我们传的参数不是枚举定义的类型时会报错
访问 http://localhost:3000/enum/555
1719126484884.png

6、 ParseUUIDPipe

用于将传入的字符串转换为 UUID。在参数里,可以用 ParseUUIDPipe 来校验是否是 UUID
首先我们安装一下 uuid 用于生成uuid

pnpm install uuid

接着我们实现2个接口 getUuid getUuid2

import { Controller, Get, HttpException, HttpStatus, Param, ParseArrayPipe, ParseEnumPipe, ParseFloatPipe, ParseIntPipe, ParseUUIDPipe, Query } from '@nestjs/common';
import { AppService } from './app.service';const uuid = require('uuid');enum TestEnum {A = '111',B = '222',C = '333'
}@Controller()
export class AppController {constructor(private readonly appService: AppService) { }@Get()getHello(@Query('test', new ParseIntPipe({errorHttpStatusCode: HttpStatus.NOT_FOUND,exceptionFactory(error) {console.log(error);throw new HttpException('测试报错' + error, HttpStatus.NOT_IMPLEMENTED)},})) test: string): string {return test + 10;}@Get('test')getTest(@Query('test', ParseFloatPipe) test: number) {return test + 10;}@Get('boolean')getBoolean(@Query('test') test: boolean) {return test;}@Get('array')getArray(@Query('test', new ParseArrayPipe({items: Number,separator: '/',optional: true,})) test: number[]) {return test}@Get('enum/:test')getEnum(@Param('test', new ParseEnumPipe(TestEnum)) e: TestEnum) {return e}@Get('uuid')getUuid() {return uuid.v4()}@Get('uuid/:uuid')getUuid2(@Param('uuid', new ParseUUIDPipe) uuid: string) {return uuid}
}

访问 http://localhost:3000/uuid 获取uuid
1719127289811.png
接着访问 http://localhost:3000/uuid/0a5c5ce9-22ff-4091-85ef-523fcb64223c 可以看到正常访问
1719127352975.png
但是如果参数不是正常的uuid那么会报错
http://localhost:3000/uuid/xxx
1719127399773.png

7、DefaultValuePipe

用于在传入的参数不存在时,给参数设置一个默认值。
创建一个接口:

@Get('default')getDefault(@Query('test', new DefaultValuePipe('test')) test: string) {return test}

当我们没传参数的时候 会使用默认值test http://localhost:3000/default
1719127714732.png
当我们传参数之后将会返回参数 例如 http://localhost:3000/default?test=123456
1719127771860.png


8、自定义Pipe

首先创建一个 pipe:

nest g pipe www --flat --no-spec

www.pipe.ts:

import { ArgumentMetadata, Injectable, PipeTransform } from '@nestjs/common';@Injectable()
export class WwwPipe implements PipeTransform {transform(value: any, metadata: ArgumentMetadata) {console.log(value,metadata);return 'www';}
}

增加接口:

  @Get('www')getWww(@Query('test', WwwPipe) test: string, @Query('test2', WwwPipe) test2: string) {return test + test2}

浏览器访问 http://localhost:3000/www?test=1234&test2=1
1719128253615.png
这就是Pipe返回的值
1719128288386.png
控制台打印了 value 就是 query、param 的值,而 metadata 里包含 type、metatype、data
1719129484281.png
1719129507003.png
type 就是 @Query、@Param、@Body 装饰器,或者自定义装饰器
metatype 是参数的 ts 类型
data 参数值

http://www.mmbaike.com/news/107316.html

相关文章:

  • 网站运营培训机构附近电脑培训速成班一个月
  • 部署php网站互联网营销案例
  • 包图网官网湖南seo推广
  • 怎么制作图片加文字带声音的视频seo的作用
  • 吕梁市网站建设公司律师推广网站排名
  • 众车网是哪家公司网站泰州网站建设优化
  • 做同城网站最赚钱网站建设设计
  • 政府网站图解怎么做短视频推广平台有哪些
  • 哪里可以做公司网站备案广州谷歌seo公司
  • 百度做网站续费费用百度贴吧官网app下载
  • 网站建设 学校中央电视台新闻联播广告价格
  • 棋牌游戏网站建设好的营销网站设计公司
  • 合肥科技职业学院网站建设与管理国家职业技能培训平台
  • 好的做网站的公司营销目标分为三个方面
  • 做公众好号的网站产品怎么做市场推广
  • 苏州网站seo公司自动搜索关键词软件
  • 成都党风廉政建设平台网站网络推广专员所需知识
  • win7 asp.net网站架设免费的黄冈网站有哪些
  • 用新华做网站名是否侵权seo流量排名软件
  • 宁波做百度网站推广十种营销方式
  • php网站开发框架企业培训师
  • 延安做网站推广普通话手抄报图片
  • 网络工具下载相城seo网站优化软件
  • 怎么自己做整人网站b站不收费网站
  • 建设厅网站突然显示不全拉新app推广平台
  • 济南网站建设找凌峰百度网址大全官方下载
  • 文昌网站 做炸饺子百度推广
  • 建设一个视频网站需要什么时候开始怎么建立自己的网站平台
  • 奇米网怎么做网站互联网营销工具
  • 做语文课文网站的好处头条广告入口