UNPKG

10.6 kBTypeScriptView Raw
1// Copyright (c) .NET Foundation. All rights reserved.
2// Licensed under the MIT License.
3
4declare module '@azure/functions' {
5 /**
6 * Interface for your Azure Function code. This function must be exported (via module.exports or exports)
7 * and will execute when triggered. It is recommended that you declare this function as async, which
8 * implicitly returns a Promise.
9 * @param context Context object passed to your function from the Azure Functions runtime.
10 * @param {any[]} args Optional array of input and trigger binding data. These binding data are passed to the
11 * function in the same order that they are defined in function.json. Valid input types are string, HttpRequest,
12 * and Buffer.
13 * @returns Output bindings (optional). If you are returning a result from a Promise (or an async function), this
14 * result will be passed to JSON.stringify unless it is a string, Buffer, ArrayBufferView, or number.
15 */
16 export type AzureFunction = (context: Context, ...args: any[]) => Promise<any> | void;
17
18 /**
19 * Context bindings object. Provided to your function binding data, as defined in function.json.
20 */
21 export interface ContextBindings {
22 [name: string]: any;
23 }
24 /**
25 * Context binding data. Provided to your function trigger metadata and function invocation data.
26 */
27 export interface ContextBindingData {
28 /**
29 * A unique GUID per function invocation.
30 */
31 invocationId: string;
32
33 [name: string]: any;
34 }
35 /**
36 * The context object can be used for writing logs, reading data from bindings, setting outputs and using
37 * the context.done callback when your exported function is synchronous. A context object is passed
38 * to your function from the Azure Functions runtime on function invocation.
39 */
40 export interface Context {
41 /**
42 * A unique GUID per function invocation.
43 */
44 invocationId: string;
45 /**
46 * Function execution metadata.
47 */
48 executionContext: ExecutionContext;
49 /**
50 * Input and trigger binding data, as defined in function.json. Properties on this object are dynamically
51 * generated and named based off of the "name" property in function.json.
52 */
53 bindings: ContextBindings;
54 /**
55 * Trigger metadata and function invocation data.
56 */
57 bindingData: ContextBindingData;
58 /**
59 * TraceContext information to enable distributed tracing scenarios.
60 */
61 traceContext: TraceContext;
62 /**
63 * Bindings your function uses, as defined in function.json.
64 */
65 bindingDefinitions: BindingDefinition[];
66 /**
67 * Allows you to write streaming function logs. Calling directly allows you to write streaming function logs
68 * at the default trace level.
69 */
70 log: Logger;
71 /**
72 * A callback function that signals to the runtime that your code has completed. If your function is synchronous,
73 * you must call context.done at the end of execution. If your function is asynchronous, you should not use this
74 * callback.
75 *
76 * @param err A user-defined error to pass back to the runtime. If present, your function execution will fail.
77 * @param result An object containing output binding data. `result` will be passed to JSON.stringify unless it is
78 * a string, Buffer, ArrayBufferView, or number.
79 */
80 done(err?: Error | string | null, result?: any): void;
81 /**
82 * HTTP request object. Provided to your function when using HTTP Bindings.
83 */
84 req?: HttpRequest;
85 /**
86 * HTTP response object. Provided to your function when using HTTP Bindings.
87 */
88 res?: {
89 [key: string]: any;
90 };
91 }
92 /**
93 * HTTP request headers.
94 */
95 export interface HttpRequestHeaders {
96 [name: string]: string;
97 }
98 /**
99 * Query string parameter keys and values from the URL.
100 */
101 export interface HttpRequestQuery {
102 [name: string]: string;
103 }
104 /**
105 * Route parameter keys and values.
106 */
107 export interface HttpRequestParams {
108 [name: string]: string;
109 }
110 /**
111 * HTTP request object. Provided to your function when using HTTP Bindings.
112 */
113 export interface HttpRequest {
114 /**
115 * HTTP request method used to invoke this function.
116 */
117 method: HttpMethod | null;
118 /**
119 * Request URL.
120 */
121 url: string;
122 /**
123 * HTTP request headers.
124 */
125 headers: HttpRequestHeaders;
126 /**
127 * Query string parameter keys and values from the URL.
128 */
129 query: HttpRequestQuery;
130 /**
131 * Route parameter keys and values.
132 */
133 params: HttpRequestParams;
134 /**
135 * The HTTP request body.
136 */
137 body?: any;
138 /**
139 * The HTTP request body as a UTF-8 string.
140 */
141 rawBody?: any;
142 }
143 /**
144 * Possible values for an HTTP request method.
145 */
146 export type HttpMethod = 'GET' | 'POST' | 'DELETE' | 'HEAD' | 'PATCH' | 'PUT' | 'OPTIONS' | 'TRACE' | 'CONNECT';
147 /**
148 * Http response cookie object to "Set-Cookie"
149 */
150 export interface Cookie {
151 /** Cookie name */
152 name: string;
153 /** Cookie value */
154 value: string;
155 /** Specifies allowed hosts to receive the cookie */
156 domain?: string;
157 /** Specifies URL path that must exist in the requested URL */
158 path?: string;
159 /**
160 * NOTE: It is generally recommended that you use maxAge over expires.
161 * Sets the cookie to expire at a specific date instead of when the client closes.
162 * This can be a Javascript Date or Unix time in milliseconds.
163 */
164 expires?: Date | number;
165 /** Sets the cookie to only be sent with an encrypted request */
166 secure?: boolean;
167 /** Sets the cookie to be inaccessible to JavaScript's Document.cookie API */
168 httpOnly?: boolean;
169 /** Can restrict the cookie to not be sent with cross-site requests */
170 sameSite?: 'Strict' | 'Lax' | 'None' | undefined;
171 /** Number of seconds until the cookie expires. A zero or negative number will expire the cookie immediately. */
172 maxAge?: number;
173 }
174 export interface ExecutionContext {
175 /**
176 * A unique GUID per function invocation.
177 */
178 invocationId: string;
179 /**
180 * The name of the function that is being invoked. The name of your function is always the same as the
181 * name of the corresponding function.json's parent directory.
182 */
183 functionName: string;
184 /**
185 * The directory your function is in (this is the parent directory of this function's function.json).
186 */
187 functionDirectory: string;
188 /**
189 * The retry context of the current function execution or null if the retry policy is not defined.
190 */
191 retryContext: RetryContext | null;
192 }
193 export interface RetryContext {
194 /**
195 * Current retry count of the function executions.
196 */
197 retryCount: number;
198 /**
199 * Max retry count is the maximum number of times an execution is retried before eventual failure. A value of -1 means to retry indefinitely.
200 */
201 maxRetryCount: number;
202 /**
203 * Exception that caused the retry
204 */
205 exception?: Exception;
206 }
207 export interface Exception {
208 /** Exception source */
209 source?: string | null;
210 /** Exception stackTrace */
211 stackTrace?: string | null;
212 /** Exception message */
213 message?: string | null;
214 }
215 /**
216 * TraceContext information to enable distributed tracing scenarios.
217 */
218 export interface TraceContext {
219 /** Describes the position of the incoming request in its trace graph in a portable, fixed-length format. */
220 traceparent: string | null | undefined;
221 /** Extends traceparent with vendor-specific data. */
222 tracestate: string | null | undefined;
223 /** Holds additional properties being sent as part of request telemetry. */
224 attributes:
225 | {
226 [k: string]: string;
227 }
228 | null
229 | undefined;
230 }
231 export interface BindingDefinition {
232 /**
233 * The name of your binding, as defined in function.json.
234 */
235 name: string;
236 /**
237 * The type of your binding, as defined in function.json.
238 */
239 type: string;
240 /**
241 * The direction of your binding, as defined in function.json.
242 */
243 direction: 'in' | 'out' | 'inout' | undefined;
244 }
245 /**
246 * Allows you to write streaming function logs.
247 */
248 export interface Logger {
249 /**
250 * Writes streaming function logs at the default trace level.
251 */
252 (...args: any[]): void;
253 /**
254 * Writes to error level logging or lower.
255 */
256 error(...args: any[]): void;
257 /**
258 * Writes to warning level logging or lower.
259 */
260 warn(...args: any[]): void;
261 /**
262 * Writes to info level logging or lower.
263 */
264 info(...args: any[]): void;
265 /**
266 * Writes to verbose level logging.
267 */
268 verbose(...args: any[]): void;
269 }
270 /**
271 * Timer schedule information. Provided to your function when using a timer binding.
272 */
273 export interface Timer {
274 /**
275 * Whether this timer invocation is due to a missed schedule occurrence.
276 */
277 isPastDue: boolean;
278 schedule: {
279 /**
280 * Whether intervals between invocations should account for DST.
281 */
282 adjustForDST: boolean;
283 };
284 scheduleStatus: {
285 /**
286 * The last recorded schedule occurrence. Date ISO string.
287 */
288 last: string;
289 /**
290 * The expected next schedule occurrence. Date ISO string.
291 */
292 next: string;
293 /**
294 * The last time this record was updated. This is used to re-calculate `next` with the current schedule after a host restart. Date ISO string.
295 */
296 lastUpdated: string;
297 };
298 }
299}