@vyriy/router 0.7.6 → 0.7.8
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 +88 -6
- package/create.d.ts +2 -1
- package/create.js +41 -1
- package/package.json +3 -2
- package/router.d.ts +7 -1
- package/router.js +26 -0
- package/types.d.ts +14 -0
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
|
|
|
@@ -86,6 +90,46 @@ streamRouter.fallback(({ event }, responseStream) => {
|
|
|
86
90
|
export const handler = streamRouter.handle();
|
|
87
91
|
```
|
|
88
92
|
|
|
93
|
+
## HTTP Router
|
|
94
|
+
|
|
95
|
+
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.
|
|
96
|
+
|
|
97
|
+
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.
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
import { createHttpRouter } from '@vyriy/router';
|
|
101
|
+
|
|
102
|
+
const httpRouter = createHttpRouter();
|
|
103
|
+
|
|
104
|
+
httpRouter.get('/health', (request, response) => {
|
|
105
|
+
response
|
|
106
|
+
.writeHead(200, {
|
|
107
|
+
'content-type': 'application/json',
|
|
108
|
+
})
|
|
109
|
+
.end(JSON.stringify({ ok: true }));
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
// transports that handle multiple HTTP methods on one path, such as MCP Streamable HTTP
|
|
113
|
+
httpRouter.all('/mcp', async (request, response) => {
|
|
114
|
+
await transport.handleRequest(request, response);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
httpRouter.fallback((request, response) => {
|
|
118
|
+
response
|
|
119
|
+
.writeHead(404, {
|
|
120
|
+
'content-type': 'application/json',
|
|
121
|
+
})
|
|
122
|
+
.end(
|
|
123
|
+
JSON.stringify({
|
|
124
|
+
message: 'Not Found',
|
|
125
|
+
path: request.url,
|
|
126
|
+
}),
|
|
127
|
+
);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
export const handler = httpRouter.handle();
|
|
131
|
+
```
|
|
132
|
+
|
|
89
133
|
## Calm Composition
|
|
90
134
|
|
|
91
135
|
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 +171,28 @@ const handler = streamApi(router.handle());
|
|
|
127
171
|
streamServer(handler);
|
|
128
172
|
```
|
|
129
173
|
|
|
174
|
+
And for native Node HTTP handlers:
|
|
175
|
+
|
|
176
|
+
```ts
|
|
177
|
+
import { httpApi } from '@vyriy/handler';
|
|
178
|
+
import { createHttpRouter } from '@vyriy/router';
|
|
179
|
+
import { httpServer } from '@vyriy/server';
|
|
180
|
+
|
|
181
|
+
const router = createHttpRouter();
|
|
182
|
+
|
|
183
|
+
router.get('/health', (request, response) => {
|
|
184
|
+
response
|
|
185
|
+
.writeHead(200, {
|
|
186
|
+
'content-type': 'application/json',
|
|
187
|
+
})
|
|
188
|
+
.end(JSON.stringify({ ok: true }));
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
const handler = httpApi(router.handle());
|
|
192
|
+
|
|
193
|
+
httpServer(handler);
|
|
194
|
+
```
|
|
195
|
+
|
|
130
196
|
For a Lambda-only entrypoint, keep the same composition and export the handler:
|
|
131
197
|
|
|
132
198
|
```ts
|
|
@@ -149,14 +215,15 @@ export const handler = api(router.handle());
|
|
|
149
215
|
The package exposes both the root entry and the direct module entry:
|
|
150
216
|
|
|
151
217
|
```ts
|
|
152
|
-
import { createRouter, createStreamRouter } from '@vyriy/router';
|
|
153
|
-
import { Router, StreamRouter } from '@vyriy/router/router';
|
|
218
|
+
import { createHttpRouter, createRouter, createStreamRouter } from '@vyriy/router';
|
|
219
|
+
import { HttpRouter, Router, StreamRouter } from '@vyriy/router/router';
|
|
154
220
|
```
|
|
155
221
|
|
|
156
222
|
## API
|
|
157
223
|
|
|
158
224
|
- `createRouter()` returns a chainable router API.
|
|
159
225
|
- `createStreamRouter()` returns a chainable response streaming router API.
|
|
226
|
+
- `createHttpRouter()` returns a chainable native HTTP router API.
|
|
160
227
|
- `router.get(path, handler)` registers a `GET` handler.
|
|
161
228
|
- `router.post(path, handler)` registers a `POST` handler.
|
|
162
229
|
- `router.put(path, handler)` registers a `PUT` handler.
|
|
@@ -167,10 +234,13 @@ import { Router, StreamRouter } from '@vyriy/router/router';
|
|
|
167
234
|
- `router.route(event)` resolves the matching route and returns an API Gateway response.
|
|
168
235
|
- `streamRouter.handle()` returns `(event, responseStream) => streamRouter.route(event, responseStream)` for stream wrappers.
|
|
169
236
|
- `streamRouter.route(event, responseStream)` resolves the matching route and writes to the stream.
|
|
237
|
+
- `httpRouter.all(path, handler)` registers a handler for any HTTP method on an exact path.
|
|
238
|
+
- `httpRouter.handle()` returns `(request, response) => httpRouter.route(request, response)` for native HTTP wrappers.
|
|
239
|
+
- `httpRouter.route(request, response)` resolves the matching route and lets the handler write the response.
|
|
170
240
|
|
|
171
241
|
Route handlers may omit `statusCode`; the router normalizes missing status codes to `200` before returning from `router.route(event)`.
|
|
172
242
|
|
|
173
|
-
The low-level `Router` and `
|
|
243
|
+
The low-level `Router`, `StreamRouter`, and `HttpRouter` classes are also available from `@vyriy/router/router`:
|
|
174
244
|
|
|
175
245
|
- `router.on(method, path, handler)`
|
|
176
246
|
- `router.fallback(handler)`
|
|
@@ -178,6 +248,10 @@ The low-level `Router` and `StreamRouter` classes are also available from `@vyri
|
|
|
178
248
|
- `streamRouter.on(method, path, handler)`
|
|
179
249
|
- `streamRouter.fallback(handler)`
|
|
180
250
|
- `streamRouter.route(event, responseStream)`
|
|
251
|
+
- `httpRouter.on(method, path, handler)`
|
|
252
|
+
- `httpRouter.all(path, handler)`
|
|
253
|
+
- `httpRouter.fallback(handler)`
|
|
254
|
+
- `httpRouter.route(request, response)`
|
|
181
255
|
|
|
182
256
|
Route handlers receive:
|
|
183
257
|
|
|
@@ -196,3 +270,11 @@ Stream route handlers receive the same `HandlerParams` as the first argument and
|
|
|
196
270
|
```ts
|
|
197
271
|
type StreamHandler = (params: HandlerParams, responseStream: ResponseStream) => void | Promise<void>;
|
|
198
272
|
```
|
|
273
|
+
|
|
274
|
+
HTTP route handlers receive the native Node request and response objects directly:
|
|
275
|
+
|
|
276
|
+
```ts
|
|
277
|
+
type HttpHandler = (request: IncomingMessage, response: ServerResponse) => void | Promise<void>;
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
When no HTTP route matches and no fallback is registered, the HTTP router writes a JSON `404` response itself.
|
package/create.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
import type { RouterApi, StreamRouterApi } from './types.js';
|
|
1
|
+
import type { HttpRouterApi, RouterApi, StreamRouterApi } from './types.js';
|
|
2
2
|
export declare const createRouter: () => RouterApi;
|
|
3
3
|
export declare const createStreamRouter: () => StreamRouterApi;
|
|
4
|
+
export declare const createHttpRouter: () => HttpRouterApi;
|
package/create.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Router, StreamRouter } from './router.js';
|
|
1
|
+
import { HttpRouter, Router, StreamRouter } from './router.js';
|
|
2
2
|
export const createRouter = () => {
|
|
3
3
|
const router = new Router();
|
|
4
4
|
const api = {
|
|
@@ -71,3 +71,43 @@ export const createStreamRouter = () => {
|
|
|
71
71
|
};
|
|
72
72
|
return api;
|
|
73
73
|
};
|
|
74
|
+
export const createHttpRouter = () => {
|
|
75
|
+
const router = new HttpRouter();
|
|
76
|
+
const api = {
|
|
77
|
+
all(path, handler) {
|
|
78
|
+
router.all(path, handler);
|
|
79
|
+
return api;
|
|
80
|
+
},
|
|
81
|
+
get(path, handler) {
|
|
82
|
+
router.on('GET', path, handler);
|
|
83
|
+
return api;
|
|
84
|
+
},
|
|
85
|
+
post(path, handler) {
|
|
86
|
+
router.on('POST', path, handler);
|
|
87
|
+
return api;
|
|
88
|
+
},
|
|
89
|
+
put(path, handler) {
|
|
90
|
+
router.on('PUT', path, handler);
|
|
91
|
+
return api;
|
|
92
|
+
},
|
|
93
|
+
delete(path, handler) {
|
|
94
|
+
router.on('DELETE', path, handler);
|
|
95
|
+
return api;
|
|
96
|
+
},
|
|
97
|
+
fallback(handler) {
|
|
98
|
+
router.fallback(handler);
|
|
99
|
+
return api;
|
|
100
|
+
},
|
|
101
|
+
handle() {
|
|
102
|
+
return (request, response) => api.route(request, response);
|
|
103
|
+
},
|
|
104
|
+
patch(path, handler) {
|
|
105
|
+
router.on('PATCH', path, handler);
|
|
106
|
+
return api;
|
|
107
|
+
},
|
|
108
|
+
route(request, response) {
|
|
109
|
+
return router.route(request, response);
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
return api;
|
|
113
|
+
};
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vyriy/router",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.8",
|
|
4
4
|
"description": "Router utility for Vyriy projects",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"@types/aws-lambda": "^8.10.162"
|
|
7
|
+
"@types/aws-lambda": "^8.10.162",
|
|
8
|
+
"@vyriy/handler": "0.7.8"
|
|
8
9
|
},
|
|
9
10
|
"agents": "./AGENTS.md",
|
|
10
11
|
"license": "MIT",
|
package/router.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
import type { APIGatewayProxyEvent, Handler, HttpHandler, ResponseStream, RouteResult, StreamHandler } from './types.js';
|
|
2
3
|
declare class BaseRouter<CurrentHandler> {
|
|
3
4
|
protected fallbackHandler?: CurrentHandler;
|
|
4
5
|
protected readonly routes: Record<string, Record<string, CurrentHandler>>;
|
|
@@ -11,4 +12,9 @@ export declare class Router extends BaseRouter<Handler> {
|
|
|
11
12
|
export declare class StreamRouter extends BaseRouter<StreamHandler> {
|
|
12
13
|
route(event: APIGatewayProxyEvent, responseStream: ResponseStream): Promise<void>;
|
|
13
14
|
}
|
|
15
|
+
export declare class HttpRouter extends BaseRouter<HttpHandler> {
|
|
16
|
+
private readonly allRoutes;
|
|
17
|
+
all(path: string, handler: HttpHandler): this;
|
|
18
|
+
route(request: IncomingMessage, response: ServerResponse): Promise<void>;
|
|
19
|
+
}
|
|
14
20
|
export {};
|
package/router.js
CHANGED
|
@@ -92,3 +92,29 @@ export class StreamRouter extends BaseRouter {
|
|
|
92
92
|
}));
|
|
93
93
|
}
|
|
94
94
|
}
|
|
95
|
+
export class HttpRouter extends BaseRouter {
|
|
96
|
+
allRoutes = {};
|
|
97
|
+
all(path, handler) {
|
|
98
|
+
if (this.allRoutes[path]) {
|
|
99
|
+
throw new Error(`ALL ${path} already exists!`);
|
|
100
|
+
}
|
|
101
|
+
this.allRoutes[path] = handler;
|
|
102
|
+
return this;
|
|
103
|
+
}
|
|
104
|
+
async route(request, response) {
|
|
105
|
+
const method = (request.method ?? 'GET').toUpperCase();
|
|
106
|
+
const pathname = (request.url ?? '/').split('?')[0];
|
|
107
|
+
const handler = this.routes[method]?.[pathname] ?? this.allRoutes[pathname] ?? this.fallbackHandler;
|
|
108
|
+
if (handler) {
|
|
109
|
+
await handler(request, response);
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
response
|
|
113
|
+
.writeHead(404, {
|
|
114
|
+
'content-type': 'application/json',
|
|
115
|
+
})
|
|
116
|
+
.end(JSON.stringify({
|
|
117
|
+
message: STATUS_CODES[404],
|
|
118
|
+
}));
|
|
119
|
+
}
|
|
120
|
+
}
|
package/types.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import type { APIGatewayProxyEvent, APIGatewayProxyEventQueryStringParameters, APIGatewayProxyResult } from 'aws-lambda';
|
|
2
|
+
import type { HttpHandler } from '@vyriy/handler';
|
|
3
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
4
|
export type { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
|
|
5
|
+
export type { HttpHandler } from '@vyriy/handler';
|
|
3
6
|
type MaybePromise<Result> = Result | Promise<Result>;
|
|
4
7
|
export type RouteResult = APIGatewayProxyResult;
|
|
5
8
|
export type RouteHandlerResult = {
|
|
@@ -49,3 +52,14 @@ export type StreamRouterApi = {
|
|
|
49
52
|
patch(path: string, handler: StreamHandler): StreamRouterApi;
|
|
50
53
|
route(event: APIGatewayProxyEvent, responseStream: ResponseStream): Promise<void>;
|
|
51
54
|
};
|
|
55
|
+
export type HttpRouterApi = {
|
|
56
|
+
all(path: string, handler: HttpHandler): HttpRouterApi;
|
|
57
|
+
get(path: string, handler: HttpHandler): HttpRouterApi;
|
|
58
|
+
handle(): HttpHandler;
|
|
59
|
+
post(path: string, handler: HttpHandler): HttpRouterApi;
|
|
60
|
+
put(path: string, handler: HttpHandler): HttpRouterApi;
|
|
61
|
+
delete(path: string, handler: HttpHandler): HttpRouterApi;
|
|
62
|
+
fallback(handler: HttpHandler): HttpRouterApi;
|
|
63
|
+
patch(path: string, handler: HttpHandler): HttpRouterApi;
|
|
64
|
+
route(request: IncomingMessage, response: ServerResponse): Promise<void>;
|
|
65
|
+
};
|