UNPKG

9.37 kBTypeScriptView Raw
1// Copyright (c) .NET Foundation. All rights reserved.
2// Licensed under the MIT License.
3
4import { Blob } from 'buffer';
5import { ReadableStream } from 'stream/web';
6import { BodyInit, FormData, Headers, HeadersInit } from 'undici';
7import { URLSearchParams } from 'url';
8import { FunctionOptions, FunctionOutput, FunctionResult, FunctionTrigger } from './index';
9import { InvocationContext } from './InvocationContext';
10
11export type HttpHandler = (
12 request: HttpRequest,
13 context: InvocationContext
14) => FunctionResult<HttpResponseInit | HttpResponse>;
15
16export interface HttpFunctionOptions extends HttpTriggerOptions, Partial<FunctionOptions> {
17 handler: HttpHandler;
18
19 trigger?: HttpTrigger;
20
21 /**
22 * Configuration for the optional primary output of the function. If not set, this will default to a standard http response output
23 * This is the main output that you should set as the return value of the function handler during invocation
24 */
25 return?: FunctionOutput;
26}
27
28export type HttpMethodFunctionOptions = Omit<HttpFunctionOptions, 'methods'>;
29
30export interface HttpTriggerOptions {
31 /**
32 * The function HTTP authorization level
33 * Defaults to 'anonymous' if not specified
34 */
35 authLevel?: 'anonymous' | 'function' | 'admin';
36
37 /**
38 * An array of the http methods for this http input
39 * Defaults to ["get", "post"] if not specified
40 */
41 methods?: HttpMethod[];
42
43 /**
44 * The route for this http input. If not specified, the function name will be used
45 */
46 route?: string;
47}
48
49export interface HttpTrigger extends FunctionTrigger {
50 /**
51 * The function HTTP authorization level.
52 */
53 authLevel: 'anonymous' | 'function' | 'admin';
54
55 /**
56 * An array of the http methods for this http input
57 */
58 methods: HttpMethod[];
59
60 /**
61 * The route for this http input. If not specified, the function name will be used
62 */
63 route?: string;
64}
65
66/**
67 * At this point in time there are no http output specific options
68 */
69export interface HttpOutputOptions {}
70
71export type HttpOutput = FunctionOutput & HttpOutputOptions;
72
73/**
74 * HTTP request object. Provided to your function when using HTTP Bindings.
75 */
76export declare class HttpRequest {
77 /**
78 * For testing purposes only. This will always be constructed for you when run in the context of the Azure Functions runtime
79 */
80 constructor(httpRequestInit: HttpRequestInit);
81
82 /**
83 * HTTP request method used to invoke this function.
84 */
85 readonly method: string;
86
87 /**
88 * Request URL.
89 */
90 readonly url: string;
91
92 /**
93 * HTTP request headers.
94 */
95 readonly headers: Headers;
96
97 /**
98 * Query string parameter keys and values from the URL.
99 */
100 readonly query: URLSearchParams;
101
102 /**
103 * Route parameter keys and values.
104 */
105 readonly params: HttpRequestParams;
106
107 /**
108 * Object representing logged-in user, either through
109 * AppService/Functions authentication, or SWA Authentication
110 * null when no such user is logged in.
111 */
112 readonly user: HttpRequestUser | null;
113
114 /**
115 * Returns the body as a ReadableStream
116 */
117 readonly body: ReadableStream | null;
118
119 /**
120 * Returns whether the body has been read from
121 */
122 readonly bodyUsed: boolean;
123
124 /**
125 * Returns a promise fulfilled with the body as an ArrayBuffer
126 */
127 readonly arrayBuffer: () => Promise<ArrayBuffer>;
128
129 /**
130 * Returns a promise fulfilled with the body as a Blob
131 */
132 readonly blob: () => Promise<Blob>;
133
134 /**
135 * Returns a promise fulfilled with the body as FormData
136 */
137 readonly formData: () => Promise<FormData>;
138
139 /**
140 * Returns a promise fulfilled with the body parsed as JSON
141 */
142 readonly json: () => Promise<unknown>;
143
144 /**
145 * Returns a promise fulfilled with the body as a string
146 */
147 readonly text: () => Promise<string>;
148}
149
150/**
151 * Route parameter keys and values.
152 */
153export type HttpRequestParams = Record<string, string>;
154
155/**
156 * Object representing logged-in user, either through
157 * AppService/Functions authentication, or SWA Authentication
158 */
159export interface HttpRequestUser {
160 /**
161 * Type of authentication, either AppService or StaticWebApps
162 */
163 type: HttpRequestUserType;
164
165 /**
166 * unique user GUID
167 */
168 id: string;
169
170 /**
171 * unique username
172 */
173 username: string;
174
175 /**
176 * provider of authentication service
177 */
178 identityProvider: string;
179
180 /**
181 * Extra authentication information, dependent on auth type
182 * and auth provider
183 */
184 claimsPrincipalData: Record<string, unknown>;
185}
186
187/**
188 * Possible values for an HTTP request method.
189 */
190export type HttpMethod = 'GET' | 'POST' | 'DELETE' | 'HEAD' | 'PATCH' | 'PUT' | 'OPTIONS' | 'TRACE' | 'CONNECT';
191
192/**
193 * Possible values for an HTTP Request user type
194 */
195export type HttpRequestUserType = 'AppService' | 'StaticWebApps';
196
197export interface HttpResponseInit {
198 /**
199 * HTTP response body
200 */
201 body?: BodyInit;
202
203 /**
204 * A JSON-serializable HTTP Response body.
205 * If set, the `HttpResponseInit.body` property will be ignored in favor of this property
206 */
207 jsonBody?: any;
208
209 /**
210 * HTTP response status code
211 * @default 200
212 */
213 status?: number;
214
215 /**
216 * HTTP response headers
217 */
218 headers?: HeadersInit;
219
220 /**
221 * HTTP response cookies
222 */
223 cookies?: Cookie[];
224
225 /**
226 * Enable content negotiation of response body if true
227 * If false, treat response body as raw
228 * @default false
229 */
230 enableContentNegotiation?: boolean;
231}
232
233/**
234 * HTTP response class
235 */
236export declare class HttpResponse {
237 constructor(responseInit?: HttpResponseInit);
238
239 /**
240 * HTTP response status code
241 * @default 200
242 */
243 readonly status: number;
244
245 /**
246 * HTTP response headers.
247 */
248 readonly headers: Headers;
249
250 /**
251 * HTTP response cookies
252 */
253 readonly cookies: Cookie[];
254
255 /**
256 * Enable content negotiation of response body if true
257 * If false, treat response body as raw
258 * @default false
259 */
260 readonly enableContentNegotiation: boolean;
261
262 /**
263 * Returns the body as a ReadableStream
264 */
265 readonly body: ReadableStream | null;
266
267 /**
268 * Returns whether the body has been read from
269 */
270 readonly bodyUsed: boolean;
271
272 /**
273 * Returns a promise fulfilled with the body as an ArrayBuffer
274 */
275 readonly arrayBuffer: () => Promise<ArrayBuffer>;
276
277 /**
278 * Returns a promise fulfilled with the body as a Blob
279 */
280 readonly blob: () => Promise<Blob>;
281
282 /**
283 * Returns a promise fulfilled with the body as FormData
284 */
285 readonly formData: () => Promise<FormData>;
286
287 /**
288 * Returns a promise fulfilled with the body parsed as JSON
289 */
290 readonly json: () => Promise<unknown>;
291
292 /**
293 * Returns a promise fulfilled with the body as a string
294 */
295 readonly text: () => Promise<string>;
296}
297
298/**
299 * Http response cookie object to "Set-Cookie"
300 */
301export interface Cookie {
302 name: string;
303
304 value: string;
305
306 /**
307 * Specifies allowed hosts to receive the cookie
308 */
309 domain?: string;
310
311 /**
312 * Specifies URL path that must exist in the requested URL
313 */
314 path?: string;
315
316 /**
317 * NOTE: It is generally recommended that you use maxAge over expires.
318 * Sets the cookie to expire at a specific date instead of when the client closes.
319 * This can be a Javascript Date or Unix time in milliseconds.
320 */
321 expires?: Date | number;
322
323 /**
324 * Sets the cookie to only be sent with an encrypted request
325 */
326 secure?: boolean;
327
328 /**
329 * Sets the cookie to be inaccessible to JavaScript's Document.cookie API
330 */
331 httpOnly?: boolean;
332
333 /**
334 * Can restrict the cookie to not be sent with cross-site requests
335 */
336 sameSite?: 'Strict' | 'Lax' | 'None' | undefined;
337
338 /**
339 * Number of seconds until the cookie expires. A zero or negative number will expire the cookie immediately.
340 */
341 maxAge?: number;
342}
343
344/**
345 * For testing purposes only. This will always be constructed for you when run in the context of the Azure Functions runtime
346 */
347export interface HttpRequestInit {
348 method?: string;
349
350 url?: string;
351
352 body?: HttpRequestBodyInit;
353
354 headers?: Record<string, string>;
355
356 query?: Record<string, string>;
357
358 params?: Record<string, string>;
359}
360
361/**
362 * For testing purposes only. This will always be constructed for you when run in the context of the Azure Functions runtime
363 */
364export interface HttpRequestBodyInit {
365 /**
366 * The body as a buffer. You only need to specify one of the `bytes` or `string` properties
367 */
368 bytes?: Uint8Array;
369
370 /**
371 * The body as a buffer. You only need to specify one of the `bytes` or `string` properties
372 */
373 string?: string;
374}