1 |
|
2 |
|
3 |
|
4 | import { Blob } from 'buffer';
|
5 | import { ReadableStream } from 'stream/web';
|
6 | import { BodyInit, FormData, Headers, HeadersInit } from 'undici';
|
7 | import { URLSearchParams } from 'url';
|
8 | import { FunctionOptions, FunctionOutput, FunctionResult, FunctionTrigger } from './index';
|
9 | import { InvocationContext } from './InvocationContext';
|
10 |
|
11 | export type HttpHandler = (
|
12 | request: HttpRequest,
|
13 | context: InvocationContext
|
14 | ) => FunctionResult<HttpResponseInit | HttpResponse>;
|
15 |
|
16 | export interface HttpFunctionOptions extends HttpTriggerOptions, Partial<FunctionOptions> {
|
17 | handler: HttpHandler;
|
18 |
|
19 | trigger?: HttpTrigger;
|
20 |
|
21 | |
22 |
|
23 |
|
24 |
|
25 | return?: FunctionOutput;
|
26 | }
|
27 |
|
28 | export type HttpMethodFunctionOptions = Omit<HttpFunctionOptions, 'methods'>;
|
29 |
|
30 | export interface HttpTriggerOptions {
|
31 | |
32 |
|
33 |
|
34 |
|
35 | authLevel?: 'anonymous' | 'function' | 'admin';
|
36 |
|
37 | |
38 |
|
39 |
|
40 |
|
41 | methods?: HttpMethod[];
|
42 |
|
43 | |
44 |
|
45 |
|
46 | route?: string;
|
47 | }
|
48 |
|
49 | export interface HttpTrigger extends FunctionTrigger {
|
50 | |
51 |
|
52 |
|
53 | authLevel: 'anonymous' | 'function' | 'admin';
|
54 |
|
55 | |
56 |
|
57 |
|
58 | methods: HttpMethod[];
|
59 |
|
60 | |
61 |
|
62 |
|
63 | route?: string;
|
64 | }
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | export interface HttpOutputOptions {}
|
70 |
|
71 | export type HttpOutput = FunctionOutput & HttpOutputOptions;
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | export declare class HttpRequest {
|
77 | |
78 |
|
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 | */
|
153 | export type HttpRequestParams = Record<string, string>;
|
154 |
|
155 | /**
|
156 | * Object representing logged-in user, either through
|
157 | * AppService/Functions authentication, or SWA Authentication
|
158 | */
|
159 | export interface HttpRequestUser {
|
160 | |
161 |
|
162 |
|
163 | type: HttpRequestUserType;
|
164 |
|
165 | |
166 |
|
167 |
|
168 | id: string;
|
169 |
|
170 | |
171 |
|
172 |
|
173 | username: string;
|
174 |
|
175 | |
176 |
|
177 |
|
178 | identityProvider: string;
|
179 |
|
180 | |
181 |
|
182 |
|
183 |
|
184 | claimsPrincipalData: Record<string, unknown>;
|
185 | }
|
186 |
|
187 |
|
188 |
|
189 |
|
190 | export type HttpMethod = 'GET' | 'POST' | 'DELETE' | 'HEAD' | 'PATCH' | 'PUT' | 'OPTIONS' | 'TRACE' | 'CONNECT';
|
191 |
|
192 |
|
193 |
|
194 |
|
195 | export type HttpRequestUserType = 'AppService' | 'StaticWebApps';
|
196 |
|
197 | export interface HttpResponseInit {
|
198 | |
199 |
|
200 |
|
201 | body?: BodyInit;
|
202 |
|
203 | |
204 |
|
205 |
|
206 |
|
207 | jsonBody?: any;
|
208 |
|
209 | |
210 |
|
211 |
|
212 |
|
213 | status?: number;
|
214 |
|
215 | |
216 |
|
217 |
|
218 | headers?: HeadersInit;
|
219 |
|
220 | |
221 |
|
222 |
|
223 | cookies?: Cookie[];
|
224 |
|
225 | |
226 |
|
227 |
|
228 |
|
229 |
|
230 | enableContentNegotiation?: boolean;
|
231 | }
|
232 |
|
233 |
|
234 |
|
235 |
|
236 | export 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 | */
|
301 | export interface Cookie {
|
302 | name: string;
|
303 |
|
304 | value: string;
|
305 |
|
306 | |
307 |
|
308 |
|
309 | domain?: string;
|
310 |
|
311 | |
312 |
|
313 |
|
314 | path?: string;
|
315 |
|
316 | |
317 |
|
318 |
|
319 |
|
320 |
|
321 | expires?: Date | number;
|
322 |
|
323 | |
324 |
|
325 |
|
326 | secure?: boolean;
|
327 |
|
328 | |
329 |
|
330 |
|
331 | httpOnly?: boolean;
|
332 |
|
333 | |
334 |
|
335 |
|
336 | sameSite?: 'Strict' | 'Lax' | 'None' | undefined;
|
337 |
|
338 | |
339 |
|
340 |
|
341 | maxAge?: number;
|
342 | }
|
343 |
|
344 |
|
345 |
|
346 |
|
347 | export 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 |
|
363 |
|
364 | export interface HttpRequestBodyInit {
|
365 | |
366 |
|
367 |
|
368 | bytes?: Uint8Array;
|
369 |
|
370 | |
371 |
|
372 |
|
373 | string?: string;
|
374 | }
|