nestjs-typebox
Version:
This library provides helper utilities for writing and validating NestJS APIs using [TypeBox](https://github.com/sinclairzx81/typebox) as an alternative to class-validator/class-transformer. Can be configured to patch @nestjs/swagger allowing OpenAPI gene
28 lines (22 loc) • 998 B
text/typescript
import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { DECORATORS } from '@nestjs/swagger/dist/constants.js';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { isSchemaValidator } from './decorators.js';
()
export class TypeboxTransformInterceptor implements NestInterceptor {
constructor(private reflector: Reflector) {}
intercept(context: ExecutionContext, next: CallHandler): Observable<unknown> {
return next.handle().pipe(
map(data => {
const responseMeta = this.reflector.get(DECORATORS.API_RESPONSE, context.getHandler()) ?? {};
const validator = (responseMeta['200'] || responseMeta['201'] || {})['type'];
if (!isSchemaValidator(validator)) {
return data;
}
return validator.validate(data);
})
);
}
}