-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
83 lines (75 loc) · 2.43 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
'use strict';
module.exports = function(config) {
let plural = 's';
let oldName = config.name;
if(!config.namespace) config.namespace = '';
if (config.name.substring()[config.name.substring().length - 1] == 'y') {
oldName = config.name;
plural = 'es';
let replacedName = config.name.slice(0, -1) + 'i';
config.name = replacedName;
}
let rest = {
show: {
path: config.namespace + '/' + config.name + plural +'/{id}',
method: 'GET',
config: {
handler: config.controller.show,
description: 'Get '+oldName.replace('_', ' ') + ' by id',
notes: 'Get all '+oldName.replace('_', ' ') + ' by id return one records',
tags: ['api', oldName]
}
},
all: {
path: config.namespace + '/' + config.name + plural,
method: 'GET',
config: {
handler: config.controller.index,
description: 'Get all '+oldName.replace('_', ' ') + ' records',
notes: 'Get all '+oldName.replace('_', ' ') + ' records',
tags: ['api', oldName]
}
},
create: {
path: config.namespace + '/' + config.name + plural,
method: 'POST',
config: {
handler: config.controller.create,
description: 'Create '+oldName.replace('_', ' ') + ' ',
notes: 'Create '+oldName.replace('_', ' ') + ' return the model',
validate: config.validate || null,
tags: ['api', oldName]
}
},
update: {
path: config.namespace + '/' + config.name + plural +'/{id}',
method: 'PUT',
config: {
handler: config.controller.update,
description: 'Update '+oldName.replace('_', ' ') + ' by id',
notes: 'Update '+oldName.replace('_', ' ') + ' by id return the model',
validate: config.validate || null,
tags: ['api', oldName]
}
},
delete: {
path: config.namespace + '/' + config.name + plural +'/{id}',
method: 'DELETE',
config: {
handler: config.controller.destroy,
description: 'Delete '+oldName.replace('_', ' ') + ' by id',
notes: 'Delete '+oldName.replace('_', ' ') + ' by id',
tags: ['api', oldName]
}
}
}
let restMethod = config.methods || [];
if (restMethod.length == 0) {
restMethod = ['show', 'all', 'update', 'delete', 'create'];
}
var buildRoute = [];
for (var i=0, len = restMethod.length; i < len; i++) {
buildRoute.push(rest[restMethod[i]]);
}
return buildRoute;
}