热搜:前端 nest neovim nvim

NestInterceptor插口建立回调函数实行

lxf2023-06-18 02:38:57

回调函数是采用@Injectable()装饰器注释的类。回调函数应当完成NestInterceptor插口

建立回调函数

实行$ nest g interceptor userInterceptor,建立一个名为 userInterceptor 的回调函数

$ nest g interceptor userInterceptor

创建一个回调函数案例

import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';

@Injectable()
// 重新定义了一个名为TransformInterceptor的回调函数,该回调函数完成了NestInterceptor插口,并采用了泛型T和Response
export class LoggingInterceptor implements NestInterceptor {
  // 完成了NestInterceptor插口里的intercept方式,此方法接受2个主要参数:执行上下文 和 启用处理过程
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    console.log('Before...');

    const now = Date.now();
    // 根据启用next.handle()方式掌握到启用处理过程返回数据信息
    return next
      .handle()
      .pipe(
        tap(() => console.log(`After... ${Date.now() - now}ms`)),
      );
  }
}

注入器(Injectable)

回调函数(NestInterceptor)

执行上下文(ExecutionContext)

启用处理过程(CallHandler)

关联回调函数

关联的方法与Nest架构守护、Nest架构过滤装置一样

方法一:种类关联

@Get()
// 传达的型
@UseInterceptors(LoggingInterceptor)
export class CatsController {}

方法二:案例关联

@Get()
// 传送案例
@UseInterceptors(new LoggingInterceptor())
export class CatsController {}

全局性回调函数

全局性回调函数关联在板块中

const app = await NestFactory.create(ApplicationModule);
app.useGlobalInterceptors(new LoggingInterceptor());

回应投射

该回调函数作用是将回应数据封装为一个标准化的Response目标,并返还给手机客户端。

import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

export interface Response<T> {
  data: T;
}

@Injectable()
export class TransformInterceptor<T> implements NestInterceptor<T, Response<T>> {
  intercept(context: ExecutionContext, next: CallHandler): Observable<Response<T>> {
    return next.handle().pipe(map(data => ({ data })));
  }
}

假定我们应该把每个所发生的null值转换成空字符串''

import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

@Injectable()
export class ExcludeNullInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    return next
      .handle()
      .pipe(map(value => value === null ? '' : value ));
  }
}

出现异常投射

import {
  Injectable,
  NestInterceptor,
  ExecutionContext,
  BadGatewayException,
  CallHandler,
} from '@nestjs/common';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';

@Injectable()
export class ErrorsInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    return next
      .handle()
      .pipe(
        catchError(err => throwError(new BadGatewayException())),
      );
  }
}

案例:

假定需要处理路由器连接超时。假如节点在一段时间后未回到一切具体内容,则会以不正确回应停止。

import { Injectable, NestInterceptor, ExecutionContext, CallHandler, RequestTimeoutException } from '@nestjs/common';
import { Observable, throwError, TimeoutError } from 'rxjs';
import { catchError, timeout } from 'rxjs/operators';

@Injectable()
export class TimeoutInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    return next.handle().pipe(
      timeout(5000),
      catchError(err => {
        if (err instanceof TimeoutError) {
          return throwError(new RequestTimeoutException());
        }
        return throwError(err);
      }),
    );
  };
};
本站是一个以CSS、JavaScript、Vue、HTML为中心的前端开发技术网址。我们的使命是为众多前端工程师者提供全方位、全方位、好用的前端工程师专业知识和技术服务。 在网站上,大家可以学到最新前端开发技术,掌握前端工程师最新发布的趋势和良好实践。大家提供大量实例教程和实例,让大家可以快速上手前端工程师的关键技术和程序。 本站还提供了一系列好用的工具软件,帮助你更高效地开展前端工程师工作中。公司提供的一种手段和软件都要经过精心策划和改进,能够帮助你节约时间精力,提高研发效率。 此外,本站还拥有一个有活力的小区,你可以在社区里与其它前端工程师者沟通交流技术性、交流经验、处理问题。我们坚信,街道的能量能够帮助你能够更好地进步与成长。 在网站上,大家可以寻找你需要的一切前端工程师网络资源,使您成为一名更加出色的网页开发者。欢迎你添加我们的大家庭,一起探索前端工程师的无限潜能!