Skip to content
This repository was archived by the owner on Feb 8, 2024. It is now read-only.

Commit e6738ce

Browse files
committed
Add methods renaming for resource
1 parent df48540 commit e6738ce

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

lib/index.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ export class Maker {
293293
* @typedef {object} resourceOptions
294294
* @property {string[]} [only] Keep only that handlers: `[read, create, update, destroy]`
295295
* @property {string[]} [except] Keep all except that handlers
296+
* @property {object} [methodNames] Change method names
296297
*
297298
* @example <caption>Usage with `only`</caption>
298299
* createRest(r => {
@@ -303,6 +304,19 @@ export class Maker {
303304
* createRest(r => {
304305
* r.resource('bar', BarController, { except: ['destroy', 'update'] })
305306
* })
307+
*
308+
* @example <caption>Method names</caption>
309+
* const Controller = {
310+
* createDemo() {},
311+
* updateMe() {},
312+
* justExample() {},
313+
* youDontNeedThis() {},
314+
* }
315+
* createRest(r => {
316+
* r.resource('demo', Controller, { methodNames: {
317+
* read: 'justExample', create: 'createDemo', update: 'updateMe', destroy: 'youDontNeedThis',
318+
* }})
319+
* })
306320
*/
307321

308322
/**
@@ -347,6 +361,10 @@ export class Maker {
347361
}
348362
const methodsList = Object.keys(methods)
349363

364+
const resolveMethodName = (handler, currentController) => options.methodNames && options.methodNames[handler]
365+
? currentController[options.methodNames[handler]]
366+
: currentController[handler]
367+
350368
/**
351369
* @var {Array<[string, string]>} usedList [[handlerName, httpMethod], ...]
352370
*/
@@ -371,7 +389,7 @@ export class Maker {
371389
.filter(handler => methodsList.includes(handler))
372390
.map(handler => [handler, methods[handler]])
373391
.forEach(([handler, httpMethod]) => {
374-
r[httpMethod](controller[handler])
392+
r[httpMethod](resolveMethodName(handler, controller))
375393
})
376394
})
377395
}

test/index.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,29 @@ test('Resource with options.except', t => {
340340
)
341341
})
342342

343+
test('Resource with options.methodNames', t => {
344+
const RenamedController = {
345+
first() {},
346+
second() {},
347+
third() {},
348+
fourth() {},
349+
}
350+
t.deepEqual(
351+
createRest(r => {
352+
r.resource('unicorn', RenamedController, {
353+
methodNames: { read: 'first', create: 'second', update: 'third', destroy: 'fourth' }
354+
})
355+
}),
356+
make([], [], {}, {
357+
unicorn: make([], [], {
358+
GET: [RenamedController.first],
359+
POST: [RenamedController.second],
360+
PUT: [RenamedController.third],
361+
DELETE: [RenamedController.fourth],
362+
}),
363+
})
364+
)
365+
})
343366

344367
test('resource in scope', t => {
345368
t.deepEqual(

0 commit comments

Comments
 (0)