-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.js
153 lines (128 loc) · 4.39 KB
/
utils.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
'use strict'
var utils = require('lazy-cache')(require)
var fn = require
require = utils // eslint-disable-line no-undef, no-native-reassign, no-global-assign
/**
* Lazily required module dependencies
*/
require('extend-shallow', 'extend')
require('inflection')
require('koa-better-router', 'Router')
require('methods')
require = fn // eslint-disable-line no-undef, no-native-reassign, no-global-assign
utils.r = function r (name, id, edit) {
name = name !== '' ? `/${name}` : ''
let url = name + (id ? `/${id}` : '') + (edit ? `/${edit}` : '')
return url
}
utils.updateRoute = function updateRoute (ctx, destRoute) {
let route = destRoute.route.slice(1)
/* istanbul ignore next */
if (!route.length) return '/:id'
if (ctx.options.prefix !== '/') {
route = route.replace(ctx.options.prefix, '')
}
let res = []
let singular = null
let parts = route.split('/')
var len = parts.length
var i = -1
while (i++ < len) {
if (!(i % 2) && parts[i] && parts[i] !== 'edit') {
let plur = parts[i]
singular = utils.inflection.singularize(plur)
res.push(plur)
res.push(`:${singular}`)
}
}
destRoute.route = `/${res.join('/')}`
return destRoute
}
utils.notImplemented = function notImplemented () {
return function notImplemented_ (ctx, next) {
ctx.status = 501
ctx.body = 'Not Implemented'
return next()
}
}
utils.defaultController = {
new: utils.notImplemented(),
show: utils.notImplemented(),
edit: utils.notImplemented(),
index: utils.notImplemented(),
create: utils.notImplemented(),
update: utils.notImplemented(),
remove: utils.notImplemented()
}
utils.defaultControllerMap = {
new: 'new',
show: 'show',
edit: 'edit',
index: 'index',
create: 'create',
update: 'update',
remove: 'remove'
}
utils.defaultRequestMethods = {}
utils.methods.forEach((method) => {
utils.defaultRequestMethods[method] = method
})
utils.mergeOptions = function merge (opts, options) {
options = utils.extend({}, options)
let map = utils.extend(opts.map, options.map)
let methods = utils.extend(opts.methods, options.methods)
opts = utils.extend(opts, options)
opts.map = map
opts.methods = methods
return opts
}
utils.createResourceRoutes = function createResourceRoutes (route, param, ctrl) {
return function (self, opts) {
self.options = utils.mergeOptions(self.options, opts)
ctrl = utils.extend({}, utils.defaultController, ctrl)
// map controller methods to be called
let map = self.options.map
// map request methods to be used
let m = self.options.methods
m.del = m.del || m.delete
let src = []
/* eslint-disable no-multi-spaces */
let routes = [
[ m.get, utils.r(route), ctrl[map.index] ],
[ m.get, utils.r(route, 'new'), ctrl[map.new] ],
[ m.post, utils.r(route), ctrl[map.create] ],
[ m.get, utils.r(route, param), ctrl[map.show] ],
[ m.get, utils.r(route, param, 'edit'), ctrl[map.edit] ],
[ m.put, utils.r(route, param), ctrl[map.update] ],
[ m.del, utils.r(route, param), ctrl[map.remove] ]
]
/* eslint-enable no-multi-spaces */
// create RESTful routes
routes.forEach((args) => {
src.push(self.createRoute(args[0], args[1], args[2]))
})
/**
* I'm tired of that fucking non-stop tricking
* this fucking service - CodeClimate!
* Below is more human-readable and human-understandable
* variant of above shit. Thanks to god that there always
* have more ways to write one thing.
*
* The whole thing is that you just use `koa-better-router`'s
* `.createRoute` method which accepts METHOD, ROUTE and MIDDLEWARES.
* In addition we allow re-mapping of request and controller methods.
*/
// src.push(self.createRoute(m.get, utils.r(route), ctrl[map.index]))
// src.push(self.createRoute(m.get, utils.r(route, 'new'), ctrl[map.new]))
// src.push(self.createRoute(m.post, utils.r(route), ctrl[map.create]))
// src.push(self.createRoute(m.get, utils.r(route, param), ctrl[map.show]))
// src.push(self.createRoute(m.get, utils.r(route, param, 'edit'), ctrl[map.edit]))
// src.push(self.createRoute(m.put, utils.r(route, param), ctrl[map.update]))
// src.push(self.createRoute(m.del, utils.r(route, param), ctrl[map.remove]))
return src
}
}
/**
* Expose `utils` modules
*/
module.exports = utils