@aura-stack/router 0.1.0 → 0.2.0
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 -21
- package/README.md +17 -114
- package/dist/assert.cjs +4 -4
- package/dist/assert.js +1 -1
- package/dist/{chunk-VBI7H3AK.js → chunk-DR4C6QTF.js} +13 -16
- package/dist/{chunk-ENOBC3XN.js → chunk-JRJKKBSH.js} +4 -4
- package/dist/{chunk-RVJ2F7OL.js → chunk-O6SY753N.js} +1 -1
- package/dist/{chunk-NNCUFWPI.js → chunk-OXDCFAMF.js} +2 -2
- package/dist/{chunk-6UBPSXKR.js → chunk-YUX3YHXF.js} +1 -1
- package/dist/context.cjs +2 -2
- package/dist/context.js +3 -3
- package/dist/endpoint.cjs +2 -2
- package/dist/endpoint.d.cts +1 -1
- package/dist/endpoint.d.ts +1 -1
- package/dist/endpoint.js +2 -2
- package/dist/index.cjs +15 -18
- package/dist/index.js +5 -5
- package/dist/{middleware.cjs → middlewares.cjs} +5 -5
- package/dist/{middleware.js → middlewares.js} +1 -1
- package/dist/router.cjs +13 -16
- package/dist/router.js +5 -5
- package/dist/types.d.cts +2 -2
- package/dist/types.d.ts +2 -2
- package/package.json +4 -2
- /package/dist/{middleware.d.cts → middlewares.d.cts} +0 -0
- /package/dist/{middleware.d.ts → middlewares.d.ts} +0 -0
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2025 Aura Stack
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Aura Stack
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,128 +1,31 @@
|
|
|
1
1
|
# `@aura-stack/router`
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
Build fully-typed APIs with declarative endpoints, automatic parameter inference, and first-class middleware support — all returning native `Response` objects for seamless compatibility with the Fetch API.
|
|
3
|
+
<div align="center">
|
|
5
4
|
|
|
6
|
-
|
|
5
|
+
**A modern, TypeScript-first router and endpoint definition library**
|
|
7
6
|
|
|
8
|
-
-
|
|
9
|
-
- [Installation](#installation)
|
|
10
|
-
- [Quick Start](#quick-start)
|
|
11
|
-
- [Defining Endpoints](#defining-endpoints)
|
|
12
|
-
- [Validation & Middlewares](#validation--middlewares)
|
|
13
|
-
- [API Reference](#api-reference)
|
|
7
|
+
Build fully-typed APIs with declarative endpoints, automatic parameter inference, and first-class middleware support — all returning native `Response` objects.
|
|
14
8
|
|
|
15
|
-
|
|
9
|
+
[](https://www.npmjs.com/package/@aura-stack/router)
|
|
10
|
+
[](https://opensource.org/licenses/MIT)
|
|
11
|
+
[](https://www.typescriptlang.org/)
|
|
16
12
|
|
|
17
|
-
-
|
|
18
|
-
- `Declarative API` — define endpoints using `createEndpoint` and group them with `createRouter`.
|
|
19
|
-
- `Typed params & queries` — automatic inference of path (`/users/:id`) and search parameters.
|
|
20
|
-
- `Schema validation` — built-in support for `zod` to validate request bodies, queries, or params.
|
|
21
|
-
- `Middleware chaining` — supports global and per-endpoint middleware execution, with short-circuit control.
|
|
13
|
+
[Documentation](https://aura-stack-router.vercel.app) · [Quick Start](https://aura-stack-router.vercel.app/docs)
|
|
22
14
|
|
|
23
|
-
|
|
15
|
+
</div>
|
|
24
16
|
|
|
25
|
-
|
|
26
|
-
pnpm add @aura-stack/router
|
|
27
|
-
# or
|
|
28
|
-
npm install @aura-stack/router
|
|
29
|
-
# or
|
|
30
|
-
yarn add @aura-stack/router
|
|
31
|
-
```
|
|
17
|
+
---
|
|
32
18
|
|
|
33
|
-
##
|
|
19
|
+
## Documentation
|
|
34
20
|
|
|
35
|
-
|
|
36
|
-
import { createEndpoint, createRouter } from "@aura-stack/router"
|
|
21
|
+
Visit our [**documentation website**](https://aura-stack-router.vercel.app) for comprehensive guides, examples, and API references.
|
|
37
22
|
|
|
38
|
-
|
|
39
|
-
return Response.json(
|
|
40
|
-
{
|
|
41
|
-
session: {
|
|
42
|
-
userId: "uuid",
|
|
43
|
-
username: "John",
|
|
44
|
-
},
|
|
45
|
-
},
|
|
46
|
-
{ headers: ctx.headers }
|
|
47
|
-
)
|
|
48
|
-
})
|
|
23
|
+
## License
|
|
49
24
|
|
|
50
|
-
|
|
51
|
-
```
|
|
25
|
+
This project is licensed under the MIT License - see the [LICENSE](./LICENSE) file for details.
|
|
52
26
|
|
|
53
|
-
|
|
27
|
+
---
|
|
54
28
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
const credentialsConfig = createEndpointConfig({
|
|
60
|
-
schemas: {
|
|
61
|
-
body: z.object({
|
|
62
|
-
username: z.string(),
|
|
63
|
-
password: z.string(),
|
|
64
|
-
}),
|
|
65
|
-
},
|
|
66
|
-
})
|
|
67
|
-
|
|
68
|
-
export const signIn = createEndpoint(
|
|
69
|
-
"POST",
|
|
70
|
-
"/auth/credentials",
|
|
71
|
-
async (request, ctx) => {
|
|
72
|
-
const { body, headers } = ctx
|
|
73
|
-
headers.set("x-login", "success")
|
|
74
|
-
return Response.json({ status: "ok", body }, { headers })
|
|
75
|
-
},
|
|
76
|
-
credentialsConfig
|
|
77
|
-
)
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
## Validation & Middlewares
|
|
81
|
-
|
|
82
|
-
**Validation.** Provide Zod schemas in `createEndpointConfig`:
|
|
83
|
-
|
|
84
|
-
```ts
|
|
85
|
-
import { createEndpoint, createEndpointConfig } from "@aura-stack/router"
|
|
86
|
-
|
|
87
|
-
const config = createEndpointConfig({
|
|
88
|
-
schemas: {
|
|
89
|
-
searchParams: z.object({
|
|
90
|
-
redirect_uri: z.string(),
|
|
91
|
-
}),
|
|
92
|
-
},
|
|
93
|
-
})
|
|
94
|
-
|
|
95
|
-
const oauth = createEndpoint(
|
|
96
|
-
"GET",
|
|
97
|
-
"/auth/signin/:provider",
|
|
98
|
-
async (_req, ctx) => {
|
|
99
|
-
const { provider } = ctx.params
|
|
100
|
-
const { redirect_uri } = ctx.searchParams
|
|
101
|
-
return Response.json({ provider, redirect_uri }, { status: 302 })
|
|
102
|
-
},
|
|
103
|
-
config
|
|
104
|
-
)
|
|
105
|
-
```
|
|
106
|
-
|
|
107
|
-
If validation fails, the helper throws an informative error before your handler executes.
|
|
108
|
-
|
|
109
|
-
**Middlewares.** Provide async middleware functions to read or modify the request.
|
|
110
|
-
|
|
111
|
-
```ts
|
|
112
|
-
import { createRouter, type GlobalMiddleware } from "@aura-stack/router"
|
|
113
|
-
|
|
114
|
-
const audit: GlobalMiddleware = async (request) => {
|
|
115
|
-
request.headers.set("x-request-id", crypto.randomUUID())
|
|
116
|
-
return request
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
const router = createRouter([oauth], { middlewares: [audit] })
|
|
120
|
-
```
|
|
121
|
-
|
|
122
|
-
## API Reference
|
|
123
|
-
|
|
124
|
-
| Function | Description |
|
|
125
|
-
| ------------------------------------------------- | -------------------------------------------------------------------------- |
|
|
126
|
-
| `createEndpoint(method, route, handler, config?)` | Define a type-safe endpoint with optional validation and middlewares. |
|
|
127
|
-
| `createEndpointConfig(config)` | Helper that preserves Zod inference for endpoint schemas. |
|
|
128
|
-
| `createRouter(endpoints, config?)` | Build a router that dispatches native Fetch requests to the right handler. |
|
|
29
|
+
<p align="center">
|
|
30
|
+
Made with ❤️ by <a href="https://github.com/aura-stack-js">Aura Stack team</a>
|
|
31
|
+
</p>
|
package/dist/assert.cjs
CHANGED
|
@@ -26,13 +26,13 @@ __export(assert_exports, {
|
|
|
26
26
|
isValidRoute: () => isValidRoute
|
|
27
27
|
});
|
|
28
28
|
module.exports = __toCommonJS(assert_exports);
|
|
29
|
-
var supportedMethods = ["GET", "POST", "DELETE", "PUT", "PATCH"];
|
|
30
|
-
var supportedBodyMethods = ["POST", "PUT", "PATCH"];
|
|
29
|
+
var supportedMethods = /* @__PURE__ */ new Set(["GET", "POST", "DELETE", "PUT", "PATCH"]);
|
|
30
|
+
var supportedBodyMethods = /* @__PURE__ */ new Set(["POST", "PUT", "PATCH"]);
|
|
31
31
|
var isSupportedMethod = (method) => {
|
|
32
|
-
return supportedMethods.
|
|
32
|
+
return supportedMethods.has(method);
|
|
33
33
|
};
|
|
34
34
|
var isSupportedBodyMethod = (method) => {
|
|
35
|
-
return supportedBodyMethods.
|
|
35
|
+
return supportedBodyMethods.has(method);
|
|
36
36
|
};
|
|
37
37
|
var isValidRoute = (route) => {
|
|
38
38
|
const routePattern = /^\/[a-zA-Z0-9/_:-]*$/;
|
package/dist/assert.js
CHANGED
|
@@ -3,14 +3,14 @@ import {
|
|
|
3
3
|
getHeaders,
|
|
4
4
|
getRouteParams,
|
|
5
5
|
getSearchParams
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-OXDCFAMF.js";
|
|
7
7
|
import {
|
|
8
8
|
createRoutePattern
|
|
9
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-YUX3YHXF.js";
|
|
10
10
|
import {
|
|
11
11
|
executeGlobalMiddlewares,
|
|
12
12
|
executeMiddlewares
|
|
13
|
-
} from "./chunk-
|
|
13
|
+
} from "./chunk-O6SY753N.js";
|
|
14
14
|
import {
|
|
15
15
|
AuraStackRouterError
|
|
16
16
|
} from "./chunk-RFYOPPMW.js";
|
|
@@ -18,16 +18,15 @@ import {
|
|
|
18
18
|
// src/router.ts
|
|
19
19
|
var createRouter = (endpoints, config = {}) => {
|
|
20
20
|
const server = {};
|
|
21
|
-
const groups =
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
server[method] = async (request, ctx) => {
|
|
21
|
+
const groups = /* @__PURE__ */ new Map();
|
|
22
|
+
for (const endpoint of endpoints) {
|
|
23
|
+
if (!groups.has(endpoint.method)) {
|
|
24
|
+
groups.set(endpoint.method, []);
|
|
25
|
+
}
|
|
26
|
+
groups.get(endpoint.method)?.push(endpoint);
|
|
27
|
+
}
|
|
28
|
+
for (const method of groups.keys()) {
|
|
29
|
+
server[method] = async (request) => {
|
|
31
30
|
try {
|
|
32
31
|
const globalRequest = await executeGlobalMiddlewares(request, config.middlewares);
|
|
33
32
|
if (globalRequest instanceof Response) {
|
|
@@ -35,7 +34,7 @@ var createRouter = (endpoints, config = {}) => {
|
|
|
35
34
|
}
|
|
36
35
|
const url = new URL(globalRequest.url);
|
|
37
36
|
const pathname = url.pathname;
|
|
38
|
-
const endpoint = groups
|
|
37
|
+
const endpoint = groups.get(method)?.find((endpoint2) => {
|
|
39
38
|
const withBasePath = config.basePath ? `${config.basePath}${endpoint2.route}` : endpoint2.route;
|
|
40
39
|
const regex = createRoutePattern(withBasePath);
|
|
41
40
|
return regex.test(pathname);
|
|
@@ -47,7 +46,6 @@ var createRouter = (endpoints, config = {}) => {
|
|
|
47
46
|
const searchParams = getSearchParams(globalRequest.url, endpoint.config);
|
|
48
47
|
const headers = getHeaders(globalRequest);
|
|
49
48
|
const context = {
|
|
50
|
-
...ctx,
|
|
51
49
|
params,
|
|
52
50
|
searchParams,
|
|
53
51
|
headers,
|
|
@@ -60,7 +58,6 @@ var createRouter = (endpoints, config = {}) => {
|
|
|
60
58
|
} catch (error) {
|
|
61
59
|
if (error instanceof AuraStackRouterError) {
|
|
62
60
|
const { message, status, statusText } = error;
|
|
63
|
-
console.log("StatusText: ", statusText);
|
|
64
61
|
return Response.json({ message }, { status, statusText });
|
|
65
62
|
}
|
|
66
63
|
return Response.json({ message: "Internal Server Error" }, { status: 500 });
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
// src/assert.ts
|
|
2
|
-
var supportedMethods = ["GET", "POST", "DELETE", "PUT", "PATCH"];
|
|
3
|
-
var supportedBodyMethods = ["POST", "PUT", "PATCH"];
|
|
2
|
+
var supportedMethods = /* @__PURE__ */ new Set(["GET", "POST", "DELETE", "PUT", "PATCH"]);
|
|
3
|
+
var supportedBodyMethods = /* @__PURE__ */ new Set(["POST", "PUT", "PATCH"]);
|
|
4
4
|
var isSupportedMethod = (method) => {
|
|
5
|
-
return supportedMethods.
|
|
5
|
+
return supportedMethods.has(method);
|
|
6
6
|
};
|
|
7
7
|
var isSupportedBodyMethod = (method) => {
|
|
8
|
-
return supportedBodyMethods.
|
|
8
|
+
return supportedBodyMethods.has(method);
|
|
9
9
|
};
|
|
10
10
|
var isValidRoute = (route) => {
|
|
11
11
|
const routePattern = /^\/[a-zA-Z0-9/_:-]*$/;
|
package/dist/context.cjs
CHANGED
|
@@ -28,9 +28,9 @@ __export(context_exports, {
|
|
|
28
28
|
module.exports = __toCommonJS(context_exports);
|
|
29
29
|
|
|
30
30
|
// src/assert.ts
|
|
31
|
-
var supportedBodyMethods = ["POST", "PUT", "PATCH"];
|
|
31
|
+
var supportedBodyMethods = /* @__PURE__ */ new Set(["POST", "PUT", "PATCH"]);
|
|
32
32
|
var isSupportedBodyMethod = (method) => {
|
|
33
|
-
return supportedBodyMethods.
|
|
33
|
+
return supportedBodyMethods.has(method);
|
|
34
34
|
};
|
|
35
35
|
|
|
36
36
|
// src/error.ts
|
package/dist/context.js
CHANGED
|
@@ -3,9 +3,9 @@ import {
|
|
|
3
3
|
getHeaders,
|
|
4
4
|
getRouteParams,
|
|
5
5
|
getSearchParams
|
|
6
|
-
} from "./chunk-
|
|
7
|
-
import "./chunk-
|
|
8
|
-
import "./chunk-
|
|
6
|
+
} from "./chunk-OXDCFAMF.js";
|
|
7
|
+
import "./chunk-YUX3YHXF.js";
|
|
8
|
+
import "./chunk-JRJKKBSH.js";
|
|
9
9
|
import "./chunk-RFYOPPMW.js";
|
|
10
10
|
export {
|
|
11
11
|
getBody,
|
package/dist/endpoint.cjs
CHANGED
|
@@ -27,9 +27,9 @@ __export(endpoint_exports, {
|
|
|
27
27
|
module.exports = __toCommonJS(endpoint_exports);
|
|
28
28
|
|
|
29
29
|
// src/assert.ts
|
|
30
|
-
var supportedMethods = ["GET", "POST", "DELETE", "PUT", "PATCH"];
|
|
30
|
+
var supportedMethods = /* @__PURE__ */ new Set(["GET", "POST", "DELETE", "PUT", "PATCH"]);
|
|
31
31
|
var isSupportedMethod = (method) => {
|
|
32
|
-
return supportedMethods.
|
|
32
|
+
return supportedMethods.has(method);
|
|
33
33
|
};
|
|
34
34
|
var isValidRoute = (route) => {
|
|
35
35
|
const routePattern = /^\/[a-zA-Z0-9/_:-]*$/;
|
package/dist/endpoint.d.cts
CHANGED
|
@@ -29,7 +29,7 @@ declare const createRoutePattern: (route: RoutePattern) => RegExp;
|
|
|
29
29
|
* return new Response("Signed in");
|
|
30
30
|
* });
|
|
31
31
|
*/
|
|
32
|
-
declare const createEndpoint: <const Method extends Uppercase<HTTPMethod>, const Route extends
|
|
32
|
+
declare const createEndpoint: <const Method extends Uppercase<HTTPMethod>, const Route extends RoutePattern, const Schemas extends EndpointSchemas>(method: Method, route: Route, handler: RouteHandler<Route, {
|
|
33
33
|
schemas: Schemas;
|
|
34
34
|
}>, config?: EndpointConfig<Route, Schemas>) => RouteEndpoint<Method, Route, {}>;
|
|
35
35
|
/**
|
package/dist/endpoint.d.ts
CHANGED
|
@@ -29,7 +29,7 @@ declare const createRoutePattern: (route: RoutePattern) => RegExp;
|
|
|
29
29
|
* return new Response("Signed in");
|
|
30
30
|
* });
|
|
31
31
|
*/
|
|
32
|
-
declare const createEndpoint: <const Method extends Uppercase<HTTPMethod>, const Route extends
|
|
32
|
+
declare const createEndpoint: <const Method extends Uppercase<HTTPMethod>, const Route extends RoutePattern, const Schemas extends EndpointSchemas>(method: Method, route: Route, handler: RouteHandler<Route, {
|
|
33
33
|
schemas: Schemas;
|
|
34
34
|
}>, config?: EndpointConfig<Route, Schemas>) => RouteEndpoint<Method, Route, {}>;
|
|
35
35
|
/**
|
package/dist/endpoint.js
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -27,13 +27,13 @@ __export(index_exports, {
|
|
|
27
27
|
module.exports = __toCommonJS(index_exports);
|
|
28
28
|
|
|
29
29
|
// src/assert.ts
|
|
30
|
-
var supportedMethods = ["GET", "POST", "DELETE", "PUT", "PATCH"];
|
|
31
|
-
var supportedBodyMethods = ["POST", "PUT", "PATCH"];
|
|
30
|
+
var supportedMethods = /* @__PURE__ */ new Set(["GET", "POST", "DELETE", "PUT", "PATCH"]);
|
|
31
|
+
var supportedBodyMethods = /* @__PURE__ */ new Set(["POST", "PUT", "PATCH"]);
|
|
32
32
|
var isSupportedMethod = (method) => {
|
|
33
|
-
return supportedMethods.
|
|
33
|
+
return supportedMethods.has(method);
|
|
34
34
|
};
|
|
35
35
|
var isSupportedBodyMethod = (method) => {
|
|
36
|
-
return supportedBodyMethods.
|
|
36
|
+
return supportedBodyMethods.has(method);
|
|
37
37
|
};
|
|
38
38
|
var isValidRoute = (route) => {
|
|
39
39
|
const routePattern = /^\/[a-zA-Z0-9/_:-]*$/;
|
|
@@ -169,7 +169,7 @@ var getBody = async (request, config) => {
|
|
|
169
169
|
return null;
|
|
170
170
|
};
|
|
171
171
|
|
|
172
|
-
// src/
|
|
172
|
+
// src/middlewares.ts
|
|
173
173
|
var executeGlobalMiddlewares = async (request, middlewares) => {
|
|
174
174
|
if (!middlewares) return request;
|
|
175
175
|
for (const middleware of middlewares) {
|
|
@@ -205,16 +205,15 @@ var executeMiddlewares = async (request, context, middlewares = []) => {
|
|
|
205
205
|
// src/router.ts
|
|
206
206
|
var createRouter = (endpoints, config = {}) => {
|
|
207
207
|
const server = {};
|
|
208
|
-
const groups =
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
server[method] = async (request, ctx) => {
|
|
208
|
+
const groups = /* @__PURE__ */ new Map();
|
|
209
|
+
for (const endpoint of endpoints) {
|
|
210
|
+
if (!groups.has(endpoint.method)) {
|
|
211
|
+
groups.set(endpoint.method, []);
|
|
212
|
+
}
|
|
213
|
+
groups.get(endpoint.method)?.push(endpoint);
|
|
214
|
+
}
|
|
215
|
+
for (const method of groups.keys()) {
|
|
216
|
+
server[method] = async (request) => {
|
|
218
217
|
try {
|
|
219
218
|
const globalRequest = await executeGlobalMiddlewares(request, config.middlewares);
|
|
220
219
|
if (globalRequest instanceof Response) {
|
|
@@ -222,7 +221,7 @@ var createRouter = (endpoints, config = {}) => {
|
|
|
222
221
|
}
|
|
223
222
|
const url = new URL(globalRequest.url);
|
|
224
223
|
const pathname = url.pathname;
|
|
225
|
-
const endpoint = groups
|
|
224
|
+
const endpoint = groups.get(method)?.find((endpoint2) => {
|
|
226
225
|
const withBasePath = config.basePath ? `${config.basePath}${endpoint2.route}` : endpoint2.route;
|
|
227
226
|
const regex = createRoutePattern(withBasePath);
|
|
228
227
|
return regex.test(pathname);
|
|
@@ -234,7 +233,6 @@ var createRouter = (endpoints, config = {}) => {
|
|
|
234
233
|
const searchParams = getSearchParams(globalRequest.url, endpoint.config);
|
|
235
234
|
const headers = getHeaders(globalRequest);
|
|
236
235
|
const context = {
|
|
237
|
-
...ctx,
|
|
238
236
|
params,
|
|
239
237
|
searchParams,
|
|
240
238
|
headers,
|
|
@@ -247,7 +245,6 @@ var createRouter = (endpoints, config = {}) => {
|
|
|
247
245
|
} catch (error) {
|
|
248
246
|
if (error instanceof AuraStackRouterError) {
|
|
249
247
|
const { message, status, statusText: statusText2 } = error;
|
|
250
|
-
console.log("StatusText: ", statusText2);
|
|
251
248
|
return Response.json({ message }, { status, statusText: statusText2 });
|
|
252
249
|
}
|
|
253
250
|
return Response.json({ message: "Internal Server Error" }, { status: 500 });
|
package/dist/index.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import {
|
|
2
2
|
createRouter
|
|
3
|
-
} from "./chunk-
|
|
4
|
-
import "./chunk-
|
|
3
|
+
} from "./chunk-DR4C6QTF.js";
|
|
4
|
+
import "./chunk-OXDCFAMF.js";
|
|
5
5
|
import {
|
|
6
6
|
createEndpoint,
|
|
7
7
|
createEndpointConfig
|
|
8
|
-
} from "./chunk-
|
|
9
|
-
import "./chunk-
|
|
10
|
-
import "./chunk-
|
|
8
|
+
} from "./chunk-YUX3YHXF.js";
|
|
9
|
+
import "./chunk-JRJKKBSH.js";
|
|
10
|
+
import "./chunk-O6SY753N.js";
|
|
11
11
|
import "./chunk-RFYOPPMW.js";
|
|
12
12
|
export {
|
|
13
13
|
createEndpoint,
|
|
@@ -17,13 +17,13 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
17
|
};
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
19
|
|
|
20
|
-
// src/
|
|
21
|
-
var
|
|
22
|
-
__export(
|
|
20
|
+
// src/middlewares.ts
|
|
21
|
+
var middlewares_exports = {};
|
|
22
|
+
__export(middlewares_exports, {
|
|
23
23
|
executeGlobalMiddlewares: () => executeGlobalMiddlewares,
|
|
24
24
|
executeMiddlewares: () => executeMiddlewares
|
|
25
25
|
});
|
|
26
|
-
module.exports = __toCommonJS(
|
|
26
|
+
module.exports = __toCommonJS(middlewares_exports);
|
|
27
27
|
|
|
28
28
|
// src/error.ts
|
|
29
29
|
var statusCode = {
|
|
@@ -67,7 +67,7 @@ var AuraStackRouterError = class extends Error {
|
|
|
67
67
|
}
|
|
68
68
|
};
|
|
69
69
|
|
|
70
|
-
// src/
|
|
70
|
+
// src/middlewares.ts
|
|
71
71
|
var executeGlobalMiddlewares = async (request, middlewares) => {
|
|
72
72
|
if (!middlewares) return request;
|
|
73
73
|
for (const middleware of middlewares) {
|
package/dist/router.cjs
CHANGED
|
@@ -25,9 +25,9 @@ __export(router_exports, {
|
|
|
25
25
|
module.exports = __toCommonJS(router_exports);
|
|
26
26
|
|
|
27
27
|
// src/assert.ts
|
|
28
|
-
var supportedBodyMethods = ["POST", "PUT", "PATCH"];
|
|
28
|
+
var supportedBodyMethods = /* @__PURE__ */ new Set(["POST", "PUT", "PATCH"]);
|
|
29
29
|
var isSupportedBodyMethod = (method) => {
|
|
30
|
-
return supportedBodyMethods.
|
|
30
|
+
return supportedBodyMethods.has(method);
|
|
31
31
|
};
|
|
32
32
|
|
|
33
33
|
// src/error.ts
|
|
@@ -140,7 +140,7 @@ var getBody = async (request, config) => {
|
|
|
140
140
|
return null;
|
|
141
141
|
};
|
|
142
142
|
|
|
143
|
-
// src/
|
|
143
|
+
// src/middlewares.ts
|
|
144
144
|
var executeGlobalMiddlewares = async (request, middlewares) => {
|
|
145
145
|
if (!middlewares) return request;
|
|
146
146
|
for (const middleware of middlewares) {
|
|
@@ -176,16 +176,15 @@ var executeMiddlewares = async (request, context, middlewares = []) => {
|
|
|
176
176
|
// src/router.ts
|
|
177
177
|
var createRouter = (endpoints, config = {}) => {
|
|
178
178
|
const server = {};
|
|
179
|
-
const groups =
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
server[method] = async (request, ctx) => {
|
|
179
|
+
const groups = /* @__PURE__ */ new Map();
|
|
180
|
+
for (const endpoint of endpoints) {
|
|
181
|
+
if (!groups.has(endpoint.method)) {
|
|
182
|
+
groups.set(endpoint.method, []);
|
|
183
|
+
}
|
|
184
|
+
groups.get(endpoint.method)?.push(endpoint);
|
|
185
|
+
}
|
|
186
|
+
for (const method of groups.keys()) {
|
|
187
|
+
server[method] = async (request) => {
|
|
189
188
|
try {
|
|
190
189
|
const globalRequest = await executeGlobalMiddlewares(request, config.middlewares);
|
|
191
190
|
if (globalRequest instanceof Response) {
|
|
@@ -193,7 +192,7 @@ var createRouter = (endpoints, config = {}) => {
|
|
|
193
192
|
}
|
|
194
193
|
const url = new URL(globalRequest.url);
|
|
195
194
|
const pathname = url.pathname;
|
|
196
|
-
const endpoint = groups
|
|
195
|
+
const endpoint = groups.get(method)?.find((endpoint2) => {
|
|
197
196
|
const withBasePath = config.basePath ? `${config.basePath}${endpoint2.route}` : endpoint2.route;
|
|
198
197
|
const regex = createRoutePattern(withBasePath);
|
|
199
198
|
return regex.test(pathname);
|
|
@@ -205,7 +204,6 @@ var createRouter = (endpoints, config = {}) => {
|
|
|
205
204
|
const searchParams = getSearchParams(globalRequest.url, endpoint.config);
|
|
206
205
|
const headers = getHeaders(globalRequest);
|
|
207
206
|
const context = {
|
|
208
|
-
...ctx,
|
|
209
207
|
params,
|
|
210
208
|
searchParams,
|
|
211
209
|
headers,
|
|
@@ -218,7 +216,6 @@ var createRouter = (endpoints, config = {}) => {
|
|
|
218
216
|
} catch (error) {
|
|
219
217
|
if (error instanceof AuraStackRouterError) {
|
|
220
218
|
const { message, status, statusText: statusText2 } = error;
|
|
221
|
-
console.log("StatusText: ", statusText2);
|
|
222
219
|
return Response.json({ message }, { status, statusText: statusText2 });
|
|
223
220
|
}
|
|
224
221
|
return Response.json({ message: "Internal Server Error" }, { status: 500 });
|
package/dist/router.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
2
|
createRouter
|
|
3
|
-
} from "./chunk-
|
|
4
|
-
import "./chunk-
|
|
5
|
-
import "./chunk-
|
|
6
|
-
import "./chunk-
|
|
7
|
-
import "./chunk-
|
|
3
|
+
} from "./chunk-DR4C6QTF.js";
|
|
4
|
+
import "./chunk-OXDCFAMF.js";
|
|
5
|
+
import "./chunk-YUX3YHXF.js";
|
|
6
|
+
import "./chunk-JRJKKBSH.js";
|
|
7
|
+
import "./chunk-O6SY753N.js";
|
|
8
8
|
import "./chunk-RFYOPPMW.js";
|
|
9
9
|
export {
|
|
10
10
|
createRouter
|
package/dist/types.d.cts
CHANGED
|
@@ -6,7 +6,7 @@ import { ZodObject, z } from 'zod';
|
|
|
6
6
|
* const getUser:RoutePattern = "/users/:userId"
|
|
7
7
|
* const getPostsComments:RoutePattern = "/posts/:postId/comments/:commentId"
|
|
8
8
|
*/
|
|
9
|
-
type RoutePattern = `/${string}`;
|
|
9
|
+
type RoutePattern = `/${string}` | `/${string}/:${string}`;
|
|
10
10
|
/**
|
|
11
11
|
* HTTP methods supported by the router.
|
|
12
12
|
*/
|
|
@@ -121,7 +121,7 @@ type InferMethod<Endpoints extends RouteEndpoint[]> = Endpoints extends unknown[
|
|
|
121
121
|
* Each method is a function that takes a request and context, returning a promise of a response.
|
|
122
122
|
*/
|
|
123
123
|
type GetHttpHandlers<Endpoints extends RouteEndpoint[]> = {
|
|
124
|
-
[Method in InferMethod<Endpoints>]: (req: Request
|
|
124
|
+
[Method in InferMethod<Endpoints>]: (req: Request) => Promise<Response>;
|
|
125
125
|
};
|
|
126
126
|
/**
|
|
127
127
|
* Configuration options for `createRouter` function.
|
package/dist/types.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ import { ZodObject, z } from 'zod';
|
|
|
6
6
|
* const getUser:RoutePattern = "/users/:userId"
|
|
7
7
|
* const getPostsComments:RoutePattern = "/posts/:postId/comments/:commentId"
|
|
8
8
|
*/
|
|
9
|
-
type RoutePattern = `/${string}`;
|
|
9
|
+
type RoutePattern = `/${string}` | `/${string}/:${string}`;
|
|
10
10
|
/**
|
|
11
11
|
* HTTP methods supported by the router.
|
|
12
12
|
*/
|
|
@@ -121,7 +121,7 @@ type InferMethod<Endpoints extends RouteEndpoint[]> = Endpoints extends unknown[
|
|
|
121
121
|
* Each method is a function that takes a request and context, returning a promise of a response.
|
|
122
122
|
*/
|
|
123
123
|
type GetHttpHandlers<Endpoints extends RouteEndpoint[]> = {
|
|
124
|
-
[Method in InferMethod<Endpoints>]: (req: Request
|
|
124
|
+
[Method in InferMethod<Endpoints>]: (req: Request) => Promise<Response>;
|
|
125
125
|
};
|
|
126
126
|
/**
|
|
127
127
|
* Configuration options for `createRouter` function.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aura-stack/router",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "A lightweight TypeScript library for building, managing, and validating API routes and endpoints in Node.js applications.",
|
|
6
6
|
"files": [
|
|
@@ -44,12 +44,14 @@
|
|
|
44
44
|
},
|
|
45
45
|
"scripts": {
|
|
46
46
|
"dev": "tsup --watch",
|
|
47
|
+
"dev:docs": "pnpm --filter docs dev",
|
|
47
48
|
"build": "tsup",
|
|
48
49
|
"format": "prettier --write .",
|
|
49
50
|
"format:check": "prettier --check .",
|
|
50
51
|
"test": "vitest --run",
|
|
51
52
|
"test:watch": "vitest",
|
|
52
53
|
"type-check": "tsc --noEmit",
|
|
53
|
-
"clean": "rm -rf dist"
|
|
54
|
+
"clean": "rm -rf dist",
|
|
55
|
+
"clean:cts": "rm -rf dist/*.cts"
|
|
54
56
|
}
|
|
55
57
|
}
|
|
File without changes
|
|
File without changes
|