@vyriy/router 0.1.9

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Vyriy contributors
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 ADDED
@@ -0,0 +1,85 @@
1
+ # @vyriy/router
2
+
3
+ Router utility for Vyriy projects.
4
+
5
+ ## Purpose
6
+
7
+ This package provides a small API Gateway router for Lambda handlers.
8
+
9
+ It is intentionally kept small:
10
+
11
+ - matches by HTTP method and exact path
12
+ - passes API Gateway event data into handlers
13
+ - returns a Lambda-friendly response shape
14
+
15
+ 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
+
17
+ ## Install
18
+
19
+ With npm:
20
+
21
+ ```bash
22
+ npm install @vyriy/router
23
+ ```
24
+
25
+ With Yarn:
26
+
27
+ ```bash
28
+ yarn add @vyriy/router
29
+ ```
30
+
31
+ ## Usage
32
+
33
+ ```ts
34
+ import { createRouter } from '@vyriy/router';
35
+
36
+ const router = createRouter();
37
+
38
+ router.get('/health', async ({ event, query, headers, pathParameters, body }) => ({
39
+ statusCode: 200,
40
+ body: JSON.stringify({
41
+ ok: true,
42
+ method: event.httpMethod,
43
+ query,
44
+ headers,
45
+ pathParameters,
46
+ body,
47
+ }),
48
+ }));
49
+ ```
50
+
51
+ ## Exports
52
+
53
+ The package exposes both the root entry and the direct module entry:
54
+
55
+ ```ts
56
+ import { createRouter } from '@vyriy/router';
57
+ import { Router } from '@vyriy/router/router';
58
+ ```
59
+
60
+ ## API
61
+
62
+ - `createRouter()` returns a chainable router API.
63
+ - `router.get(path, handler)` registers a `GET` handler.
64
+ - `router.post(path, handler)` registers a `POST` handler.
65
+ - `router.put(path, handler)` registers a `PUT` handler.
66
+ - `router.delete(path, handler)` registers a `DELETE` handler.
67
+ - `router.patch(path, handler)` registers a `PATCH` handler.
68
+ - `router.route(event)` resolves the matching route and returns an API Gateway response.
69
+
70
+ The low-level `Router` class is also available from `@vyriy/router/router` and exposes only:
71
+
72
+ - `router.on(method, path, handler)`
73
+ - `router.route(event)`
74
+
75
+ Route handlers receive:
76
+
77
+ ```ts
78
+ type HandlerParams = {
79
+ query?: APIGatewayProxyEventQueryStringParameters;
80
+ body?: string;
81
+ headers?: APIGatewayProxyEvent['headers'];
82
+ pathParameters?: APIGatewayProxyEvent['pathParameters'];
83
+ event: APIGatewayProxyEvent;
84
+ };
85
+ ```
package/create.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import type { RouterApi } from './types.js';
2
+ export declare const createRouter: () => RouterApi;
package/create.js ADDED
@@ -0,0 +1,30 @@
1
+ import { Router } from './router.js';
2
+ export const createRouter = () => {
3
+ const router = new Router();
4
+ const api = {
5
+ get(path, handler) {
6
+ router.on('GET', path, handler);
7
+ return api;
8
+ },
9
+ post(path, handler) {
10
+ router.on('POST', path, handler);
11
+ return api;
12
+ },
13
+ put(path, handler) {
14
+ router.on('PUT', path, handler);
15
+ return api;
16
+ },
17
+ delete(path, handler) {
18
+ router.on('DELETE', path, handler);
19
+ return api;
20
+ },
21
+ patch(path, handler) {
22
+ router.on('PATCH', path, handler);
23
+ return api;
24
+ },
25
+ route(event) {
26
+ return router.route(event);
27
+ },
28
+ };
29
+ return api;
30
+ };
package/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './create.js';
2
+ export type * from './types.js';
package/index.js ADDED
@@ -0,0 +1 @@
1
+ export * from './create.js';
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@vyriy/router",
3
+ "version": "0.1.9",
4
+ "description": "Router utility for Vyriy projects",
5
+ "type": "module",
6
+ "main": "./index.js",
7
+ "dependencies": {
8
+ "@types/aws-lambda": "^8.10.161"
9
+ },
10
+ "license": "MIT",
11
+ "types": "./index.d.ts",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./index.d.ts",
15
+ "import": "./index.js",
16
+ "default": "./index.js"
17
+ },
18
+ "./create": {
19
+ "types": "./create.d.ts",
20
+ "import": "./create.js",
21
+ "default": "./create.js"
22
+ },
23
+ "./create.js": {
24
+ "types": "./create.d.ts",
25
+ "import": "./create.js",
26
+ "default": "./create.js"
27
+ },
28
+ "./index": {
29
+ "types": "./index.d.ts",
30
+ "import": "./index.js",
31
+ "default": "./index.js"
32
+ },
33
+ "./index.js": {
34
+ "types": "./index.d.ts",
35
+ "import": "./index.js",
36
+ "default": "./index.js"
37
+ },
38
+ "./router": {
39
+ "types": "./router.d.ts",
40
+ "import": "./router.js",
41
+ "default": "./router.js"
42
+ },
43
+ "./router.js": {
44
+ "types": "./router.d.ts",
45
+ "import": "./router.js",
46
+ "default": "./router.js"
47
+ }
48
+ }
49
+ }
package/router.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ import type { APIGatewayProxyEvent, APIGatewayProxyResult, Handler } from './types.js';
2
+ export declare class Router {
3
+ private readonly routes;
4
+ on(method: string, path: string, handler: Handler): this;
5
+ route(event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult>;
6
+ }
package/router.js ADDED
@@ -0,0 +1,39 @@
1
+ import { METHODS, STATUS_CODES } from 'node:http';
2
+ const SUPPORTED_METHODS = new Set(METHODS.map((method) => method.toUpperCase()));
3
+ export class Router {
4
+ routes = {};
5
+ on(method, path, handler) {
6
+ const normalizedMethod = method.toUpperCase();
7
+ if (!SUPPORTED_METHODS.has(normalizedMethod)) {
8
+ throw new Error(`Unsupported HTTP method: ${normalizedMethod}`);
9
+ }
10
+ const routeGroup = (this.routes[normalizedMethod] ??= {});
11
+ if (routeGroup[path]) {
12
+ throw new Error(`${normalizedMethod} ${path} already exists!`);
13
+ }
14
+ routeGroup[path] = handler;
15
+ return this;
16
+ }
17
+ async route(event) {
18
+ const { httpMethod, path, queryStringParameters, body, headers, pathParameters } = event;
19
+ const result = await this.routes[httpMethod]?.[path]?.({
20
+ query: queryStringParameters ?? undefined,
21
+ body: body ?? undefined,
22
+ headers,
23
+ pathParameters: pathParameters ?? undefined,
24
+ event,
25
+ });
26
+ return result
27
+ ? {
28
+ statusCode: result.statusCode ?? 200,
29
+ body: result.body,
30
+ headers: result.headers,
31
+ }
32
+ : {
33
+ statusCode: 404,
34
+ body: JSON.stringify({
35
+ message: STATUS_CODES[404],
36
+ }),
37
+ };
38
+ }
39
+ }
package/types.d.ts ADDED
@@ -0,0 +1,23 @@
1
+ import type { APIGatewayProxyEvent, APIGatewayProxyEventQueryStringParameters, APIGatewayProxyResult } from 'aws-lambda';
2
+ export type { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
3
+ export type Result = {
4
+ body: APIGatewayProxyResult['body'];
5
+ headers?: APIGatewayProxyResult['headers'];
6
+ statusCode?: APIGatewayProxyResult['statusCode'];
7
+ };
8
+ export type HandlerParams = {
9
+ query?: APIGatewayProxyEventQueryStringParameters;
10
+ body?: string;
11
+ headers?: APIGatewayProxyEvent['headers'];
12
+ pathParameters?: APIGatewayProxyEvent['pathParameters'];
13
+ event: APIGatewayProxyEvent;
14
+ };
15
+ export type Handler = (params: HandlerParams) => Promise<Result | undefined>;
16
+ export type RouterApi = {
17
+ get(path: string, handler: Handler): RouterApi;
18
+ post(path: string, handler: Handler): RouterApi;
19
+ put(path: string, handler: Handler): RouterApi;
20
+ delete(path: string, handler: Handler): RouterApi;
21
+ patch(path: string, handler: Handler): RouterApi;
22
+ route(event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult>;
23
+ };