Is there a way to add additional metadata to a NestJS pipe?
The metadata property has these values:
export interface ArgumentMetadata {
type: 'body' | 'query' | 'param' | 'custom';
metatype?: Type<any>;
data?: string;
}
I was able to add additional metadata via a custom parameter decorator, and a custom pipe.
import { createParamDecorator} from '@nestjs/common'
export const ExtractIdFromBody = createParamDecorator(
(
{
property,
entityLookupProperty = 'id'
}: {
property: string
entityLookupProperty?: string
},
req
) => {
const value = get(req.body, property)
return {
value,
entityLookupProperty // the extra property
}
}
)
Then I used a the decorator like this:
@Post()
@UsePipes(new ValidationPipe({ transform: true }))
async doThing(
@ExtractIdFromBody({ property: 'userId', entityLookupProperty: 'someProperty' }, GetEntityOr404Pipe) entity: Entity,
): Promise<Entity[]> {
return await this.Service.doThing(entity)
}