UNPKG

19.3 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 * @deprecated Use of sync functions with `context.done()` is not recommended. Use async/await and pass the response as the return value instead.
81 * See the docs here for more information: https://aka.ms/functions-js-async-await
82 */
83 done(err?: Error | string | null, result?: any): void;
84 /**
85 * HTTP request object. Provided to your function when using HTTP Bindings.
86 */
87 req?: HttpRequest;
88 /**
89 * HTTP response object. Provided to your function when using HTTP Bindings.
90 */
91 res?: {
92 [key: string]: any;
93 };
94 }
95 /**
96 * HTTP request headers.
97 */
98 export interface HttpRequestHeaders {
99 [name: string]: string;
100 }
101 /**
102 * HTTP response headers.
103 */
104 export interface HttpResponseHeaders {
105 [name: string]: string;
106 }
107 /**
108 * Query string parameter keys and values from the URL.
109 */
110 export interface HttpRequestQuery {
111 [name: string]: string;
112 }
113 /**
114 * Route parameter keys and values.
115 */
116 export interface HttpRequestParams {
117 [name: string]: string;
118 }
119 /**
120 * Object representing logged-in user, either through
121 * AppService/Functions authentication, or SWA Authentication
122 */
123 export interface HttpRequestUser {
124 /**
125 * Type of authentication, either AppService or StaticWebApps
126 */
127 type: HttpRequestUserType;
128 /**
129 * unique user GUID
130 */
131 id: string;
132 /**
133 * unique username
134 */
135 username: string;
136 /**
137 * provider of authentication service
138 */
139 identityProvider: string;
140 /**
141 * Extra authentication information, dependent on auth type
142 * and auth provider
143 */
144 claimsPrincipalData: {
145 [key: string]: any;
146 };
147 }
148 /**
149 * HTTP request object. Provided to your function when using HTTP Bindings.
150 */
151 export interface HttpRequest {
152 /**
153 * HTTP request method used to invoke this function.
154 */
155 method: HttpMethod | null;
156 /**
157 * Request URL.
158 */
159 url: string;
160 /**
161 * HTTP request headers.
162 */
163 headers: HttpRequestHeaders;
164 /**
165 * Query string parameter keys and values from the URL.
166 */
167 query: HttpRequestQuery;
168 /**
169 * Route parameter keys and values.
170 */
171 params: HttpRequestParams;
172 /**
173 * Object representing logged-in user, either through
174 * AppService/Functions authentication, or SWA Authentication
175 * null when no such user is logged in.
176 */
177 user: HttpRequestUser | null;
178 /**
179 * The HTTP request body.
180 */
181 body?: any;
182 /**
183 * The HTTP request body as a UTF-8 string.
184 */
185 rawBody?: any;
186
187 /**
188 * Parses the body and returns an object representing a form
189 * @throws if the content type is not "multipart/form-data" or "application/x-www-form-urlencoded"
190 */
191 parseFormBody(): Form;
192 }
193
194 export interface Form extends Iterable<[string, FormPart]> {
195 /**
196 * Returns the value of the first name-value pair whose name is `name`. If there are no such pairs, `null` is returned.
197 */
198 get(name: string): FormPart | null;
199
200 /**
201 * Returns the values of all name-value pairs whose name is `name`. If there are no such pairs, an empty array is returned.
202 */
203 getAll(name: string): FormPart[];
204
205 /**
206 * Returns `true` if there is at least one name-value pair whose name is `name`.
207 */
208 has(name: string): boolean;
209
210 /**
211 * The number of parts in this form
212 */
213 length: number;
214 }
215
216 export interface FormPart {
217 /**
218 * The value for this part of the form
219 */
220 value: Buffer;
221
222 /**
223 * The file name for this part of the form, if specified
224 */
225 fileName?: string;
226
227 /**
228 * The content type for this part of the form, assumed to be "text/plain" if not specified
229 */
230 contentType?: string;
231 }
232
233 /**
234 * Possible values for an HTTP request method.
235 */
236 export type HttpMethod = 'GET' | 'POST' | 'DELETE' | 'HEAD' | 'PATCH' | 'PUT' | 'OPTIONS' | 'TRACE' | 'CONNECT';
237 /**
238 * Possible values for an HTTP Request user type
239 */
240 export type HttpRequestUserType = 'AppService' | 'StaticWebApps';
241 /**
242 * Http response object and methods.
243 * This is the default of the res property in the Context object provided to your function when using HTTP triggers.
244 */
245 export interface HttpResponseFull {
246 /**
247 * HTTP response headers.
248 */
249 headers?: HttpResponseHeaders;
250 /**
251 * HTTP response cookies.
252 */
253 cookies?: Cookie[];
254 /**
255 * HTTP response body.
256 */
257 body?: any;
258 /**
259 * HTTP response status code.
260 * @default 200
261 */
262 statusCode?: number | string;
263 /**
264 * Enable content negotiation of response body if true
265 * If false, treat response body as raw
266 * @default false
267 */
268 enableContentNegotiation?: boolean;
269 /**
270 * Sets the HTTP response status code
271 * @returns the updated HttpResponseFull instance
272 */
273 status: (statusCode: number | string) => HttpResponseFull;
274 /**
275 * Sets a particular header field to a value
276 * @returns the updated HttpResponseFull instance
277 */
278 setHeader(field: string, val: any): HttpResponseFull;
279 /**
280 * Has the same functionality as setHeader.
281 * Sets a particular header field to a value
282 * @returns the updated HttpResponseFull instance
283 */
284 header(field: string, val: any): HttpResponseFull;
285 /**
286 * Has the same functionality as setHeader.
287 * Sets a particular header field to a value
288 * @returns the updated HttpResponseFull instance
289 */
290 set(field: string, val: any): HttpResponseFull;
291 /**
292 * Get the value of a particular header field
293 */
294 getHeader(field: string): any;
295 /**
296 * Has the same functionality as getHeader
297 * Get the value of a particular header field
298 */
299 get(field: string): any;
300 /**
301 * Removes a particular header field
302 * @returns the updated HttpResponseFull instance
303 */
304 removeHeader(field: string): HttpResponseFull;
305 /**
306 * Set the 'Content-Type' header to a particular value
307 * @returns the updated HttpResponseFull instance
308 */
309 type(type: string): HttpResponseFull;
310 /**
311 * Automatically sets the content-type then calls context.done()
312 * @returns updated HttpResponseFull instance
313 * @deprecated this method calls context.done() which is deprecated, use async/await and pass the response as the return value instead.
314 * See the docs here for more information: https://aka.ms/functions-js-async-await
315 */
316 send(body?: any): HttpResponseFull;
317 /**
318 * Same as send()
319 * Automatically sets the content-type then calls context.done()
320 * @returns updated HttpResponseFull instance
321 * @deprecated this method calls context.done() which is deprecated, use async/await and pass the response as your function's return value instead.
322 * See the docs here for more information: https://aka.ms/functions-js-async-await
323 */
324 end(body?: any): HttpResponseFull;
325 /**
326 * Sets the status code then calls send()
327 * @returns updated HttpResponseFull instance
328 * @deprecated this method calls context.done() which is deprecated, use async/await and pass the response as your function's return value instead.
329 * See the docs here for more information: https://aka.ms/functions-js-async-await
330 */
331 sendStatus(statusCode: string | number): HttpResponseFull;
332 /**
333 * Sets the 'Content-Type' header to 'application/json' then calls send(body)
334 * @deprecated this method calls context.done() which is deprecated, use async/await and pass the response as your function's return value instead.
335 * See the docs here for more information: https://aka.ms/functions-js-async-await
336 */
337 json(body?: any): void;
338 }
339 /**
340 * Http response object.
341 * This is not the default on the Context object, but you may replace context.res with an object of this type when using HTTP triggers.
342 */
343 export interface HttpResponseSimple {
344 /**
345 * HTTP response headers.
346 */
347 headers?: HttpResponseHeaders;
348 /**
349 * HTTP response cookies.
350 */
351 cookies?: Cookie[];
352 /**
353 * HTTP response body.
354 */
355 body?: any;
356 /**
357 * HTTP response status code.
358 * This property takes precedence over the `status` property
359 * @default 200
360 */
361 statusCode?: number | string;
362 /**
363 * HTTP response status code
364 * The same as `statusCode`. This property is ignored if `statusCode` is set
365 * @default 200
366 */
367 status?: number | string;
368 /**
369 * Enable content negotiation of response body if true
370 * If false, treat response body as raw
371 * @default false
372 */
373 enableContentNegotiation?: boolean;
374 }
375 /**
376 * Http response type.
377 */
378 export type HttpResponse = HttpResponseSimple | HttpResponseFull;
379
380 /**
381 * Http response cookie object to "Set-Cookie"
382 */
383 export interface Cookie {
384 /** Cookie name */
385 name: string;
386 /** Cookie value */
387 value: string;
388 /** Specifies allowed hosts to receive the cookie */
389 domain?: string;
390 /** Specifies URL path that must exist in the requested URL */
391 path?: string;
392 /**
393 * NOTE: It is generally recommended that you use maxAge over expires.
394 * Sets the cookie to expire at a specific date instead of when the client closes.
395 * This can be a Javascript Date or Unix time in milliseconds.
396 */
397 expires?: Date | number;
398 /** Sets the cookie to only be sent with an encrypted request */
399 secure?: boolean;
400 /** Sets the cookie to be inaccessible to JavaScript's Document.cookie API */
401 httpOnly?: boolean;
402 /** Can restrict the cookie to not be sent with cross-site requests */
403 sameSite?: 'Strict' | 'Lax' | 'None' | undefined;
404 /** Number of seconds until the cookie expires. A zero or negative number will expire the cookie immediately. */
405 maxAge?: number;
406 }
407 export interface ExecutionContext {
408 /**
409 * A unique GUID per function invocation.
410 */
411 invocationId: string;
412 /**
413 * The name of the function that is being invoked. The name of your function is always the same as the
414 * name of the corresponding function.json's parent directory.
415 */
416 functionName: string;
417 /**
418 * The directory your function is in (this is the parent directory of this function's function.json).
419 */
420 functionDirectory: string;
421 /**
422 * The retry context of the current function execution or null if the retry policy is not defined.
423 */
424 retryContext: RetryContext | null;
425 }
426 export interface RetryContext {
427 /**
428 * Current retry count of the function executions.
429 */
430 retryCount: number;
431 /**
432 * Max retry count is the maximum number of times an execution is retried before eventual failure. A value of -1 means to retry indefinitely.
433 */
434 maxRetryCount: number;
435 /**
436 * Exception that caused the retry
437 */
438 exception?: Exception;
439 }
440 export interface Exception {
441 /** Exception source */
442 source?: string | null;
443 /** Exception stackTrace */
444 stackTrace?: string | null;
445 /** Exception message */
446 message?: string | null;
447 }
448 /**
449 * TraceContext information to enable distributed tracing scenarios.
450 */
451 export interface TraceContext {
452 /** Describes the position of the incoming request in its trace graph in a portable, fixed-length format. */
453 traceparent: string | null | undefined;
454 /** Extends traceparent with vendor-specific data. */
455 tracestate: string | null | undefined;
456 /** Holds additional properties being sent as part of request telemetry. */
457 attributes:
458 | {
459 [k: string]: string;
460 }
461 | null
462 | undefined;
463 }
464 export interface BindingDefinition {
465 /**
466 * The name of your binding, as defined in function.json.
467 */
468 name: string;
469 /**
470 * The type of your binding, as defined in function.json.
471 */
472 type: string;
473 /**
474 * The direction of your binding, as defined in function.json.
475 */
476 direction: 'in' | 'out' | 'inout' | undefined;
477 }
478 /**
479 * Allows you to write streaming function logs.
480 */
481 export interface Logger {
482 /**
483 * Writes streaming function logs at the default trace level.
484 */
485 (...args: any[]): void;
486 /**
487 * Writes to error level logging or lower.
488 */
489 error(...args: any[]): void;
490 /**
491 * Writes to warning level logging or lower.
492 */
493 warn(...args: any[]): void;
494 /**
495 * Writes to info level logging or lower.
496 */
497 info(...args: any[]): void;
498 /**
499 * Writes to verbose level logging.
500 */
501 verbose(...args: any[]): void;
502 }
503 /**
504 * Timer schedule information. Provided to your function when using a timer binding.
505 */
506 export interface Timer {
507 /**
508 * Whether this timer invocation is due to a missed schedule occurrence.
509 */
510 isPastDue: boolean;
511 schedule: {
512 /**
513 * Whether intervals between invocations should account for DST.
514 */
515 adjustForDST: boolean;
516 };
517 scheduleStatus: {
518 /**
519 * The last recorded schedule occurrence. Date ISO string.
520 */
521 last: string;
522 /**
523 * The expected next schedule occurrence. Date ISO string.
524 */
525 next: string;
526 /**
527 * 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.
528 */
529 lastUpdated: string;
530 };
531 }
532}