Adding a REST endpoint
This plugin adds a single REST endpoint at http://localhost:3000/products
which returns a list of all Products. Find out more about Nestjs REST Controllers.
// products.controller.ts
import { Controller, Get } from '@nestjs/common';
import { Ctx, ProductService, RequestContext } from '@vendure/core';
@Controller('products')
export class ProductsController {
constructor(private productService: ProductService) {}
@Get()
findAll(@Ctx() ctx: RequestContext) {
return this.productService.findAll(ctx);
}
}
// rest.plugin.ts
import { PluginCommonModule, VendurePlugin } from '@vendure/core';
import { ProductsController } from './products.controller';
@VendurePlugin({
imports: [PluginCommonModule],
controllers: [ProductsController],
})
export class RestPlugin {}
Note: The PluginCommonModule
should be imported to gain access to Vendure core providers - in this case it is required in order to be able to inject ProductService
into our controller.
Side note: since this uses no Vendure-specific metadata, it could also be written using the Nestjs @Module()
decorator rather than the @VendurePlugin()
decorator.
Controlling access to REST endpoints
You can use the Allow decorator to declare the permissions required to access a REST endpoint:
import { Controller, Get } from '@nestjs/common';
import { Allow, Permission, Ctx, ProductService, RequestContext } from '@vendure/core';
@Controller('products')
export class ProductsController {
constructor(private productService: ProductService) {}
@Allow(Permission.ReadProduct)
@Get()
findAll(@Ctx() ctx: RequestContext) {
return this.productService.findAll(ctx);
}
}