1 | // Copyright (c) .NET Foundation. All rights reserved.
|
2 | // Licensed under the MIT License.
|
3 |
|
4 | /**
|
5 | * HTTP request headers.
|
6 | */
|
7 | export interface HttpRequestHeaders {
|
8 | [name: string]: string;
|
9 | }
|
10 |
|
11 | /**
|
12 | * HTTP response headers.
|
13 | */
|
14 | export interface HttpResponseHeaders {
|
15 | [name: string]: string;
|
16 | }
|
17 |
|
18 | /**
|
19 | * Query string parameter keys and values from the URL.
|
20 | */
|
21 | export interface HttpRequestQuery {
|
22 | [name: string]: string;
|
23 | }
|
24 |
|
25 | /**
|
26 | * Route parameter keys and values.
|
27 | */
|
28 | export interface HttpRequestParams {
|
29 | [name: string]: string;
|
30 | }
|
31 |
|
32 | /**
|
33 | * Object representing logged-in user, either through
|
34 | * AppService/Functions authentication, or SWA Authentication
|
35 | */
|
36 | export interface HttpRequestUser {
|
37 | /**
|
38 | * Type of authentication, either AppService or StaticWebApps
|
39 | */
|
40 | type: HttpRequestUserType;
|
41 | /**
|
42 | * unique user GUID
|
43 | */
|
44 | id: string;
|
45 | /**
|
46 | * unique username
|
47 | */
|
48 | username: string;
|
49 | /**
|
50 | * provider of authentication service
|
51 | */
|
52 | identityProvider: string;
|
53 | /**
|
54 | * Extra authentication information, dependent on auth type
|
55 | * and auth provider
|
56 | */
|
57 | claimsPrincipalData: {
|
58 | [key: string]: any;
|
59 | };
|
60 | }
|
61 |
|
62 | /**
|
63 | * HTTP request object. Provided to your function when using HTTP Bindings.
|
64 | */
|
65 | export interface HttpRequest {
|
66 | /**
|
67 | * HTTP request method used to invoke this function.
|
68 | */
|
69 | method: HttpMethod | null;
|
70 | /**
|
71 | * Request URL.
|
72 | */
|
73 | url: string;
|
74 | /**
|
75 | * HTTP request headers.
|
76 | */
|
77 | headers: HttpRequestHeaders;
|
78 | /**
|
79 | * Query string parameter keys and values from the URL.
|
80 | */
|
81 | query: HttpRequestQuery;
|
82 | /**
|
83 | * Route parameter keys and values.
|
84 | */
|
85 | params: HttpRequestParams;
|
86 | /**
|
87 | * Object representing logged-in user, either through
|
88 | * AppService/Functions authentication, or SWA Authentication
|
89 | * null when no such user is logged in.
|
90 | */
|
91 | user: HttpRequestUser | null;
|
92 | /**
|
93 | * The HTTP request body.
|
94 | * If the media type is 'application/octet-stream' or 'multipart/*', this will be a Buffer
|
95 | * If the value is a JSON parse-able string, this will be the parsed object
|
96 | * Otherwise, this will be a string
|
97 | */
|
98 | body?: any;
|
99 |
|
100 | /**
|
101 | * The HTTP request body as a UTF-8 string. In this case, the name "raw" is used because the string will never be parsed to an object even if it is json.
|
102 | * Improvements to the naming are tracked in https://github.com/Azure/azure-functions-nodejs-worker/issues/294
|
103 | */
|
104 | rawBody?: any;
|
105 |
|
106 | /**
|
107 | * The raw Buffer representing the body before any decoding or parsing has been done
|
108 | */
|
109 | bufferBody?: Buffer;
|
110 |
|
111 | /**
|
112 | * Get the value of a particular header field
|
113 | */
|
114 | get(field: string): string | undefined;
|
115 |
|
116 | /**
|
117 | * Parses the body and returns an object representing a form
|
118 | * @throws if the content type is not "multipart/form-data" or "application/x-www-form-urlencoded"
|
119 | */
|
120 | parseFormBody(): Form;
|
121 | }
|
122 |
|
123 | export interface Form extends Iterable<[string, FormPart]> {
|
124 | /**
|
125 | * Returns the value of the first name-value pair whose name is `name`. If there are no such pairs, `null` is returned.
|
126 | */
|
127 | get(name: string): FormPart | null;
|
128 |
|
129 | /**
|
130 | * Returns the values of all name-value pairs whose name is `name`. If there are no such pairs, an empty array is returned.
|
131 | */
|
132 | getAll(name: string): FormPart[];
|
133 |
|
134 | /**
|
135 | * Returns `true` if there is at least one name-value pair whose name is `name`.
|
136 | */
|
137 | has(name: string): boolean;
|
138 |
|
139 | /**
|
140 | * The number of parts in this form
|
141 | */
|
142 | length: number;
|
143 | }
|
144 |
|
145 | export interface FormPart {
|
146 | /**
|
147 | * The value for this part of the form
|
148 | */
|
149 | value: Buffer;
|
150 |
|
151 | /**
|
152 | * The file name for this part of the form, if specified
|
153 | */
|
154 | fileName?: string;
|
155 |
|
156 | /**
|
157 | * The content type for this part of the form, assumed to be "text/plain" if not specified
|
158 | */
|
159 | contentType?: string;
|
160 | }
|
161 |
|
162 | /**
|
163 | * Possible values for an HTTP request method.
|
164 | */
|
165 | export type HttpMethod = 'GET' | 'POST' | 'DELETE' | 'HEAD' | 'PATCH' | 'PUT' | 'OPTIONS' | 'TRACE' | 'CONNECT';
|
166 |
|
167 | /**
|
168 | * Possible values for an HTTP Request user type
|
169 | */
|
170 | export type HttpRequestUserType = 'AppService' | 'StaticWebApps';
|
171 |
|
172 | /**
|
173 | * Http response object and methods.
|
174 | * This is the default of the res property in the Context object provided to your function when using HTTP triggers.
|
175 | */
|
176 | export interface HttpResponseFull {
|
177 | /**
|
178 | * HTTP response headers.
|
179 | */
|
180 | headers?: HttpResponseHeaders;
|
181 | /**
|
182 | * HTTP response cookies.
|
183 | */
|
184 | cookies?: Cookie[];
|
185 | /**
|
186 | * HTTP response body.
|
187 | */
|
188 | body?: any;
|
189 | /**
|
190 | * HTTP response status code.
|
191 | * @default 200
|
192 | */
|
193 | statusCode?: number | string;
|
194 | /**
|
195 | * Enable content negotiation of response body if true
|
196 | * If false, treat response body as raw
|
197 | * @default false
|
198 | */
|
199 | enableContentNegotiation?: boolean;
|
200 | /**
|
201 | * Sets the HTTP response status code
|
202 | * @returns the updated HttpResponseFull instance
|
203 | */
|
204 | status: (statusCode: number | string) => HttpResponseFull;
|
205 | /**
|
206 | * Sets a particular header field to a value
|
207 | * @returns the updated HttpResponseFull instance
|
208 | */
|
209 | setHeader(field: string, val: any): HttpResponseFull;
|
210 | /**
|
211 | * Has the same functionality as setHeader.
|
212 | * Sets a particular header field to a value
|
213 | * @returns the updated HttpResponseFull instance
|
214 | */
|
215 | header(field: string, val: any): HttpResponseFull;
|
216 | /**
|
217 | * Has the same functionality as setHeader.
|
218 | * Sets a particular header field to a value
|
219 | * @returns the updated HttpResponseFull instance
|
220 | */
|
221 | set(field: string, val: any): HttpResponseFull;
|
222 | /**
|
223 | * Get the value of a particular header field
|
224 | */
|
225 | getHeader(field: string): any;
|
226 | /**
|
227 | * Has the same functionality as getHeader
|
228 | * Get the value of a particular header field
|
229 | */
|
230 | get(field: string): any;
|
231 | /**
|
232 | * Removes a particular header field
|
233 | * @returns the updated HttpResponseFull instance
|
234 | */
|
235 | removeHeader(field: string): HttpResponseFull;
|
236 | /**
|
237 | * Set the 'Content-Type' header to a particular value
|
238 | * @returns the updated HttpResponseFull instance
|
239 | */
|
240 | type(type: string): HttpResponseFull;
|
241 | /**
|
242 | * Automatically sets the content-type then calls context.done()
|
243 | * @returns updated HttpResponseFull instance
|
244 | * @deprecated this method calls context.done() which is deprecated, use async/await and pass the response as the return value instead.
|
245 | * See the docs here for more information: https://aka.ms/functions-js-async-await
|
246 | */
|
247 | send(body?: any): HttpResponseFull;
|
248 | /**
|
249 | * Same as send()
|
250 | * Automatically sets the content-type then calls context.done()
|
251 | * @returns updated HttpResponseFull instance
|
252 | * @deprecated this method calls context.done() which is deprecated, use async/await and pass the response as your function's return value instead.
|
253 | * See the docs here for more information: https://aka.ms/functions-js-async-await
|
254 | */
|
255 | end(body?: any): HttpResponseFull;
|
256 | /**
|
257 | * Sets the status code then calls send()
|
258 | * @returns updated HttpResponseFull instance
|
259 | * @deprecated this method calls context.done() which is deprecated, use async/await and pass the response as your function's return value instead.
|
260 | * See the docs here for more information: https://aka.ms/functions-js-async-await
|
261 | */
|
262 | sendStatus(statusCode: string | number): HttpResponseFull;
|
263 | /**
|
264 | * Sets the 'Content-Type' header to 'application/json' then calls send(body)
|
265 | * @deprecated this method calls context.done() which is deprecated, use async/await and pass the response as your function's return value instead.
|
266 | * See the docs here for more information: https://aka.ms/functions-js-async-await
|
267 | */
|
268 | json(body?: any): void;
|
269 | }
|
270 |
|
271 | /**
|
272 | * Http response object.
|
273 | * 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.
|
274 | */
|
275 | export interface HttpResponseSimple {
|
276 | /**
|
277 | * HTTP response headers.
|
278 | */
|
279 | headers?: HttpResponseHeaders;
|
280 | /**
|
281 | * HTTP response cookies.
|
282 | */
|
283 | cookies?: Cookie[];
|
284 | /**
|
285 | * HTTP response body.
|
286 | */
|
287 | body?: any;
|
288 | /**
|
289 | * HTTP response status code.
|
290 | * This property takes precedence over the `status` property
|
291 | * @default 200
|
292 | */
|
293 | statusCode?: number | string;
|
294 | /**
|
295 | * HTTP response status code
|
296 | * The same as `statusCode`. This property is ignored if `statusCode` is set
|
297 | * @default 200
|
298 | */
|
299 | status?: number | string;
|
300 | /**
|
301 | * Enable content negotiation of response body if true
|
302 | * If false, treat response body as raw
|
303 | * @default false
|
304 | */
|
305 | enableContentNegotiation?: boolean;
|
306 | }
|
307 |
|
308 | /**
|
309 | * Http response type.
|
310 | */
|
311 | export type HttpResponse = HttpResponseSimple | HttpResponseFull;
|
312 |
|
313 | /**
|
314 | * Http response cookie object to "Set-Cookie"
|
315 | */
|
316 | export interface Cookie {
|
317 | /** Cookie name */
|
318 | name: string;
|
319 | /** Cookie value */
|
320 | value: string;
|
321 | /** Specifies allowed hosts to receive the cookie */
|
322 | domain?: string;
|
323 | /** Specifies URL path that must exist in the requested URL */
|
324 | path?: string;
|
325 | /**
|
326 | * NOTE: It is generally recommended that you use maxAge over expires.
|
327 | * Sets the cookie to expire at a specific date instead of when the client closes.
|
328 | * This can be a Javascript Date or Unix time in milliseconds.
|
329 | */
|
330 | expires?: Date | number;
|
331 | /** Sets the cookie to only be sent with an encrypted request */
|
332 | secure?: boolean;
|
333 | /** Sets the cookie to be inaccessible to JavaScript's Document.cookie API */
|
334 | httpOnly?: boolean;
|
335 | /** Can restrict the cookie to not be sent with cross-site requests */
|
336 | sameSite?: 'Strict' | 'Lax' | 'None' | undefined;
|
337 | /** Number of seconds until the cookie expires. A zero or negative number will expire the cookie immediately. */
|
338 | maxAge?: number;
|
339 | }
|