Important:Before running this generator, you must create an application using the application generator.Then you must run the command from the root directory of the application.

Synopsis

Adds a new interceptor class to aLoopBack application.

  1. lb4 interceptor [--global] [--group <group>] [<name>]

Arguments and options

<name> - Name of the interceptor to create as an argument to the command. Ifprovided, the tool will use that as the default when it prompts for the name.

—global - Optional flag to indicate a global interceptor (default to true).Use —no-global to set it to false.

—group <group> - Optional name of the interceptor group to sort the executionof global interceptors by group. This option is only supported for globalinterceptors.

Interactive Prompts

The tool will prompt you for:

  • Name of the interceptor.(interceptorName) If the name had been suppliedfrom the command line, the prompt is skipped.

  • Is it a global interceptor.(isGlobal) If the flag had been suppliedfrom the command line, the prompt is skipped.

  • Group of the interceptor.(groupName) If the group had been suppliedfrom the command line, the prompt is skipped. SeeOrder of invocation for interceptors.

Output

Once all the prompts have been answered, the CLI will do the following:

  • Create an interceptor class as follows:/src/interceptors/${interceptorName}.interceptor.ts
  • Update /src/interceptors/index.ts to export the newly created globalinterceptor class.The generated class looks like:

  • A global interceptor

  1. import {
  2. /* inject, */
  3. globalInterceptor,
  4. bind,
  5. Interceptor,
  6. Provider,
  7. } from '@loopback/context';
  8. /**
  9. * This class will be bound to the application as a global `Interceptor` during
  10. * `boot`
  11. */
  12. @globalInterceptor('auth', {tags: {name: 'test'}})
  13. export class TestInterceptor implements Provider<Interceptor> {
  14. /*
  15. constructor() {}
  16. */
  17. /**
  18. * This method is used by LoopBack context to produce an interceptor function
  19. * for the binding.
  20. *
  21. * @returns An interceptor function
  22. */
  23. value() {
  24. return this.intercept.bind(this);
  25. }
  26. /**
  27. * The logic to intercept an invocation
  28. * @param invocationCtx - Invocation context
  29. * @param next - A function to invoke next interceptor or the target method
  30. */
  31. async intercept(
  32. invocationCtx: InvocationContext,
  33. next: () => ValueOrPromise<InvocationResult>,
  34. ) {
  35. try {
  36. // Add pre-invocation logic here
  37. const result = await next();
  38. // Add post-invocation logic here
  39. return result;
  40. } catch (err) {
  41. // Add error handling logic here
  42. throw err;
  43. }
  44. }
  45. }
  • A non-global interceptor
  1. import {
  2. /* inject, */
  3. bind,
  4. Interceptor,
  5. Provider,
  6. } from '@loopback/context';
  7. /**
  8. * This class will be bound to the application as a global `Interceptor` during
  9. * `boot`
  10. */
  11. @bind({tags: {namespace: 'interceptors', name: 'test'}})
  12. export class TestInterceptor implements Provider<Interceptor> {
  13. /*
  14. constructor() {}
  15. */
  16. /**
  17. * This method is used by LoopBack context to produce an interceptor function
  18. * for the binding.
  19. *
  20. * @returns An interceptor function
  21. */
  22. value() {
  23. return this.intercept.bind(this);
  24. }
  25. /**
  26. * The logic to intercept an invocation
  27. * @param invocationCtx - Invocation context
  28. * @param next - A function to invoke next interceptor or the target method
  29. */
  30. async intercept(
  31. invocationCtx: InvocationContext,
  32. next: () => ValueOrPromise<InvocationResult>,
  33. ) {
  34. try {
  35. // Add pre-invocation logic here
  36. const result = await next();
  37. // Add post-invocation logic here
  38. return result;
  39. } catch (err) {
  40. // Add error handling logic here
  41. throw err;
  42. }
  43. }
  44. }