@vyriy/router 0.7.8 → 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 CHANGED
@@ -63,10 +63,12 @@ export const handler = router.handle();
63
63
 
64
64
  ## Stream Router
65
65
 
66
- Use `createStreamRouter()` 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.
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.
67
69
 
68
70
  ```ts
69
- import { createStreamRouter } from '@vyriy/router';
71
+ import { createRouter as createStreamRouter } from '@vyriy/router/stream';
70
72
 
71
73
  const streamRouter = createStreamRouter();
72
74
 
@@ -96,8 +98,10 @@ Use `createHttpRouter()` when handlers work directly with native Node `IncomingM
96
98
 
97
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.
98
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
+
99
103
  ```ts
100
- import { createHttpRouter } from '@vyriy/router';
104
+ import { createRouter as createHttpRouter } from '@vyriy/router/http';
101
105
 
102
106
  const httpRouter = createHttpRouter();
103
107
 
@@ -212,13 +216,27 @@ export const handler = api(router.handle());
212
216
 
213
217
  ## Exports
214
218
 
215
- The package exposes both the root entry and the direct module entry:
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:
221
+
222
+ ```ts
223
+ import { createRouter, router } from '@vyriy/router';
224
+ import { createRouter, router } from '@vyriy/router/http';
225
+ import { createRouter, router } from '@vyriy/router/stream';
226
+ ```
227
+
228
+ The root entry also re-exports every variant under prefixed names, so a single import keeps
229
+ working:
216
230
 
217
231
  ```ts
218
232
  import { createHttpRouter, createRouter, createStreamRouter } from '@vyriy/router';
219
- import { HttpRouter, Router, StreamRouter } from '@vyriy/router/router';
233
+ import { HttpRouter, Router, StreamRouter } from '@vyriy/router';
220
234
  ```
221
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
+
222
240
  ## API
223
241
 
224
242
  - `createRouter()` returns a chainable router API.
@@ -240,7 +258,7 @@ import { HttpRouter, Router, StreamRouter } from '@vyriy/router/router';
240
258
 
241
259
  Route handlers may omit `statusCode`; the router normalizes missing status codes to `200` before returning from `router.route(event)`.
242
260
 
243
- The low-level `Router`, `StreamRouter`, and `HttpRouter` classes are also available from `@vyriy/router/router`:
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):
244
262
 
245
263
  - `router.on(method, path, handler)`
246
264
  - `router.fallback(handler)`
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,4 +1,3 @@
1
- import type { HttpRouterApi, RouterApi, StreamRouterApi } from './types.js';
1
+ import type { RouterApi } from './types.js';
2
2
  export declare const createRouter: () => RouterApi;
3
- export declare const createStreamRouter: () => StreamRouterApi;
4
- export declare const createHttpRouter: () => HttpRouterApi;
3
+ export declare const router: RouterApi;
package/create.js CHANGED
@@ -1,113 +1,38 @@
1
- import { HttpRouter, Router, StreamRouter } from './router.js';
1
+ import { Router } from './router.js';
2
2
  export const createRouter = () => {
3
- const router = new Router();
3
+ const instance = new Router();
4
4
  const api = {
5
5
  get(path, handler) {
6
- router.on('GET', path, handler);
6
+ instance.on('GET', path, handler);
7
7
  return api;
8
8
  },
9
9
  post(path, handler) {
10
- router.on('POST', path, handler);
10
+ instance.on('POST', path, handler);
11
11
  return api;
12
12
  },
13
13
  put(path, handler) {
14
- router.on('PUT', path, handler);
14
+ instance.on('PUT', path, handler);
15
15
  return api;
16
16
  },
17
17
  delete(path, handler) {
18
- router.on('DELETE', path, handler);
18
+ instance.on('DELETE', path, handler);
19
19
  return api;
20
20
  },
21
21
  fallback(handler) {
22
- router.fallback(handler);
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
- router.on('PATCH', path, handler);
29
+ instance.on('PATCH', path, handler);
30
30
  return api;
31
31
  },
32
32
  route(event) {
33
- return router.route(event);
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);
70
- },
71
- };
72
- return api;
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);
33
+ return instance.route(event);
110
34
  },
111
35
  };
112
36
  return api;
113
37
  };
38
+ export const router = createRouter();
@@ -0,0 +1,3 @@
1
+ import type { RouterApi } from './types.js';
2
+ export declare const createRouter: () => RouterApi;
3
+ export declare const router: RouterApi;
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();
@@ -0,0 +1,3 @@
1
+ export { createRouter, router } from './create.js';
2
+ export { Router } from './router.js';
3
+ export type * from './types.js';
package/http/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { createRouter, router } from './create.js';
2
+ export { Router } from './router.js';
@@ -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
+ }
@@ -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 * from './create.js';
2
- export * from './router.js';
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 * from './create.js';
2
- export * from './router.js';
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,11 +1,12 @@
1
1
  {
2
2
  "name": "@vyriy/router",
3
- "version": "0.7.8",
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
8
  "@types/aws-lambda": "^8.10.162",
8
- "@vyriy/handler": "0.7.8"
9
+ "@vyriy/handler": "0.8.0"
9
10
  },
10
11
  "agents": "./AGENTS.md",
11
12
  "license": "MIT",
@@ -22,6 +23,16 @@
22
23
  "import": "./index.js",
23
24
  "default": "./index.js"
24
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
+ },
25
36
  "./create": {
26
37
  "types": "./create.d.ts",
27
38
  "import": "./create.js",
@@ -32,6 +43,41 @@
32
43
  "import": "./create.js",
33
44
  "default": "./create.js"
34
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
+ },
35
81
  "./index": {
36
82
  "types": "./index.d.ts",
37
83
  "import": "./index.js",
@@ -51,6 +97,41 @@
51
97
  "types": "./router.d.ts",
52
98
  "import": "./router.js",
53
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"
54
135
  }
55
136
  }
56
137
  }
package/router.d.ts CHANGED
@@ -1,20 +1,5 @@
1
- import type { IncomingMessage, ServerResponse } from 'node:http';
2
- import type { APIGatewayProxyEvent, Handler, HttpHandler, ResponseStream, RouteResult, StreamHandler } from './types.js';
3
- declare class BaseRouter<CurrentHandler> {
4
- protected fallbackHandler?: CurrentHandler;
5
- protected readonly routes: Record<string, Record<string, CurrentHandler>>;
6
- on(method: string, path: string, handler: CurrentHandler): this;
7
- fallback(handler: CurrentHandler): this;
8
- }
1
+ import { BaseRouter } from './base.js';
2
+ import type { APIGatewayProxyEvent, Handler, RouteResult } from './types.js';
9
3
  export declare class Router extends BaseRouter<Handler> {
10
4
  route(event: APIGatewayProxyEvent): Promise<RouteResult>;
11
5
  }
12
- export declare class StreamRouter extends BaseRouter<StreamHandler> {
13
- route(event: APIGatewayProxyEvent, responseStream: ResponseStream): Promise<void>;
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
- }
20
- export {};
package/router.js CHANGED
@@ -1,5 +1,5 @@
1
- import { METHODS, STATUS_CODES } from 'node:http';
2
- const SUPPORTED_METHODS = new Set(METHODS.map((method) => method.toUpperCase()));
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,59 +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
- }
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
- }
@@ -0,0 +1,3 @@
1
+ import type { RouterApi } from './types.js';
2
+ export declare const createRouter: () => RouterApi;
3
+ export declare const router: RouterApi;
@@ -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();
@@ -0,0 +1,3 @@
1
+ export { createRouter, router } from './create.js';
2
+ export { Router } from './router.js';
3
+ export type * from './types.js';
@@ -0,0 +1,2 @@
1
+ export { createRouter, router } from './create.js';
2
+ export { Router } from './router.js';
@@ -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
+ }
@@ -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
@@ -1,8 +1,5 @@
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';
4
2
  export type { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
5
- export type { HttpHandler } from '@vyriy/handler';
6
3
  type MaybePromise<Result> = Result | Promise<Result>;
7
4
  export type RouteResult = APIGatewayProxyResult;
8
5
  export type RouteHandlerResult = {
@@ -12,15 +9,6 @@ export type RouteHandlerResult = {
12
9
  multiValueHeaders?: APIGatewayProxyResult['multiValueHeaders'];
13
10
  statusCode?: APIGatewayProxyResult['statusCode'];
14
11
  };
15
- export type ResponseStream = {
16
- end: {
17
- (): unknown;
18
- (chunk: string | Buffer | Uint8Array): unknown;
19
- };
20
- setContentType?: (contentType: string) => unknown;
21
- writableEnded?: boolean;
22
- write(chunk: string | Buffer | Uint8Array): unknown;
23
- };
24
12
  export type HandlerParams = {
25
13
  query?: APIGatewayProxyEventQueryStringParameters;
26
14
  body?: string;
@@ -29,9 +17,7 @@ export type HandlerParams = {
29
17
  event: APIGatewayProxyEvent;
30
18
  };
31
19
  export type Handler = (params: HandlerParams) => MaybePromise<RouteHandlerResult>;
32
- export type StreamHandler = (params: HandlerParams, responseStream: ResponseStream) => MaybePromise<void>;
33
20
  export type RouterHandler = (event: APIGatewayProxyEvent) => Promise<RouteResult>;
34
- export type StreamRouterHandler = (event: APIGatewayProxyEvent, responseStream: ResponseStream) => Promise<void>;
35
21
  export type RouterApi = {
36
22
  get(path: string, handler: Handler): RouterApi;
37
23
  handle(): RouterHandler;
@@ -42,24 +28,3 @@ export type RouterApi = {
42
28
  patch(path: string, handler: Handler): RouterApi;
43
29
  route(event: APIGatewayProxyEvent): Promise<RouteResult>;
44
30
  };
45
- export type StreamRouterApi = {
46
- get(path: string, handler: StreamHandler): StreamRouterApi;
47
- handle(): StreamRouterHandler;
48
- post(path: string, handler: StreamHandler): StreamRouterApi;
49
- put(path: string, handler: StreamHandler): StreamRouterApi;
50
- delete(path: string, handler: StreamHandler): StreamRouterApi;
51
- fallback(handler: StreamHandler): StreamRouterApi;
52
- patch(path: string, handler: StreamHandler): StreamRouterApi;
53
- route(event: APIGatewayProxyEvent, responseStream: ResponseStream): Promise<void>;
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
- };