@strav/http 0.1.6 → 0.2.2

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/http/router.ts +92 -9
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@strav/http",
3
- "version": "0.1.6",
3
+ "version": "0.2.2",
4
4
  "type": "module",
5
5
  "description": "HTTP layer for the Strav framework — router, server, middleware, authentication, sessions, validation, and views",
6
6
  "license": "MIT",
@@ -64,6 +64,8 @@ interface GroupState {
64
64
  middleware: Middleware[]
65
65
  subdomain?: string
66
66
  subdomainParamName?: string
67
+ alias?: string
68
+ ref?: GroupRef // Reference to the GroupRef for deferred alias assignment
67
69
  }
68
70
 
69
71
  // ---------------------------------------------------------------------------
@@ -102,13 +104,69 @@ function parseSubdomain(pattern: string): { value: string; paramName?: string }
102
104
  // ---------------------------------------------------------------------------
103
105
 
104
106
  class RouteRef {
105
- constructor(private route: RouteDefinition) {}
107
+ private groupRefs: (GroupRef | undefined)[]
108
+ private routeName?: string
109
+
110
+ constructor(private route: RouteDefinition, private router: Router) {
111
+ // Capture references to the GroupRefs from the current stack
112
+ this.groupRefs = router.groupStack.map(state => state.ref)
113
+ }
106
114
 
107
115
  /** Assign a name to this route (for future URL generation). */
108
116
  as(name: string): this {
109
- this.route.name = name
117
+ this.routeName = name
118
+
119
+ // Define a getter that builds the full name lazily
120
+ Object.defineProperty(this.route, 'name', {
121
+ get: () => {
122
+ const aliases = this.groupRefs
123
+ .map(ref => ref?.getAlias())
124
+ .filter(alias => alias)
125
+
126
+ const groupAlias = aliases.join('.')
127
+ return groupAlias ? `${groupAlias}.${this.routeName}` : this.routeName
128
+ },
129
+ configurable: true
130
+ })
131
+
132
+ return this
133
+ }
134
+ }
135
+
136
+ // ---------------------------------------------------------------------------
137
+ // GroupRef — returned by group methods for chaining (.as)
138
+ // ---------------------------------------------------------------------------
139
+
140
+ class GroupRef {
141
+ private alias?: string
142
+
143
+ constructor(
144
+ private router: Router,
145
+ private groupState: GroupState,
146
+ private callback: (router: Router) => void
147
+ ) {
148
+ // Store reference to this GroupRef in the state
149
+ this.groupState.ref = this
150
+ }
151
+
152
+ /** Assign an alias to this group (for hierarchical route naming). */
153
+ as(alias: string): this {
154
+ this.alias = alias
155
+ this.groupState.alias = alias
110
156
  return this
111
157
  }
158
+
159
+ /** @internal Get the alias assigned to this group */
160
+ getAlias(): string | undefined {
161
+ return this.alias
162
+ }
163
+
164
+ /** @internal Execute the group callback with the current state */
165
+ execute(): void {
166
+ this.router.groupStack.push(this.groupState)
167
+ this.callback(this.router)
168
+ this.router.groupStack.pop()
169
+ }
112
170
  }
113
171
 
114
172
  // ---------------------------------------------------------------------------
@@ -321,8 +379,13 @@ export default class Router {
321
379
  * router.group({ prefix: '/api', middleware: [auth] }, (r) => {
322
380
  * r.get('/users', listUsers)
323
381
  * })
382
+ *
383
+ * @example With group aliasing:
384
+ * router.group({ prefix: '/api' }, (r) => {
385
+ * r.get('/users', listUsers).as('index')
386
+ * }).as('api')
324
387
  */
325
- group(options: GroupOptions, callback: (router: Router) => void): void {
388
+ group(options: GroupOptions, callback: (router: Router) => void): GroupRef {
326
389
  const parent = this.currentGroup()
327
390
  const prefix = (parent?.prefix ?? '') + (options.prefix ?? '')
328
391
  const middleware = [...(parent?.middleware ?? []), ...(options.middleware ?? [])]
@@ -336,9 +399,21 @@ export default class Router {
336
399
  subdomainParamName = parsed.paramName
337
400
  }
338
401
 
339
- this.groupStack.push({ prefix, middleware, subdomain, subdomainParamName })
340
- callback(this)
341
- this.groupStack.pop()
402
+ const groupState: GroupState = {
403
+ prefix,
404
+ middleware,
405
+ subdomain,
406
+ subdomainParamName,
407
+ alias: undefined
408
+ }
409
+
410
+ const ref = new GroupRef(this, groupState, callback)
411
+
412
+ // Execute immediately for backward compatibility
413
+ // The group can still be chained with .as() but routes are registered immediately
414
+ ref.execute()
415
+
416
+ return ref
342
417
  }
343
418
 
344
419
  /**
@@ -354,8 +429,8 @@ export default class Router {
354
429
  * // ctx.params.tenant === 'acme'
355
430
  * })
356
431
  */
357
- subdomain(pattern: string, callback: (router: Router) => void): void {
358
- this.group({ subdomain: pattern }, callback)
432
+ subdomain(pattern: string, callback: (router: Router) => void): GroupRef {
433
+ return this.group({ subdomain: pattern }, callback)
359
434
  }
360
435
 
361
436
  // ---- Dispatch ------------------------------------------------------------
@@ -496,6 +571,14 @@ export default class Router {
496
571
  return this.currentGroup()?.prefix ?? ''
497
572
  }
498
573
 
574
+ /** @internal Get the concatenated alias chain from all parent groups */
575
+ getCurrentGroupAlias(): string {
576
+ const aliases = this.groupStack
577
+ .filter(group => group.alias)
578
+ .map(group => group.alias)
579
+ return aliases.join('.')
580
+ }
581
+
499
582
  /** Resolve a `[Controller, 'method']` tuple into a Handler. */
500
583
  private toHandler(input: HandlerInput): Handler {
501
584
  if (Array.isArray(input)) {
@@ -523,7 +606,7 @@ export default class Router {
523
606
  }
524
607
 
525
608
  this.routes.push(route)
526
- return new RouteRef(route)
609
+ return new RouteRef(route, this)
527
610
  }
528
611
 
529
612
  private extractSubdomain(request: Request): string {