Skip to content

Commit f8c2cdb

Browse files
committed
feat: add support to rename the resource param names
1 parent 5d92a96 commit f8c2cdb

File tree

4 files changed

+642
-4
lines changed

4 files changed

+642
-4
lines changed

adonis-typings/route.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,16 @@ declare module '@ioc:Adonis/Core/Route' {
228228
*/
229229
namespace(namespace: string): this
230230

231+
/**
232+
* Get the route pattern
233+
*/
234+
getPattern(): string
235+
236+
/**
237+
* Update route pattern
238+
*/
239+
setPattern(pattern: string): this
240+
231241
/**
232242
* Returns [[RouteDefinition]] that can be passed to the [[Store]] for
233243
* registering the route
@@ -290,6 +300,11 @@ declare module '@ioc:Adonis/Core/Route' {
290300
*/
291301
namespace(namespace: string): this
292302

303+
/**
304+
* Set the param name for a given resource
305+
*/
306+
paramFor(resource: string, param: string): this
307+
293308
/**
294309
* Prepend name to the routes names
295310
*/

src/Router/Resource.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ export class RouteResource extends Macroable implements RouteResourceContract {
3737
protected static macros = {}
3838
protected static getters = {}
3939

40+
/**
41+
* The param names used to create the resource URLs.
42+
*
43+
* We need these later when someone explicitly wants to remap
44+
* param name for a given resource using the "paramFor" method.
45+
*/
46+
private resourceParamNames: Record<string, string> = {}
47+
4048
/**
4149
* A copy of routes that belongs to this resource
4250
*/
@@ -79,8 +87,18 @@ export class RouteResource extends Macroable implements RouteResourceContract {
7987
const resourceTokens = this.resource.split('.')
8088
const mainResource = resourceTokens.pop()!
8189

90+
/**
91+
* The main resource always uses ids
92+
*/
93+
this.resourceParamNames[mainResource] = ':id'
94+
8295
const fullUrl = `${resourceTokens
83-
.map((token) => `${token}/:${string.snakeCase(singular(token))}_id`)
96+
.map((token) => {
97+
const paramName = `:${string.snakeCase(singular(token))}_id`
98+
this.resourceParamNames[token] = paramName
99+
100+
return `${token}/${paramName}`
101+
})
84102
.join('/')}/${mainResource}`
85103

86104
this.makeRoute(fullUrl, ['GET', 'HEAD'], 'index')
@@ -173,6 +191,24 @@ export class RouteResource extends Macroable implements RouteResourceContract {
173191
return this
174192
}
175193

194+
/**
195+
* Set the param name for a given resource
196+
*/
197+
public paramFor(resource: string, param: string): this {
198+
const existingParam = this.resourceParamNames[resource]
199+
this.resourceParamNames[resource] = `:${param}`
200+
201+
this.routes.forEach((route) => {
202+
/**
203+
* Update the pattern for the route with the new param name
204+
*/
205+
route.setPattern(
206+
route.getPattern().replace(`${resource}/${existingParam}`, `${resource}/:${param}`)
207+
)
208+
})
209+
return this
210+
}
211+
176212
/**
177213
* Prepend name to the routes names
178214
*/

src/Router/Route.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export class Route extends Macroable implements RouteContract {
105105
/**
106106
* Returns a normalized pattern string by prefixing the `prefix` (if defined).
107107
*/
108-
private getPattern(): string {
108+
private computePattern(): string {
109109
const pattern = dropSlash(this.pattern)
110110
const prefix = this.prefixes
111111
.slice()
@@ -208,14 +208,29 @@ export class Route extends Macroable implements RouteContract {
208208
return this
209209
}
210210

211+
/**
212+
* Get the route pattern
213+
*/
214+
public getPattern(): string {
215+
return this.pattern
216+
}
217+
218+
/**
219+
* Set the route pattern
220+
*/
221+
public setPattern(pattern: string): this {
222+
this.pattern = pattern
223+
return this
224+
}
225+
211226
/**
212227
* Returns [[RouteDefinition]] that can be passed to the [[Store]] for
213228
* registering the route
214229
*/
215230
public toJSON(): RouteJSON {
216231
return {
217232
domain: this.routeDomain,
218-
pattern: this.getPattern(),
233+
pattern: this.computePattern(),
219234
matchers: this.getMatchers(),
220235
meta: {
221236
namespace: this.routeNamespace,

0 commit comments

Comments
 (0)