Skip to content

Commit dafe042

Browse files
committed
feat: add routeKey to the matched route and the context
1 parent b20e313 commit dafe042

File tree

10 files changed

+81
-14
lines changed

10 files changed

+81
-14
lines changed

adonis-typings/context.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ declare module '@ioc:Adonis/Core/HttpContext' {
3232
logger: LoggerContract
3333
profiler: ProfilerRowContract
3434
route?: RouteNode
35+
routeKey: string,
3536
params: any
3637
subdomains: any
3738
}

adonis-typings/route.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ declare module '@ioc:Adonis/Core/Route' {
4646
*/
4747
export type RouteNode = {
4848
pattern: string,
49+
4950
/**
5051
* The router itself doesn't use the handler for anything, it
5152
* leaves the type to `any` for the consumer to decide the
@@ -130,6 +131,11 @@ declare module '@ioc:Adonis/Core/Route' {
130131
*/
131132
export type MatchedRoute = {
132133
route: RouteNode,
134+
135+
/**
136+
* A unique key for the looked up route
137+
*/
138+
routeKey: string,
133139
params: any,
134140
subdomains: any,
135141
}

package-lock.json

Lines changed: 25 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"devDependencies": {
3434
"@adonisjs/encryption": "^2.0.2",
3535
"@adonisjs/fold": "^6.3.5",
36-
"@adonisjs/logger": "^2.0.2",
36+
"@adonisjs/logger": "^2.0.3",
3737
"@adonisjs/mrm-preset": "^2.3.0",
3838
"@adonisjs/profiler": "^3.0.1",
3939
"@poppinss/dev-utils": "^1.0.6",

src/HttpContext/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import { processPattern } from '../helpers'
3232
* error handler and server hooks.
3333
*/
3434
export class HttpContext extends Macroable implements HttpContextContract {
35+
public routeKey: string
3536
public params: any = {}
3637
public subdomains: any = {}
3738
public route?: RouteNode
@@ -136,6 +137,11 @@ export class HttpContext extends Macroable implements HttpContextContract {
136137
meta: {},
137138
}
138139

140+
/**
141+
* Defining route key
142+
*/
143+
ctx.routeKey = `${request.method()}-${ctx.route.pattern}`
144+
139145
/**
140146
* Attaching params to the ctx
141147
*/

src/Router/Store.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,10 @@ export class Store {
189189
method: string,
190190
domain?: { storeMatch: RouteStoreMatch[], value: string },
191191
): null | MatchedRoute {
192-
const matchedDomain = this.tree.domains[domain?.storeMatch[0]?.old || 'root']
192+
const matchingDomain = domain && domain.storeMatch[0] && domain.storeMatch[0].old
193+
const domainName = matchingDomain || 'root'
194+
195+
const matchedDomain = this.tree.domains[domainName]
193196
if (!matchedDomain) {
194197
return null
195198
}
@@ -199,7 +202,7 @@ export class Store {
199202
* method node is missing, means no routes ever got registered for that
200203
* method
201204
*/
202-
const matchedMethod = this.tree.domains[domain?.storeMatch[0].old || 'root'][method]
205+
const matchedMethod = this.tree.domains[domainName][method]
203206
if (!matchedMethod) {
204207
return null
205208
}
@@ -213,11 +216,15 @@ export class Store {
213216
return null
214217
}
215218

219+
const route = matchedMethod.routes[matchedRoute[0].old]
216220
return {
217-
route: matchedMethod.routes[matchedRoute[0].old],
221+
route: route,
222+
routeKey: matchingDomain
223+
? `${matchingDomain}-${method}-${route.pattern}`
224+
: `${method}-${route.pattern}`,
218225
params: matchit.exec(url, matchedRoute),
219226
subdomains: domain?.value
220-
? matchit.exec(domain.value || 'root', domain.storeMatch)
227+
? matchit.exec(domain.value, domain.storeMatch)
221228
: {},
222229
}
223230
}

src/Server/RequestHandler/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export class RequestHandler {
6767
ctx.params = route!.params
6868
ctx.subdomains = route!.subdomains
6969
ctx.route = route!.route
70+
ctx.routeKey = route!.routeKey
7071
}
7172

7273
/**

test/router.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,6 +1723,7 @@ test.group('Router | match', () => {
17231723
name: 'photos.index',
17241724
handler: 'PhotosController.index',
17251725
},
1726+
routeKey: 'GET-/photos',
17261727
subdomains: {},
17271728
})
17281729

@@ -1737,6 +1738,7 @@ test.group('Router | match', () => {
17371738
name: 'photos.create',
17381739
handler: 'PhotosController.create',
17391740
},
1741+
routeKey: 'GET-/photos/create',
17401742
subdomains: {},
17411743
})
17421744

@@ -1751,6 +1753,7 @@ test.group('Router | match', () => {
17511753
name: 'photos.store',
17521754
handler: 'PhotosController.store',
17531755
},
1756+
routeKey: 'POST-/photos',
17541757
subdomains: {},
17551758
})
17561759

@@ -1767,6 +1770,7 @@ test.group('Router | match', () => {
17671770
name: 'photos.show',
17681771
handler: 'PhotosController.show',
17691772
},
1773+
routeKey: 'GET-/photos/:id',
17701774
subdomains: {},
17711775
})
17721776

@@ -1783,6 +1787,7 @@ test.group('Router | match', () => {
17831787
name: 'photos.edit',
17841788
handler: 'PhotosController.edit',
17851789
},
1790+
routeKey: 'GET-/photos/:id/edit',
17861791
subdomains: {},
17871792
})
17881793

@@ -1799,6 +1804,7 @@ test.group('Router | match', () => {
17991804
name: 'photos.update',
18001805
handler: 'PhotosController.update',
18011806
},
1807+
routeKey: 'PUT-/photos/:id',
18021808
subdomains: {},
18031809
})
18041810

@@ -1815,6 +1821,7 @@ test.group('Router | match', () => {
18151821
name: 'photos.delete',
18161822
handler: 'PhotosController.delete',
18171823
},
1824+
routeKey: 'DELETE-/photos/:id',
18181825
subdomains: {},
18191826
})
18201827
})

test/server.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,4 +932,17 @@ test.group('Server | all', (group) => {
932932
const { text } = await supertest(httpServer).get('/').expect(200)
933933
assert.equal(text, 'application/json')
934934
})
935+
936+
test('pass routeKey to the context', async (assert) => {
937+
const server = new Server(new Ioc(), logger, profiler, encryption, Object.assign({
938+
forceContentNegotiationToJSON: true,
939+
}, config))
940+
const httpServer = createServer(server.handle.bind(server))
941+
942+
server.router.get('/', async ({ routeKey, response }) => response.send(routeKey))
943+
server.optimize()
944+
945+
const { text } = await supertest(httpServer).get('/').expect(200)
946+
assert.equal(text, 'GET-/')
947+
})
935948
})

test/store.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ test.group('Store | match', () => {
275275
},
276276
params: {},
277277
subdomains: {},
278+
routeKey: 'GET-/',
278279
})
279280
})
280281

@@ -302,6 +303,7 @@ test.group('Store | match', () => {
302303
username: 'virk',
303304
},
304305
subdomains: {},
306+
routeKey: 'GET-/:username',
305307
})
306308
})
307309

@@ -329,6 +331,7 @@ test.group('Store | match', () => {
329331
username: 'virk',
330332
},
331333
subdomains: {},
334+
routeKey: 'GET-/:username?',
332335
})
333336

334337
assert.deepEqual(store.match('/', 'GET'), {
@@ -340,6 +343,7 @@ test.group('Store | match', () => {
340343
},
341344
params: {},
342345
subdomains: {},
346+
routeKey: 'GET-/:username?',
343347
})
344348
})
345349

@@ -376,6 +380,7 @@ test.group('Store | match', () => {
376380
username: 'virk',
377381
},
378382
subdomains: {},
383+
routeKey: 'GET-/:username',
379384
})
380385
})
381386

@@ -416,6 +421,7 @@ test.group('Store | match', () => {
416421
id: '1',
417422
},
418423
subdomains: {},
424+
routeKey: 'GET-/:id',
419425
})
420426
})
421427

@@ -457,6 +463,7 @@ test.group('Store | match', () => {
457463
id: '1',
458464
},
459465
subdomains: {},
466+
routeKey: 'foo.com-GET-/:id',
460467
})
461468
})
462469

@@ -491,6 +498,7 @@ test.group('Store | match', () => {
491498
subdomains: {
492499
subdomain: 'blog',
493500
},
501+
routeKey: ':subdomain.adonisjs.com-GET-/:id',
494502
})
495503
})
496504

@@ -568,6 +576,7 @@ test.group('Store | match', () => {
568576
params: {
569577
},
570578
subdomains: {},
579+
routeKey: 'GET-/users/:id?',
571580
})
572581
})
573582

@@ -597,6 +606,7 @@ test.group('Store | match', () => {
597606
id: '1',
598607
},
599608
subdomains: {},
609+
routeKey: 'GET-/users/:id?',
600610
})
601611
})
602612
})

0 commit comments

Comments
 (0)