forked from deployd/dpd-event
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (54 loc) · 1.57 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
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) {
return ctx.req.headers[name];
}
}
, setHeader: function (name, value) {
if (ctx.res.setHeader) {
ctx.res.setHeader(name, value);
}
}
, setResult: function(val) {
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();
}
};