Is it possible to make a generic interceptor that works similar to a generic pipe where the return type of the handler can be used to transform the response?
For example, look at this Zod pipe:
import { ArgumentMetadata, Injectable, PipeTransform } from '@nestjs/common'
import { z } from 'zod'
import { ZodPipeException } from './zod-pipe.exception'
@Injectable()
export class ZodPipe<T extends z.ZodTypeAny> implements PipeTransform<unknown, z.infer<T>> {
async transform(value: unknown, metadata: ArgumentMetadata): Promise<z.infer<T>> {
if (metadata.metatype instanceof z.ZodType) {
const result = await metadata.metatype.spa(value)
if (!result.success) throw new ZodPipeException(result.error)
return result.data
}
return value
}
}
I would like to make a similar interceptor that will parse the returned values from a controller method