Skip to content

Commit 3dab746

Browse files
Harminder VirkHarminder Virk
authored andcommitted
feat: allow making url without performaing route lookup
1 parent 94bd379 commit 3dab746

File tree

5 files changed

+58
-6
lines changed

5 files changed

+58
-6
lines changed

adonis-typings/route.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ declare module '@ioc:Adonis/Core/Route' {
419419
qs?: Record<string, any>
420420
domain?: string
421421
prefixUrl?: string
422+
disableRouteLookup?: boolean
422423
} & Record<string, any>
423424

424425
/**
@@ -614,6 +615,12 @@ declare module '@ioc:Adonis/Core/Route' {
614615
*/
615616
make(identifier: string): string
616617

618+
/**
619+
* Disable route lookup and consider identifier
620+
* as the route pattern
621+
*/
622+
disableRouteLookup(): this
623+
617624
/**
618625
* Generate signed url for the given route
619626
*/

src/Router/LookupStore.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ export class UrlBuilder implements UrlBuilderContract {
3434
*/
3535
private queryString: Record<string, any> = {}
3636

37+
/**
38+
* A boolean to know if the route should be looked
39+
* up inside the route store or not
40+
*/
41+
private lookupRoute = true
42+
3743
/**
3844
* A baseUrl to prefix to the endpoint
3945
*/
@@ -134,6 +140,15 @@ export class UrlBuilder implements UrlBuilderContract {
134140
return this
135141
}
136142

143+
/**
144+
* Disable route lookup. Calling this method considers
145+
* the "identifier" as the route pattern
146+
*/
147+
public disableRouteLookup(): this {
148+
this.lookupRoute = false
149+
return this
150+
}
151+
137152
/**
138153
* Append query string to the final URI
139154
*/
@@ -161,8 +176,14 @@ export class UrlBuilder implements UrlBuilderContract {
161176
* Generate url for the given route identifier
162177
*/
163178
public make(identifier: string) {
164-
const route = this.findRouteOrFail(identifier)
165-
const url = this.processPattern(route.pattern)
179+
let url: string
180+
181+
if (this.lookupRoute) {
182+
const route = this.findRouteOrFail(identifier)
183+
url = this.processPattern(route.pattern)
184+
} else {
185+
url = this.processPattern(identifier)
186+
}
166187
return this.suffixQueryString(this.baseUrl ? `${this.baseUrl}${url}` : url)
167188
}
168189

@@ -173,8 +194,14 @@ export class UrlBuilder implements UrlBuilderContract {
173194
identifier: string,
174195
options?: { expiresIn?: string | number; purpose?: string }
175196
) {
176-
const route = this.findRouteOrFail(identifier)
177-
const url = this.processPattern(route.pattern)
197+
let url: string
198+
199+
if (this.lookupRoute) {
200+
const route = this.findRouteOrFail(identifier)
201+
url = this.processPattern(route.pattern)
202+
} else {
203+
url = this.processPattern(identifier)
204+
}
178205

179206
/*
180207
* Making the signature from the qualified url. We do not prefix the domain when

src/Router/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ export class Router implements RouterContract {
392392
normalizedOptions.params && builder.params(normalizedOptions.params)
393393
normalizedOptions.qs && builder.qs(normalizedOptions.qs)
394394
normalizedOptions.prefixUrl && builder.prefixUrl(normalizedOptions.prefixUrl)
395+
normalizedOptions.disableRouteLookup && builder.disableRouteLookup()
395396

396397
return builder.make(routeIdentifier)
397398
}
@@ -413,6 +414,7 @@ export class Router implements RouterContract {
413414
normalizedOptions.params && builder.params(normalizedOptions.params)
414415
normalizedOptions.qs && builder.qs(normalizedOptions.qs)
415416
normalizedOptions.prefixUrl && builder.prefixUrl(normalizedOptions.prefixUrl)
417+
normalizedOptions.disableRouteLookup && builder.disableRouteLookup()
416418

417419
return builder.makeSigned(routeIdentifier, normalizedOptions)
418420
}

src/helpers.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ export function normalizeMakeUrlOptions(params?: any[] | MakeUrlOptions, options
166166
const qs = options.qs || params['qs']
167167
const domain = options.domain
168168
const prefixUrl = options.prefixUrl
169+
const disableRouteLookup = options.disableRouteLookup || false
169170

170171
/**
171172
* Using legacy options
@@ -177,7 +178,7 @@ export function normalizeMakeUrlOptions(params?: any[] | MakeUrlOptions, options
177178
)
178179
})
179180

180-
return { params: normalizedParams, qs, domain, prefixUrl }
181+
return { params: normalizedParams, qs, domain, prefixUrl, disableRouteLookup }
181182
}
182183

183184
/**
@@ -200,6 +201,7 @@ export function normalizeMakeSignedUrlOptions(
200201
const purpose = options.purpose || params['purpose']
201202
const domain = options.domain
202203
const prefixUrl = options.prefixUrl
204+
const disableRouteLookup = options.disableRouteLookup || false
203205

204206
/**
205207
* Using legacy options
@@ -215,7 +217,7 @@ export function normalizeMakeSignedUrlOptions(
215217
}
216218
)
217219

218-
return { params: normalizedParams, qs, domain, prefixUrl, expiresIn, purpose }
220+
return { params: normalizedParams, qs, domain, prefixUrl, expiresIn, purpose, disableRouteLookup }
219221
}
220222

221223
/**

test/router.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3018,6 +3018,12 @@ test.group('Router | Make url', () => {
30183018
'E_CANNOT_MAKE_ROUTE_URL: "*" param is required to make URL for "/posts/*" route'
30193019
)
30203020
})
3021+
3022+
test('make url without performing route lookup', ({ assert }) => {
3023+
const router = new Router(encryption)
3024+
const url = router.makeUrl('/posts/:id', { id: 1 }, { disableRouteLookup: true })
3025+
assert.equal(url, '/posts/1')
3026+
})
30213027
})
30223028

30233029
test.group('Make signed url', () => {
@@ -3181,6 +3187,14 @@ test.group('Make signed url', () => {
31813187
'E_CANNOT_MAKE_ROUTE_URL: "*" param is required to make URL for "/posts/*" route'
31823188
)
31833189
})
3190+
3191+
test('make signed url without performing route lookup', ({ assert }) => {
3192+
const router = new Router(encryption)
3193+
3194+
const url = router.makeSignedUrl('/posts/:id', { id: 1 }, { disableRouteLookup: true })!
3195+
const qs = parse(url.split('?')[1])
3196+
assert.equal(encryption.verifier.unsign(qs.signature as string), '/posts/1')
3197+
}).pin()
31843198
})
31853199

31863200
test.group('Regression', () => {

0 commit comments

Comments
 (0)