forked from palanik/restpress
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
190 lines (152 loc) · 3.94 KB
/
index.js
File metadata and controls
190 lines (152 loc) · 3.94 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright 2013 N. Palani Kumanan. All rights reserved.
var _restActions = {};
// http://en.wikipedia.org/wiki/Representational_state_transfer#RESTful_web_APIs
_restActions['index'] = _restActions['list'] = {
"route" : '',
"method" : 'get'
};
_restActions['putAll'] = {
"route" : '',
"method" : 'put'
};
_restActions['post'] = _restActions['create'] = {
"route" : '',
"method" : 'post'
};
_restActions['deleteAll'] = _restActions['delAll'] = {
"route" : '',
"method" : 'delete'
};
_restActions['get'] = _restActions['read'] = {
"route" : '/:id',
"method" : 'get'
};
_restActions['put'] = _restActions['update'] = {
"route" : '/:id',
"method" : 'put'
};
_restActions['delete'] = _restActions['del'] = {
"route" : '/:id',
"method" : 'delete'
};
function restpress(resource, actions) {
this.resourceName = resource;
this.actions = actions || _restActions;
if (this.actions.hasOwnProperty('all')) {
throw new Error('all is a reserverd action');
}
this.locals = function locals(obj) {
for (var key in obj) {
locals[key] = obj[key];
}
return obj;
};
this._stack = [];
this.endpoints = [];
this._init();
}
// Special method 'all' that is applied to all known actions
restpress.prototype.all = function() {
for (var a in this.actions) {
if (this.actions.hasOwnProperty(a)) {
this[a].apply(this, arguments);
}
}
return this;
};
restpress.prototype._init = function() {
var self = this;
var identify = function(actionName, action) {
self[actionName](function(req, res, next) {
res.set('XX-Powered-By', 'Restpress');
req.resource = self;
req.actionName = actionName;
req.action = action;
next();
});
};
// Kick the can down the road for app to pick it up later
var deferRoutes = function(actionName) {
self[actionName] = function() {
var args = arguments;
self._stack.push({action : actionName, args : args});
return self;
};
};
// Create methods for known actions
for (var a in this.actions) {
if (this.actions.hasOwnProperty(a)) {
// endpoints
var action = this.actions[a];
var endpoint = {name: a, route: action.route, method: action.method};
if (action.doc) {
endpoint.doc = action.doc;
}
self.endpoints.push(endpoint);
deferRoutes(a);
identify(a, action);
}
}
};
restpress.prototype.app = function (basePath, app) {
if (arguments.length == 0) {
return this.app;
}
if (arguments.length == 1) {
app = arguments[0];
basePath = '/';
}
if (! /\//.test(basePath)) {
basePath += '/';
}
this.app = app;
var self = this;
// Set end points
if (this._endpoints == undefined) {
// Adjust routes
this.endpoints.forEach(function(ep){
ep.route = basePath + self.resourceName + ep.route;
});
app.get(basePath + this.resourceName + '/_endpoints', function (req, res, next) {
res.json(self.endpoints);
});
}
// Re-Create methods working via app for known actions
var routeViaApp = function(actionName, action, resourcePath) {
self[actionName] = function() {
// insert actionPath as first argument
[].unshift.call(arguments, resourcePath + action.route);
// Call express app VERB function
self.app[action.method].apply(self.app, arguments);
// return this for chaining
return self;
};
};
for (var a in this.actions) {
if (this.actions.hasOwnProperty(a)) {
// Set action methods to connect to app
routeViaApp(a, this.actions[a], basePath + this.resourceName);
}
}
// make app pickup the kicked cans
this._stack.forEach(function(can) {
this[can.action].apply(this, can.args);
}, this);
// Clear deferred actions
delete this._stack;
};
restpress.prototype.name = function() {
return this.resourceName;
};
// Super method
restpress.prototype.rest = function(methods) {
for (var m in methods) {
if (methods.hasOwnProperty(m)) {
this[m](methods[m]);
}
}
return this;
};
module.exports = restpress;
module.exports.actions = _restActions;
module.exports.version = '0.0.3';