UNPKG

3.42 kBTypeScriptView Raw
1import { Context } from './context';
2import { ResolutionSession } from './resolution-session';
3import { ValueOrPromise } from './value-promise';
4/**
5 * Return value for a method invocation
6 */
7export type InvocationResult = any;
8/**
9 * Array of arguments for a method invocation
10 */
11export type InvocationArgs = any[];
12/**
13 * An interface to represent the caller of the invocation
14 */
15export interface InvocationSource<T = unknown> {
16 /**
17 * Type of the invoker, such as `proxy` and `route`
18 */
19 readonly type: string;
20 /**
21 * Metadata for the source, such as `ResolutionSession`
22 */
23 readonly value: T;
24}
25/**
26 * InvocationContext represents the context to invoke interceptors for a method.
27 * The context can be used to access metadata about the invocation as well as
28 * other dependencies.
29 */
30export declare class InvocationContext extends Context {
31 readonly target: object;
32 readonly methodName: string;
33 readonly args: InvocationArgs;
34 readonly source?: InvocationSource<unknown> | undefined;
35 /**
36 * Construct a new instance of `InvocationContext`
37 * @param parent - Parent context, such as the RequestContext
38 * @param target - Target class (for static methods) or prototype/object
39 * (for instance methods)
40 * @param methodName - Method name
41 * @param args - An array of arguments
42 */
43 constructor(parent: Context, target: object, methodName: string, args: InvocationArgs, source?: InvocationSource<unknown> | undefined);
44 /**
45 * The target class, such as `OrderController`
46 */
47 get targetClass(): Function;
48 /**
49 * The target name, such as `OrderController.prototype.cancelOrder`
50 */
51 get targetName(): string;
52 /**
53 * Description of the invocation
54 */
55 get description(): string;
56 toString(): string;
57 /**
58 * Assert the method exists on the target. An error will be thrown if otherwise.
59 * @param context - Invocation context
60 */
61 assertMethodExists(): Record<string, Function>;
62 /**
63 * Invoke the target method with the given context
64 * @param context - Invocation context
65 * @param options - Options for the invocation
66 */
67 invokeTargetMethod(options?: InvocationOptions): any;
68}
69/**
70 * Options to control invocations
71 */
72export type InvocationOptions = {
73 /**
74 * Skip dependency injection on method parameters
75 */
76 skipParameterInjection?: boolean;
77 /**
78 * Skip invocation of interceptors
79 */
80 skipInterceptors?: boolean;
81 /**
82 * Information about the source object that makes the invocation. For REST,
83 * it's a `Route`. For injected proxies, it's a `Binding`.
84 */
85 source?: InvocationSource;
86 /**
87 * Resolution session
88 */
89 session?: ResolutionSession;
90};
91/**
92 * Invoke a method using dependency injection. Interceptors are invoked as part
93 * of the invocation.
94 * @param target - Target of the method, it will be the class for a static
95 * method, and instance or class prototype for a prototype method
96 * @param method - Name of the method
97 * @param ctx - Context object
98 * @param nonInjectedArgs - Optional array of args for non-injected parameters
99 * @param options - Options for the invocation
100 */
101export declare function invokeMethod(target: object, method: string, ctx: Context, nonInjectedArgs?: InvocationArgs, options?: InvocationOptions): ValueOrPromise<InvocationResult>;