UNPKG

5.41 kBTypeScriptView Raw
1import { MetadataAccessor } from '@loopback/metadata';
2import { Binding, BindingTemplate } from './binding';
3import { BindingFromClassOptions, BindingSpec } from './binding-inspector';
4import { Context } from './context';
5import { GenericInterceptor, GenericInterceptorOrKey } from './interceptor-chain';
6import { InvocationArgs, InvocationContext, InvocationOptions, InvocationResult } from './invocation';
7import { Provider } from './provider';
8import { Constructor, ValueOrPromise } from './value-promise';
9/**
10 * A specialized InvocationContext for interceptors
11 */
12export declare class InterceptedInvocationContext extends InvocationContext {
13 /**
14 * Discover all binding keys for global interceptors (tagged by
15 * ContextTags.GLOBAL_INTERCEPTOR)
16 */
17 getGlobalInterceptorBindingKeys(): string[];
18 /**
19 * Check if the binding for a global interceptor matches the source type
20 * of the invocation
21 * @param binding - Binding
22 */
23 private applicableTo;
24 /**
25 * Sort global interceptor bindings by `globalInterceptorGroup` tags
26 * @param bindings - An array of global interceptor bindings
27 */
28 private sortGlobalInterceptorBindings;
29 /**
30 * Load all interceptors for the given invocation context. It adds
31 * interceptors from possibly three sources:
32 * 1. method level `@intercept`
33 * 2. class level `@intercept`
34 * 3. global interceptors discovered in the context
35 */
36 loadInterceptors(): InterceptorOrKey[];
37}
38/**
39 * The `BindingTemplate` function to configure a binding as a global interceptor
40 * by tagging it with `ContextTags.INTERCEPTOR`
41 * @param group - Group for ordering the interceptor
42 */
43export declare function asGlobalInterceptor(group?: string): BindingTemplate;
44/**
45 * `@globalInterceptor` decorator to mark the class as a global interceptor
46 * @param group - Group for ordering the interceptor
47 * @param specs - Extra binding specs
48 */
49export declare function globalInterceptor(group?: string, ...specs: BindingSpec[]): ClassDecorator;
50/**
51 * Interceptor function to intercept method invocations
52 */
53export interface Interceptor extends GenericInterceptor<InvocationContext> {
54}
55/**
56 * Interceptor function or binding key that can be used as parameters for
57 * `@intercept()`
58 */
59export type InterceptorOrKey = GenericInterceptorOrKey<InvocationContext>;
60/**
61 * Metadata key for method-level interceptors
62 */
63export declare const INTERCEPT_METHOD_KEY: MetadataAccessor<InterceptorOrKey[], MethodDecorator>;
64/**
65 * Adding interceptors from the spec to the front of existing ones. Duplicate
66 * entries are eliminated from the spec side.
67 *
68 * For example:
69 *
70 * - [log] + [cache, log] => [cache, log]
71 * - [log] + [log, cache] => [log, cache]
72 * - [] + [cache, log] => [cache, log]
73 * - [cache, log] + [] => [cache, log]
74 * - [log] + [cache] => [log, cache]
75 *
76 * @param interceptorsFromSpec - Interceptors from `@intercept`
77 * @param existingInterceptors - Interceptors already applied for the method
78 */
79export declare function mergeInterceptors(interceptorsFromSpec: InterceptorOrKey[], existingInterceptors: InterceptorOrKey[]): InterceptorOrKey[];
80/**
81 * Metadata key for method-level interceptors
82 */
83export declare const INTERCEPT_CLASS_KEY: MetadataAccessor<InterceptorOrKey[], ClassDecorator>;
84/**
85 * Decorator function `@intercept` for classes/methods to apply interceptors. It
86 * can be applied on a class and its public methods. Multiple occurrences of
87 * `@intercept` are allowed on the same target class or method. The decorator
88 * takes a list of `interceptor` functions or binding keys.
89 *
90 * @example
91 * ```ts
92 * @intercept(log, metrics)
93 * class MyController {
94 * @intercept('caching-interceptor')
95 * @intercept('name-validation-interceptor')
96 * greet(name: string) {
97 * return `Hello, ${name}`;
98 * }
99 * }
100 * ```
101 *
102 * @param interceptorOrKeys - One or more interceptors or binding keys that are
103 * resolved to be interceptors
104 */
105export declare function intercept(...interceptorOrKeys: InterceptorOrKey[]): (target: any, method?: string, methodDescriptor?: TypedPropertyDescriptor<any>) => any;
106/**
107 * Invoke a method with the given context
108 * @param context - Context object
109 * @param target - Target class (for static methods) or object (for instance methods)
110 * @param methodName - Method name
111 * @param args - An array of argument values
112 * @param options - Options for the invocation
113 */
114export declare function invokeMethodWithInterceptors(context: Context, target: object, methodName: string, args: InvocationArgs, options?: InvocationOptions): ValueOrPromise<InvocationResult>;
115/**
116 * Options for an interceptor binding
117 */
118export interface InterceptorBindingOptions extends BindingFromClassOptions {
119 /**
120 * Global or local interceptor
121 */
122 global?: boolean;
123 /**
124 * Group name for a global interceptor
125 */
126 group?: string;
127 /**
128 * Source filter for a global interceptor
129 */
130 source?: string | string[];
131}
132/**
133 * Register an interceptor function or provider class to the given context
134 * @param ctx - Context object
135 * @param interceptor - An interceptor function or provider class
136 * @param options - Options for the interceptor binding
137 */
138export declare function registerInterceptor(ctx: Context, interceptor: Interceptor | Constructor<Provider<Interceptor>>, options?: InterceptorBindingOptions): Binding<Interceptor>;