@vida-global/core 1.2.3 → 1.2.5
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.
package/lib/server/server.js
CHANGED
|
@@ -200,6 +200,7 @@ class VidaServer {
|
|
|
200
200
|
#registerAction(action, controllerCls) {
|
|
201
201
|
const method = action.method.toLowerCase();
|
|
202
202
|
const requestHandler = this.requestHandler(action.action, controllerCls)
|
|
203
|
+
logger.info(`ROUTE: ${method.toUpperCase().padEnd(6)} ${action.path}`);
|
|
203
204
|
this['_'+method](action.path, requestHandler);
|
|
204
205
|
}
|
|
205
206
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
const { logger }
|
|
2
|
-
const { camelize } = require('inflection');
|
|
1
|
+
const { logger } = require('../logger');
|
|
2
|
+
const { camelize, singularize } = require('inflection');
|
|
3
3
|
|
|
4
4
|
|
|
5
5
|
class VidaServerController {
|
|
@@ -304,7 +304,26 @@ class VidaServerController {
|
|
|
304
304
|
|
|
305
305
|
static _routeForAction(method, path, routePrefix, actionName) {
|
|
306
306
|
if (this.routes[actionName]) return this.routes[actionName];
|
|
307
|
-
|
|
307
|
+
|
|
308
|
+
// If the path starts with Record, use :id as a path parameter
|
|
309
|
+
/*
|
|
310
|
+
class UsersController {
|
|
311
|
+
// Get /users
|
|
312
|
+
async getIndex() {}
|
|
313
|
+
|
|
314
|
+
// Get /user/:id
|
|
315
|
+
async getRecord() {}
|
|
316
|
+
|
|
317
|
+
// Get /user/:id/status
|
|
318
|
+
async getRecordStatus() {}
|
|
319
|
+
}
|
|
320
|
+
*/
|
|
321
|
+
if (/^Record($|[A-Z])/.test(path)) {
|
|
322
|
+
routePrefix = `${singularize(routePrefix)}/:id`;
|
|
323
|
+
path = path == 'Record' ? 'Index' : path.replace(/^Record/, '');
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
if (path == 'Index') return routePrefix;
|
|
308
327
|
|
|
309
328
|
path = path.charAt(0).toLowerCase() + path.slice(1);
|
|
310
329
|
return `${routePrefix}/${path}`;
|
package/package.json
CHANGED
|
@@ -23,22 +23,55 @@ describe('VidaServerController', () => {
|
|
|
23
23
|
|
|
24
24
|
|
|
25
25
|
describe('VidaServerController._constructAction', () => {
|
|
26
|
+
class UsersController extends VidaServerController {
|
|
27
|
+
static get routes() {
|
|
28
|
+
return {getBazBan: '/baz/:id/ban'};
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
|
|
26
32
|
it ('returns an object with path, method, and action', () => {
|
|
27
33
|
const expected = {
|
|
28
34
|
method: 'GET',
|
|
29
|
-
path: '/
|
|
35
|
+
path: '/users/fooBar',
|
|
30
36
|
action: 'getFooBar'
|
|
31
37
|
}
|
|
32
|
-
expect(
|
|
38
|
+
expect(UsersController._constructAction('getFooBar', '')).toEqual(expected);
|
|
33
39
|
});
|
|
34
40
|
|
|
35
41
|
it ('uses a blank path for index actions', () => {
|
|
36
42
|
const expected = {
|
|
37
43
|
method: 'POST',
|
|
38
|
-
path: '/
|
|
44
|
+
path: '/users',
|
|
39
45
|
action: 'postIndex'
|
|
40
46
|
}
|
|
41
|
-
expect(
|
|
47
|
+
expect(UsersController._constructAction('postIndex', '')).toEqual(expected);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it ('creates a single resource route for Record', () => {
|
|
51
|
+
const expected = {
|
|
52
|
+
method: 'GET',
|
|
53
|
+
path: '/user/:id',
|
|
54
|
+
action: 'getRecord'
|
|
55
|
+
}
|
|
56
|
+
expect(UsersController._constructAction('getRecord', '')).toEqual(expected);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it ('only uses Record when checking for single resource routes', () => {
|
|
60
|
+
const expected = {
|
|
61
|
+
method: 'GET',
|
|
62
|
+
path: '/users/recording',
|
|
63
|
+
action: 'getRecording'
|
|
64
|
+
}
|
|
65
|
+
expect(UsersController._constructAction('getRecording', '')).toEqual(expected);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it ('creates a single sub resource route for RecordFoo', () => {
|
|
69
|
+
const expected = {
|
|
70
|
+
method: 'GET',
|
|
71
|
+
path: '/user/:id/foo',
|
|
72
|
+
action: 'getRecordFoo'
|
|
73
|
+
}
|
|
74
|
+
expect(UsersController._constructAction('getRecordFoo', '')).toEqual(expected);
|
|
42
75
|
});
|
|
43
76
|
|
|
44
77
|
it ('uses a custom path when defined', () => {
|
|
@@ -47,7 +80,7 @@ describe('VidaServerController', () => {
|
|
|
47
80
|
path: '/baz/:id/ban',
|
|
48
81
|
action: 'getBazBan'
|
|
49
82
|
}
|
|
50
|
-
expect(
|
|
83
|
+
expect(UsersController._constructAction('getBazBan', '')).toEqual(expected);
|
|
51
84
|
});
|
|
52
85
|
});
|
|
53
86
|
|