@bepalo/router 1.11.32 → 1.12.33
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/framework.d.ts +2 -4
- package/dist/cjs/framework.d.ts.map +1 -1
- package/dist/cjs/framework.js +4 -6
- package/dist/cjs/framework.js.map +1 -1
- package/dist/cjs/helpers.d.ts +2 -2
- package/dist/cjs/helpers.d.ts.map +1 -1
- package/dist/cjs/helpers.js +1 -1
- package/dist/cjs/helpers.js.map +1 -1
- package/dist/cjs/index.d.ts +5 -5
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +5 -5
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/middlewares.d.ts +2 -2
- package/dist/cjs/middlewares.d.ts.map +1 -1
- package/dist/cjs/middlewares.js +24 -24
- package/dist/cjs/middlewares.js.map +1 -1
- package/dist/cjs/router.d.ts +2 -2
- package/dist/cjs/router.d.ts.map +1 -1
- package/dist/cjs/router.js +8 -8
- package/dist/cjs/router.js.map +1 -1
- package/dist/cjs/types.d.ts +1 -1
- package/dist/cjs/types.d.ts.map +1 -1
- package/dist/cjs/upload-stream.d.ts +1 -1
- package/dist/cjs/upload-stream.d.ts.map +1 -1
- package/dist/cjs/upload-stream.js +7 -7
- package/dist/cjs/upload-stream.js.map +1 -1
- package/dist/framework.d.ts +2 -4
- package/dist/framework.d.ts.map +1 -1
- package/dist/framework.js +4 -6
- package/dist/framework.js.map +1 -1
- package/dist/helpers.d.ts +2 -2
- package/dist/helpers.d.ts.map +1 -1
- package/dist/helpers.js +1 -1
- package/dist/helpers.js.map +1 -1
- package/dist/index.d.ts +5 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -5
- package/dist/index.js.map +1 -1
- package/dist/middlewares.d.ts +2 -2
- package/dist/middlewares.d.ts.map +1 -1
- package/dist/middlewares.js +24 -24
- package/dist/middlewares.js.map +1 -1
- package/dist/router.d.ts +2 -2
- package/dist/router.d.ts.map +1 -1
- package/dist/router.js +8 -8
- package/dist/router.js.map +1 -1
- package/dist/types.d.ts +1 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/upload-stream.d.ts +1 -1
- package/dist/upload-stream.d.ts.map +1 -1
- package/dist/upload-stream.js +7 -7
- package/dist/upload-stream.js.map +1 -1
- package/package.json +8 -1
- package/src/framework.deno.ts +194 -0
- package/src/framework.ts +197 -0
- package/src/helpers.ts +829 -0
- package/src/index.ts +5 -0
- package/src/list.ts +462 -0
- package/src/middlewares.deno.ts +851 -0
- package/src/middlewares.ts +851 -0
- package/src/router.ts +993 -0
- package/src/tree.ts +139 -0
- package/src/types.ts +197 -0
- package/src/upload-stream.ts +661 -0
- package/dist/cjs/framework.deno.d.ts +0 -31
- package/dist/cjs/framework.deno.d.ts.map +0 -1
- package/dist/cjs/framework.deno.js +0 -245
- package/dist/cjs/framework.deno.js.map +0 -1
- package/dist/framework.deno.d.ts +0 -31
- package/dist/framework.deno.d.ts.map +0 -1
- package/dist/framework.deno.js +0 -245
- package/dist/framework.deno.js.map +0 -1
package/src/helpers.ts
ADDED
|
@@ -0,0 +1,829 @@
|
|
|
1
|
+
import Router from "./router.ts";
|
|
2
|
+
import type { BoundHandler, HttpMethod } from "./types.ts";
|
|
3
|
+
|
|
4
|
+
export * from "./upload-stream.ts";
|
|
5
|
+
|
|
6
|
+
export enum Status {
|
|
7
|
+
_100_Continue = 100,
|
|
8
|
+
_101_SwitchingProtocols = 101,
|
|
9
|
+
_102_Processing = 102,
|
|
10
|
+
_103_EarlyHints = 103,
|
|
11
|
+
_200_OK = 200,
|
|
12
|
+
_201_Created = 201,
|
|
13
|
+
_202_Accepted = 202,
|
|
14
|
+
_203_NonAuthoritativeInformation = 203,
|
|
15
|
+
_204_NoContent = 204,
|
|
16
|
+
_205_ResetContent = 205,
|
|
17
|
+
_206_PartialContent = 206,
|
|
18
|
+
_207_MultiStatus = 207,
|
|
19
|
+
_208_AlreadyReported = 208,
|
|
20
|
+
_226_IMUsed = 226,
|
|
21
|
+
_300_MultipleChoices = 300,
|
|
22
|
+
_301_MovedPermanently = 301,
|
|
23
|
+
_302_Found = 302,
|
|
24
|
+
_303_SeeOther = 303,
|
|
25
|
+
_304_NotModified = 304,
|
|
26
|
+
_305_UseProxy = 305,
|
|
27
|
+
_307_TemporaryRedirect = 307,
|
|
28
|
+
_308_PermanentRedirect = 308,
|
|
29
|
+
_400_BadRequest = 400,
|
|
30
|
+
_401_Unauthorized = 401,
|
|
31
|
+
_402_PaymentRequired = 402,
|
|
32
|
+
_403_Forbidden = 403,
|
|
33
|
+
_404_NotFound = 404,
|
|
34
|
+
_405_MethodNotAllowed = 405,
|
|
35
|
+
_406_NotAcceptable = 406,
|
|
36
|
+
_407_ProxyAuthenticationRequired = 407,
|
|
37
|
+
_408_RequestTimeout = 408,
|
|
38
|
+
_409_Conflict = 409,
|
|
39
|
+
_410_Gone = 410,
|
|
40
|
+
_411_LengthRequired = 411,
|
|
41
|
+
_412_PreconditionFailed = 412,
|
|
42
|
+
_413_PayloadTooLarge = 413,
|
|
43
|
+
_414_URITooLong = 414,
|
|
44
|
+
_415_UnsupportedMediaType = 415,
|
|
45
|
+
_416_RangeNotSatisfiable = 416,
|
|
46
|
+
_417_ExpectationFailed = 417,
|
|
47
|
+
_418_IMATeapot = 418,
|
|
48
|
+
_421_MisdirectedRequest = 421,
|
|
49
|
+
_422_UnprocessableEntity = 422,
|
|
50
|
+
_423_Locked = 423,
|
|
51
|
+
_424_FailedDependency = 424,
|
|
52
|
+
_425_TooEarly = 425,
|
|
53
|
+
_426_UpgradeRequired = 426,
|
|
54
|
+
_428_PreconditionRequired = 428,
|
|
55
|
+
_429_TooManyRequests = 429,
|
|
56
|
+
_431_RequestHeaderFieldsTooLarge = 431,
|
|
57
|
+
_451_UnavailableForLegalReasons = 451,
|
|
58
|
+
_500_InternalServerError = 500,
|
|
59
|
+
_501_NotImplemented = 501,
|
|
60
|
+
_502_BadGateway = 502,
|
|
61
|
+
_503_ServiceUnavailable = 503,
|
|
62
|
+
_504_GatewayTimeout = 504,
|
|
63
|
+
_505_HTTPVersionNotSupported = 505,
|
|
64
|
+
_506_VariantAlsoNegotiates = 506,
|
|
65
|
+
_507_InsufficientStorage = 507,
|
|
66
|
+
_508_LoopDetected = 508,
|
|
67
|
+
_510_NotExtended = 510,
|
|
68
|
+
_511_NetworkAuthenticationRequired = 511,
|
|
69
|
+
_419_PageExpired = 419,
|
|
70
|
+
_420_EnhanceYourCalm = 420,
|
|
71
|
+
_450_BlockedbyWindowsParentalControls = 450,
|
|
72
|
+
_498_InvalidToken = 498,
|
|
73
|
+
_499_TokenRequired = 499,
|
|
74
|
+
_509_BandwidthLimitExceeded = 509,
|
|
75
|
+
_526_InvalidSSLCertificate = 526,
|
|
76
|
+
_529_Siteisoverloaded = 529,
|
|
77
|
+
_530_Siteisfrozen = 530,
|
|
78
|
+
_598_NetworkReadTimeoutError = 598,
|
|
79
|
+
_599_NetworkConnectTimeoutError = 599,
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export function getHttpStatusText(code: number): string {
|
|
83
|
+
switch (code) {
|
|
84
|
+
// 1xx Informational
|
|
85
|
+
case 100:
|
|
86
|
+
return "Continue";
|
|
87
|
+
case 101:
|
|
88
|
+
return "Switching Protocols";
|
|
89
|
+
case 102:
|
|
90
|
+
return "Processing";
|
|
91
|
+
case 103:
|
|
92
|
+
return "Early Hints";
|
|
93
|
+
|
|
94
|
+
// 2xx Success
|
|
95
|
+
case 200:
|
|
96
|
+
return "OK";
|
|
97
|
+
case 201:
|
|
98
|
+
return "Created";
|
|
99
|
+
case 202:
|
|
100
|
+
return "Accepted";
|
|
101
|
+
case 203:
|
|
102
|
+
return "Non-Authoritative Information";
|
|
103
|
+
case 204:
|
|
104
|
+
return "No Content";
|
|
105
|
+
case 205:
|
|
106
|
+
return "Reset Content";
|
|
107
|
+
case 206:
|
|
108
|
+
return "Partial Content";
|
|
109
|
+
case 207:
|
|
110
|
+
return "Multi-Status";
|
|
111
|
+
case 208:
|
|
112
|
+
return "Already Reported";
|
|
113
|
+
case 226:
|
|
114
|
+
return "IM Used";
|
|
115
|
+
|
|
116
|
+
// 3xx Redirection
|
|
117
|
+
case 300:
|
|
118
|
+
return "Multiple Choices";
|
|
119
|
+
case 301:
|
|
120
|
+
return "Moved Permanently";
|
|
121
|
+
case 302:
|
|
122
|
+
return "Found";
|
|
123
|
+
case 303:
|
|
124
|
+
return "See Other";
|
|
125
|
+
case 304:
|
|
126
|
+
return "Not Modified";
|
|
127
|
+
case 305:
|
|
128
|
+
return "Use Proxy";
|
|
129
|
+
case 307:
|
|
130
|
+
return "Temporary Redirect";
|
|
131
|
+
case 308:
|
|
132
|
+
return "Permanent Redirect";
|
|
133
|
+
|
|
134
|
+
// 4xx Client Error
|
|
135
|
+
case 400:
|
|
136
|
+
return "Bad Request";
|
|
137
|
+
case 401:
|
|
138
|
+
return "Unauthorized";
|
|
139
|
+
case 402:
|
|
140
|
+
return "Payment Required";
|
|
141
|
+
case 403:
|
|
142
|
+
return "Forbidden";
|
|
143
|
+
case 404:
|
|
144
|
+
return "Not Found";
|
|
145
|
+
case 405:
|
|
146
|
+
return "Method Not Allowed";
|
|
147
|
+
case 406:
|
|
148
|
+
return "Not Acceptable";
|
|
149
|
+
case 407:
|
|
150
|
+
return "Proxy Authentication Required";
|
|
151
|
+
case 408:
|
|
152
|
+
return "Request Timeout";
|
|
153
|
+
case 409:
|
|
154
|
+
return "Conflict";
|
|
155
|
+
case 410:
|
|
156
|
+
return "Gone";
|
|
157
|
+
case 411:
|
|
158
|
+
return "Length Required";
|
|
159
|
+
case 412:
|
|
160
|
+
return "Precondition Failed";
|
|
161
|
+
case 413:
|
|
162
|
+
return "Payload Too Large";
|
|
163
|
+
case 414:
|
|
164
|
+
return "URI Too Long";
|
|
165
|
+
case 415:
|
|
166
|
+
return "Unsupported Media Type";
|
|
167
|
+
case 416:
|
|
168
|
+
return "Range Not Satisfiable";
|
|
169
|
+
case 417:
|
|
170
|
+
return "Expectation Failed";
|
|
171
|
+
case 418:
|
|
172
|
+
return "I'm a teapot";
|
|
173
|
+
case 421:
|
|
174
|
+
return "Misdirected Request";
|
|
175
|
+
case 422:
|
|
176
|
+
return "Unprocessable Entity";
|
|
177
|
+
case 423:
|
|
178
|
+
return "Locked";
|
|
179
|
+
case 424:
|
|
180
|
+
return "Failed Dependency";
|
|
181
|
+
case 425:
|
|
182
|
+
return "Too Early";
|
|
183
|
+
case 426:
|
|
184
|
+
return "Upgrade Required";
|
|
185
|
+
case 428:
|
|
186
|
+
return "Precondition Required";
|
|
187
|
+
case 429:
|
|
188
|
+
return "Too Many Requests";
|
|
189
|
+
case 431:
|
|
190
|
+
return "Request Header Fields Too Large";
|
|
191
|
+
case 451:
|
|
192
|
+
return "Unavailable For Legal Reasons";
|
|
193
|
+
|
|
194
|
+
// 5xx Server Error
|
|
195
|
+
case 500:
|
|
196
|
+
return "Internal Server Error";
|
|
197
|
+
case 501:
|
|
198
|
+
return "Not Implemented";
|
|
199
|
+
case 502:
|
|
200
|
+
return "Bad Gateway";
|
|
201
|
+
case 503:
|
|
202
|
+
return "Service Unavailable";
|
|
203
|
+
case 504:
|
|
204
|
+
return "Gateway Timeout";
|
|
205
|
+
case 505:
|
|
206
|
+
return "HTTP Version Not Supported";
|
|
207
|
+
case 506:
|
|
208
|
+
return "Variant Also Negotiates";
|
|
209
|
+
case 507:
|
|
210
|
+
return "Insufficient Storage";
|
|
211
|
+
case 508:
|
|
212
|
+
return "Loop Detected";
|
|
213
|
+
case 510:
|
|
214
|
+
return "Not Extended";
|
|
215
|
+
case 511:
|
|
216
|
+
return "Network Authentication Required";
|
|
217
|
+
|
|
218
|
+
// Unofficial/Custom codes
|
|
219
|
+
case 419:
|
|
220
|
+
return "Page Expired"; // Laravel Framework
|
|
221
|
+
case 420:
|
|
222
|
+
return "Enhance Your Calm"; // Twitter
|
|
223
|
+
case 430:
|
|
224
|
+
return "Request Header Fields Too Large"; // Shopify
|
|
225
|
+
case 450:
|
|
226
|
+
return "Blocked by Windows Parental Controls"; // Microsoft
|
|
227
|
+
case 498:
|
|
228
|
+
return "Invalid Token"; // Esri
|
|
229
|
+
case 499:
|
|
230
|
+
return "Token Required"; // Esri
|
|
231
|
+
case 509:
|
|
232
|
+
return "Bandwidth Limit Exceeded"; // Apache
|
|
233
|
+
case 526:
|
|
234
|
+
return "Invalid SSL Certificate"; // Cloudflare
|
|
235
|
+
case 529:
|
|
236
|
+
return "Site is overloaded"; // Qualys
|
|
237
|
+
case 530:
|
|
238
|
+
return "Site is frozen"; // Pantheon
|
|
239
|
+
case 598:
|
|
240
|
+
return "Network Read Timeout Error"; // Informal convention
|
|
241
|
+
case 599:
|
|
242
|
+
return "Network Connect Timeout Error"; // Informal convention
|
|
243
|
+
|
|
244
|
+
default:
|
|
245
|
+
// Categorize unknown codes
|
|
246
|
+
if (code >= 100 && code < 200) return "Informational Response";
|
|
247
|
+
if (code >= 200 && code < 300) return "Successful Response";
|
|
248
|
+
if (code >= 300 && code < 400) return "Redirection Message";
|
|
249
|
+
if (code >= 400 && code < 500) return "Client Error Response";
|
|
250
|
+
if (code >= 500 && code < 600) return "Server Error Response";
|
|
251
|
+
return "Unknown Status Code";
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Creates a Response with the specified status code.
|
|
257
|
+
* Defaults to 'text/plain; charset=utf-8' content-type if not provided in init.headers.
|
|
258
|
+
* @param {number} status - The HTTP status code
|
|
259
|
+
* @param {string|null} [content] - The response body content
|
|
260
|
+
* @param {ResponseInit} [init] - Additional response initialization options
|
|
261
|
+
* @returns {Response} A Response object
|
|
262
|
+
* @example
|
|
263
|
+
* status(200, "Success");
|
|
264
|
+
* status(404, "Not Found");
|
|
265
|
+
* status(204, null); // No content response
|
|
266
|
+
*/
|
|
267
|
+
export const status = (
|
|
268
|
+
status: number,
|
|
269
|
+
content?: string | null,
|
|
270
|
+
init?: ResponseInit,
|
|
271
|
+
): Response => {
|
|
272
|
+
const statusText = init?.statusText ?? getHttpStatusText(status);
|
|
273
|
+
const headers = new Headers(init?.headers);
|
|
274
|
+
if (content !== null && !headers.has("content-type")) {
|
|
275
|
+
headers.set("content-type", "text/plain; charset=utf-8");
|
|
276
|
+
}
|
|
277
|
+
return new Response(content !== undefined ? content : statusText, {
|
|
278
|
+
statusText,
|
|
279
|
+
...init,
|
|
280
|
+
status,
|
|
281
|
+
headers,
|
|
282
|
+
});
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Creates a redirect Response.
|
|
287
|
+
* Defaults to 302 Found unless another status is provided.
|
|
288
|
+
* @param {string} location - The URL to redirect to
|
|
289
|
+
* @param {number} [code=302] - The HTTP status code (301, 302, 303, 307, 308)
|
|
290
|
+
* @param {ResponseInit} [init] - Additional response initialization options
|
|
291
|
+
* @returns {Response} A Response object with Location header
|
|
292
|
+
*/
|
|
293
|
+
export const redirect = (location: string, init?: ResponseInit): Response => {
|
|
294
|
+
const status = init?.status ?? 302;
|
|
295
|
+
const statusText = init?.statusText ?? getHttpStatusText(status);
|
|
296
|
+
const headers = new Headers(init?.headers);
|
|
297
|
+
headers.set("Location", location);
|
|
298
|
+
return new Response(null, {
|
|
299
|
+
statusText,
|
|
300
|
+
...init,
|
|
301
|
+
status,
|
|
302
|
+
headers,
|
|
303
|
+
});
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Forwards the request to another route internally.
|
|
308
|
+
* Does not send a redirect to the client but changes the path and method,
|
|
309
|
+
* adds X-Forwarded-[Method|Path] and X-Original-Path headers and calls
|
|
310
|
+
* `(this as Router).respond(newReq, ctx)`.
|
|
311
|
+
* NOTE: parse body only once at the first handler using `parseBody({once: true})`
|
|
312
|
+
* as the body will be consumed at the first parseBody call.
|
|
313
|
+
* @param {string} path - The new path to forward to
|
|
314
|
+
* @returns {Response} A Response object with the forwarded request's response
|
|
315
|
+
*/
|
|
316
|
+
export const forward = <XContext = {}>(
|
|
317
|
+
path: string,
|
|
318
|
+
options?: {
|
|
319
|
+
method?: HttpMethod;
|
|
320
|
+
},
|
|
321
|
+
): BoundHandler<XContext> => {
|
|
322
|
+
return async function (this: Router<XContext>, req, ctx) {
|
|
323
|
+
const method = options?.method ?? req.method;
|
|
324
|
+
const headers = new Headers(req.headers);
|
|
325
|
+
const body = req.body ? await req.clone().arrayBuffer() : undefined;
|
|
326
|
+
const url = new URL(req.url);
|
|
327
|
+
const originalPathname = url.pathname;
|
|
328
|
+
url.pathname = path;
|
|
329
|
+
if (method != req.method) headers.set("X-Forwarded-Method", req.method);
|
|
330
|
+
headers.set("X-Forwarded-Path", originalPathname);
|
|
331
|
+
if (!req.headers.has("X-Original-Path")) {
|
|
332
|
+
headers.set("X-Original-Path", originalPathname);
|
|
333
|
+
}
|
|
334
|
+
const newReq = new Request(url.toString(), { method, headers, body });
|
|
335
|
+
return this.respond(newReq, ctx);
|
|
336
|
+
};
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* Creates a text/plain Response.
|
|
341
|
+
* Defaults to status 200 and 'text/plain; charset=utf-8' content-type if not specified.
|
|
342
|
+
* @param {string} content - The text content to return
|
|
343
|
+
* @param {ResponseInit} [init] - Additional response initialization options
|
|
344
|
+
* @returns {Response} A Response object with text/plain content-type
|
|
345
|
+
* @example
|
|
346
|
+
* text("Hello, world!");
|
|
347
|
+
* text("Error occurred", { status: 500 });
|
|
348
|
+
*/
|
|
349
|
+
export const text = (content: string, init?: ResponseInit): Response => {
|
|
350
|
+
const status = init?.status ?? 200;
|
|
351
|
+
const statusText = init?.statusText ?? getHttpStatusText(status);
|
|
352
|
+
const headers = new Headers(init?.headers);
|
|
353
|
+
if (!headers.has("content-type")) {
|
|
354
|
+
headers.set("content-type", "text/plain; charset=utf-8");
|
|
355
|
+
}
|
|
356
|
+
return new Response(content, {
|
|
357
|
+
statusText,
|
|
358
|
+
...init,
|
|
359
|
+
status,
|
|
360
|
+
headers,
|
|
361
|
+
});
|
|
362
|
+
};
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Creates an HTML Response.
|
|
366
|
+
* Defaults to status 200 and text/html content-type if not specified.
|
|
367
|
+
* @param {string} content - The HTML content to return
|
|
368
|
+
* @param {ResponseInit} [init] - Additional response initialization options
|
|
369
|
+
* @returns {Response} A Response object with text/html content-type
|
|
370
|
+
* @example
|
|
371
|
+
* html("<h1>Hello</h1>");
|
|
372
|
+
* html("<p>Not Found</p>", { status: 404 });
|
|
373
|
+
*/
|
|
374
|
+
export const html = (content: string, init?: ResponseInit): Response => {
|
|
375
|
+
const status = init?.status ?? 200;
|
|
376
|
+
const statusText = init?.statusText ?? getHttpStatusText(status);
|
|
377
|
+
const headers = new Headers(init?.headers);
|
|
378
|
+
if (!headers.has("content-type")) {
|
|
379
|
+
headers.set("content-type", "text/html; charset=utf-8");
|
|
380
|
+
}
|
|
381
|
+
return new Response(content, {
|
|
382
|
+
statusText,
|
|
383
|
+
...init,
|
|
384
|
+
status,
|
|
385
|
+
headers,
|
|
386
|
+
});
|
|
387
|
+
};
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Creates a JSON Response.
|
|
391
|
+
* Defaults to status 200 and 'application/json; charset=utf-8' content-type if not specified.
|
|
392
|
+
* Uses Response.json() internally which automatically serializes the body.
|
|
393
|
+
* @param {any} body - The data to serialize as JSON
|
|
394
|
+
* @param {ResponseInit} [init] - Additional response initialization options
|
|
395
|
+
* @returns {Response} A Response object with application/json content-type
|
|
396
|
+
* @example
|
|
397
|
+
* json({ message: "Success" });
|
|
398
|
+
* json({ error: "Not found" }, { status: 404 });
|
|
399
|
+
*/
|
|
400
|
+
export const json = (body: any, init?: ResponseInit): Response => {
|
|
401
|
+
const status = init?.status ?? 200;
|
|
402
|
+
const statusText = init?.statusText ?? getHttpStatusText(status);
|
|
403
|
+
const headers = new Headers(init?.headers);
|
|
404
|
+
if (!headers.has("content-type")) {
|
|
405
|
+
headers.set("content-type", "application/json; charset=utf-8");
|
|
406
|
+
}
|
|
407
|
+
return Response.json(body, {
|
|
408
|
+
statusText,
|
|
409
|
+
...init,
|
|
410
|
+
status,
|
|
411
|
+
headers,
|
|
412
|
+
});
|
|
413
|
+
};
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* Creates a Response from a Blob.
|
|
417
|
+
* Automatically sets content-type from blob.type or defaults to application/octet-stream.
|
|
418
|
+
* Also sets content-length header.
|
|
419
|
+
* @param {Blob} blob - The blob data to return
|
|
420
|
+
* @param {ResponseInit} [init] - Additional response initialization options
|
|
421
|
+
* @returns {Response} A Response object with appropriate content-type
|
|
422
|
+
* @example
|
|
423
|
+
* const blob = new Blob(["file content"], { type: "text/plain" });
|
|
424
|
+
* blob(blob);
|
|
425
|
+
*/
|
|
426
|
+
export const blob = (blob: Blob, init?: ResponseInit): Response => {
|
|
427
|
+
const status = init?.status ?? 200;
|
|
428
|
+
const statusText = init?.statusText ?? getHttpStatusText(status);
|
|
429
|
+
const headers = new Headers(init?.headers);
|
|
430
|
+
if (!headers.has("content-type")) {
|
|
431
|
+
headers.set("content-type", blob.type || "application/octet-stream");
|
|
432
|
+
}
|
|
433
|
+
headers.set("content-length", blob.size.toFixed());
|
|
434
|
+
return new Response(blob, {
|
|
435
|
+
statusText,
|
|
436
|
+
...init,
|
|
437
|
+
status,
|
|
438
|
+
headers,
|
|
439
|
+
});
|
|
440
|
+
};
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* Creates a Response from a Blob or ArrayBuffer with application/octet-stream content-type.
|
|
444
|
+
* Forces octet-stream content-type.
|
|
445
|
+
* Also sets content-length header.
|
|
446
|
+
* @param {Blob|ArrayBuffer} octetStream - The blob data to return
|
|
447
|
+
* @param {ResponseInit} [init] - Additional response initialization options
|
|
448
|
+
* @returns {Response} A Response object with application/octet-stream content-type
|
|
449
|
+
* @example
|
|
450
|
+
* const blob = new Blob([binaryData]);
|
|
451
|
+
* octetStream(blob);
|
|
452
|
+
*/
|
|
453
|
+
export const octetStream = (
|
|
454
|
+
octet: Blob | ArrayBuffer | ReadableStream,
|
|
455
|
+
init?: ResponseInit,
|
|
456
|
+
): Response => {
|
|
457
|
+
const status = init?.status ?? 200;
|
|
458
|
+
const statusText = init?.statusText ?? getHttpStatusText(status);
|
|
459
|
+
const headers = new Headers(init?.headers);
|
|
460
|
+
if (!headers.has("content-type")) {
|
|
461
|
+
headers.set("content-type", "application/octet-stream");
|
|
462
|
+
}
|
|
463
|
+
if (!(octet instanceof ReadableStream)) {
|
|
464
|
+
headers.set(
|
|
465
|
+
"content-length",
|
|
466
|
+
(octet instanceof Blob ? octet.size : octet.byteLength).toFixed(),
|
|
467
|
+
);
|
|
468
|
+
}
|
|
469
|
+
return new Response(octet, {
|
|
470
|
+
statusText,
|
|
471
|
+
...init,
|
|
472
|
+
status,
|
|
473
|
+
headers,
|
|
474
|
+
});
|
|
475
|
+
};
|
|
476
|
+
|
|
477
|
+
/**
|
|
478
|
+
* Creates a Response from FormData.
|
|
479
|
+
* @param {FormData} [formData] - The form data to return
|
|
480
|
+
* @param {ResponseInit} [init] - Additional response initialization options
|
|
481
|
+
* @returns {Response} A Response object
|
|
482
|
+
* @example
|
|
483
|
+
* const form = new FormData();
|
|
484
|
+
* form.append("key", "value");
|
|
485
|
+
* formData(form);
|
|
486
|
+
*/
|
|
487
|
+
export const formData = (
|
|
488
|
+
formData?: FormData,
|
|
489
|
+
init?: ResponseInit,
|
|
490
|
+
): Response => {
|
|
491
|
+
const status = init?.status ?? 200;
|
|
492
|
+
const statusText = init?.statusText ?? getHttpStatusText(status);
|
|
493
|
+
return new Response(formData, {
|
|
494
|
+
statusText,
|
|
495
|
+
...init,
|
|
496
|
+
status,
|
|
497
|
+
});
|
|
498
|
+
};
|
|
499
|
+
|
|
500
|
+
/**
|
|
501
|
+
* Creates a Response from URLSearchParams with application/x-www-form-urlencoded content-type.
|
|
502
|
+
* @param {URLSearchParams} [usp] - The URL search parameters to return
|
|
503
|
+
* @param {ResponseInit} [init] - Additional response initialization options
|
|
504
|
+
* @returns {Response} A Response object with application/x-www-form-urlencoded content-type
|
|
505
|
+
* @example
|
|
506
|
+
* const params = new URLSearchParams({ q: "search term" });
|
|
507
|
+
* usp(params);
|
|
508
|
+
*/
|
|
509
|
+
export const usp = (usp?: URLSearchParams, init?: ResponseInit): Response => {
|
|
510
|
+
const status = init?.status ?? 200;
|
|
511
|
+
const statusText = init?.statusText ?? getHttpStatusText(status);
|
|
512
|
+
const headers = new Headers(init?.headers);
|
|
513
|
+
if (!headers.has("content-type")) {
|
|
514
|
+
headers.set("content-type", "application/x-www-form-urlencoded");
|
|
515
|
+
}
|
|
516
|
+
return new Response(usp, {
|
|
517
|
+
statusText,
|
|
518
|
+
...init,
|
|
519
|
+
status,
|
|
520
|
+
headers,
|
|
521
|
+
});
|
|
522
|
+
};
|
|
523
|
+
|
|
524
|
+
/**
|
|
525
|
+
* Creates a Response from various body types with automatic content-type detection.
|
|
526
|
+
* Supports strings, objects (JSON), Blobs, ArrayBuffers, FormData, URLSearchParams, and ReadableStreams.
|
|
527
|
+
* @param {BodyInit|Record<string, unknown>} [body] - The body content to return
|
|
528
|
+
* @param {ResponseInit} [init] - Additional response initialization options
|
|
529
|
+
* @returns {Response} A Response object with appropriate content-type
|
|
530
|
+
* @example
|
|
531
|
+
* send("text"); // text/plain
|
|
532
|
+
* send({ message: "success" }); // application/json; charset=utf-8
|
|
533
|
+
* send(new Blob([])); // blob.type || application/octet-stream
|
|
534
|
+
* send(new FormData()); // multipart/form-data
|
|
535
|
+
* send(new URLSearchParams()); // application/x-www-form-urlencoded
|
|
536
|
+
*/
|
|
537
|
+
export const send = (
|
|
538
|
+
body?: BodyInit | Record<string, unknown>,
|
|
539
|
+
init?: ResponseInit,
|
|
540
|
+
): Response => {
|
|
541
|
+
const status = init?.status ?? 200;
|
|
542
|
+
const statusText = init?.statusText ?? getHttpStatusText(status);
|
|
543
|
+
const headers = new Headers(init?.headers);
|
|
544
|
+
const isContentTypeNotSet = !headers.has("content-type");
|
|
545
|
+
if (body instanceof URLSearchParams) {
|
|
546
|
+
if (isContentTypeNotSet) {
|
|
547
|
+
headers.set("content-type", "application/x-www-form-urlencoded");
|
|
548
|
+
}
|
|
549
|
+
} else if (body instanceof FormData) {
|
|
550
|
+
// content type will be generated
|
|
551
|
+
} else if (typeof body === "string") {
|
|
552
|
+
if (isContentTypeNotSet) {
|
|
553
|
+
headers.set("content-type", "text/plain; charset=utf-8");
|
|
554
|
+
}
|
|
555
|
+
} else if (body instanceof Blob) {
|
|
556
|
+
if (isContentTypeNotSet) {
|
|
557
|
+
headers.set("content-type", body.type || "application/octet-stream");
|
|
558
|
+
}
|
|
559
|
+
} else if (
|
|
560
|
+
body instanceof ArrayBuffer ||
|
|
561
|
+
ArrayBuffer.isView(body) ||
|
|
562
|
+
body instanceof ReadableStream
|
|
563
|
+
) {
|
|
564
|
+
if (isContentTypeNotSet) {
|
|
565
|
+
headers.set("content-type", "application/octet-stream");
|
|
566
|
+
}
|
|
567
|
+
} else if (body != null) {
|
|
568
|
+
if (isContentTypeNotSet) {
|
|
569
|
+
headers.set("content-type", "application/json; charset=utf-8");
|
|
570
|
+
}
|
|
571
|
+
return Response.json(body, {
|
|
572
|
+
status,
|
|
573
|
+
statusText,
|
|
574
|
+
...init,
|
|
575
|
+
headers,
|
|
576
|
+
});
|
|
577
|
+
}
|
|
578
|
+
return new Response(body, {
|
|
579
|
+
statusText,
|
|
580
|
+
...init,
|
|
581
|
+
status,
|
|
582
|
+
headers,
|
|
583
|
+
});
|
|
584
|
+
};
|
|
585
|
+
|
|
586
|
+
/**
|
|
587
|
+
* Options for setting cookies.
|
|
588
|
+
* @typedef {Object} CookieOptions
|
|
589
|
+
* @property {string} [path] - The path for which the cookie is valid
|
|
590
|
+
* @property {string} [domain] - The domain for which the cookie is valid
|
|
591
|
+
* @property {Date|number|string} [expires] - Expiration date of the cookie
|
|
592
|
+
* @property {number} [maxAge] - Maximum age of the cookie in seconds
|
|
593
|
+
* @property {boolean} [httpOnly] - If true, the cookie is not accessible via JavaScript
|
|
594
|
+
* @property {boolean} [secure] - If true, the cookie is only sent over HTTPS
|
|
595
|
+
* @property {"Strict"|"Lax"|"None"} [sameSite] - SameSite attribute for the cookie
|
|
596
|
+
*/
|
|
597
|
+
export interface CookieOptions {
|
|
598
|
+
path?: string;
|
|
599
|
+
domain?: string;
|
|
600
|
+
expires?: Date | number | string;
|
|
601
|
+
maxAge?: number;
|
|
602
|
+
httpOnly?: boolean;
|
|
603
|
+
secure?: boolean;
|
|
604
|
+
sameSite?: "Strict" | "Lax" | "None";
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
/**
|
|
608
|
+
* Tuple representing a cookie header (key-value pair for Set-Cookie header).
|
|
609
|
+
* @typedef {[string, string]} CookieTuple
|
|
610
|
+
*/
|
|
611
|
+
type CookieTuple = [string, string];
|
|
612
|
+
|
|
613
|
+
/**
|
|
614
|
+
* Creates a Set-Cookie header tuple with the given name, value, and options.
|
|
615
|
+
* @param {string} name - The name of the cookie
|
|
616
|
+
* @param {string} value - The value of the cookie
|
|
617
|
+
* @param {CookieOptions} [options] - Cookie configuration options
|
|
618
|
+
* @returns {CookieTuple} A tuple containing the header name "Set-Cookie" and the cookie string
|
|
619
|
+
* @example
|
|
620
|
+
* const cookie = setCookie("session", "abc123", { httpOnly: true, secure: true });
|
|
621
|
+
* // Returns: ["Set-Cookie", "session=abc123; HttpOnly; Secure"]
|
|
622
|
+
*/
|
|
623
|
+
export const setCookie = (
|
|
624
|
+
name: string,
|
|
625
|
+
value: string,
|
|
626
|
+
options?: CookieOptions,
|
|
627
|
+
): CookieTuple => {
|
|
628
|
+
const parts = [`${name}=${value}`];
|
|
629
|
+
if (options) {
|
|
630
|
+
if (options.path) parts.push(`Path=${options.path}`);
|
|
631
|
+
if (options.domain) parts.push(`Domain=${options.domain}`);
|
|
632
|
+
if (options.expires)
|
|
633
|
+
parts.push(`Expires=${new Date(options.expires).toUTCString()}`);
|
|
634
|
+
if (options.maxAge !== undefined) parts.push(`Max-Age=${options.maxAge}`);
|
|
635
|
+
if (options.httpOnly) parts.push(`HttpOnly`);
|
|
636
|
+
if (options.secure) parts.push(`Secure`);
|
|
637
|
+
if (options.sameSite) parts.push(`SameSite=${options.sameSite}`);
|
|
638
|
+
}
|
|
639
|
+
const cookie = parts.join("; ");
|
|
640
|
+
return ["Set-Cookie", cookie];
|
|
641
|
+
};
|
|
642
|
+
|
|
643
|
+
/**
|
|
644
|
+
* Creates a Set-Cookie header tuple to clear/remove a cookie.
|
|
645
|
+
* Sets the cookie with an empty value and an expired date.
|
|
646
|
+
* @param {string} name - The name of the cookie to clear
|
|
647
|
+
* @param {CookieOptions} [options] - Cookie configuration options (path/domain must match original cookie)
|
|
648
|
+
* @returns {CookieTuple} A tuple containing the header name "Set-Cookie" and the cookie clearing string
|
|
649
|
+
* @example
|
|
650
|
+
* const cookie = clearCookie("session", { path: "/" });
|
|
651
|
+
* // Returns: ["Set-Cookie", "session=; Expires=Thu, 01 Jan 1970 00:00:00 GMT; Path=/"]
|
|
652
|
+
*/
|
|
653
|
+
export const clearCookie = (
|
|
654
|
+
name: string,
|
|
655
|
+
options?: CookieOptions,
|
|
656
|
+
): CookieTuple => {
|
|
657
|
+
const parts = [`${name}=`];
|
|
658
|
+
const expires = options?.expires
|
|
659
|
+
? new Date(options.expires).toUTCString()
|
|
660
|
+
: "Thu, 01 Jan 1970 00:00:00 GMT";
|
|
661
|
+
if (options) {
|
|
662
|
+
if (options.path) parts.push(`Path=${options.path}`);
|
|
663
|
+
if (options.domain) parts.push(`Domain=${options.domain}`);
|
|
664
|
+
if (options.maxAge !== undefined) parts.push(`Max-Age=${options.maxAge}`);
|
|
665
|
+
if (options.httpOnly) parts.push(`HttpOnly`);
|
|
666
|
+
if (options.secure) parts.push(`Secure`);
|
|
667
|
+
if (options.sameSite) parts.push(`SameSite=${options.sameSite}`);
|
|
668
|
+
}
|
|
669
|
+
if (expires) parts.push(`Expires=${expires}`);
|
|
670
|
+
const cookie = parts.join("; ");
|
|
671
|
+
return ["Set-Cookie", cookie];
|
|
672
|
+
};
|
|
673
|
+
|
|
674
|
+
/**
|
|
675
|
+
* Parses cookies from a Request object's Cookie header.
|
|
676
|
+
* @template {Record<string, string>} Expected
|
|
677
|
+
* @param {Request} req - The request object containing cookies
|
|
678
|
+
* @returns {Expected|undefined} An object with cookie name-value pairs, or undefined if no cookies
|
|
679
|
+
* @example
|
|
680
|
+
* const cookies = parseCookieFromRequest(req);
|
|
681
|
+
* // Returns: { session: "abc123", theme: "dark" }
|
|
682
|
+
*/
|
|
683
|
+
export const parseCookieFromRequest = <Expected extends Record<string, string>>(
|
|
684
|
+
req: Request,
|
|
685
|
+
): Expected | undefined => {
|
|
686
|
+
const cookieHeader = req.headers.get("cookie");
|
|
687
|
+
if (cookieHeader != null) {
|
|
688
|
+
const cookies: Record<string, string> = {};
|
|
689
|
+
for (const pair of cookieHeader.split(";")) {
|
|
690
|
+
const [rawName, rawValue, extra] = pair
|
|
691
|
+
.trim()
|
|
692
|
+
.split("=", 3)
|
|
693
|
+
.map((token) => token.trim());
|
|
694
|
+
if (
|
|
695
|
+
rawName &&
|
|
696
|
+
rawValue !== undefined &&
|
|
697
|
+
rawValue !== "" &&
|
|
698
|
+
extra === undefined
|
|
699
|
+
) {
|
|
700
|
+
cookies[rawName] = decodeURIComponent(rawValue);
|
|
701
|
+
}
|
|
702
|
+
}
|
|
703
|
+
return cookies as Expected;
|
|
704
|
+
}
|
|
705
|
+
return undefined;
|
|
706
|
+
};
|
|
707
|
+
|
|
708
|
+
/**
|
|
709
|
+
* Request handler function type.
|
|
710
|
+
* @callback RequestHandler
|
|
711
|
+
* @template Context
|
|
712
|
+
* @param {Request} req - The incoming request
|
|
713
|
+
* @param {Context} ctx - The request context
|
|
714
|
+
* @returns {Response|void|Promise<Response|void>} A Response, or void to continue to next handler
|
|
715
|
+
*/
|
|
716
|
+
export interface RequestHandler<Context = any> {
|
|
717
|
+
(req: Request, ctx: Context): Response | void | Promise<Response | void>;
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
/**
|
|
721
|
+
* Request handler with error, function type.
|
|
722
|
+
* @callback RequestErrorHandler
|
|
723
|
+
* @template Context
|
|
724
|
+
* @param {Request} req - The incoming request
|
|
725
|
+
* @param {Error} error - The caught error
|
|
726
|
+
* @param {Context} ctx - The request context
|
|
727
|
+
* @returns {Response|void|Promise<Response|void>} A Response, or void to continue
|
|
728
|
+
*/
|
|
729
|
+
export interface RequestErrorHandler<Context = any> {
|
|
730
|
+
(
|
|
731
|
+
req: Request,
|
|
732
|
+
error: Error,
|
|
733
|
+
ctx: Context,
|
|
734
|
+
): Response | void | Promise<Response | void>;
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
/**
|
|
738
|
+
* Creates a request handler that processes requests through a series of middleware/handlers.
|
|
739
|
+
* Handlers are executed in order. If a handler returns a Response, that response is returned immediately.
|
|
740
|
+
* If no handler returns a Response, returns a 204 No Content response.
|
|
741
|
+
* @template Context
|
|
742
|
+
* @template {Array<RequestHandler<Context>>} Handlers
|
|
743
|
+
* @param {Context} ctxInit - Initial context object
|
|
744
|
+
* @param {...RequestHandler<Context>} handlers - Handler functions to process the request
|
|
745
|
+
* @returns {Function} A function that takes a Request and returns a Promise<Response>
|
|
746
|
+
* @example
|
|
747
|
+
* const handler = respondWith(
|
|
748
|
+
* {},
|
|
749
|
+
* parseCookie(),
|
|
750
|
+
* parseBody(),
|
|
751
|
+
* (req, ctx) => {
|
|
752
|
+
* return json({ cookie: ctx.cookie, body: ctx.body });
|
|
753
|
+
* }
|
|
754
|
+
* );
|
|
755
|
+
*/
|
|
756
|
+
export const respondWith = <
|
|
757
|
+
Context = any,
|
|
758
|
+
Handlers extends Array<RequestHandler<Context>> = Array<
|
|
759
|
+
RequestHandler<Context>
|
|
760
|
+
>,
|
|
761
|
+
>(
|
|
762
|
+
ctxInit: Context,
|
|
763
|
+
...handlers: [...Handlers]
|
|
764
|
+
): { (req: Request): Promise<Response> } => {
|
|
765
|
+
return async (req: Request) => {
|
|
766
|
+
const ctx = ctxInit;
|
|
767
|
+
for (const handler of handlers) {
|
|
768
|
+
let response = handler(req, ctx);
|
|
769
|
+
if (response instanceof Promise) response = await response;
|
|
770
|
+
if (response instanceof Response) return response;
|
|
771
|
+
}
|
|
772
|
+
return status(204, null);
|
|
773
|
+
};
|
|
774
|
+
};
|
|
775
|
+
|
|
776
|
+
/**
|
|
777
|
+
* Creates a request handler with error catching.
|
|
778
|
+
* Similar to respondWith but includes an error handler to catch exceptions.
|
|
779
|
+
* @template Context
|
|
780
|
+
* @template {RequestErrorHandler<Context>} Handler
|
|
781
|
+
* @template {Array<RequestHandler<Context>>} Handlers
|
|
782
|
+
* @param {Context} ctxInit - Initial context object
|
|
783
|
+
* @param {Handler} catcher - Error handler function
|
|
784
|
+
* @param {...RequestHandler<Context>} handlers - Handler functions to process the request
|
|
785
|
+
* @returns {Function} A function that takes a Request and returns a Promise<Response>
|
|
786
|
+
* @example
|
|
787
|
+
* const handler = respondWithCatcher(
|
|
788
|
+
* {},
|
|
789
|
+
* (req, error, ctx) => {
|
|
790
|
+
* return json({ error: error.message }, { status: 500 });
|
|
791
|
+
* },
|
|
792
|
+
* parseBody(),
|
|
793
|
+
* (req, ctx) => {
|
|
794
|
+
* // This might throw an error
|
|
795
|
+
* return json({ data: ctx.body });
|
|
796
|
+
* }
|
|
797
|
+
* );
|
|
798
|
+
*/
|
|
799
|
+
export const respondWithCatcher = <
|
|
800
|
+
Context = any,
|
|
801
|
+
Handler extends RequestErrorHandler<Context> = RequestErrorHandler<Context>,
|
|
802
|
+
Handlers extends Array<RequestHandler<Context>> = Array<
|
|
803
|
+
RequestHandler<Context>
|
|
804
|
+
>,
|
|
805
|
+
>(
|
|
806
|
+
ctxInit: Context,
|
|
807
|
+
catcher: Handler,
|
|
808
|
+
...handlers: [...Handlers]
|
|
809
|
+
): { (req: Request): Promise<Response> } => {
|
|
810
|
+
return async (req: Request) => {
|
|
811
|
+
const ctx = ctxInit;
|
|
812
|
+
try {
|
|
813
|
+
for (const handler of handlers) {
|
|
814
|
+
let response = handler(req, ctx);
|
|
815
|
+
if (response instanceof Promise) response = await response;
|
|
816
|
+
if (response instanceof Response) return response;
|
|
817
|
+
}
|
|
818
|
+
} catch (error) {
|
|
819
|
+
let response = catcher(
|
|
820
|
+
req,
|
|
821
|
+
error instanceof Error ? error : new Error(String(error)),
|
|
822
|
+
ctx,
|
|
823
|
+
);
|
|
824
|
+
if (response instanceof Promise) response = await response;
|
|
825
|
+
if (response instanceof Response) return response;
|
|
826
|
+
}
|
|
827
|
+
return status(204, null);
|
|
828
|
+
};
|
|
829
|
+
};
|