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.
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
import {
/* inject, */
globalInterceptor,
bind,
Interceptor,
Provider,
} from '@loopback/context';
/**
* This class will be bound to the application as a global `Interceptor` during
* `boot`
*/
@globalInterceptor('auth', {tags: {name: 'test'}})
export class TestInterceptor implements Provider<Interceptor> {
/*
constructor() {}
*/
/**
* This method is used by LoopBack context to produce an interceptor function
* for the binding.
*
* @returns An interceptor function
*/
value() {
return this.intercept.bind(this);
}
/**
* The logic to intercept an invocation
* @param invocationCtx - Invocation context
* @param next - A function to invoke next interceptor or the target method
*/
async intercept(
invocationCtx: InvocationContext,
next: () => ValueOrPromise<InvocationResult>,
) {
try {
// Add pre-invocation logic here
const result = await next();
// Add post-invocation logic here
return result;
} catch (err) {
// Add error handling logic here
throw err;
}
}
}
- A non-global interceptor
import {
/* inject, */
bind,
Interceptor,
Provider,
} from '@loopback/context';
/**
* This class will be bound to the application as a global `Interceptor` during
* `boot`
*/
@bind({tags: {namespace: 'interceptors', name: 'test'}})
export class TestInterceptor implements Provider<Interceptor> {
/*
constructor() {}
*/
/**
* This method is used by LoopBack context to produce an interceptor function
* for the binding.
*
* @returns An interceptor function
*/
value() {
return this.intercept.bind(this);
}
/**
* The logic to intercept an invocation
* @param invocationCtx - Invocation context
* @param next - A function to invoke next interceptor or the target method
*/
async intercept(
invocationCtx: InvocationContext,
next: () => ValueOrPromise<InvocationResult>,
) {
try {
// Add pre-invocation logic here
const result = await next();
// Add post-invocation logic here
return result;
} catch (err) {
// Add error handling logic here
throw err;
}
}
}