-
Notifications
You must be signed in to change notification settings - Fork 25
/
index.js
72 lines (62 loc) · 1.93 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
var Resource = require('deployd/lib/resource')
, Script = require('deployd/lib/script')
, util = require('util');
function EventResource() {
Resource.apply(this, arguments);
}
util.inherits(EventResource, Resource);
EventResource.label = "Event";
EventResource.events = ["get", "post", "put", "delete"];
module.exports = EventResource;
EventResource.prototype.clientGeneration = true;
EventResource.prototype.handle = function (ctx, next) {
var parts = ctx.url.split('/').filter(function(p) { return p; });
var result = {};
var domain = {
url: ctx.url
, parts: parts
, query: ctx.query
, body: ctx.body
, 'this': result
, getHeader: function (name) {
if (ctx.req.headers && typeof name == 'string' && name) {
return ctx.req.headers[name.toLowerCase()];
}
}
, setHeader: function (name, value) {
if (ctx.res.setHeader) {
ctx.res.setHeader(name, value);
}
}
, setStatusCode: function (statusCode) {
if (typeof statusCode !== "number") throw new TypeError("Status code must be a number")
ctx.res.statusCode = statusCode;
}
, setResult: function(val) {
if (typeof val === 'string' || typeof val === 'object') {
result = val;
} else {
result = '' + val;
}
}
};
if (ctx.method === "POST" && this.events.post) {
this.events.post.run(ctx, domain, function(err) {
ctx.done(err, result);
});
} else if (ctx.method === "GET" && this.events.get) {
this.events.get.run(ctx, domain, function(err) {
ctx.done(err, result);
});
} else if (ctx.method === "DELETE" && this.events.delete) {
this.events.delete.run(ctx, domain, function(err) {
ctx.done(err, result);
});
} else if (ctx.method === "PUT" && this.events.put) {
this.events.put.run(ctx, domain, function(err) {
ctx.done(err, result);
});
} else {
next();
}
};