UNPKG

6.19 kBTypeScriptView Raw
1// Copyright (c) .NET Foundation. All rights reserved.
2// Licensed under the MIT License.
3
4import { HttpRequest } from './http';
5
6/**
7 * The context object can be used for writing logs, reading data from bindings, setting outputs and using
8 * the context.done callback when your exported function is synchronous. A context object is passed
9 * to your function from the Azure Functions runtime on function invocation.
10 */
11export interface Context {
12 /**
13 * A unique GUID per function invocation.
14 */
15 invocationId: string;
16 /**
17 * Function execution metadata.
18 */
19 executionContext: ExecutionContext;
20 /**
21 * Input and trigger binding data, as defined in function.json. Properties on this object are dynamically
22 * generated and named based off of the "name" property in function.json.
23 */
24 bindings: ContextBindings;
25 /**
26 * Trigger metadata and function invocation data.
27 */
28 bindingData: ContextBindingData;
29 /**
30 * TraceContext information to enable distributed tracing scenarios.
31 */
32 traceContext: TraceContext;
33 /**
34 * Bindings your function uses, as defined in function.json.
35 */
36 bindingDefinitions: BindingDefinition[];
37 /**
38 * Allows you to write streaming function logs. Calling directly allows you to write streaming function logs
39 * at the default trace level.
40 */
41 log: Logger;
42 /**
43 * A callback function that signals to the runtime that your code has completed. If your function is synchronous,
44 * you must call context.done at the end of execution. If your function is asynchronous, you should not use this
45 * callback.
46 *
47 * @param err A user-defined error to pass back to the runtime. If present, your function execution will fail.
48 * @param result An object containing output binding data. `result` will be passed to JSON.stringify unless it is
49 * a string, Buffer, ArrayBufferView, or number.
50 *
51 * @deprecated Use of sync functions with `context.done()` is not recommended. Use async/await and pass the response as the return value instead.
52 * See the docs here for more information: https://aka.ms/functions-js-async-await
53 */
54 done(err?: Error | string | null, result?: any): void;
55 /**
56 * HTTP request object. Provided to your function when using HTTP Bindings.
57 */
58 req?: HttpRequest;
59 /**
60 * HTTP response object. Provided to your function when using HTTP Bindings.
61 */
62 res?: {
63 [key: string]: any;
64 };
65 /**
66 * If this flag is set to true in your function, the error for calling `context.done()` within
67 * an async function will not be logged. More info: https://go.microsoft.com/fwlink/?linkid=2097909
68 * @default false
69 */
70 suppressAsyncDoneError?: boolean;
71}
72
73/**
74 * Context bindings object. Provided to your function binding data, as defined in function.json.
75 */
76export interface ContextBindings {
77 [name: string]: any;
78}
79
80/**
81 * Context binding data. Provided to your function trigger metadata and function invocation data.
82 */
83export interface ContextBindingData {
84 /**
85 * A unique GUID per function invocation.
86 */
87 invocationId: string;
88
89 [name: string]: any;
90}
91
92export interface ExecutionContext {
93 /**
94 * A unique GUID per function invocation.
95 */
96 invocationId: string;
97 /**
98 * The name of the function that is being invoked. The name of your function is always the same as the
99 * name of the corresponding function.json's parent directory.
100 */
101 functionName: string;
102 /**
103 * The directory your function is in (this is the parent directory of this function's function.json).
104 */
105 functionDirectory: string;
106 /**
107 * The retry context of the current function execution or null if the retry policy is not defined.
108 */
109 retryContext: RetryContext | null;
110}
111
112export interface RetryContext {
113 /**
114 * Current retry count of the function executions.
115 */
116 retryCount: number;
117 /**
118 * Max retry count is the maximum number of times an execution is retried before eventual failure. A value of -1 means to retry indefinitely.
119 */
120 maxRetryCount: number;
121 /**
122 * Exception that caused the retry
123 */
124 exception?: Exception;
125}
126
127export interface Exception {
128 /** Exception source */
129 source?: string | null;
130 /** Exception stackTrace */
131 stackTrace?: string | null;
132 /** Exception message */
133 message?: string | null;
134}
135
136/**
137 * TraceContext information to enable distributed tracing scenarios.
138 */
139export interface TraceContext {
140 /** Describes the position of the incoming request in its trace graph in a portable, fixed-length format. */
141 traceparent: string | null | undefined;
142 /** Extends traceparent with vendor-specific data. */
143 tracestate: string | null | undefined;
144 /** Holds additional properties being sent as part of request telemetry. */
145 attributes:
146 | {
147 [k: string]: string;
148 }
149 | null
150 | undefined;
151}
152
153export interface BindingDefinition {
154 /**
155 * The name of your binding, as defined in function.json.
156 */
157 name: string;
158 /**
159 * The type of your binding, as defined in function.json.
160 */
161 type: string;
162 /**
163 * The direction of your binding, as defined in function.json.
164 */
165 direction: 'in' | 'out' | 'inout' | undefined;
166}
167
168/**
169 * Allows you to write streaming function logs.
170 */
171export interface Logger {
172 /**
173 * Writes streaming function logs at the default trace level.
174 */
175 (...args: any[]): void;
176 /**
177 * Writes to error level logging or lower.
178 */
179 error(...args: any[]): void;
180 /**
181 * Writes to warning level logging or lower.
182 */
183 warn(...args: any[]): void;
184 /**
185 * Writes to info level logging or lower.
186 */
187 info(...args: any[]): void;
188 /**
189 * Writes to verbose level logging.
190 */
191 verbose(...args: any[]): void;
192}