@vyriy/router 0.7.7 → 0.8.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/README.md +109 -9
- package/base.d.ts +6 -0
- package/base.js +25 -0
- package/create.d.ts +2 -2
- package/create.js +10 -45
- package/http/create.d.ts +3 -0
- package/http/create.js +42 -0
- package/http/index.d.ts +3 -0
- package/http/index.js +2 -0
- package/http/router.d.ts +8 -0
- package/http/router.js +28 -0
- package/http/types.d.ts +14 -0
- package/index.d.ts +8 -2
- package/index.js +6 -2
- package/package.json +84 -2
- package/router.d.ts +2 -11
- package/router.js +2 -55
- package/stream/create.d.ts +3 -0
- package/stream/create.js +38 -0
- package/stream/index.d.ts +3 -0
- package/stream/index.js +2 -0
- package/stream/router.d.ts +6 -0
- package/stream/router.js +32 -0
- package/stream/types.d.ts +25 -0
- package/types.d.ts +0 -21
package/README.md
CHANGED
|
@@ -4,13 +4,17 @@ Router utility for Vyriy projects.
|
|
|
4
4
|
|
|
5
5
|
## Purpose
|
|
6
6
|
|
|
7
|
-
This package provides a small
|
|
7
|
+
This package provides a small router for Lambda handlers and native Node HTTP servers:
|
|
8
|
+
|
|
9
|
+
- `createRouter()` for API Gateway events
|
|
10
|
+
- `createStreamRouter()` for Lambda response streaming
|
|
11
|
+
- `createHttpRouter()` for native Node `IncomingMessage`/`ServerResponse` handlers
|
|
8
12
|
|
|
9
13
|
It is intentionally kept small:
|
|
10
14
|
|
|
11
15
|
- matches by HTTP method and exact path
|
|
12
|
-
- passes API Gateway event data into handlers
|
|
13
|
-
- returns a Lambda-friendly response shape
|
|
16
|
+
- passes API Gateway event data into handlers (or raw Node request/response for the HTTP router)
|
|
17
|
+
- returns a Lambda-friendly response shape (the stream and HTTP routers write the response instead)
|
|
14
18
|
|
|
15
19
|
It does not try to be a path parser or dispatcher. If you later need regular expressions, wildcard matching, route params extraction, or more advanced dispatch rules, that logic should live in a separate package or layer.
|
|
16
20
|
|
|
@@ -59,10 +63,12 @@ export const handler = router.handle();
|
|
|
59
63
|
|
|
60
64
|
## Stream Router
|
|
61
65
|
|
|
62
|
-
Use
|
|
66
|
+
Use the streaming router when handlers write directly to a Lambda response stream. The stream is passed as the second handler argument, and stream handlers do not return a response object.
|
|
67
|
+
|
|
68
|
+
Import it from the `@vyriy/router/stream` subpath (clean `createRouter`/`router` names), or use the aggregated `createStreamRouter` alias from the root entry.
|
|
63
69
|
|
|
64
70
|
```ts
|
|
65
|
-
import { createStreamRouter } from '@vyriy/router';
|
|
71
|
+
import { createRouter as createStreamRouter } from '@vyriy/router/stream';
|
|
66
72
|
|
|
67
73
|
const streamRouter = createStreamRouter();
|
|
68
74
|
|
|
@@ -86,6 +92,48 @@ streamRouter.fallback(({ event }, responseStream) => {
|
|
|
86
92
|
export const handler = streamRouter.handle();
|
|
87
93
|
```
|
|
88
94
|
|
|
95
|
+
## HTTP Router
|
|
96
|
+
|
|
97
|
+
Use `createHttpRouter()` when handlers work directly with native Node `IncomingMessage` and `ServerResponse` objects. Handlers own the response lifecycle and write the response themselves, which fits transports such as MCP Streamable HTTP.
|
|
98
|
+
|
|
99
|
+
In addition to the method helpers, the HTTP router supports `all(path, handler)` for routes that accept any HTTP method. Method-specific routes take precedence over `all` routes on the same path.
|
|
100
|
+
|
|
101
|
+
Import it from the `@vyriy/router/http` subpath (clean `createRouter`/`router` names), or use the aggregated `createHttpRouter` alias from the root entry.
|
|
102
|
+
|
|
103
|
+
```ts
|
|
104
|
+
import { createRouter as createHttpRouter } from '@vyriy/router/http';
|
|
105
|
+
|
|
106
|
+
const httpRouter = createHttpRouter();
|
|
107
|
+
|
|
108
|
+
httpRouter.get('/health', (request, response) => {
|
|
109
|
+
response
|
|
110
|
+
.writeHead(200, {
|
|
111
|
+
'content-type': 'application/json',
|
|
112
|
+
})
|
|
113
|
+
.end(JSON.stringify({ ok: true }));
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
// transports that handle multiple HTTP methods on one path, such as MCP Streamable HTTP
|
|
117
|
+
httpRouter.all('/mcp', async (request, response) => {
|
|
118
|
+
await transport.handleRequest(request, response);
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
httpRouter.fallback((request, response) => {
|
|
122
|
+
response
|
|
123
|
+
.writeHead(404, {
|
|
124
|
+
'content-type': 'application/json',
|
|
125
|
+
})
|
|
126
|
+
.end(
|
|
127
|
+
JSON.stringify({
|
|
128
|
+
message: 'Not Found',
|
|
129
|
+
path: request.url,
|
|
130
|
+
}),
|
|
131
|
+
);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
export const handler = httpRouter.handle();
|
|
135
|
+
```
|
|
136
|
+
|
|
89
137
|
## Calm Composition
|
|
90
138
|
|
|
91
139
|
The router keeps request matching separate from handler wrappers and local server adapters. A small API can stay as a plain composition of focused packages:
|
|
@@ -127,6 +175,28 @@ const handler = streamApi(router.handle());
|
|
|
127
175
|
streamServer(handler);
|
|
128
176
|
```
|
|
129
177
|
|
|
178
|
+
And for native Node HTTP handlers:
|
|
179
|
+
|
|
180
|
+
```ts
|
|
181
|
+
import { httpApi } from '@vyriy/handler';
|
|
182
|
+
import { createHttpRouter } from '@vyriy/router';
|
|
183
|
+
import { httpServer } from '@vyriy/server';
|
|
184
|
+
|
|
185
|
+
const router = createHttpRouter();
|
|
186
|
+
|
|
187
|
+
router.get('/health', (request, response) => {
|
|
188
|
+
response
|
|
189
|
+
.writeHead(200, {
|
|
190
|
+
'content-type': 'application/json',
|
|
191
|
+
})
|
|
192
|
+
.end(JSON.stringify({ ok: true }));
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
const handler = httpApi(router.handle());
|
|
196
|
+
|
|
197
|
+
httpServer(handler);
|
|
198
|
+
```
|
|
199
|
+
|
|
130
200
|
For a Lambda-only entrypoint, keep the same composition and export the handler:
|
|
131
201
|
|
|
132
202
|
```ts
|
|
@@ -146,17 +216,32 @@ export const handler = api(router.handle());
|
|
|
146
216
|
|
|
147
217
|
## Exports
|
|
148
218
|
|
|
149
|
-
The
|
|
219
|
+
The classic Lambda router lives at the root entry, while the streaming and native HTTP
|
|
220
|
+
variants each have their own subpath with clean, unprefixed names:
|
|
150
221
|
|
|
151
222
|
```ts
|
|
152
|
-
import { createRouter,
|
|
153
|
-
import {
|
|
223
|
+
import { createRouter, router } from '@vyriy/router';
|
|
224
|
+
import { createRouter, router } from '@vyriy/router/http';
|
|
225
|
+
import { createRouter, router } from '@vyriy/router/stream';
|
|
154
226
|
```
|
|
155
227
|
|
|
228
|
+
The root entry also re-exports every variant under prefixed names, so a single import keeps
|
|
229
|
+
working:
|
|
230
|
+
|
|
231
|
+
```ts
|
|
232
|
+
import { createHttpRouter, createRouter, createStreamRouter } from '@vyriy/router';
|
|
233
|
+
import { HttpRouter, Router, StreamRouter } from '@vyriy/router';
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
The low-level `Router` classes are exported from each entry: `Router` from `@vyriy/router`,
|
|
237
|
+
`@vyriy/router/http`, and `@vyriy/router/stream`, plus the aggregated `HttpRouter` and
|
|
238
|
+
`StreamRouter` names from the root entry.
|
|
239
|
+
|
|
156
240
|
## API
|
|
157
241
|
|
|
158
242
|
- `createRouter()` returns a chainable router API.
|
|
159
243
|
- `createStreamRouter()` returns a chainable response streaming router API.
|
|
244
|
+
- `createHttpRouter()` returns a chainable native HTTP router API.
|
|
160
245
|
- `router.get(path, handler)` registers a `GET` handler.
|
|
161
246
|
- `router.post(path, handler)` registers a `POST` handler.
|
|
162
247
|
- `router.put(path, handler)` registers a `PUT` handler.
|
|
@@ -167,10 +252,13 @@ import { Router, StreamRouter } from '@vyriy/router/router';
|
|
|
167
252
|
- `router.route(event)` resolves the matching route and returns an API Gateway response.
|
|
168
253
|
- `streamRouter.handle()` returns `(event, responseStream) => streamRouter.route(event, responseStream)` for stream wrappers.
|
|
169
254
|
- `streamRouter.route(event, responseStream)` resolves the matching route and writes to the stream.
|
|
255
|
+
- `httpRouter.all(path, handler)` registers a handler for any HTTP method on an exact path.
|
|
256
|
+
- `httpRouter.handle()` returns `(request, response) => httpRouter.route(request, response)` for native HTTP wrappers.
|
|
257
|
+
- `httpRouter.route(request, response)` resolves the matching route and lets the handler write the response.
|
|
170
258
|
|
|
171
259
|
Route handlers may omit `statusCode`; the router normalizes missing status codes to `200` before returning from `router.route(event)`.
|
|
172
260
|
|
|
173
|
-
The low-level `Router` and `
|
|
261
|
+
The low-level `Router`, `StreamRouter`, and `HttpRouter` classes are also available from `@vyriy/router` (each variant additionally exports its class as `Router` from its own subpath):
|
|
174
262
|
|
|
175
263
|
- `router.on(method, path, handler)`
|
|
176
264
|
- `router.fallback(handler)`
|
|
@@ -178,6 +266,10 @@ The low-level `Router` and `StreamRouter` classes are also available from `@vyri
|
|
|
178
266
|
- `streamRouter.on(method, path, handler)`
|
|
179
267
|
- `streamRouter.fallback(handler)`
|
|
180
268
|
- `streamRouter.route(event, responseStream)`
|
|
269
|
+
- `httpRouter.on(method, path, handler)`
|
|
270
|
+
- `httpRouter.all(path, handler)`
|
|
271
|
+
- `httpRouter.fallback(handler)`
|
|
272
|
+
- `httpRouter.route(request, response)`
|
|
181
273
|
|
|
182
274
|
Route handlers receive:
|
|
183
275
|
|
|
@@ -196,3 +288,11 @@ Stream route handlers receive the same `HandlerParams` as the first argument and
|
|
|
196
288
|
```ts
|
|
197
289
|
type StreamHandler = (params: HandlerParams, responseStream: ResponseStream) => void | Promise<void>;
|
|
198
290
|
```
|
|
291
|
+
|
|
292
|
+
HTTP route handlers receive the native Node request and response objects directly:
|
|
293
|
+
|
|
294
|
+
```ts
|
|
295
|
+
type HttpHandler = (request: IncomingMessage, response: ServerResponse) => void | Promise<void>;
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
When no HTTP route matches and no fallback is registered, the HTTP router writes a JSON `404` response itself.
|
package/base.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare class BaseRouter<CurrentHandler> {
|
|
2
|
+
protected fallbackHandler?: CurrentHandler;
|
|
3
|
+
protected readonly routes: Record<string, Record<string, CurrentHandler>>;
|
|
4
|
+
on(method: string, path: string, handler: CurrentHandler): this;
|
|
5
|
+
fallback(handler: CurrentHandler): this;
|
|
6
|
+
}
|
package/base.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { METHODS } from 'node:http';
|
|
2
|
+
const SUPPORTED_METHODS = new Set(METHODS.map((method) => method.toUpperCase()));
|
|
3
|
+
const registerRoute = (routes, method, path, handler) => {
|
|
4
|
+
const normalizedMethod = method.toUpperCase();
|
|
5
|
+
if (!SUPPORTED_METHODS.has(normalizedMethod)) {
|
|
6
|
+
throw new Error(`Unsupported HTTP method: ${normalizedMethod}`);
|
|
7
|
+
}
|
|
8
|
+
const routeGroup = (routes[normalizedMethod] ??= {});
|
|
9
|
+
if (routeGroup[path]) {
|
|
10
|
+
throw new Error(`${normalizedMethod} ${path} already exists!`);
|
|
11
|
+
}
|
|
12
|
+
routeGroup[path] = handler;
|
|
13
|
+
};
|
|
14
|
+
export class BaseRouter {
|
|
15
|
+
fallbackHandler;
|
|
16
|
+
routes = {};
|
|
17
|
+
on(method, path, handler) {
|
|
18
|
+
registerRoute(this.routes, method, path, handler);
|
|
19
|
+
return this;
|
|
20
|
+
}
|
|
21
|
+
fallback(handler) {
|
|
22
|
+
this.fallbackHandler = handler;
|
|
23
|
+
return this;
|
|
24
|
+
}
|
|
25
|
+
}
|
package/create.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type { RouterApi
|
|
1
|
+
import type { RouterApi } from './types.js';
|
|
2
2
|
export declare const createRouter: () => RouterApi;
|
|
3
|
-
export declare const
|
|
3
|
+
export declare const router: RouterApi;
|
package/create.js
CHANGED
|
@@ -1,73 +1,38 @@
|
|
|
1
|
-
import { Router
|
|
1
|
+
import { Router } from './router.js';
|
|
2
2
|
export const createRouter = () => {
|
|
3
|
-
const
|
|
3
|
+
const instance = new Router();
|
|
4
4
|
const api = {
|
|
5
5
|
get(path, handler) {
|
|
6
|
-
|
|
6
|
+
instance.on('GET', path, handler);
|
|
7
7
|
return api;
|
|
8
8
|
},
|
|
9
9
|
post(path, handler) {
|
|
10
|
-
|
|
10
|
+
instance.on('POST', path, handler);
|
|
11
11
|
return api;
|
|
12
12
|
},
|
|
13
13
|
put(path, handler) {
|
|
14
|
-
|
|
14
|
+
instance.on('PUT', path, handler);
|
|
15
15
|
return api;
|
|
16
16
|
},
|
|
17
17
|
delete(path, handler) {
|
|
18
|
-
|
|
18
|
+
instance.on('DELETE', path, handler);
|
|
19
19
|
return api;
|
|
20
20
|
},
|
|
21
21
|
fallback(handler) {
|
|
22
|
-
|
|
22
|
+
instance.fallback(handler);
|
|
23
23
|
return api;
|
|
24
24
|
},
|
|
25
25
|
handle() {
|
|
26
26
|
return (event) => api.route(event);
|
|
27
27
|
},
|
|
28
28
|
patch(path, handler) {
|
|
29
|
-
|
|
29
|
+
instance.on('PATCH', path, handler);
|
|
30
30
|
return api;
|
|
31
31
|
},
|
|
32
32
|
route(event) {
|
|
33
|
-
return
|
|
34
|
-
},
|
|
35
|
-
};
|
|
36
|
-
return api;
|
|
37
|
-
};
|
|
38
|
-
export const createStreamRouter = () => {
|
|
39
|
-
const router = new StreamRouter();
|
|
40
|
-
const api = {
|
|
41
|
-
get(path, handler) {
|
|
42
|
-
router.on('GET', path, handler);
|
|
43
|
-
return api;
|
|
44
|
-
},
|
|
45
|
-
post(path, handler) {
|
|
46
|
-
router.on('POST', path, handler);
|
|
47
|
-
return api;
|
|
48
|
-
},
|
|
49
|
-
put(path, handler) {
|
|
50
|
-
router.on('PUT', path, handler);
|
|
51
|
-
return api;
|
|
52
|
-
},
|
|
53
|
-
delete(path, handler) {
|
|
54
|
-
router.on('DELETE', path, handler);
|
|
55
|
-
return api;
|
|
56
|
-
},
|
|
57
|
-
fallback(handler) {
|
|
58
|
-
router.fallback(handler);
|
|
59
|
-
return api;
|
|
60
|
-
},
|
|
61
|
-
handle() {
|
|
62
|
-
return (event, responseStream) => api.route(event, responseStream);
|
|
63
|
-
},
|
|
64
|
-
patch(path, handler) {
|
|
65
|
-
router.on('PATCH', path, handler);
|
|
66
|
-
return api;
|
|
67
|
-
},
|
|
68
|
-
route(event, responseStream) {
|
|
69
|
-
return router.route(event, responseStream);
|
|
33
|
+
return instance.route(event);
|
|
70
34
|
},
|
|
71
35
|
};
|
|
72
36
|
return api;
|
|
73
37
|
};
|
|
38
|
+
export const router = createRouter();
|
package/http/create.d.ts
ADDED
package/http/create.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { Router } from './router.js';
|
|
2
|
+
export const createRouter = () => {
|
|
3
|
+
const instance = new Router();
|
|
4
|
+
const api = {
|
|
5
|
+
all(path, handler) {
|
|
6
|
+
instance.all(path, handler);
|
|
7
|
+
return api;
|
|
8
|
+
},
|
|
9
|
+
get(path, handler) {
|
|
10
|
+
instance.on('GET', path, handler);
|
|
11
|
+
return api;
|
|
12
|
+
},
|
|
13
|
+
post(path, handler) {
|
|
14
|
+
instance.on('POST', path, handler);
|
|
15
|
+
return api;
|
|
16
|
+
},
|
|
17
|
+
put(path, handler) {
|
|
18
|
+
instance.on('PUT', path, handler);
|
|
19
|
+
return api;
|
|
20
|
+
},
|
|
21
|
+
delete(path, handler) {
|
|
22
|
+
instance.on('DELETE', path, handler);
|
|
23
|
+
return api;
|
|
24
|
+
},
|
|
25
|
+
fallback(handler) {
|
|
26
|
+
instance.fallback(handler);
|
|
27
|
+
return api;
|
|
28
|
+
},
|
|
29
|
+
handle() {
|
|
30
|
+
return (request, response) => api.route(request, response);
|
|
31
|
+
},
|
|
32
|
+
patch(path, handler) {
|
|
33
|
+
instance.on('PATCH', path, handler);
|
|
34
|
+
return api;
|
|
35
|
+
},
|
|
36
|
+
route(request, response) {
|
|
37
|
+
return instance.route(request, response);
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
return api;
|
|
41
|
+
};
|
|
42
|
+
export const router = createRouter();
|
package/http/index.d.ts
ADDED
package/http/index.js
ADDED
package/http/router.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
import { BaseRouter } from '../base.js';
|
|
3
|
+
import type { Handler } from './types.js';
|
|
4
|
+
export declare class Router extends BaseRouter<Handler> {
|
|
5
|
+
private readonly allRoutes;
|
|
6
|
+
all(path: string, handler: Handler): this;
|
|
7
|
+
route(request: IncomingMessage, response: ServerResponse): Promise<void>;
|
|
8
|
+
}
|
package/http/router.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { STATUS_CODES } from 'node:http';
|
|
2
|
+
import { BaseRouter } from '../base.js';
|
|
3
|
+
export class Router extends BaseRouter {
|
|
4
|
+
allRoutes = {};
|
|
5
|
+
all(path, handler) {
|
|
6
|
+
if (this.allRoutes[path]) {
|
|
7
|
+
throw new Error(`ALL ${path} already exists!`);
|
|
8
|
+
}
|
|
9
|
+
this.allRoutes[path] = handler;
|
|
10
|
+
return this;
|
|
11
|
+
}
|
|
12
|
+
async route(request, response) {
|
|
13
|
+
const method = (request.method ?? 'GET').toUpperCase();
|
|
14
|
+
const pathname = (request.url ?? '/').split('?')[0];
|
|
15
|
+
const handler = this.routes[method]?.[pathname] ?? this.allRoutes[pathname] ?? this.fallbackHandler;
|
|
16
|
+
if (handler) {
|
|
17
|
+
await handler(request, response);
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
response
|
|
21
|
+
.writeHead(404, {
|
|
22
|
+
'content-type': 'application/json',
|
|
23
|
+
})
|
|
24
|
+
.end(JSON.stringify({
|
|
25
|
+
message: STATUS_CODES[404],
|
|
26
|
+
}));
|
|
27
|
+
}
|
|
28
|
+
}
|
package/http/types.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
import type { HttpHandler } from '@vyriy/handler';
|
|
3
|
+
export type Handler = HttpHandler;
|
|
4
|
+
export type RouterApi = {
|
|
5
|
+
all(path: string, handler: Handler): RouterApi;
|
|
6
|
+
get(path: string, handler: Handler): RouterApi;
|
|
7
|
+
handle(): Handler;
|
|
8
|
+
post(path: string, handler: Handler): RouterApi;
|
|
9
|
+
put(path: string, handler: Handler): RouterApi;
|
|
10
|
+
delete(path: string, handler: Handler): RouterApi;
|
|
11
|
+
fallback(handler: Handler): RouterApi;
|
|
12
|
+
patch(path: string, handler: Handler): RouterApi;
|
|
13
|
+
route(request: IncomingMessage, response: ServerResponse): Promise<void>;
|
|
14
|
+
};
|
package/index.d.ts
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
1
|
+
export { createRouter, router } from './create.js';
|
|
2
|
+
export { Router } from './router.js';
|
|
3
|
+
export { createRouter as createStreamRouter } from './stream/create.js';
|
|
4
|
+
export { Router as StreamRouter } from './stream/router.js';
|
|
5
|
+
export { createRouter as createHttpRouter } from './http/create.js';
|
|
6
|
+
export { Router as HttpRouter } from './http/router.js';
|
|
3
7
|
export type * from './types.js';
|
|
8
|
+
export type { Handler as StreamHandler, ResponseStream, RouterApi as StreamRouterApi, RouterHandler as StreamRouterHandler, } from './stream/types.js';
|
|
9
|
+
export type { Handler as HttpHandler, RouterApi as HttpRouterApi } from './http/types.js';
|
package/index.js
CHANGED
|
@@ -1,2 +1,6 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
1
|
+
export { createRouter, router } from './create.js';
|
|
2
|
+
export { Router } from './router.js';
|
|
3
|
+
export { createRouter as createStreamRouter } from './stream/create.js';
|
|
4
|
+
export { Router as StreamRouter } from './stream/router.js';
|
|
5
|
+
export { createRouter as createHttpRouter } from './http/create.js';
|
|
6
|
+
export { Router as HttpRouter } from './http/router.js';
|
package/package.json
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vyriy/router",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "Router utility for Vyriy projects",
|
|
5
|
+
"homepage": "https://vyriy.dev/docs/router/",
|
|
5
6
|
"type": "module",
|
|
6
7
|
"dependencies": {
|
|
7
|
-
"@types/aws-lambda": "^8.10.162"
|
|
8
|
+
"@types/aws-lambda": "^8.10.162",
|
|
9
|
+
"@vyriy/handler": "0.8.0"
|
|
8
10
|
},
|
|
9
11
|
"agents": "./AGENTS.md",
|
|
10
12
|
"license": "MIT",
|
|
@@ -21,6 +23,16 @@
|
|
|
21
23
|
"import": "./index.js",
|
|
22
24
|
"default": "./index.js"
|
|
23
25
|
},
|
|
26
|
+
"./base": {
|
|
27
|
+
"types": "./base.d.ts",
|
|
28
|
+
"import": "./base.js",
|
|
29
|
+
"default": "./base.js"
|
|
30
|
+
},
|
|
31
|
+
"./base.js": {
|
|
32
|
+
"types": "./base.d.ts",
|
|
33
|
+
"import": "./base.js",
|
|
34
|
+
"default": "./base.js"
|
|
35
|
+
},
|
|
24
36
|
"./create": {
|
|
25
37
|
"types": "./create.d.ts",
|
|
26
38
|
"import": "./create.js",
|
|
@@ -31,6 +43,41 @@
|
|
|
31
43
|
"import": "./create.js",
|
|
32
44
|
"default": "./create.js"
|
|
33
45
|
},
|
|
46
|
+
"./http/create": {
|
|
47
|
+
"types": "./http/create.d.ts",
|
|
48
|
+
"import": "./http/create.js",
|
|
49
|
+
"default": "./http/create.js"
|
|
50
|
+
},
|
|
51
|
+
"./http/create.js": {
|
|
52
|
+
"types": "./http/create.d.ts",
|
|
53
|
+
"import": "./http/create.js",
|
|
54
|
+
"default": "./http/create.js"
|
|
55
|
+
},
|
|
56
|
+
"./http": {
|
|
57
|
+
"types": "./http/index.d.ts",
|
|
58
|
+
"import": "./http/index.js",
|
|
59
|
+
"default": "./http/index.js"
|
|
60
|
+
},
|
|
61
|
+
"./http/index": {
|
|
62
|
+
"types": "./http/index.d.ts",
|
|
63
|
+
"import": "./http/index.js",
|
|
64
|
+
"default": "./http/index.js"
|
|
65
|
+
},
|
|
66
|
+
"./http/index.js": {
|
|
67
|
+
"types": "./http/index.d.ts",
|
|
68
|
+
"import": "./http/index.js",
|
|
69
|
+
"default": "./http/index.js"
|
|
70
|
+
},
|
|
71
|
+
"./http/router": {
|
|
72
|
+
"types": "./http/router.d.ts",
|
|
73
|
+
"import": "./http/router.js",
|
|
74
|
+
"default": "./http/router.js"
|
|
75
|
+
},
|
|
76
|
+
"./http/router.js": {
|
|
77
|
+
"types": "./http/router.d.ts",
|
|
78
|
+
"import": "./http/router.js",
|
|
79
|
+
"default": "./http/router.js"
|
|
80
|
+
},
|
|
34
81
|
"./index": {
|
|
35
82
|
"types": "./index.d.ts",
|
|
36
83
|
"import": "./index.js",
|
|
@@ -50,6 +97,41 @@
|
|
|
50
97
|
"types": "./router.d.ts",
|
|
51
98
|
"import": "./router.js",
|
|
52
99
|
"default": "./router.js"
|
|
100
|
+
},
|
|
101
|
+
"./stream/create": {
|
|
102
|
+
"types": "./stream/create.d.ts",
|
|
103
|
+
"import": "./stream/create.js",
|
|
104
|
+
"default": "./stream/create.js"
|
|
105
|
+
},
|
|
106
|
+
"./stream/create.js": {
|
|
107
|
+
"types": "./stream/create.d.ts",
|
|
108
|
+
"import": "./stream/create.js",
|
|
109
|
+
"default": "./stream/create.js"
|
|
110
|
+
},
|
|
111
|
+
"./stream": {
|
|
112
|
+
"types": "./stream/index.d.ts",
|
|
113
|
+
"import": "./stream/index.js",
|
|
114
|
+
"default": "./stream/index.js"
|
|
115
|
+
},
|
|
116
|
+
"./stream/index": {
|
|
117
|
+
"types": "./stream/index.d.ts",
|
|
118
|
+
"import": "./stream/index.js",
|
|
119
|
+
"default": "./stream/index.js"
|
|
120
|
+
},
|
|
121
|
+
"./stream/index.js": {
|
|
122
|
+
"types": "./stream/index.d.ts",
|
|
123
|
+
"import": "./stream/index.js",
|
|
124
|
+
"default": "./stream/index.js"
|
|
125
|
+
},
|
|
126
|
+
"./stream/router": {
|
|
127
|
+
"types": "./stream/router.d.ts",
|
|
128
|
+
"import": "./stream/router.js",
|
|
129
|
+
"default": "./stream/router.js"
|
|
130
|
+
},
|
|
131
|
+
"./stream/router.js": {
|
|
132
|
+
"types": "./stream/router.d.ts",
|
|
133
|
+
"import": "./stream/router.js",
|
|
134
|
+
"default": "./stream/router.js"
|
|
53
135
|
}
|
|
54
136
|
}
|
|
55
137
|
}
|
package/router.d.ts
CHANGED
|
@@ -1,14 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
protected fallbackHandler?: CurrentHandler;
|
|
4
|
-
protected readonly routes: Record<string, Record<string, CurrentHandler>>;
|
|
5
|
-
on(method: string, path: string, handler: CurrentHandler): this;
|
|
6
|
-
fallback(handler: CurrentHandler): this;
|
|
7
|
-
}
|
|
1
|
+
import { BaseRouter } from './base.js';
|
|
2
|
+
import type { APIGatewayProxyEvent, Handler, RouteResult } from './types.js';
|
|
8
3
|
export declare class Router extends BaseRouter<Handler> {
|
|
9
4
|
route(event: APIGatewayProxyEvent): Promise<RouteResult>;
|
|
10
5
|
}
|
|
11
|
-
export declare class StreamRouter extends BaseRouter<StreamHandler> {
|
|
12
|
-
route(event: APIGatewayProxyEvent, responseStream: ResponseStream): Promise<void>;
|
|
13
|
-
}
|
|
14
|
-
export {};
|
package/router.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { STATUS_CODES } from 'node:http';
|
|
2
|
+
import { BaseRouter } from './base.js';
|
|
3
3
|
const normalizeResult = (result) => ({
|
|
4
4
|
statusCode: result.statusCode ?? 200,
|
|
5
5
|
body: result.body,
|
|
@@ -7,29 +7,6 @@ const normalizeResult = (result) => ({
|
|
|
7
7
|
isBase64Encoded: result.isBase64Encoded,
|
|
8
8
|
multiValueHeaders: result.multiValueHeaders,
|
|
9
9
|
});
|
|
10
|
-
const registerRoute = (routes, method, path, handler) => {
|
|
11
|
-
const normalizedMethod = method.toUpperCase();
|
|
12
|
-
if (!SUPPORTED_METHODS.has(normalizedMethod)) {
|
|
13
|
-
throw new Error(`Unsupported HTTP method: ${normalizedMethod}`);
|
|
14
|
-
}
|
|
15
|
-
const routeGroup = (routes[normalizedMethod] ??= {});
|
|
16
|
-
if (routeGroup[path]) {
|
|
17
|
-
throw new Error(`${normalizedMethod} ${path} already exists!`);
|
|
18
|
-
}
|
|
19
|
-
routeGroup[path] = handler;
|
|
20
|
-
};
|
|
21
|
-
class BaseRouter {
|
|
22
|
-
fallbackHandler;
|
|
23
|
-
routes = {};
|
|
24
|
-
on(method, path, handler) {
|
|
25
|
-
registerRoute(this.routes, method, path, handler);
|
|
26
|
-
return this;
|
|
27
|
-
}
|
|
28
|
-
fallback(handler) {
|
|
29
|
-
this.fallbackHandler = handler;
|
|
30
|
-
return this;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
10
|
export class Router extends BaseRouter {
|
|
34
11
|
async route(event) {
|
|
35
12
|
const { httpMethod, path, queryStringParameters, body, headers, pathParameters } = event;
|
|
@@ -62,33 +39,3 @@ export class Router extends BaseRouter {
|
|
|
62
39
|
};
|
|
63
40
|
}
|
|
64
41
|
}
|
|
65
|
-
export class StreamRouter extends BaseRouter {
|
|
66
|
-
async route(event, responseStream) {
|
|
67
|
-
const { httpMethod, path, queryStringParameters, body, headers, pathParameters } = event;
|
|
68
|
-
const exactHandler = this.routes[httpMethod]?.[path];
|
|
69
|
-
if (exactHandler) {
|
|
70
|
-
await exactHandler({
|
|
71
|
-
query: queryStringParameters ?? undefined,
|
|
72
|
-
body: body ?? undefined,
|
|
73
|
-
headers,
|
|
74
|
-
pathParameters: pathParameters ?? undefined,
|
|
75
|
-
event,
|
|
76
|
-
}, responseStream);
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
if (this.fallbackHandler) {
|
|
80
|
-
await this.fallbackHandler({
|
|
81
|
-
query: queryStringParameters ?? undefined,
|
|
82
|
-
body: body ?? undefined,
|
|
83
|
-
headers,
|
|
84
|
-
pathParameters: pathParameters ?? undefined,
|
|
85
|
-
event,
|
|
86
|
-
}, responseStream);
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
responseStream.setContentType?.('application/json');
|
|
90
|
-
responseStream.end(JSON.stringify({
|
|
91
|
-
message: STATUS_CODES[404],
|
|
92
|
-
}));
|
|
93
|
-
}
|
|
94
|
-
}
|
package/stream/create.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { Router } from './router.js';
|
|
2
|
+
export const createRouter = () => {
|
|
3
|
+
const instance = new Router();
|
|
4
|
+
const api = {
|
|
5
|
+
get(path, handler) {
|
|
6
|
+
instance.on('GET', path, handler);
|
|
7
|
+
return api;
|
|
8
|
+
},
|
|
9
|
+
post(path, handler) {
|
|
10
|
+
instance.on('POST', path, handler);
|
|
11
|
+
return api;
|
|
12
|
+
},
|
|
13
|
+
put(path, handler) {
|
|
14
|
+
instance.on('PUT', path, handler);
|
|
15
|
+
return api;
|
|
16
|
+
},
|
|
17
|
+
delete(path, handler) {
|
|
18
|
+
instance.on('DELETE', path, handler);
|
|
19
|
+
return api;
|
|
20
|
+
},
|
|
21
|
+
fallback(handler) {
|
|
22
|
+
instance.fallback(handler);
|
|
23
|
+
return api;
|
|
24
|
+
},
|
|
25
|
+
handle() {
|
|
26
|
+
return (event, responseStream) => api.route(event, responseStream);
|
|
27
|
+
},
|
|
28
|
+
patch(path, handler) {
|
|
29
|
+
instance.on('PATCH', path, handler);
|
|
30
|
+
return api;
|
|
31
|
+
},
|
|
32
|
+
route(event, responseStream) {
|
|
33
|
+
return instance.route(event, responseStream);
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
return api;
|
|
37
|
+
};
|
|
38
|
+
export const router = createRouter();
|
package/stream/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { BaseRouter } from '../base.js';
|
|
2
|
+
import type { APIGatewayProxyEvent } from '../types.js';
|
|
3
|
+
import type { Handler, ResponseStream } from './types.js';
|
|
4
|
+
export declare class Router extends BaseRouter<Handler> {
|
|
5
|
+
route(event: APIGatewayProxyEvent, responseStream: ResponseStream): Promise<void>;
|
|
6
|
+
}
|
package/stream/router.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { STATUS_CODES } from 'node:http';
|
|
2
|
+
import { BaseRouter } from '../base.js';
|
|
3
|
+
export class Router extends BaseRouter {
|
|
4
|
+
async route(event, responseStream) {
|
|
5
|
+
const { httpMethod, path, queryStringParameters, body, headers, pathParameters } = event;
|
|
6
|
+
const exactHandler = this.routes[httpMethod]?.[path];
|
|
7
|
+
if (exactHandler) {
|
|
8
|
+
await exactHandler({
|
|
9
|
+
query: queryStringParameters ?? undefined,
|
|
10
|
+
body: body ?? undefined,
|
|
11
|
+
headers,
|
|
12
|
+
pathParameters: pathParameters ?? undefined,
|
|
13
|
+
event,
|
|
14
|
+
}, responseStream);
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
if (this.fallbackHandler) {
|
|
18
|
+
await this.fallbackHandler({
|
|
19
|
+
query: queryStringParameters ?? undefined,
|
|
20
|
+
body: body ?? undefined,
|
|
21
|
+
headers,
|
|
22
|
+
pathParameters: pathParameters ?? undefined,
|
|
23
|
+
event,
|
|
24
|
+
}, responseStream);
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
responseStream.setContentType?.('application/json');
|
|
28
|
+
responseStream.end(JSON.stringify({
|
|
29
|
+
message: STATUS_CODES[404],
|
|
30
|
+
}));
|
|
31
|
+
}
|
|
32
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { APIGatewayProxyEvent } from 'aws-lambda';
|
|
2
|
+
import type { HandlerParams } from '../types.js';
|
|
3
|
+
type MaybePromise<Result> = Result | Promise<Result>;
|
|
4
|
+
export type ResponseStream = {
|
|
5
|
+
end: {
|
|
6
|
+
(): unknown;
|
|
7
|
+
(chunk: string | Buffer | Uint8Array): unknown;
|
|
8
|
+
};
|
|
9
|
+
setContentType?: (contentType: string) => unknown;
|
|
10
|
+
writableEnded?: boolean;
|
|
11
|
+
write(chunk: string | Buffer | Uint8Array): unknown;
|
|
12
|
+
};
|
|
13
|
+
export type Handler = (params: HandlerParams, responseStream: ResponseStream) => MaybePromise<void>;
|
|
14
|
+
export type RouterHandler = (event: APIGatewayProxyEvent, responseStream: ResponseStream) => Promise<void>;
|
|
15
|
+
export type RouterApi = {
|
|
16
|
+
get(path: string, handler: Handler): RouterApi;
|
|
17
|
+
handle(): RouterHandler;
|
|
18
|
+
post(path: string, handler: Handler): RouterApi;
|
|
19
|
+
put(path: string, handler: Handler): RouterApi;
|
|
20
|
+
delete(path: string, handler: Handler): RouterApi;
|
|
21
|
+
fallback(handler: Handler): RouterApi;
|
|
22
|
+
patch(path: string, handler: Handler): RouterApi;
|
|
23
|
+
route(event: APIGatewayProxyEvent, responseStream: ResponseStream): Promise<void>;
|
|
24
|
+
};
|
|
25
|
+
export {};
|
package/types.d.ts
CHANGED
|
@@ -9,15 +9,6 @@ export type RouteHandlerResult = {
|
|
|
9
9
|
multiValueHeaders?: APIGatewayProxyResult['multiValueHeaders'];
|
|
10
10
|
statusCode?: APIGatewayProxyResult['statusCode'];
|
|
11
11
|
};
|
|
12
|
-
export type ResponseStream = {
|
|
13
|
-
end: {
|
|
14
|
-
(): unknown;
|
|
15
|
-
(chunk: string | Buffer | Uint8Array): unknown;
|
|
16
|
-
};
|
|
17
|
-
setContentType?: (contentType: string) => unknown;
|
|
18
|
-
writableEnded?: boolean;
|
|
19
|
-
write(chunk: string | Buffer | Uint8Array): unknown;
|
|
20
|
-
};
|
|
21
12
|
export type HandlerParams = {
|
|
22
13
|
query?: APIGatewayProxyEventQueryStringParameters;
|
|
23
14
|
body?: string;
|
|
@@ -26,9 +17,7 @@ export type HandlerParams = {
|
|
|
26
17
|
event: APIGatewayProxyEvent;
|
|
27
18
|
};
|
|
28
19
|
export type Handler = (params: HandlerParams) => MaybePromise<RouteHandlerResult>;
|
|
29
|
-
export type StreamHandler = (params: HandlerParams, responseStream: ResponseStream) => MaybePromise<void>;
|
|
30
20
|
export type RouterHandler = (event: APIGatewayProxyEvent) => Promise<RouteResult>;
|
|
31
|
-
export type StreamRouterHandler = (event: APIGatewayProxyEvent, responseStream: ResponseStream) => Promise<void>;
|
|
32
21
|
export type RouterApi = {
|
|
33
22
|
get(path: string, handler: Handler): RouterApi;
|
|
34
23
|
handle(): RouterHandler;
|
|
@@ -39,13 +28,3 @@ export type RouterApi = {
|
|
|
39
28
|
patch(path: string, handler: Handler): RouterApi;
|
|
40
29
|
route(event: APIGatewayProxyEvent): Promise<RouteResult>;
|
|
41
30
|
};
|
|
42
|
-
export type StreamRouterApi = {
|
|
43
|
-
get(path: string, handler: StreamHandler): StreamRouterApi;
|
|
44
|
-
handle(): StreamRouterHandler;
|
|
45
|
-
post(path: string, handler: StreamHandler): StreamRouterApi;
|
|
46
|
-
put(path: string, handler: StreamHandler): StreamRouterApi;
|
|
47
|
-
delete(path: string, handler: StreamHandler): StreamRouterApi;
|
|
48
|
-
fallback(handler: StreamHandler): StreamRouterApi;
|
|
49
|
-
patch(path: string, handler: StreamHandler): StreamRouterApi;
|
|
50
|
-
route(event: APIGatewayProxyEvent, responseStream: ResponseStream): Promise<void>;
|
|
51
|
-
};
|