Reputation: 105
async checkUserMemberOfProduct(userId: string, productId: string) {
if (userId && productId) {
const user = await Product.find({ where: { userId: userId, id: productId } });
return !!user;
}
return false;
}
I want to create this function as decorator. And call it above endpoint. For example
@checkUserMemberOfProduct()
@post('/roles')
async create(
@requestBody() user: User
): Promise<Role> {
return this.roleRepository.create(role);
}
But I am not sure how to pass request body to this decorator. Is it possible to use this fn as decorator?
Upvotes: 1
Views: 490
Reputation: 1147
I think an interceptor may work. Heres an example code.
import {inject, intercept, Interceptor} from '@loopback/context';
import {get, HttpErrors, RestBindings} from '@loopback/rest';
const myInterceptor: Interceptor = async (invocationCtx, next) => {
// your code
// goes here
// if success
next()
// if error
throw new HttpErrors.NotFound();
};
export class PingController {
constructor() {}
@intercept(myInterceptor)
@post('/test')
async test(@requestBody() body: any) {
// some code
}
}
Upvotes: 2