UNPKG

11.5 kBTypeScriptView Raw
1// Copyright (c) .NET Foundation. All rights reserved.
2// Licensed under the MIT License.
3
4import { CosmosDBInput, CosmosDBOutput } from './cosmosDB';
5import { EventGridOutput, EventGridPartialEvent } from './eventGrid';
6import { EventHubOutput } from './eventHub';
7import { HttpOutput, HttpResponse } from './http';
8import { FunctionInput, FunctionOutput, FunctionTrigger } from './index';
9import { ServiceBusQueueOutput, ServiceBusTopicOutput } from './serviceBus';
10import { SqlInput, SqlOutput } from './sql';
11import { StorageBlobInput, StorageBlobOutput, StorageQueueOutput } from './storage';
12import { TableInput, TableOutput } from './table';
13
14/**
15 * Contains metadata and helper methods specific to this invocation
16 */
17export declare class InvocationContext {
18 /**
19 * For testing purposes only. This will always be constructed for you when run in the context of the Azure Functions runtime
20 */
21 constructor(init?: InvocationContextInit);
22
23 /**
24 * A unique guid specific to this invocation
25 */
26 invocationId: string;
27
28 /**
29 * The name of the function that is being invoked
30 */
31 functionName: string;
32
33 /**
34 * An object used to get secondary inputs
35 */
36 extraInputs: InvocationContextExtraInputs;
37
38 /**
39 * An object used to set secondary outputs
40 */
41 extraOutputs: InvocationContextExtraOutputs;
42
43 /**
44 * The recommended way to log data during invocation.
45 * Similar to Node.js's `console.log`, but has integration with Azure features like application insights
46 * Uses the 'information' log level
47 */
48 log(...args: any[]): void;
49
50 /**
51 * The recommended way to log trace data (level 0) during invocation.
52 * Similar to Node.js's `console.trace`, but has integration with Azure features like application insights
53 */
54 trace(...args: any[]): void;
55
56 /**
57 * The recommended way to log debug data (level 1) during invocation.
58 * Similar to Node.js's `console.debug`, but has integration with Azure features like application insights
59 */
60 debug(...args: any[]): void;
61
62 /**
63 * The recommended way to log information data (level 2) during invocation.
64 * Similar to Node.js's `console.info`, but has integration with Azure features like application insights
65 */
66 info(...args: any[]): void;
67
68 /**
69 * The recommended way to log warning data (level 3) during invocation.
70 * Similar to Node.js's `console.warn`, but has integration with Azure features like application insights
71 */
72 warn(...args: any[]): void;
73
74 /**
75 * The recommended way to log error data (level 4) during invocation.
76 * Similar to Node.js's `console.error`, but has integration with Azure features like application insights
77 */
78 error(...args: any[]): void;
79
80 /**
81 * The retry context of the current function execution if the retry policy is defined
82 */
83 retryContext?: RetryContext;
84
85 /**
86 * TraceContext information to enable distributed tracing scenarios
87 */
88 traceContext?: TraceContext;
89
90 /**
91 * Metadata about the trigger or undefined if the metadata is already represented elsewhere
92 * For example, this will be undefined for http and timer triggers because you can find that information on the request & timer object instead
93 */
94 triggerMetadata?: TriggerMetadata;
95
96 /**
97 * The options used when registering the function
98 * NOTE: This value may differ slightly from the original because it has been validated and defaults may have been explicitly added
99 */
100 options: EffectiveFunctionOptions;
101}
102
103/**
104 * An object used to get secondary inputs
105 */
106export interface InvocationContextExtraInputs {
107 /**
108 * Get a secondary storage blob entry input for this invocation
109 * @input the configuration object for this storage blob input
110 */
111 get(input: StorageBlobInput): unknown;
112
113 /**
114 * Get a secondary table input for this invocation
115 * @input the configuration object for this table input
116 */
117 get(input: TableInput): unknown;
118
119 /**
120 * Get a secondary Cosmos DB documents input for this invocation
121 * @input the configuration object for this Cosmos DB input
122 */
123 get(input: CosmosDBInput): unknown;
124
125 /**
126 * Get a secondary SQL items input for this invocation
127 * @input the configuration object for this SQL input
128 */
129 get(input: SqlInput): unknown;
130
131 /**
132 * Get a secondary generic input for this invocation
133 * @inputOrName the configuration object or name for this input
134 */
135 get(inputOrName: FunctionInput | string): unknown;
136
137 /**
138 * Set a secondary generic input for this invocation
139 * @inputOrName the configuration object or name for this input
140 * @value the input value
141 */
142 set(inputOrName: FunctionInput | string, value: unknown): void;
143}
144
145/**
146 * An object used to set secondary outputs
147 */
148export interface InvocationContextExtraOutputs {
149 /**
150 * Set a secondary http response output for this invocation
151 * @output the configuration object for this http output
152 * @response the http response output value
153 */
154 set(output: HttpOutput, response: HttpResponse): void;
155
156 /**
157 * Set a secondary storage blob entry output for this invocation
158 * @output the configuration object for this storage blob output
159 * @blob the blob output value
160 */
161 set(output: StorageBlobOutput, blob: unknown): void;
162
163 /**
164 * Set a secondary table output for this invocation
165 * @output the configuration object for this table output
166 * @tableEntity the table output value
167 */
168 set(output: TableOutput, tableEntity: unknown): void;
169
170 /**
171 * Set a secondary storage queue entry output for this invocation
172 * @output the configuration object for this storage queue output
173 * @queueItem the queue entry output value
174 */
175 set(output: StorageQueueOutput, queueItem: unknown): void;
176
177 /**
178 * Set a secondary Cosmos DB documents output for this invocation
179 * @output the configuration object for this Cosmos DB output
180 * @documents the output document(s) value
181 */
182 set(output: CosmosDBOutput, documents: unknown): void;
183
184 /**
185 * Set a secondary SQL items output for this invocation
186 * @output the configuration object for this SQL output
187 * @documents the output item(s) value
188 */
189 set(output: SqlOutput, items: unknown): void;
190
191 /**
192 * Set a secondary Service Bus queue output for this invocation
193 * @output the configuration object for this Service Bus output
194 * @message the output message(s) value
195 */
196 set(output: ServiceBusQueueOutput, messages: unknown): void;
197
198 /**
199 * Set a secondary Service Bus topic output for this invocation
200 * @output the configuration object for this Service Bus output
201 * @message the output message(s) value
202 */
203 set(output: ServiceBusTopicOutput, messages: unknown): void;
204
205 /**
206 * Set a secondary Event Hub output for this invocation
207 * @output the configuration object for this EventHub output
208 * @message the output message(s) value
209 */
210 set(output: EventHubOutput, messages: unknown): void;
211
212 /**
213 * Set a secondary Event Grid output for this invocation
214 * @output the configuration object for this Event Grid output
215 * @message the output event(s) value
216 */
217 set(output: EventGridOutput, events: EventGridPartialEvent | EventGridPartialEvent[]): void;
218
219 /**
220 * Set a secondary generic output for this invocation
221 * @outputOrName the configuration object or name for this output
222 * @value the output value
223 */
224 set(outputOrName: FunctionOutput | string, value: unknown): void;
225
226 /**
227 * Get a secondary generic output for this invocation
228 * @outputOrName the configuration object or name for this output
229 */
230 get(outputOrName: FunctionOutput | string): unknown;
231}
232
233/**
234 * Metadata related to the input that triggered your function
235 */
236export type TriggerMetadata = Record<string, unknown>;
237
238export interface RetryContext {
239 /**
240 * Current retry count of the function executions.
241 */
242 retryCount: number;
243
244 /**
245 * Max retry count is the maximum number of times an execution is retried before eventual failure. A value of -1 means to retry indefinitely.
246 */
247 maxRetryCount: number;
248
249 /**
250 * Exception that caused the retry
251 */
252 exception?: Exception;
253}
254
255export interface Exception {
256 source?: string;
257
258 stackTrace?: string;
259
260 message?: string;
261}
262
263/**
264 * TraceContext information to enable distributed tracing scenarios
265 */
266export interface TraceContext {
267 /**
268 * Describes the position of the incoming request in its trace graph in a portable, fixed-length format
269 */
270 traceParent?: string | undefined;
271
272 /**
273 * Extends traceparent with vendor-specific data
274 */
275 traceState?: string | undefined;
276
277 /**
278 * Holds additional properties being sent as part of request telemetry
279 */
280 attributes?: Record<string, string>;
281}
282
283/**
284 * The options used when registering the function, as passed to a specific invocation
285 * NOTE: This value may differ slightly from the original because it has been validated and defaults may have been explicitly added
286 */
287export interface EffectiveFunctionOptions {
288 /**
289 * Configuration for the primary input to the function, aka the reason it will be triggered
290 * This is the only input that is passed as an argument to the function handler during invocation
291 */
292 trigger: FunctionTrigger;
293
294 /**
295 * Configuration for the optional primary output of the function
296 * This is the main output that you should set as the return value of the function handler during invocation
297 */
298 return?: FunctionOutput;
299
300 /**
301 * Configuration for an optional set of secondary inputs
302 * During invocation, get these values with `context.extraInputs.get()`
303 */
304 extraInputs: FunctionInput[];
305
306 /**
307 * Configuration for an optional set of secondary outputs
308 * During invocation, set these values with `context.extraOutputs.set()`
309 */
310 extraOutputs: FunctionOutput[];
311}
312
313/**
314 * For testing purposes only. This will always be constructed for you when run in the context of the Azure Functions runtime
315 */
316export interface InvocationContextInit {
317 /**
318 * Defaults to 'unknown' if not specified
319 */
320 invocationId?: string;
321
322 /**
323 * Defaults to 'unknown' if not specified
324 */
325 functionName?: string;
326
327 /**
328 * Defaults to Node.js console if not specified
329 */
330 logHandler?: LogHandler;
331
332 traceContext?: TraceContext;
333
334 retryContext?: RetryContext;
335
336 triggerMetadata?: TriggerMetadata;
337
338 /**
339 * Defaults to a trigger with 'unknown' type and name if not specified
340 */
341 options?: Partial<EffectiveFunctionOptions>;
342}
343
344export type LogHandler = (level: LogLevel, ...args: unknown[]) => void;
345
346export type LogLevel = 'trace' | 'debug' | 'information' | 'warning' | 'error' | 'critical' | 'none';