@stacksjs/bun-router 0.0.7 → 0.0.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/dist/cli.js +1 -1
- package/dist/index.js +16 -0
- package/dist/router/index.d.ts +1 -0
- package/dist/router/router.d.ts +18 -0
- package/dist/types.d.ts +6 -0
- package/package.json +1 -1
- package/src/router/index.ts +3 -1
- package/src/router/router.ts +57 -0
- package/src/types.ts +6 -0
package/dist/cli.js
CHANGED
|
@@ -1879,7 +1879,7 @@ var prefix = getPrefix();
|
|
|
1879
1879
|
var color18 = __toESM(require_picocolors(), 1);
|
|
1880
1880
|
var import_picocolors19 = __toESM(require_picocolors(), 1);
|
|
1881
1881
|
// package.json
|
|
1882
|
-
var version = "0.0.
|
|
1882
|
+
var version = "0.0.8";
|
|
1883
1883
|
|
|
1884
1884
|
// src/cli/middleware.ts
|
|
1885
1885
|
import fs from "fs/promises";
|
package/dist/index.js
CHANGED
|
@@ -9276,6 +9276,10 @@ class Router {
|
|
|
9276
9276
|
enhancedReq.header = (name) => {
|
|
9277
9277
|
return req.headers.get(name) || req.headers.get(name.toLowerCase()) || null;
|
|
9278
9278
|
};
|
|
9279
|
+
enhancedReq.cookie = (name, defaultValue) => {
|
|
9280
|
+
const value = getCookies()[name];
|
|
9281
|
+
return value !== undefined ? value : defaultValue ?? null;
|
|
9282
|
+
};
|
|
9279
9283
|
enhancedReq.getParam = (name, defaultValue) => {
|
|
9280
9284
|
const value = params?.[name];
|
|
9281
9285
|
return value !== undefined ? value : defaultValue;
|
|
@@ -9530,6 +9534,17 @@ class RouteGroupBuilder2 {
|
|
|
9530
9534
|
return this;
|
|
9531
9535
|
}
|
|
9532
9536
|
}
|
|
9537
|
+
var _enhancementHost = null;
|
|
9538
|
+
function getEnhancementHost() {
|
|
9539
|
+
if (_enhancementHost === null)
|
|
9540
|
+
_enhancementHost = new Router;
|
|
9541
|
+
return _enhancementHost;
|
|
9542
|
+
}
|
|
9543
|
+
function applyRequestEnhancements(req, params = {}) {
|
|
9544
|
+
if (typeof req.bearerToken === "function")
|
|
9545
|
+
return req;
|
|
9546
|
+
return getEnhancementHost().enhanceRequest(req, params);
|
|
9547
|
+
}
|
|
9533
9548
|
|
|
9534
9549
|
// src/router/server.ts
|
|
9535
9550
|
function registerServerHandling(RouterClass) {
|
|
@@ -13815,6 +13830,7 @@ export {
|
|
|
13815
13830
|
authMocks,
|
|
13816
13831
|
authHelpers,
|
|
13817
13832
|
auth,
|
|
13833
|
+
applyRequestEnhancements,
|
|
13818
13834
|
apiKeyAuth,
|
|
13819
13835
|
WebSocketTester,
|
|
13820
13836
|
ValidationMiddlewareBuilder,
|
package/dist/router/index.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { FluentRouteBuilder, FluentRouter, RouteFactory, router, RouterUtils } f
|
|
|
4
4
|
import { Router } from './router';
|
|
5
5
|
import '../types';
|
|
6
6
|
export { Router };
|
|
7
|
+
export { applyRequestEnhancements } from './router';
|
|
7
8
|
declare module './router' {
|
|
8
9
|
interface Router {
|
|
9
10
|
stream: (callback: () => Generator<string | Uint8Array, void, unknown> | AsyncGenerator<string | Uint8Array, void, unknown>, status?: number, headers?: Record<string, string>) => Response;
|
package/dist/router/router.d.ts
CHANGED
|
@@ -274,3 +274,21 @@ export declare class RouteGroupBuilder {
|
|
|
274
274
|
put(_path: string, _handler: RouteHandler): this;
|
|
275
275
|
delete(_path: string, _handler: RouteHandler): this;
|
|
276
276
|
}
|
|
277
|
+
/**
|
|
278
|
+
* Attach bun-router's request macros (`bearerToken`, `getParam`, `cookie`,
|
|
279
|
+
* `cookies`, `header`, `params`, plus the Laravel-style input helpers
|
|
280
|
+
* `get`, `input`, `string`, `integer`, `float`, `boolean`, `array`, `has`,
|
|
281
|
+
* `filled`, etc.) to a request.
|
|
282
|
+
*
|
|
283
|
+
* Idempotent: calling on an already-enhanced request returns it unchanged.
|
|
284
|
+
* We sniff `req.bearerToken` as the marker — cheap and reliable since no
|
|
285
|
+
* native Request has it.
|
|
286
|
+
*
|
|
287
|
+
* @example
|
|
288
|
+
* import { applyRequestEnhancements } from '@stacksjs/bun-router'
|
|
289
|
+
*
|
|
290
|
+
* const enhanced = applyRequestEnhancements(req, { id: '42' })
|
|
291
|
+
* enhanced.bearerToken() // → string | null
|
|
292
|
+
* enhanced.getParam('id') // → '42'
|
|
293
|
+
*/
|
|
294
|
+
export declare function applyRequestEnhancements(req: Request | EnhancedRequest, params?: Record<string, string>): EnhancedRequest;
|
package/dist/types.d.ts
CHANGED
|
@@ -688,6 +688,12 @@ export interface EnhancedRequest extends Request, Omit<RequestMacroMethods, 'ip'
|
|
|
688
688
|
* Route parameters extracted from the URL
|
|
689
689
|
*/
|
|
690
690
|
params: Record<string, string>;
|
|
691
|
+
/**
|
|
692
|
+
* Lookup a single route param. Equivalent to `request.params[name]` but
|
|
693
|
+
* with optional default-value handling, matching the Laravel-style
|
|
694
|
+
* `$request->route('name')` ergonomics.
|
|
695
|
+
*/
|
|
696
|
+
getParam: <T = string>(name: string, defaultValue?: T) => T | undefined;
|
|
691
697
|
/**
|
|
692
698
|
* Query parameters from the URL
|
|
693
699
|
*/
|
package/package.json
CHANGED
package/src/router/index.ts
CHANGED
|
@@ -33,8 +33,10 @@ registerModelBinding(Router)
|
|
|
33
33
|
registerFileBasedRouting(Router)
|
|
34
34
|
registerApiRoutes(Router)
|
|
35
35
|
|
|
36
|
-
// Export the Router class
|
|
36
|
+
// Export the Router class, the standalone request-enhancement helper, and
|
|
37
|
+
// fluent routing features.
|
|
37
38
|
export { Router }
|
|
39
|
+
export { applyRequestEnhancements } from './router'
|
|
38
40
|
|
|
39
41
|
// Type augmentation for Laravel-style methods
|
|
40
42
|
declare module './router' {
|
package/src/router/router.ts
CHANGED
|
@@ -1147,6 +1147,15 @@ export class Router {
|
|
|
1147
1147
|
return req.headers.get(name) || req.headers.get(name.toLowerCase()) || null
|
|
1148
1148
|
}
|
|
1149
1149
|
|
|
1150
|
+
// Convenience cookie reader. The full `enhancedReq.cookies.get(name)` API
|
|
1151
|
+
// is also available below — `cookie(name)` is the shorter form callers
|
|
1152
|
+
// (and the Laravel-style macros) reach for first, so it deserves a
|
|
1153
|
+
// direct method on the enhanced request, not just on the macros class.
|
|
1154
|
+
;(enhancedReq as any).cookie = (name: string, defaultValue?: string): string | null => {
|
|
1155
|
+
const value = getCookies()[name]
|
|
1156
|
+
return value !== undefined ? value : (defaultValue ?? null)
|
|
1157
|
+
}
|
|
1158
|
+
|
|
1150
1159
|
;(enhancedReq as any).getParam = <T = string>(name: string, defaultValue?: T): T | undefined => {
|
|
1151
1160
|
const value = params?.[name] as T | undefined
|
|
1152
1161
|
return value !== undefined ? value : defaultValue
|
|
@@ -1522,3 +1531,51 @@ export class RouteGroupBuilder {
|
|
|
1522
1531
|
return this
|
|
1523
1532
|
}
|
|
1524
1533
|
}
|
|
1534
|
+
|
|
1535
|
+
// ---------------------------------------------------------------------------
|
|
1536
|
+
// Standalone enhancement helper
|
|
1537
|
+
// ---------------------------------------------------------------------------
|
|
1538
|
+
|
|
1539
|
+
// Singleton scratch Router used to expose `enhanceRequest` as a standalone
|
|
1540
|
+
// function. Downstream consumers (frameworks layered on bun-router) often
|
|
1541
|
+
// need to attach the request macros to a request that was created outside
|
|
1542
|
+
// of `route.serve()` — for instance, when a higher-level router wraps
|
|
1543
|
+
// each handler with its own middleware chain. Without an exported function,
|
|
1544
|
+
// those consumers either spin up their own `new Router()` per call or
|
|
1545
|
+
// duplicate the attachment logic in user code.
|
|
1546
|
+
//
|
|
1547
|
+
// We share one instance because `enhanceRequest` does not depend on any
|
|
1548
|
+
// per-router state (route table, middleware groups, etc.) — it only reads
|
|
1549
|
+
// from the request and the supplied params.
|
|
1550
|
+
let _enhancementHost: Router | null = null
|
|
1551
|
+
|
|
1552
|
+
function getEnhancementHost(): Router {
|
|
1553
|
+
if (_enhancementHost === null) _enhancementHost = new Router()
|
|
1554
|
+
return _enhancementHost
|
|
1555
|
+
}
|
|
1556
|
+
|
|
1557
|
+
/**
|
|
1558
|
+
* Attach bun-router's request macros (`bearerToken`, `getParam`, `cookie`,
|
|
1559
|
+
* `cookies`, `header`, `params`, plus the Laravel-style input helpers
|
|
1560
|
+
* `get`, `input`, `string`, `integer`, `float`, `boolean`, `array`, `has`,
|
|
1561
|
+
* `filled`, etc.) to a request.
|
|
1562
|
+
*
|
|
1563
|
+
* Idempotent: calling on an already-enhanced request returns it unchanged.
|
|
1564
|
+
* We sniff `req.bearerToken` as the marker — cheap and reliable since no
|
|
1565
|
+
* native Request has it.
|
|
1566
|
+
*
|
|
1567
|
+
* @example
|
|
1568
|
+
* import { applyRequestEnhancements } from '@stacksjs/bun-router'
|
|
1569
|
+
*
|
|
1570
|
+
* const enhanced = applyRequestEnhancements(req, { id: '42' })
|
|
1571
|
+
* enhanced.bearerToken() // → string | null
|
|
1572
|
+
* enhanced.getParam('id') // → '42'
|
|
1573
|
+
*/
|
|
1574
|
+
export function applyRequestEnhancements(
|
|
1575
|
+
req: Request | EnhancedRequest,
|
|
1576
|
+
params: Record<string, string> = {},
|
|
1577
|
+
): EnhancedRequest {
|
|
1578
|
+
if (typeof (req as any).bearerToken === 'function')
|
|
1579
|
+
return req as EnhancedRequest
|
|
1580
|
+
return getEnhancementHost().enhanceRequest(req as Request, params)
|
|
1581
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -793,6 +793,12 @@ export interface EnhancedRequest extends Request, Omit<RequestMacroMethods, 'ip'
|
|
|
793
793
|
* Route parameters extracted from the URL
|
|
794
794
|
*/
|
|
795
795
|
params: Record<string, string>
|
|
796
|
+
/**
|
|
797
|
+
* Lookup a single route param. Equivalent to `request.params[name]` but
|
|
798
|
+
* with optional default-value handling, matching the Laravel-style
|
|
799
|
+
* `$request->route('name')` ergonomics.
|
|
800
|
+
*/
|
|
801
|
+
getParam: <T = string>(name: string, defaultValue?: T) => T | undefined
|
|
796
802
|
/**
|
|
797
803
|
* Query parameters from the URL
|
|
798
804
|
*/
|