@bepalo/router 1.2.14 → 1.6.21
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/README.md +141 -49
- package/dist/cjs/helpers.d.ts +83 -63
- package/dist/cjs/helpers.d.ts.map +1 -1
- package/dist/cjs/helpers.js +94 -91
- package/dist/cjs/helpers.js.map +1 -1
- package/dist/cjs/middlewares.d.ts +122 -3
- package/dist/cjs/middlewares.d.ts.map +1 -1
- package/dist/cjs/middlewares.js +188 -1
- package/dist/cjs/middlewares.js.map +1 -1
- package/dist/cjs/router.d.ts +3 -2
- package/dist/cjs/router.d.ts.map +1 -1
- package/dist/cjs/router.js +56 -28
- package/dist/cjs/router.js.map +1 -1
- package/dist/cjs/types.d.ts +30 -8
- package/dist/cjs/types.d.ts.map +1 -1
- package/dist/cjs/upload-stream.d.ts +3 -3
- package/dist/cjs/upload-stream.d.ts.map +1 -1
- package/dist/cjs/upload-stream.js +1 -2
- package/dist/cjs/upload-stream.js.map +1 -1
- package/dist/helpers.d.ts +83 -63
- package/dist/helpers.d.ts.map +1 -1
- package/dist/helpers.js +94 -91
- package/dist/helpers.js.map +1 -1
- package/dist/middlewares.d.ts +122 -3
- package/dist/middlewares.d.ts.map +1 -1
- package/dist/middlewares.js +188 -1
- package/dist/middlewares.js.map +1 -1
- package/dist/router.d.ts +3 -2
- package/dist/router.d.ts.map +1 -1
- package/dist/router.js +56 -28
- package/dist/router.js.map +1 -1
- package/dist/types.d.ts +30 -8
- package/dist/types.d.ts.map +1 -1
- package/dist/upload-stream.d.ts +3 -3
- package/dist/upload-stream.d.ts.map +1 -1
- package/dist/upload-stream.js +1 -2
- package/dist/upload-stream.js.map +1 -1
- package/package.json +9 -6
package/README.md
CHANGED
|
@@ -10,6 +10,20 @@
|
|
|
10
10
|
|
|
11
11
|
**A fast, feature-rich HTTP router for modern JavaScript runtimes.** [jump to example](#example)
|
|
12
12
|
|
|
13
|
+
## What is new in this version
|
|
14
|
+
|
|
15
|
+
Please refer to the [change-log](CHANGELOG.md).
|
|
16
|
+
|
|
17
|
+
Added `ALL` and `CRUD` shortcuts to method-path definition.
|
|
18
|
+
|
|
19
|
+
```js
|
|
20
|
+
router.handle("ALL /.**", ...); // HEAD|OPTIONS|GET|POST|PUT|PATCH|DELETE
|
|
21
|
+
router.handle("CRUD /api/.**", ...); // GET|POST|PUT|PATCH|DELETE
|
|
22
|
+
// instead of
|
|
23
|
+
router.handle(["HEAD /", "OPTIONS /", ...], ...); // "ALL /"
|
|
24
|
+
router.handle(["GET /api", "POST /api", ...], ...); // "CRUD /api"
|
|
25
|
+
```
|
|
26
|
+
|
|
13
27
|
## 📑 Table of Contents
|
|
14
28
|
|
|
15
29
|
1. [🏆 @bepalo/router](#-bepalorouter)
|
|
@@ -49,16 +63,16 @@
|
|
|
49
63
|
## ✨ Features
|
|
50
64
|
|
|
51
65
|
- ⚡ **High Performance** - Built on a radix tree for O(k) route matching (where k is path length)
|
|
52
|
-
- 🎯 **Flexible Routing** - Support for path parameters, wildcards (`*`, `.*`, `**`, `.**`), and all HTTP methods
|
|
53
|
-
- 🎭 **Multiple Handler Types** - Filters, hooks, handlers, fallbacks, and catchers
|
|
66
|
+
- 🎯 **Flexible Routing Engine** - Support for path parameters, wildcards (`*`, `.*`, `**`, `.**`), and all HTTP methods
|
|
67
|
+
- 🎭 **Multiple Handler Types** - Filters, hooks, afters, handlers, fallbacks, and catchers
|
|
54
68
|
- 🔌 **Middleware Pipeline** - Chain multiple handlers with early exit capability
|
|
55
69
|
- 🛡️ **Error Handling** - Built-in error catching with contextual error handlers
|
|
56
70
|
- 🔄 **Method-Based Routing** - Separate routing trees for each HTTP method
|
|
57
|
-
- 📦 **Local Dependencies** - Only @bepalo
|
|
71
|
+
- 📦 **Local Dependencies** - Minimal Dependencies — Only internal @bepalo packages
|
|
58
72
|
- 🌐 **Runtime Agnostic** - Works with Bun, Deno, Node.js, and other runtimes
|
|
59
73
|
- 🔧 **TypeScript Ready** - Full type definitions included
|
|
60
|
-
- 🧩 **Router
|
|
61
|
-
- 🛠️ **Helper
|
|
74
|
+
- 🧩 **Composable Router Architecture** - Append one router to another with a path prefix.
|
|
75
|
+
- 🛠️ **Built-in Helper Utilities** - Built-in response helpers (json, html, parseBody, upload, etc.)
|
|
62
76
|
- 🔐 **Middleware Integration** - CORS, rate limiting, authentication helpers
|
|
63
77
|
|
|
64
78
|
## 🚀 Get Started
|
|
@@ -183,8 +197,8 @@ const router = new Router<CTXAddress>({
|
|
|
183
197
|
///...
|
|
184
198
|
});
|
|
185
199
|
|
|
186
|
-
// Global CORS
|
|
187
|
-
router.filter("
|
|
200
|
+
// Global rate-limit and CORS
|
|
201
|
+
router.filter("ALL /.**", [
|
|
188
202
|
limitRate({
|
|
189
203
|
key: (req, ctx) => ctx.address.address, // used to identify client
|
|
190
204
|
maxTokens: 30,
|
|
@@ -198,35 +212,26 @@ router.filter("GET /.**", [
|
|
|
198
212
|
]);
|
|
199
213
|
|
|
200
214
|
// Rate limiting for API
|
|
201
|
-
router.filter(
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
cors({
|
|
218
|
-
origins: ["http://localhost:3000", "https://example.com"],
|
|
219
|
-
methods: ["GET", "POST", "PUT", "DELETE"],
|
|
220
|
-
allowedHeaders: ["Content-Type", "Authorization"],
|
|
221
|
-
credentials: true,
|
|
222
|
-
endHere: true,
|
|
223
|
-
}),
|
|
224
|
-
],
|
|
225
|
-
);
|
|
215
|
+
router.filter("CRUD /api/.**",[
|
|
216
|
+
limitRate({
|
|
217
|
+
key: (req, ctx) => ctx.address.address, // used to identify client
|
|
218
|
+
maxTokens: 100,
|
|
219
|
+
refillInterval: 30_000, // every 30 seconds
|
|
220
|
+
refillRate: 50, // 50 tokens every refillInterval
|
|
221
|
+
setXRateLimitHeaders: true,
|
|
222
|
+
}),
|
|
223
|
+
cors({
|
|
224
|
+
origins: ["http://localhost:3000", "https://example.com"],
|
|
225
|
+
methods: ["GET", "POST", "PUT", "DELETE"],
|
|
226
|
+
allowedHeaders: ["Content-Type", "Authorization"],
|
|
227
|
+
credentials: true,
|
|
228
|
+
endHere: true,
|
|
229
|
+
}),
|
|
230
|
+
]);
|
|
226
231
|
|
|
227
232
|
// Main route
|
|
228
233
|
router.handle("GET /", () =>
|
|
229
|
-
html("<h1>Welcome! Enjoy using @
|
|
234
|
+
html("<h1>Welcome! Enjoy using @bepalo/router</h1>"),
|
|
230
235
|
);
|
|
231
236
|
router.handle("GET /status", () => status(200));
|
|
232
237
|
|
|
@@ -351,7 +356,7 @@ Deno.serve(
|
|
|
351
356
|
#### Nodejs
|
|
352
357
|
|
|
353
358
|
```js
|
|
354
|
-
//
|
|
359
|
+
// Node.js compatibility example (uses Fetch bridge; not optimized)
|
|
355
360
|
http
|
|
356
361
|
.createServer(async (req, res) => {
|
|
357
362
|
const url = new URL(req.url || "/", `http://${req.headers.host}`);
|
|
@@ -541,6 +546,7 @@ router.respond(
|
|
|
541
546
|
|
|
542
547
|
```ts
|
|
543
548
|
import {
|
|
549
|
+
Status, // Status enum
|
|
544
550
|
status, // HTTP status response
|
|
545
551
|
redirect, // Redirect response with location header set
|
|
546
552
|
forward, // forward to other path within the router.
|
|
@@ -568,10 +574,45 @@ const router = new Router();
|
|
|
568
574
|
router.handle("GET /text", () => text("Hello World"));
|
|
569
575
|
router.handle("GET /html", () => html("<h1>Title</h1>"));
|
|
570
576
|
router.handle("GET /json", () => json({ data: "value" }));
|
|
571
|
-
router.handle("GET /status", () => status(
|
|
577
|
+
router.handle("GET /status", () => status(Status._204_NoContent, null)); // No Content
|
|
572
578
|
router.handle("GET /intro.mp4", () => blob(Bun.file("./intro.mp4")));
|
|
573
579
|
router.handle("GET /download", () => octetStream(Bun.file("./intro.mp4")));
|
|
574
580
|
|
|
581
|
+
router.handle("GET /new-location", () => html("GET new-location"));
|
|
582
|
+
// router.handle("POST /new-location", () => html("POST new-location"));
|
|
583
|
+
router.handle<CTXBody>("POST /new-location", [
|
|
584
|
+
parseBody({ once: true, clone: true }),
|
|
585
|
+
(req, { body }) => console.log(body),
|
|
586
|
+
forward("/new-location2"),
|
|
587
|
+
]);
|
|
588
|
+
router.handle<CTXBody>("POST /new-location2", [
|
|
589
|
+
parseBody({ once: true, clone: false }),
|
|
590
|
+
(req, { body }) => console.log(body),
|
|
591
|
+
() => html("POST new-location2"),
|
|
592
|
+
]);
|
|
593
|
+
router.handle("GET /redirected", () => redirect("/new-location"));
|
|
594
|
+
router.handle("GET /forwarded", forward("/new-location"));
|
|
595
|
+
// this would set the headers:
|
|
596
|
+
// "x-forwarded-path": "/forwarded"
|
|
597
|
+
// "x-original-path": "/forwarded"
|
|
598
|
+
router.handle<CTXBody>("PUT /forwarded-with-new-method", [
|
|
599
|
+
parseBody({ once: true, clone: true }),
|
|
600
|
+
(req, { body }) => console.log(body),
|
|
601
|
+
forward("/new-location", { method: "POST" }),
|
|
602
|
+
]);
|
|
603
|
+
// this would set the headers:
|
|
604
|
+
// "x-forwarded-method": "PUT"
|
|
605
|
+
// "x-forwarded-path": "/new-location"
|
|
606
|
+
// "x-original-path": "/forwarded-with-new-method"
|
|
607
|
+
router.handle("GET /forwarded-conditional", function (this: Router, req, ctx) {
|
|
608
|
+
if (req.headers.get("authorization"))
|
|
609
|
+
return forward("/new-location").bind(this)(req, ctx);
|
|
610
|
+
// or return forward("/new-location").apply(this, [req, ctx]);
|
|
611
|
+
// NOTE: be careful when binding instance `router` instead of `this`
|
|
612
|
+
// as it might be called from a different router due to append.
|
|
613
|
+
return status(Status._401_Unauthorized);
|
|
614
|
+
});
|
|
615
|
+
|
|
575
616
|
router.handle<CTXCookie>("GET /cookie", [
|
|
576
617
|
parseCookie(),
|
|
577
618
|
(req, { cookie }) => json({ cookie }),
|
|
@@ -580,7 +621,8 @@ router.handle<CTXCookie>("GET /cookie", [
|
|
|
580
621
|
router.handle<CTXBody>("POST /cookie", [
|
|
581
622
|
parseBody(),
|
|
582
623
|
(req, { body }) => {
|
|
583
|
-
return status(
|
|
624
|
+
return status(Status._200_OK, "OK", {
|
|
625
|
+
// or `, undefined, {` and the status text will be set
|
|
584
626
|
headers: [
|
|
585
627
|
...Object.entries(body).map(([name, value]) =>
|
|
586
628
|
setCookie(name, String(value), {
|
|
@@ -595,24 +637,55 @@ router.handle<CTXBody>("POST /cookie", [
|
|
|
595
637
|
|
|
596
638
|
router.handle("DELETE /cookie/:name", [
|
|
597
639
|
(req, ctx) =>
|
|
598
|
-
status(
|
|
640
|
+
status(Status._200_OK, undefined, {
|
|
599
641
|
headers: [clearCookie(ctx.params.name, { path: "/" })],
|
|
600
642
|
}),
|
|
601
643
|
]);
|
|
602
644
|
|
|
645
|
+
// be sure to create ./upload folder for this example
|
|
603
646
|
router.handle<CTXUpload>("POST /upload", [
|
|
604
647
|
(req, ctx) => {
|
|
605
648
|
let file: Bun.BunFile;
|
|
606
649
|
let fileWriter: Bun.FileSink;
|
|
607
650
|
return parseUploadStreaming({
|
|
608
651
|
allowedTypes: ["image/jpeg"],
|
|
609
|
-
|
|
610
|
-
|
|
652
|
+
maxFileSize: 5 * 1024 * 1024,
|
|
653
|
+
maxTotalSize: 5 * 1024 * 1024 + 1024,
|
|
654
|
+
maxFields: 1,
|
|
655
|
+
maxFiles: 1,
|
|
656
|
+
// uploadIdGenerator: () =>
|
|
657
|
+
// `upload_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`,
|
|
658
|
+
async onUploadStart(uploadId, totalSize) {
|
|
659
|
+
console.log("onUploadStart", { uploadId, totalSize });
|
|
660
|
+
},
|
|
661
|
+
async onError(uploadId, error) {
|
|
662
|
+
console.error("onError", uploadId, error);
|
|
663
|
+
},
|
|
664
|
+
async onFileError(uploadId, fieldName, fileName, error) {
|
|
665
|
+
console.error("onFileError", uploadId, error);
|
|
666
|
+
},
|
|
667
|
+
async onField(uploadId, fieldName, value) {
|
|
668
|
+
console.log("onField", { uploadId, fieldName, value });
|
|
669
|
+
},
|
|
670
|
+
async onFileStart(uploadId, fieldName, fileName, contentType) {
|
|
671
|
+
console.log("onFileStart", {
|
|
672
|
+
uploadId,
|
|
673
|
+
fieldName,
|
|
674
|
+
fileName,
|
|
675
|
+
contentType,
|
|
676
|
+
});
|
|
611
677
|
const ext = fileName.substring(fileName.lastIndexOf("."));
|
|
612
|
-
|
|
678
|
+
const customFilename = uploadId + ext;
|
|
679
|
+
file = Bun.file("./uploads/" + customFilename);
|
|
613
680
|
fileWriter = file.writer();
|
|
681
|
+
return {
|
|
682
|
+
customFilename,
|
|
683
|
+
// metadata
|
|
684
|
+
};
|
|
614
685
|
},
|
|
615
686
|
async onFileChunk(uploadId, fieldName, fileName, chunk, offset, isLast) {
|
|
687
|
+
const chunkSize = chunk.byteLength;
|
|
688
|
+
console.log("onFileChunk", { uploadId, chunkSize, offset, isLast });
|
|
616
689
|
fileWriter.write(chunk);
|
|
617
690
|
},
|
|
618
691
|
async onFileComplete(
|
|
@@ -623,17 +696,26 @@ router.handle<CTXUpload>("POST /upload", [
|
|
|
623
696
|
customFilename,
|
|
624
697
|
metadata,
|
|
625
698
|
) {
|
|
626
|
-
console.log(
|
|
699
|
+
console.log("onFileComplete", {
|
|
700
|
+
uploadId,
|
|
701
|
+
fieldName,
|
|
702
|
+
fileName,
|
|
703
|
+
fileSize,
|
|
704
|
+
customFilename,
|
|
705
|
+
metadata,
|
|
706
|
+
});
|
|
707
|
+
if (fileWriter) {
|
|
708
|
+
fileWriter.end();
|
|
709
|
+
}
|
|
627
710
|
},
|
|
628
711
|
async onUploadComplete(uploadId, success) {
|
|
629
|
-
console.log({ uploadId, success });
|
|
712
|
+
console.log("onUploadComplete", { uploadId, success });
|
|
630
713
|
},
|
|
631
714
|
})(req, ctx);
|
|
632
715
|
},
|
|
633
716
|
(req, { uploadId, fields, files }) => {
|
|
634
717
|
console.log({ uploadId, fields, files });
|
|
635
|
-
|
|
636
|
-
return status(200);
|
|
718
|
+
return status(Status._200_OK);
|
|
637
719
|
},
|
|
638
720
|
]);
|
|
639
721
|
```
|
|
@@ -642,12 +724,21 @@ router.handle<CTXUpload>("POST /upload", [
|
|
|
642
724
|
|
|
643
725
|
```ts
|
|
644
726
|
import {
|
|
727
|
+
type CTXCookie,
|
|
728
|
+
type CTXBody,
|
|
729
|
+
type CTXAuth,
|
|
730
|
+
type CTXUpload,
|
|
731
|
+
parseCookie, // Cookie parser
|
|
732
|
+
parseBody, // Body parser
|
|
733
|
+
parseUploadStreaming, // multi-part-form-data and file upload stream parser
|
|
645
734
|
cors, // CORS middleware
|
|
646
735
|
limitRate, // Rate limiting
|
|
736
|
+
authenticate, // Generic authentication middleware
|
|
737
|
+
authorize, // Generic authorization middleware
|
|
647
738
|
authBasic, // Basic authentication
|
|
648
739
|
authAPIKey, // API key authentication
|
|
649
740
|
authJWT, // JWT authentication
|
|
650
|
-
} from "@bepalo/router
|
|
741
|
+
} from "@bepalo/router";
|
|
651
742
|
```
|
|
652
743
|
|
|
653
744
|
### 🔧 Advanced Usage
|
|
@@ -768,13 +859,14 @@ The router uses a radix tree (trie) data structure for route matching, providing
|
|
|
768
859
|
| Feature | @bepalo/router | Express | Hono | Fastify |
|
|
769
860
|
| ------------------------------- | -------------- | ------- | ---- | ------- |
|
|
770
861
|
| Radix Tree Routing | ✅ | ❌ | ✅ | ✅ |
|
|
771
|
-
| Few Dependencies | ✅ | ❌ |
|
|
862
|
+
| Few Dependencies | ✅ | ❌ | ⚠️ | ⚠️ |
|
|
772
863
|
| TypeScript Native | ✅ | ❌ | ✅ | ✅ |
|
|
773
|
-
| Extended Handler Phases | ✅ |
|
|
774
|
-
| Built-in Middleware | ✅ |
|
|
775
|
-
| Runtime Agnostic | ✅ | ❌ | ✅ |
|
|
864
|
+
| Extended Handler Phases | ✅ | ❌ | ❌ | ⚠️ |
|
|
865
|
+
| Built-in Middleware | ✅ | ⚠️ | ✅ | ✅ |
|
|
866
|
+
| Runtime Agnostic | ✅ | ❌ | ✅ | ⚠️ |
|
|
776
867
|
| Router Composition | ✅ | ✅ | ✅ | ✅ |
|
|
777
868
|
| Structured Multi-Phase Pipeline | ✅ | ❌ | ❌ | ❌ |
|
|
869
|
+
| Server | ❌ | ✅ | ⚠️ | ✅ |
|
|
778
870
|
|
|
779
871
|
## 📄 License
|
|
780
872
|
|
package/dist/cjs/helpers.d.ts
CHANGED
|
@@ -1,17 +1,80 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Handler } from "./types";
|
|
1
|
+
import type { BoundHandler, HttpMethod } from "./types";
|
|
3
2
|
export * from "./upload-stream";
|
|
4
|
-
export
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
export declare enum Status {
|
|
4
|
+
_100_Continue = 100,
|
|
5
|
+
_101_SwitchingProtocols = 101,
|
|
6
|
+
_102_Processing = 102,
|
|
7
|
+
_103_EarlyHints = 103,
|
|
8
|
+
_200_OK = 200,
|
|
9
|
+
_201_Created = 201,
|
|
10
|
+
_202_Accepted = 202,
|
|
11
|
+
_203_NonAuthoritativeInformation = 203,
|
|
12
|
+
_204_NoContent = 204,
|
|
13
|
+
_205_ResetContent = 205,
|
|
14
|
+
_206_PartialContent = 206,
|
|
15
|
+
_207_MultiStatus = 207,
|
|
16
|
+
_208_AlreadyReported = 208,
|
|
17
|
+
_226_IMUsed = 226,
|
|
18
|
+
_300_MultipleChoices = 300,
|
|
19
|
+
_301_MovedPermanently = 301,
|
|
20
|
+
_302_Found = 302,
|
|
21
|
+
_303_SeeOther = 303,
|
|
22
|
+
_304_NotModified = 304,
|
|
23
|
+
_305_UseProxy = 305,
|
|
24
|
+
_307_TemporaryRedirect = 307,
|
|
25
|
+
_308_PermanentRedirect = 308,
|
|
26
|
+
_400_BadRequest = 400,
|
|
27
|
+
_401_Unauthorized = 401,
|
|
28
|
+
_402_PaymentRequired = 402,
|
|
29
|
+
_403_Forbidden = 403,
|
|
30
|
+
_404_NotFound = 404,
|
|
31
|
+
_405_MethodNotAllowed = 405,
|
|
32
|
+
_406_NotAcceptable = 406,
|
|
33
|
+
_407_ProxyAuthenticationRequired = 407,
|
|
34
|
+
_408_RequestTimeout = 408,
|
|
35
|
+
_409_Conflict = 409,
|
|
36
|
+
_410_Gone = 410,
|
|
37
|
+
_411_LengthRequired = 411,
|
|
38
|
+
_412_PreconditionFailed = 412,
|
|
39
|
+
_413_PayloadTooLarge = 413,
|
|
40
|
+
_414_URITooLong = 414,
|
|
41
|
+
_415_UnsupportedMediaType = 415,
|
|
42
|
+
_416_RangeNotSatisfiable = 416,
|
|
43
|
+
_417_ExpectationFailed = 417,
|
|
44
|
+
_418_IMATeapot = 418,
|
|
45
|
+
_421_MisdirectedRequest = 421,
|
|
46
|
+
_422_UnprocessableEntity = 422,
|
|
47
|
+
_423_Locked = 423,
|
|
48
|
+
_424_FailedDependency = 424,
|
|
49
|
+
_425_TooEarly = 425,
|
|
50
|
+
_426_UpgradeRequired = 426,
|
|
51
|
+
_428_PreconditionRequired = 428,
|
|
52
|
+
_429_TooManyRequests = 429,
|
|
53
|
+
_431_RequestHeaderFieldsTooLarge = 431,
|
|
54
|
+
_451_UnavailableForLegalReasons = 451,
|
|
55
|
+
_500_InternalServerError = 500,
|
|
56
|
+
_501_NotImplemented = 501,
|
|
57
|
+
_502_BadGateway = 502,
|
|
58
|
+
_503_ServiceUnavailable = 503,
|
|
59
|
+
_504_GatewayTimeout = 504,
|
|
60
|
+
_505_HTTPVersionNotSupported = 505,
|
|
61
|
+
_506_VariantAlsoNegotiates = 506,
|
|
62
|
+
_507_InsufficientStorage = 507,
|
|
63
|
+
_508_LoopDetected = 508,
|
|
64
|
+
_510_NotExtended = 510,
|
|
65
|
+
_511_NetworkAuthenticationRequired = 511,
|
|
66
|
+
_419_PageExpired = 419,
|
|
67
|
+
_420_EnhanceYourCalm = 420,
|
|
68
|
+
_450_BlockedbyWindowsParentalControls = 450,
|
|
69
|
+
_498_InvalidToken = 498,
|
|
70
|
+
_499_TokenRequired = 499,
|
|
71
|
+
_509_BandwidthLimitExceeded = 509,
|
|
72
|
+
_526_InvalidSSLCertificate = 526,
|
|
73
|
+
_529_Siteisoverloaded = 529,
|
|
74
|
+
_530_Siteisfrozen = 530,
|
|
75
|
+
_598_NetworkReadTimeoutError = 598,
|
|
76
|
+
_599_NetworkConnectTimeoutError = 599
|
|
8
77
|
}
|
|
9
|
-
export type CTXError = {
|
|
10
|
-
error: Error;
|
|
11
|
-
};
|
|
12
|
-
export type CTXAddress = {
|
|
13
|
-
address: SocketAddress;
|
|
14
|
-
};
|
|
15
78
|
export declare function getHttpStatusText(code: number): string;
|
|
16
79
|
/**
|
|
17
80
|
* Creates a Response with the specified status code.
|
|
@@ -36,14 +99,18 @@ export declare const status: (status: number, content?: string | null, init?: Re
|
|
|
36
99
|
*/
|
|
37
100
|
export declare const redirect: (location: string, init?: ResponseInit) => Response;
|
|
38
101
|
/**
|
|
39
|
-
* Forwards the request to another
|
|
40
|
-
* Does not
|
|
102
|
+
* Forwards the request to another route internally.
|
|
103
|
+
* Does not send a redirect to the client but changes the path and method,
|
|
104
|
+
* adds X-Forwarded-[Method|Path] and X-Original-Path headers and calls
|
|
105
|
+
* `(this as Router).respond(newReq, ctx)`.
|
|
106
|
+
* NOTE: parse body only once at the first handler using `parseBody({once: true})`
|
|
107
|
+
* as the body will be consumed at the first parseBody call.
|
|
41
108
|
* @param {string} path - The new path to forward to
|
|
42
109
|
* @returns {Response} A Response object with the forwarded request's response
|
|
43
110
|
*/
|
|
44
111
|
export declare const forward: <XContext = {}>(path: string, options?: {
|
|
45
|
-
method
|
|
46
|
-
}) =>
|
|
112
|
+
method?: HttpMethod;
|
|
113
|
+
}) => BoundHandler<XContext>;
|
|
47
114
|
/**
|
|
48
115
|
* Creates a text/plain Response.
|
|
49
116
|
* Defaults to status 200 and text/plain content-type if not specified.
|
|
@@ -194,53 +261,6 @@ export declare const clearCookie: (name: string, options?: CookieOptions) => Coo
|
|
|
194
261
|
* // Returns: { session: "abc123", theme: "dark" }
|
|
195
262
|
*/
|
|
196
263
|
export declare const parseCookieFromRequest: <Expected extends Record<string, string>>(req: Request) => Expected | undefined;
|
|
197
|
-
/**
|
|
198
|
-
* Context object containing parsed cookies.
|
|
199
|
-
* @typedef {Object} CTXCookie
|
|
200
|
-
* @property {Record<string, string>} cookie - Parsed cookies from the request
|
|
201
|
-
*/
|
|
202
|
-
export type CTXCookie = {
|
|
203
|
-
cookie: Record<string, string>;
|
|
204
|
-
};
|
|
205
|
-
/**
|
|
206
|
-
* Creates middleware that parses cookies from the request and adds them to the context.
|
|
207
|
-
* @returns {Function} A middleware function that adds parsed cookies to context.cookie
|
|
208
|
-
* @example
|
|
209
|
-
* const cookieParser = parseCookie();
|
|
210
|
-
* // Use in respondWith: respondWith({}, cookieParser(), ...otherHandlers)
|
|
211
|
-
*/
|
|
212
|
-
export declare const parseCookie: <Context extends CTXCookie>() => Handler<Context>;
|
|
213
|
-
/**
|
|
214
|
-
* Context object containing parsed request body.
|
|
215
|
-
* @typedef {Object} CTXBody
|
|
216
|
-
* @property {Record<string, unknown>} body - Parsed request body data
|
|
217
|
-
*/
|
|
218
|
-
export type CTXBody = {
|
|
219
|
-
body: Record<string, unknown>;
|
|
220
|
-
};
|
|
221
|
-
/**
|
|
222
|
-
* Supported media types for request body parsing.
|
|
223
|
-
* @typedef {"application/x-www-form-urlencoded"|"application/json"|"text/plain"} SupportedBodyMediaTypes
|
|
224
|
-
*/
|
|
225
|
-
export type SupportedBodyMediaTypes = "application/x-www-form-urlencoded" | "application/json" | "text/plain";
|
|
226
|
-
/**
|
|
227
|
-
* Creates middleware that parses the request body based on Content-Type.
|
|
228
|
-
* Supports url-encoded forms, JSON, and plain text.
|
|
229
|
-
* @param {Object} [options] - Configuration options for body parsing
|
|
230
|
-
* @param {SupportedBodyMediaTypes|SupportedBodyMediaTypes[]} [options.accept] - Media types to accept (defaults to all supported)
|
|
231
|
-
* @param {number} [options.maxSize] - Maximum body size in bytes (defaults to 1MB)
|
|
232
|
-
* @returns {Function} A middleware function that adds parsed body to context.body
|
|
233
|
-
* @throws {Response} Returns a 415 response if content-type is not accepted
|
|
234
|
-
* @throws {Response} Returns a 413 response if body exceeds maxSize
|
|
235
|
-
* @throws {Response} Returns a 400 response if body is malformed
|
|
236
|
-
* @example
|
|
237
|
-
* const bodyParser = parseBody({ maxSize: 5000 });
|
|
238
|
-
* // Use in respondWith: respondWith({}, bodyParser(), ...otherHandlers)
|
|
239
|
-
*/
|
|
240
|
-
export declare const parseBody: <Context extends CTXBody>(options?: {
|
|
241
|
-
accept?: SupportedBodyMediaTypes | SupportedBodyMediaTypes[];
|
|
242
|
-
maxSize?: number;
|
|
243
|
-
}) => Handler<Context>;
|
|
244
264
|
/**
|
|
245
265
|
* Request handler function type.
|
|
246
266
|
* @callback RequestHandler
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../src/helpers.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../src/helpers.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAExD,cAAc,iBAAiB,CAAC;AAEhC,oBAAY,MAAM;IAChB,aAAa,MAAM;IACnB,uBAAuB,MAAM;IAC7B,eAAe,MAAM;IACrB,eAAe,MAAM;IACrB,OAAO,MAAM;IACb,YAAY,MAAM;IAClB,aAAa,MAAM;IACnB,gCAAgC,MAAM;IACtC,cAAc,MAAM;IACpB,iBAAiB,MAAM;IACvB,mBAAmB,MAAM;IACzB,gBAAgB,MAAM;IACtB,oBAAoB,MAAM;IAC1B,WAAW,MAAM;IACjB,oBAAoB,MAAM;IAC1B,qBAAqB,MAAM;IAC3B,UAAU,MAAM;IAChB,aAAa,MAAM;IACnB,gBAAgB,MAAM;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