forked from deployd/dpd-event
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
88 lines (78 loc) · 2.51 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
84
85
86
87
88
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", "Head", "BeforeRequest"];
module.exports = EventResource;
EventResource.prototype.clientGeneration = true;
EventResource.prototype.doBeforeRequestEvent = function (ctx, domain, fn) {
var collection = this;
if (collection.events.BeforeRequest) {
collection.events.BeforeRequest.run(ctx, domain, fn);
} else {
fn();
}
};
EventResource.prototype.handle = function (ctx, next) {
var collection = this;
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;
}
}
};
collection.doBeforeRequestEvent(ctx, domain, function (err) {
if (err) return ctx.done(err, {});
if (ctx.method === "POST" && collection.events.Post) {
collection.events.Post.run(ctx, domain, function (err) {
ctx.done(err, result);
});
} else if (ctx.method === "GET" && collection.events.Get) {
collection.events.Get.run(ctx, domain, function (err) {
ctx.done(err, result);
});
} else if (ctx.method === "DELETE" && collection.events.Delete) {
collection.events.Delete.run(ctx, domain, function (err) {
ctx.done(err, result);
});
} else if (ctx.method === "PUT" && collection.events.Put) {
collection.events.Put.run(ctx, domain, function (err) {
ctx.done(err, result);
});
} else if (ctx.method === "HEAD" && collection.events.Head) {
collection.events.Head.run(ctx, domain, function (err) {
ctx.done();
});
} else {
next();
}
});
};