@bepalo/router 1.0.3
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/LICENSE +21 -0
- package/README.md +557 -0
- package/dist/cjs/helpers.d.ts +290 -0
- package/dist/cjs/helpers.d.ts.map +1 -0
- package/dist/cjs/helpers.js +691 -0
- package/dist/cjs/helpers.js.map +1 -0
- package/dist/cjs/index.d.ts +5 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js +21 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/list.d.ts +166 -0
- package/dist/cjs/list.d.ts.map +1 -0
- package/dist/cjs/list.js +483 -0
- package/dist/cjs/list.js.map +1 -0
- package/dist/cjs/middlewares.d.ts +251 -0
- package/dist/cjs/middlewares.d.ts.map +1 -0
- package/dist/cjs/middlewares.js +359 -0
- package/dist/cjs/middlewares.js.map +1 -0
- package/dist/cjs/router.d.ts +333 -0
- package/dist/cjs/router.d.ts.map +1 -0
- package/dist/cjs/router.js +659 -0
- package/dist/cjs/router.js.map +1 -0
- package/dist/cjs/tree.d.ts +18 -0
- package/dist/cjs/tree.d.ts.map +1 -0
- package/dist/cjs/tree.js +162 -0
- package/dist/cjs/tree.js.map +1 -0
- package/dist/cjs/types.d.ts +127 -0
- package/dist/cjs/types.d.ts.map +1 -0
- package/dist/cjs/types.js +3 -0
- package/dist/cjs/types.js.map +1 -0
- package/dist/cjs/upload-stream.d.ts +105 -0
- package/dist/cjs/upload-stream.d.ts.map +1 -0
- package/dist/cjs/upload-stream.js +417 -0
- package/dist/cjs/upload-stream.js.map +1 -0
- package/dist/helpers.d.ts +290 -0
- package/dist/helpers.d.ts.map +1 -0
- package/dist/helpers.js +691 -0
- package/dist/helpers.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +21 -0
- package/dist/index.js.map +1 -0
- package/dist/list.d.ts +166 -0
- package/dist/list.d.ts.map +1 -0
- package/dist/list.js +483 -0
- package/dist/list.js.map +1 -0
- package/dist/middlewares.d.ts +251 -0
- package/dist/middlewares.d.ts.map +1 -0
- package/dist/middlewares.js +359 -0
- package/dist/middlewares.js.map +1 -0
- package/dist/router.d.ts +333 -0
- package/dist/router.d.ts.map +1 -0
- package/dist/router.js +659 -0
- package/dist/router.js.map +1 -0
- package/dist/tree.d.ts +18 -0
- package/dist/tree.d.ts.map +1 -0
- package/dist/tree.js +162 -0
- package/dist/tree.js.map +1 -0
- package/dist/types.d.ts +127 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/dist/upload-stream.d.ts +105 -0
- package/dist/upload-stream.d.ts.map +1 -0
- package/dist/upload-stream.js +417 -0
- package/dist/upload-stream.js.map +1 -0
- package/package.json +51 -0
package/dist/helpers.js
ADDED
|
@@ -0,0 +1,691 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
17
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
18
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
19
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
20
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
21
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
22
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
23
|
+
});
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.respondWithCatcher = exports.respondWith = exports.parseBody = exports.parseCookie = exports.parseCookieFromRequest = exports.clearCookie = exports.setCookie = exports.send = exports.usp = exports.formData = exports.octetStream = exports.blob = exports.json = exports.html = exports.text = exports.status = void 0;
|
|
27
|
+
exports.getHttpStatusText = getHttpStatusText;
|
|
28
|
+
__exportStar(require("./upload-stream.js"), exports);
|
|
29
|
+
function getHttpStatusText(code) {
|
|
30
|
+
switch (code) {
|
|
31
|
+
// 1xx Informational
|
|
32
|
+
case 100:
|
|
33
|
+
return "Continue";
|
|
34
|
+
case 101:
|
|
35
|
+
return "Switching Protocols";
|
|
36
|
+
case 102:
|
|
37
|
+
return "Processing";
|
|
38
|
+
case 103:
|
|
39
|
+
return "Early Hints";
|
|
40
|
+
// 2xx Success
|
|
41
|
+
case 200:
|
|
42
|
+
return "OK";
|
|
43
|
+
case 201:
|
|
44
|
+
return "Created";
|
|
45
|
+
case 202:
|
|
46
|
+
return "Accepted";
|
|
47
|
+
case 203:
|
|
48
|
+
return "Non-Authoritative Information";
|
|
49
|
+
case 204:
|
|
50
|
+
return "No Content";
|
|
51
|
+
case 205:
|
|
52
|
+
return "Reset Content";
|
|
53
|
+
case 206:
|
|
54
|
+
return "Partial Content";
|
|
55
|
+
case 207:
|
|
56
|
+
return "Multi-Status";
|
|
57
|
+
case 208:
|
|
58
|
+
return "Already Reported";
|
|
59
|
+
case 226:
|
|
60
|
+
return "IM Used";
|
|
61
|
+
// 3xx Redirection
|
|
62
|
+
case 300:
|
|
63
|
+
return "Multiple Choices";
|
|
64
|
+
case 301:
|
|
65
|
+
return "Moved Permanently";
|
|
66
|
+
case 302:
|
|
67
|
+
return "Found";
|
|
68
|
+
case 303:
|
|
69
|
+
return "See Other";
|
|
70
|
+
case 304:
|
|
71
|
+
return "Not Modified";
|
|
72
|
+
case 305:
|
|
73
|
+
return "Use Proxy";
|
|
74
|
+
case 307:
|
|
75
|
+
return "Temporary Redirect";
|
|
76
|
+
case 308:
|
|
77
|
+
return "Permanent Redirect";
|
|
78
|
+
// 4xx Client Error
|
|
79
|
+
case 400:
|
|
80
|
+
return "Bad Request";
|
|
81
|
+
case 401:
|
|
82
|
+
return "Unauthorized";
|
|
83
|
+
case 402:
|
|
84
|
+
return "Payment Required";
|
|
85
|
+
case 403:
|
|
86
|
+
return "Forbidden";
|
|
87
|
+
case 404:
|
|
88
|
+
return "Not Found";
|
|
89
|
+
case 405:
|
|
90
|
+
return "Method Not Allowed";
|
|
91
|
+
case 406:
|
|
92
|
+
return "Not Acceptable";
|
|
93
|
+
case 407:
|
|
94
|
+
return "Proxy Authentication Required";
|
|
95
|
+
case 408:
|
|
96
|
+
return "Request Timeout";
|
|
97
|
+
case 409:
|
|
98
|
+
return "Conflict";
|
|
99
|
+
case 410:
|
|
100
|
+
return "Gone";
|
|
101
|
+
case 411:
|
|
102
|
+
return "Length Required";
|
|
103
|
+
case 412:
|
|
104
|
+
return "Precondition Failed";
|
|
105
|
+
case 413:
|
|
106
|
+
return "Payload Too Large";
|
|
107
|
+
case 414:
|
|
108
|
+
return "URI Too Long";
|
|
109
|
+
case 415:
|
|
110
|
+
return "Unsupported Media Type";
|
|
111
|
+
case 416:
|
|
112
|
+
return "Range Not Satisfiable";
|
|
113
|
+
case 417:
|
|
114
|
+
return "Expectation Failed";
|
|
115
|
+
case 418:
|
|
116
|
+
return "I'm a teapot";
|
|
117
|
+
case 421:
|
|
118
|
+
return "Misdirected Request";
|
|
119
|
+
case 422:
|
|
120
|
+
return "Unprocessable Entity";
|
|
121
|
+
case 423:
|
|
122
|
+
return "Locked";
|
|
123
|
+
case 424:
|
|
124
|
+
return "Failed Dependency";
|
|
125
|
+
case 425:
|
|
126
|
+
return "Too Early";
|
|
127
|
+
case 426:
|
|
128
|
+
return "Upgrade Required";
|
|
129
|
+
case 428:
|
|
130
|
+
return "Precondition Required";
|
|
131
|
+
case 429:
|
|
132
|
+
return "Too Many Requests";
|
|
133
|
+
case 431:
|
|
134
|
+
return "Request Header Fields Too Large";
|
|
135
|
+
case 451:
|
|
136
|
+
return "Unavailable For Legal Reasons";
|
|
137
|
+
// 5xx Server Error
|
|
138
|
+
case 500:
|
|
139
|
+
return "Internal Server Error";
|
|
140
|
+
case 501:
|
|
141
|
+
return "Not Implemented";
|
|
142
|
+
case 502:
|
|
143
|
+
return "Bad Gateway";
|
|
144
|
+
case 503:
|
|
145
|
+
return "Service Unavailable";
|
|
146
|
+
case 504:
|
|
147
|
+
return "Gateway Timeout";
|
|
148
|
+
case 505:
|
|
149
|
+
return "HTTP Version Not Supported";
|
|
150
|
+
case 506:
|
|
151
|
+
return "Variant Also Negotiates";
|
|
152
|
+
case 507:
|
|
153
|
+
return "Insufficient Storage";
|
|
154
|
+
case 508:
|
|
155
|
+
return "Loop Detected";
|
|
156
|
+
case 510:
|
|
157
|
+
return "Not Extended";
|
|
158
|
+
case 511:
|
|
159
|
+
return "Network Authentication Required";
|
|
160
|
+
// Unofficial/Custom codes
|
|
161
|
+
case 419:
|
|
162
|
+
return "Page Expired"; // Laravel Framework
|
|
163
|
+
case 420:
|
|
164
|
+
return "Enhance Your Calm"; // Twitter
|
|
165
|
+
case 430:
|
|
166
|
+
return "Request Header Fields Too Large"; // Shopify
|
|
167
|
+
case 450:
|
|
168
|
+
return "Blocked by Windows Parental Controls"; // Microsoft
|
|
169
|
+
case 498:
|
|
170
|
+
return "Invalid Token"; // Esri
|
|
171
|
+
case 499:
|
|
172
|
+
return "Token Required"; // Esri
|
|
173
|
+
case 509:
|
|
174
|
+
return "Bandwidth Limit Exceeded"; // Apache
|
|
175
|
+
case 526:
|
|
176
|
+
return "Invalid SSL Certificate"; // Cloudflare
|
|
177
|
+
case 529:
|
|
178
|
+
return "Site is overloaded"; // Qualys
|
|
179
|
+
case 530:
|
|
180
|
+
return "Site is frozen"; // Pantheon
|
|
181
|
+
case 598:
|
|
182
|
+
return "Network Read Timeout Error"; // Informal convention
|
|
183
|
+
case 599:
|
|
184
|
+
return "Network Connect Timeout Error"; // Informal convention
|
|
185
|
+
default:
|
|
186
|
+
// Categorize unknown codes
|
|
187
|
+
if (code >= 100 && code < 200)
|
|
188
|
+
return "Informational Response";
|
|
189
|
+
if (code >= 200 && code < 300)
|
|
190
|
+
return "Successful Response";
|
|
191
|
+
if (code >= 300 && code < 400)
|
|
192
|
+
return "Redirection Message";
|
|
193
|
+
if (code >= 400 && code < 500)
|
|
194
|
+
return "Client Error Response";
|
|
195
|
+
if (code >= 500 && code < 600)
|
|
196
|
+
return "Server Error Response";
|
|
197
|
+
return "Unknown Status Code";
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Creates a Response with the specified status code.
|
|
202
|
+
* Defaults to text/plain content-type if not provided in init.headers.
|
|
203
|
+
* @param {number} status - The HTTP status code
|
|
204
|
+
* @param {string|null} [content] - The response body content
|
|
205
|
+
* @param {ResponseInit} [init] - Additional response initialization options
|
|
206
|
+
* @returns {Response} A Response object
|
|
207
|
+
* @example
|
|
208
|
+
* status(200, "Success");
|
|
209
|
+
* status(404, "Not Found");
|
|
210
|
+
* status(204, null); // No content response
|
|
211
|
+
*/
|
|
212
|
+
const status = (status, content, init) => {
|
|
213
|
+
var _a;
|
|
214
|
+
const statusText = (_a = init === null || init === void 0 ? void 0 : init.statusText) !== null && _a !== void 0 ? _a : getHttpStatusText(status);
|
|
215
|
+
const headers = new Headers(init === null || init === void 0 ? void 0 : init.headers);
|
|
216
|
+
if (!headers.has("content-type")) {
|
|
217
|
+
headers.set("content-type", "text/plain");
|
|
218
|
+
}
|
|
219
|
+
return new Response(content !== undefined ? content : statusText, Object.assign({ status,
|
|
220
|
+
statusText,
|
|
221
|
+
headers }, init));
|
|
222
|
+
};
|
|
223
|
+
exports.status = status;
|
|
224
|
+
/**
|
|
225
|
+
* Creates a text/plain Response.
|
|
226
|
+
* Defaults to status 200 and text/plain content-type if not specified.
|
|
227
|
+
* @param {string} content - The text content to return
|
|
228
|
+
* @param {ResponseInit} [init] - Additional response initialization options
|
|
229
|
+
* @returns {Response} A Response object with text/plain content-type
|
|
230
|
+
* @example
|
|
231
|
+
* text("Hello, world!");
|
|
232
|
+
* text("Error occurred", { status: 500 });
|
|
233
|
+
*/
|
|
234
|
+
const text = (content, init) => {
|
|
235
|
+
var _a, _b;
|
|
236
|
+
const status = (_a = init === null || init === void 0 ? void 0 : init.status) !== null && _a !== void 0 ? _a : 200;
|
|
237
|
+
const statusText = (_b = init === null || init === void 0 ? void 0 : init.statusText) !== null && _b !== void 0 ? _b : getHttpStatusText(status);
|
|
238
|
+
const headers = new Headers(init === null || init === void 0 ? void 0 : init.headers);
|
|
239
|
+
if (!headers.has("content-type")) {
|
|
240
|
+
headers.set("content-type", "text/plain");
|
|
241
|
+
}
|
|
242
|
+
return new Response(content, Object.assign({ status,
|
|
243
|
+
statusText,
|
|
244
|
+
headers }, init));
|
|
245
|
+
};
|
|
246
|
+
exports.text = text;
|
|
247
|
+
/**
|
|
248
|
+
* Creates an HTML Response.
|
|
249
|
+
* Defaults to status 200 and text/html content-type if not specified.
|
|
250
|
+
* @param {string} content - The HTML content to return
|
|
251
|
+
* @param {ResponseInit} [init] - Additional response initialization options
|
|
252
|
+
* @returns {Response} A Response object with text/html content-type
|
|
253
|
+
* @example
|
|
254
|
+
* html("<h1>Hello</h1>");
|
|
255
|
+
* html("<p>Not Found</p>", { status: 404 });
|
|
256
|
+
*/
|
|
257
|
+
const html = (content, init) => {
|
|
258
|
+
var _a, _b;
|
|
259
|
+
const status = (_a = init === null || init === void 0 ? void 0 : init.status) !== null && _a !== void 0 ? _a : 200;
|
|
260
|
+
const statusText = (_b = init === null || init === void 0 ? void 0 : init.statusText) !== null && _b !== void 0 ? _b : getHttpStatusText(status);
|
|
261
|
+
const headers = new Headers(init === null || init === void 0 ? void 0 : init.headers);
|
|
262
|
+
if (!headers.has("content-type")) {
|
|
263
|
+
headers.set("content-type", "text/html");
|
|
264
|
+
}
|
|
265
|
+
return new Response(content, Object.assign({ status,
|
|
266
|
+
statusText,
|
|
267
|
+
headers }, init));
|
|
268
|
+
};
|
|
269
|
+
exports.html = html;
|
|
270
|
+
/**
|
|
271
|
+
* Creates a JSON Response.
|
|
272
|
+
* Defaults to status 200 and application/json content-type if not specified.
|
|
273
|
+
* Uses Response.json() internally which automatically serializes the body.
|
|
274
|
+
* @param {any} body - The data to serialize as JSON
|
|
275
|
+
* @param {ResponseInit} [init] - Additional response initialization options
|
|
276
|
+
* @returns {Response} A Response object with application/json content-type
|
|
277
|
+
* @example
|
|
278
|
+
* json({ message: "Success" });
|
|
279
|
+
* json({ error: "Not found" }, { status: 404 });
|
|
280
|
+
*/
|
|
281
|
+
const json = (body, init) => {
|
|
282
|
+
var _a, _b;
|
|
283
|
+
const status = (_a = init === null || init === void 0 ? void 0 : init.status) !== null && _a !== void 0 ? _a : 200;
|
|
284
|
+
const statusText = (_b = init === null || init === void 0 ? void 0 : init.statusText) !== null && _b !== void 0 ? _b : getHttpStatusText(status);
|
|
285
|
+
const headers = new Headers(init === null || init === void 0 ? void 0 : init.headers);
|
|
286
|
+
if (!headers.has("content-type")) {
|
|
287
|
+
headers.set("content-type", "application/json");
|
|
288
|
+
}
|
|
289
|
+
return Response.json(body, Object.assign({ status,
|
|
290
|
+
statusText,
|
|
291
|
+
headers }, init));
|
|
292
|
+
};
|
|
293
|
+
exports.json = json;
|
|
294
|
+
/**
|
|
295
|
+
* Creates a Response from a Blob.
|
|
296
|
+
* Automatically sets content-type from blob.type or defaults to application/octet-stream.
|
|
297
|
+
* Also sets content-length header.
|
|
298
|
+
* @param {Blob} blob - The blob data to return
|
|
299
|
+
* @param {ResponseInit} [init] - Additional response initialization options
|
|
300
|
+
* @returns {Response} A Response object with appropriate content-type
|
|
301
|
+
* @example
|
|
302
|
+
* const blob = new Blob(["file content"], { type: "text/plain" });
|
|
303
|
+
* blob(blob);
|
|
304
|
+
*/
|
|
305
|
+
const blob = (blob, init) => {
|
|
306
|
+
var _a, _b;
|
|
307
|
+
const status = (_a = init === null || init === void 0 ? void 0 : init.status) !== null && _a !== void 0 ? _a : 200;
|
|
308
|
+
const statusText = (_b = init === null || init === void 0 ? void 0 : init.statusText) !== null && _b !== void 0 ? _b : getHttpStatusText(status);
|
|
309
|
+
const headers = new Headers(init === null || init === void 0 ? void 0 : init.headers);
|
|
310
|
+
if (!headers.has("content-type")) {
|
|
311
|
+
headers.set("content-type", blob.type || "application/octet-stream");
|
|
312
|
+
}
|
|
313
|
+
headers.set("content-length", blob.size.toFixed());
|
|
314
|
+
return new Response(blob, Object.assign({ status,
|
|
315
|
+
statusText,
|
|
316
|
+
headers }, init));
|
|
317
|
+
};
|
|
318
|
+
exports.blob = blob;
|
|
319
|
+
/**
|
|
320
|
+
* Creates a Response from a Blob or ArrayBuffer with application/octet-stream content-type.
|
|
321
|
+
* Forces octet-stream content-type.
|
|
322
|
+
* Also sets content-length header.
|
|
323
|
+
* @param {Blob|ArrayBuffer} octetStream - The blob data to return
|
|
324
|
+
* @param {ResponseInit} [init] - Additional response initialization options
|
|
325
|
+
* @returns {Response} A Response object with application/octet-stream content-type
|
|
326
|
+
* @example
|
|
327
|
+
* const blob = new Blob([binaryData]);
|
|
328
|
+
* octetStream(blob);
|
|
329
|
+
*/
|
|
330
|
+
const octetStream = (octet, init) => {
|
|
331
|
+
var _a, _b;
|
|
332
|
+
const status = (_a = init === null || init === void 0 ? void 0 : init.status) !== null && _a !== void 0 ? _a : 200;
|
|
333
|
+
const statusText = (_b = init === null || init === void 0 ? void 0 : init.statusText) !== null && _b !== void 0 ? _b : getHttpStatusText(status);
|
|
334
|
+
const headers = new Headers(init === null || init === void 0 ? void 0 : init.headers);
|
|
335
|
+
if (!headers.has("content-type")) {
|
|
336
|
+
headers.set("content-type", "application/octet-stream");
|
|
337
|
+
}
|
|
338
|
+
if (!(octet instanceof ReadableStream)) {
|
|
339
|
+
headers.set("content-length", (octet instanceof Blob ? octet.size : octet.byteLength).toFixed());
|
|
340
|
+
}
|
|
341
|
+
return new Response(octet, Object.assign({ status,
|
|
342
|
+
statusText,
|
|
343
|
+
headers }, init));
|
|
344
|
+
};
|
|
345
|
+
exports.octetStream = octetStream;
|
|
346
|
+
/**
|
|
347
|
+
* Creates a Response from FormData.
|
|
348
|
+
* @param {FormData} [formData] - The form data to return
|
|
349
|
+
* @param {ResponseInit} [init] - Additional response initialization options
|
|
350
|
+
* @returns {Response} A Response object
|
|
351
|
+
* @example
|
|
352
|
+
* const form = new FormData();
|
|
353
|
+
* form.append("key", "value");
|
|
354
|
+
* formData(form);
|
|
355
|
+
*/
|
|
356
|
+
const formData = (formData, init) => {
|
|
357
|
+
var _a, _b;
|
|
358
|
+
const status = (_a = init === null || init === void 0 ? void 0 : init.status) !== null && _a !== void 0 ? _a : 200;
|
|
359
|
+
const statusText = (_b = init === null || init === void 0 ? void 0 : init.statusText) !== null && _b !== void 0 ? _b : getHttpStatusText(status);
|
|
360
|
+
return new Response(formData, Object.assign({ status,
|
|
361
|
+
statusText }, init));
|
|
362
|
+
};
|
|
363
|
+
exports.formData = formData;
|
|
364
|
+
/**
|
|
365
|
+
* Creates a Response from URLSearchParams with application/x-www-form-urlencoded content-type.
|
|
366
|
+
* @param {URLSearchParams} [usp] - The URL search parameters to return
|
|
367
|
+
* @param {ResponseInit} [init] - Additional response initialization options
|
|
368
|
+
* @returns {Response} A Response object with application/x-www-form-urlencoded content-type
|
|
369
|
+
* @example
|
|
370
|
+
* const params = new URLSearchParams({ q: "search term" });
|
|
371
|
+
* usp(params);
|
|
372
|
+
*/
|
|
373
|
+
const usp = (usp, init) => {
|
|
374
|
+
var _a, _b;
|
|
375
|
+
const status = (_a = init === null || init === void 0 ? void 0 : init.status) !== null && _a !== void 0 ? _a : 200;
|
|
376
|
+
const statusText = (_b = init === null || init === void 0 ? void 0 : init.statusText) !== null && _b !== void 0 ? _b : getHttpStatusText(status);
|
|
377
|
+
const headers = new Headers(init === null || init === void 0 ? void 0 : init.headers);
|
|
378
|
+
if (!headers.has("content-type")) {
|
|
379
|
+
headers.set("content-type", "application/x-www-form-urlencoded");
|
|
380
|
+
}
|
|
381
|
+
return new Response(usp, Object.assign({ status,
|
|
382
|
+
statusText,
|
|
383
|
+
headers }, init));
|
|
384
|
+
};
|
|
385
|
+
exports.usp = usp;
|
|
386
|
+
/**
|
|
387
|
+
* Creates a Response from various body types with automatic content-type detection.
|
|
388
|
+
* Supports strings, objects (JSON), Blobs, ArrayBuffers, FormData, URLSearchParams, and ReadableStreams.
|
|
389
|
+
* @param {BodyInit} [body] - The body content to return
|
|
390
|
+
* @param {ResponseInit} [init] - Additional response initialization options
|
|
391
|
+
* @returns {Response} A Response object with appropriate content-type
|
|
392
|
+
* @example
|
|
393
|
+
* send("text"); // text/plain
|
|
394
|
+
* send({ message: "success" }); // application/json
|
|
395
|
+
* send(new Blob([])); // blob.type || application/octet-stream
|
|
396
|
+
* send(new FormData()); // multipart/form-data
|
|
397
|
+
* send(new URLSearchParams()); // application/x-www-form-urlencoded
|
|
398
|
+
*/
|
|
399
|
+
const send = (body, init) => {
|
|
400
|
+
var _a, _b;
|
|
401
|
+
const status = (_a = init === null || init === void 0 ? void 0 : init.status) !== null && _a !== void 0 ? _a : 200;
|
|
402
|
+
const statusText = (_b = init === null || init === void 0 ? void 0 : init.statusText) !== null && _b !== void 0 ? _b : getHttpStatusText(status);
|
|
403
|
+
const headers = new Headers(init === null || init === void 0 ? void 0 : init.headers);
|
|
404
|
+
if (body != null && !headers.has("content-type")) {
|
|
405
|
+
if (body instanceof URLSearchParams) {
|
|
406
|
+
headers.set("content-type", "application/x-www-form-urlencoded");
|
|
407
|
+
body = body.toString();
|
|
408
|
+
}
|
|
409
|
+
else if (body instanceof FormData || body instanceof Buffer) {
|
|
410
|
+
}
|
|
411
|
+
else if (typeof body === "string") {
|
|
412
|
+
headers.set("content-type", "text/plain; charset=utf-8");
|
|
413
|
+
}
|
|
414
|
+
else if (body instanceof Blob) {
|
|
415
|
+
headers.set("content-type", body.type || "application/octet-stream");
|
|
416
|
+
}
|
|
417
|
+
else if (body instanceof ArrayBuffer) {
|
|
418
|
+
headers.set("content-type", "application/octet-stream");
|
|
419
|
+
}
|
|
420
|
+
else if (!(body instanceof ReadableStream)) {
|
|
421
|
+
headers.set("content-type", "application/json");
|
|
422
|
+
body = JSON.stringify(body);
|
|
423
|
+
}
|
|
424
|
+
else {
|
|
425
|
+
headers.set("content-type", "application/octet-stream");
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
return new Response(body, Object.assign({ status,
|
|
429
|
+
statusText,
|
|
430
|
+
headers }, init));
|
|
431
|
+
};
|
|
432
|
+
exports.send = send;
|
|
433
|
+
/**
|
|
434
|
+
* Creates a Set-Cookie header tuple with the given name, value, and options.
|
|
435
|
+
* @param {string} name - The name of the cookie
|
|
436
|
+
* @param {string} value - The value of the cookie
|
|
437
|
+
* @param {CookieOptions} [options] - Cookie configuration options
|
|
438
|
+
* @returns {CookieTuple} A tuple containing the header name "Set-Cookie" and the cookie string
|
|
439
|
+
* @example
|
|
440
|
+
* const cookie = setCookie("session", "abc123", { httpOnly: true, secure: true });
|
|
441
|
+
* // Returns: ["Set-Cookie", "session=abc123; HttpOnly; Secure"]
|
|
442
|
+
*/
|
|
443
|
+
const setCookie = (name, value, options) => {
|
|
444
|
+
const parts = [`${name}=${value}`];
|
|
445
|
+
if (options) {
|
|
446
|
+
if (options.path)
|
|
447
|
+
parts.push(`Path=${options.path}`);
|
|
448
|
+
if (options.domain)
|
|
449
|
+
parts.push(`Domain=${options.domain}`);
|
|
450
|
+
if (options.expires)
|
|
451
|
+
parts.push(`Expires=${new Date(options.expires).toUTCString()}`);
|
|
452
|
+
if (options.maxAge !== undefined)
|
|
453
|
+
parts.push(`Max-Age=${options.maxAge}`);
|
|
454
|
+
if (options.httpOnly)
|
|
455
|
+
parts.push(`HttpOnly`);
|
|
456
|
+
if (options.secure)
|
|
457
|
+
parts.push(`Secure`);
|
|
458
|
+
if (options.sameSite)
|
|
459
|
+
parts.push(`SameSite=${options.sameSite}`);
|
|
460
|
+
}
|
|
461
|
+
const cookie = parts.join("; ");
|
|
462
|
+
return ["Set-Cookie", cookie];
|
|
463
|
+
};
|
|
464
|
+
exports.setCookie = setCookie;
|
|
465
|
+
/**
|
|
466
|
+
* Creates a Set-Cookie header tuple to clear/remove a cookie.
|
|
467
|
+
* Sets the cookie with an empty value and an expired date.
|
|
468
|
+
* @param {string} name - The name of the cookie to clear
|
|
469
|
+
* @param {CookieOptions} [options] - Cookie configuration options (path/domain must match original cookie)
|
|
470
|
+
* @returns {CookieTuple} A tuple containing the header name "Set-Cookie" and the cookie clearing string
|
|
471
|
+
* @example
|
|
472
|
+
* const cookie = clearCookie("session", { path: "/" });
|
|
473
|
+
* // Returns: ["Set-Cookie", "session=; Expires=Thu, 01 Jan 1970 00:00:00 GMT; Path=/"]
|
|
474
|
+
*/
|
|
475
|
+
const clearCookie = (name, options) => {
|
|
476
|
+
const parts = [`${name}=`];
|
|
477
|
+
const expires = (options === null || options === void 0 ? void 0 : options.expires)
|
|
478
|
+
? new Date(options.expires).toUTCString()
|
|
479
|
+
: "Thu, 01 Jan 1970 00:00:00 GMT";
|
|
480
|
+
if (options) {
|
|
481
|
+
if (options.path)
|
|
482
|
+
parts.push(`Path=${options.path}`);
|
|
483
|
+
if (options.domain)
|
|
484
|
+
parts.push(`Domain=${options.domain}`);
|
|
485
|
+
if (options.maxAge !== undefined)
|
|
486
|
+
parts.push(`Max-Age=${options.maxAge}`);
|
|
487
|
+
if (options.httpOnly)
|
|
488
|
+
parts.push(`HttpOnly`);
|
|
489
|
+
if (options.secure)
|
|
490
|
+
parts.push(`Secure`);
|
|
491
|
+
if (options.sameSite)
|
|
492
|
+
parts.push(`SameSite=${options.sameSite}`);
|
|
493
|
+
}
|
|
494
|
+
if (expires)
|
|
495
|
+
parts.push(`Expires=${expires}`);
|
|
496
|
+
const cookie = parts.join("; ");
|
|
497
|
+
return ["Set-Cookie", cookie];
|
|
498
|
+
};
|
|
499
|
+
exports.clearCookie = clearCookie;
|
|
500
|
+
/**
|
|
501
|
+
* Parses cookies from a Request object's Cookie header.
|
|
502
|
+
* @template {Record<string, string>} Expected
|
|
503
|
+
* @param {Request} req - The request object containing cookies
|
|
504
|
+
* @returns {Expected|undefined} An object with cookie name-value pairs, or undefined if no cookies
|
|
505
|
+
* @example
|
|
506
|
+
* const cookies = parseCookieFromRequest(req);
|
|
507
|
+
* // Returns: { session: "abc123", theme: "dark" }
|
|
508
|
+
*/
|
|
509
|
+
const parseCookieFromRequest = (req) => {
|
|
510
|
+
const cookieHeader = req.headers.get("cookie");
|
|
511
|
+
if (cookieHeader != null) {
|
|
512
|
+
const cookies = {};
|
|
513
|
+
for (const pair of cookieHeader.split(";")) {
|
|
514
|
+
const [rawName, rawValue, extra] = pair
|
|
515
|
+
.trim()
|
|
516
|
+
.split("=", 3)
|
|
517
|
+
.map((token) => token.trim());
|
|
518
|
+
if (rawName &&
|
|
519
|
+
rawValue !== undefined &&
|
|
520
|
+
rawValue !== "" &&
|
|
521
|
+
extra === undefined) {
|
|
522
|
+
cookies[rawName] = decodeURIComponent(rawValue);
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
return cookies;
|
|
526
|
+
}
|
|
527
|
+
return undefined;
|
|
528
|
+
};
|
|
529
|
+
exports.parseCookieFromRequest = parseCookieFromRequest;
|
|
530
|
+
/**
|
|
531
|
+
* Creates middleware that parses cookies from the request and adds them to the context.
|
|
532
|
+
* @returns {Function} A middleware function that adds parsed cookies to context.cookie
|
|
533
|
+
* @example
|
|
534
|
+
* const cookieParser = parseCookie();
|
|
535
|
+
* // Use in respondWith: respondWith({}, cookieParser(), ...otherHandlers)
|
|
536
|
+
*/
|
|
537
|
+
const parseCookie = () => {
|
|
538
|
+
return (req, ctx) => {
|
|
539
|
+
var _a;
|
|
540
|
+
const cookie = (_a = (0, exports.parseCookieFromRequest)(req)) !== null && _a !== void 0 ? _a : {};
|
|
541
|
+
ctx.cookie = cookie;
|
|
542
|
+
};
|
|
543
|
+
};
|
|
544
|
+
exports.parseCookie = parseCookie;
|
|
545
|
+
/**
|
|
546
|
+
* Creates middleware that parses the request body based on Content-Type.
|
|
547
|
+
* Supports url-encoded forms, JSON, and plain text.
|
|
548
|
+
* @param {Object} [options] - Configuration options for body parsing
|
|
549
|
+
* @param {SupportedBodyMediaTypes|SupportedBodyMediaTypes[]} [options.accept] - Media types to accept (defaults to all supported)
|
|
550
|
+
* @param {number} [options.maxSize] - Maximum body size in bytes (defaults to 1MB)
|
|
551
|
+
* @returns {Function} A middleware function that adds parsed body to context.body
|
|
552
|
+
* @throws {Response} Returns a 415 response if content-type is not accepted
|
|
553
|
+
* @throws {Response} Returns a 413 response if body exceeds maxSize
|
|
554
|
+
* @throws {Response} Returns a 400 response if body is malformed
|
|
555
|
+
* @example
|
|
556
|
+
* const bodyParser = parseBody({ maxSize: 5000 });
|
|
557
|
+
* // Use in respondWith: respondWith({}, bodyParser(), ...otherHandlers)
|
|
558
|
+
*/
|
|
559
|
+
const parseBody = (options) => {
|
|
560
|
+
var _a;
|
|
561
|
+
const accept = (options === null || options === void 0 ? void 0 : options.accept)
|
|
562
|
+
? Array.isArray(options.accept)
|
|
563
|
+
? options.accept
|
|
564
|
+
: [options.accept]
|
|
565
|
+
: [
|
|
566
|
+
"application/x-www-form-urlencoded",
|
|
567
|
+
"application/json",
|
|
568
|
+
"text/plain",
|
|
569
|
+
];
|
|
570
|
+
const maxSize = (_a = options === null || options === void 0 ? void 0 : options.maxSize) !== null && _a !== void 0 ? _a : 1024 * 1024; // Default 1MB
|
|
571
|
+
return (req, ctx) => __awaiter(void 0, void 0, void 0, function* () {
|
|
572
|
+
var _a, _b, _c, _d;
|
|
573
|
+
const contentType = (_a = req.headers.get("content-type")) === null || _a === void 0 ? void 0 : _a.split(";", 2)[0];
|
|
574
|
+
if (!(contentType && accept.includes(contentType))) {
|
|
575
|
+
yield ((_b = req.body) === null || _b === void 0 ? void 0 : _b.cancel().catch(() => { }));
|
|
576
|
+
return (0, exports.status)(415);
|
|
577
|
+
}
|
|
578
|
+
try {
|
|
579
|
+
const contentLengthHeader = req.headers.get("content-length");
|
|
580
|
+
if (!contentLengthHeader || parseInt(contentLengthHeader) > maxSize) {
|
|
581
|
+
yield ((_c = req.body) === null || _c === void 0 ? void 0 : _c.cancel().catch(() => { }));
|
|
582
|
+
return (0, exports.status)(413);
|
|
583
|
+
}
|
|
584
|
+
switch (contentType) {
|
|
585
|
+
case "application/x-www-form-urlencoded": {
|
|
586
|
+
const body = yield req.formData();
|
|
587
|
+
ctx.body = Object.fromEntries(body.entries());
|
|
588
|
+
break;
|
|
589
|
+
}
|
|
590
|
+
case "application/json": {
|
|
591
|
+
const body = yield req.json();
|
|
592
|
+
ctx.body = typeof body === "object" ? body : {};
|
|
593
|
+
break;
|
|
594
|
+
}
|
|
595
|
+
case "text/plain": {
|
|
596
|
+
const text = yield req.text();
|
|
597
|
+
ctx.body = { text };
|
|
598
|
+
break;
|
|
599
|
+
}
|
|
600
|
+
default:
|
|
601
|
+
ctx.body = {};
|
|
602
|
+
break;
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
catch (_e) {
|
|
606
|
+
yield ((_d = req.body) === null || _d === void 0 ? void 0 : _d.cancel().catch(() => { }));
|
|
607
|
+
return (0, exports.status)(400, "Malformed Payload");
|
|
608
|
+
}
|
|
609
|
+
});
|
|
610
|
+
};
|
|
611
|
+
exports.parseBody = parseBody;
|
|
612
|
+
/**
|
|
613
|
+
* Creates a request handler that processes requests through a series of middleware/handlers.
|
|
614
|
+
* Handlers are executed in order. If a handler returns a Response, that response is returned immediately.
|
|
615
|
+
* If no handler returns a Response, returns a 204 No Content response.
|
|
616
|
+
* @template Context
|
|
617
|
+
* @template {Array<RequestHandler<Context>>} Handlers
|
|
618
|
+
* @param {Context} ctxInit - Initial context object
|
|
619
|
+
* @param {...RequestHandler<Context>} handlers - Handler functions to process the request
|
|
620
|
+
* @returns {Function} A function that takes a Request and returns a Promise<Response>
|
|
621
|
+
* @example
|
|
622
|
+
* const handler = respondWith(
|
|
623
|
+
* {},
|
|
624
|
+
* parseCookie(),
|
|
625
|
+
* parseBody(),
|
|
626
|
+
* (req, ctx) => {
|
|
627
|
+
* return json({ cookie: ctx.cookie, body: ctx.body });
|
|
628
|
+
* }
|
|
629
|
+
* );
|
|
630
|
+
*/
|
|
631
|
+
const respondWith = (ctxInit, ...handlers) => {
|
|
632
|
+
return (req) => __awaiter(void 0, void 0, void 0, function* () {
|
|
633
|
+
const ctx = ctxInit;
|
|
634
|
+
for (const handler of handlers) {
|
|
635
|
+
let response = handler(req, ctx);
|
|
636
|
+
if (response instanceof Promise)
|
|
637
|
+
response = yield response;
|
|
638
|
+
if (response instanceof Response)
|
|
639
|
+
return response;
|
|
640
|
+
}
|
|
641
|
+
return (0, exports.status)(204, null);
|
|
642
|
+
});
|
|
643
|
+
};
|
|
644
|
+
exports.respondWith = respondWith;
|
|
645
|
+
/**
|
|
646
|
+
* Creates a request handler with error catching.
|
|
647
|
+
* Similar to respondWith but includes an error handler to catch exceptions.
|
|
648
|
+
* @template Context
|
|
649
|
+
* @template {RequestErrorHandler<Context>} Handler
|
|
650
|
+
* @template {Array<RequestHandler<Context>>} Handlers
|
|
651
|
+
* @param {Context} ctxInit - Initial context object
|
|
652
|
+
* @param {Handler} catcher - Error handler function
|
|
653
|
+
* @param {...RequestHandler<Context>} handlers - Handler functions to process the request
|
|
654
|
+
* @returns {Function} A function that takes a Request and returns a Promise<Response>
|
|
655
|
+
* @example
|
|
656
|
+
* const handler = respondWithCatcher(
|
|
657
|
+
* {},
|
|
658
|
+
* (req, error, ctx) => {
|
|
659
|
+
* return json({ error: error.message }, { status: 500 });
|
|
660
|
+
* },
|
|
661
|
+
* parseBody(),
|
|
662
|
+
* (req, ctx) => {
|
|
663
|
+
* // This might throw an error
|
|
664
|
+
* return json({ data: ctx.body });
|
|
665
|
+
* }
|
|
666
|
+
* );
|
|
667
|
+
*/
|
|
668
|
+
const respondWithCatcher = (ctxInit, catcher, ...handlers) => {
|
|
669
|
+
return (req) => __awaiter(void 0, void 0, void 0, function* () {
|
|
670
|
+
const ctx = ctxInit;
|
|
671
|
+
try {
|
|
672
|
+
for (const handler of handlers) {
|
|
673
|
+
let response = handler(req, ctx);
|
|
674
|
+
if (response instanceof Promise)
|
|
675
|
+
response = yield response;
|
|
676
|
+
if (response instanceof Response)
|
|
677
|
+
return response;
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
catch (error) {
|
|
681
|
+
let response = catcher(req, error instanceof Error ? error : new Error(String(error)), ctx);
|
|
682
|
+
if (response instanceof Promise)
|
|
683
|
+
response = yield response;
|
|
684
|
+
if (response instanceof Response)
|
|
685
|
+
return response;
|
|
686
|
+
}
|
|
687
|
+
return (0, exports.status)(204, null);
|
|
688
|
+
});
|
|
689
|
+
};
|
|
690
|
+
exports.respondWithCatcher = respondWithCatcher;
|
|
691
|
+
//# sourceMappingURL=helpers.js.map
|