1 |
|
2 |
|
3 |
|
4 | import * as types from '@azure/functions';
|
5 | import {
|
6 | EffectiveFunctionOptions,
|
7 | InvocationContextInit,
|
8 | LogHandler,
|
9 | RetryContext,
|
10 | TraceContext,
|
11 | TriggerMetadata,
|
12 | } from '@azure/functions';
|
13 |
|
14 | export class InvocationContext implements types.InvocationContext {
|
15 | invocationId: string;
|
16 | functionName: string;
|
17 | extraInputs: InvocationContextExtraInputs;
|
18 | extraOutputs: InvocationContextExtraOutputs;
|
19 | retryContext?: RetryContext;
|
20 | traceContext?: TraceContext;
|
21 | triggerMetadata?: TriggerMetadata;
|
22 | options: EffectiveFunctionOptions;
|
23 | #userLogHandler: LogHandler;
|
24 |
|
25 | constructor(init?: InvocationContextInit) {
|
26 | init = init || {};
|
27 | const fallbackString = 'unknown';
|
28 | this.invocationId = init.invocationId || fallbackString;
|
29 | this.functionName = init.functionName || fallbackString;
|
30 | this.extraInputs = new InvocationContextExtraInputs();
|
31 | this.extraOutputs = new InvocationContextExtraOutputs();
|
32 | this.retryContext = init.retryContext;
|
33 | this.traceContext = init.traceContext;
|
34 | this.triggerMetadata = init.triggerMetadata;
|
35 | this.options = {
|
36 | trigger: init.options?.trigger || {
|
37 | name: fallbackString,
|
38 | type: fallbackString,
|
39 | },
|
40 | return: init.options?.return,
|
41 | extraInputs: init.options?.extraInputs || [],
|
42 | extraOutputs: init.options?.extraOutputs || [],
|
43 | };
|
44 | this.#userLogHandler = init.logHandler || fallbackLogHandler;
|
45 | }
|
46 |
|
47 | log(...args: unknown[]): void {
|
48 | this.#userLogHandler('information', ...args);
|
49 | }
|
50 |
|
51 | trace(...args: unknown[]): void {
|
52 | this.#userLogHandler('trace', ...args);
|
53 | }
|
54 |
|
55 | debug(...args: unknown[]): void {
|
56 | this.#userLogHandler('debug', ...args);
|
57 | }
|
58 |
|
59 | info(...args: unknown[]): void {
|
60 | this.#userLogHandler('information', ...args);
|
61 | }
|
62 |
|
63 | warn(...args: unknown[]): void {
|
64 | this.#userLogHandler('warning', ...args);
|
65 | }
|
66 |
|
67 | error(...args: unknown[]): void {
|
68 | this.#userLogHandler('error', ...args);
|
69 | }
|
70 | }
|
71 |
|
72 | class InvocationContextExtraInputs implements types.InvocationContextExtraInputs {
|
73 | #inputs: Record<string, unknown> = {};
|
74 | get(inputOrName: types.FunctionInput | string): any {
|
75 | const name = typeof inputOrName === 'string' ? inputOrName : inputOrName.name;
|
76 | return this.#inputs[name];
|
77 | }
|
78 | set(inputOrName: types.FunctionInput | string, value: unknown): void {
|
79 | const name = typeof inputOrName === 'string' ? inputOrName : inputOrName.name;
|
80 | this.#inputs[name] = value;
|
81 | }
|
82 | }
|
83 |
|
84 | class InvocationContextExtraOutputs implements types.InvocationContextExtraOutputs {
|
85 | #outputs: Record<string, unknown> = {};
|
86 | get(outputOrName: types.FunctionOutput | string): unknown {
|
87 | const name = typeof outputOrName === 'string' ? outputOrName : outputOrName.name;
|
88 | return this.#outputs[name];
|
89 | }
|
90 | set(outputOrName: types.FunctionOutput | string, value: unknown): void {
|
91 | const name = typeof outputOrName === 'string' ? outputOrName : outputOrName.name;
|
92 | this.#outputs[name] = value;
|
93 | }
|
94 | }
|
95 |
|
96 | function fallbackLogHandler(level: types.LogLevel, ...args: unknown[]): void {
|
97 | switch (level) {
|
98 | case 'trace':
|
99 | console.trace(...args);
|
100 | break;
|
101 | case 'debug':
|
102 | console.debug(...args);
|
103 | break;
|
104 | case 'information':
|
105 | console.info(...args);
|
106 | break;
|
107 | case 'warning':
|
108 | console.warn(...args);
|
109 | break;
|
110 | case 'critical':
|
111 | case 'error':
|
112 | console.error(...args);
|
113 | break;
|
114 | default:
|
115 | console.log(...args);
|
116 | }
|
117 | }
|