1 | "use strict";
|
2 | var __extends = (this && this.__extends) || function (d, b) {
|
3 | for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
|
4 | function __() { this.constructor = d; }
|
5 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
6 | };
|
7 | var nest_1 = require("./nest");
|
8 | var interfaceManager_1 = require("../ui/interfaceManager");
|
9 | var http = require("http");
|
10 | var WebhookNest = (function (_super) {
|
11 | __extends(WebhookNest, _super);
|
12 | |
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | function WebhookNest(e, path, httpMethod, handleRequest) {
|
20 | _super.call(this, e, path.toString());
|
21 | var wh = this;
|
22 | wh.setPath(path);
|
23 | wh.setHttpMethod(httpMethod);
|
24 | if (handleRequest) {
|
25 | wh.setCustomHandleRequest(handleRequest);
|
26 | }
|
27 | this.im = new interfaceManager_1.InterfaceManager(this.e, this);
|
28 | this._holdResponse = false;
|
29 | }
|
30 | Object.defineProperty(WebhookNest.prototype, "holdResponse", {
|
31 | |
32 |
|
33 |
|
34 |
|
35 | get: function () {
|
36 | return this._holdResponse;
|
37 | },
|
38 | |
39 |
|
40 |
|
41 |
|
42 |
|
43 | set: function (holdResponse) {
|
44 | this._holdResponse = holdResponse;
|
45 | },
|
46 | enumerable: true,
|
47 | configurable: true
|
48 | });
|
49 | |
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | WebhookNest.prototype.releaseResponse = function (job, message) {
|
67 | var wn = this;
|
68 | if (wn.holdResponse === false) {
|
69 | wn.e.log(3, "Nest responses must be held to release a response.", wn);
|
70 | }
|
71 | else if (job.responseSent === true) {
|
72 | wn.e.log(0, "Nest responses was already sent. Skipping.", wn);
|
73 | }
|
74 | else {
|
75 | wn.e.server.sendHookResponse(false, job, wn, job.getRequest(), job.getResponse(), wn.getCustomHandleRequest(), message);
|
76 | }
|
77 | };
|
78 | |
79 |
|
80 |
|
81 |
|
82 | WebhookNest.prototype.getCustomHandleRequest = function () {
|
83 | return this.handleRequest;
|
84 | };
|
85 | |
86 |
|
87 |
|
88 |
|
89 | WebhookNest.prototype.setCustomHandleRequest = function (handleRequest) {
|
90 | if (handleRequest !== null && typeof handleRequest !== "function") {
|
91 | throw ("Custom handleRequest must be a function.");
|
92 | }
|
93 | this.handleRequest = handleRequest;
|
94 | };
|
95 | |
96 |
|
97 |
|
98 |
|
99 |
|
100 | WebhookNest.prototype.setPath = function (path) {
|
101 | var wh = this;
|
102 | var modifiedPath = "";
|
103 | if (typeof (path) === "string") {
|
104 | modifiedPath = encodeURIComponent(path);
|
105 | }
|
106 | else if (path instanceof Array) {
|
107 | path.forEach(function (pi) {
|
108 | modifiedPath += "/" + encodeURIComponent(pi);
|
109 | });
|
110 | }
|
111 | else {
|
112 | throw "Path should be a string or array, " + typeof (path) + " found.";
|
113 | }
|
114 | if (modifiedPath.charAt(0) !== "/") {
|
115 | modifiedPath = "/" + modifiedPath;
|
116 | }
|
117 | wh.path = modifiedPath;
|
118 | };
|
119 | |
120 |
|
121 |
|
122 |
|
123 | WebhookNest.prototype.getPath = function () {
|
124 | return this.path;
|
125 | };
|
126 | |
127 |
|
128 |
|
129 |
|
130 | WebhookNest.prototype.getHttpMethod = function () {
|
131 | return this.httpMethod;
|
132 | };
|
133 | |
134 |
|
135 |
|
136 |
|
137 | WebhookNest.prototype.setHttpMethod = function (httpMethod) {
|
138 | var lower = httpMethod.toLowerCase();
|
139 | var acceptableMethods = ["get", "post", "put", "head", "delete", "options", "trace", "copy", "lock", "mkcol", "move", "purge", "propfind", "proppatch", "unlock", "report", "mkactivity", "checkout", "merge", "m-search", "notify", "subscribe", "unsubscribe", "patch", "search", "connect", "all"];
|
140 | if (acceptableMethods.indexOf(lower) === -1) {
|
141 | throw "HTTP method \"" + lower + "\" is not an acceptable value. " + JSON.stringify(acceptableMethods);
|
142 | }
|
143 | this.httpMethod = lower;
|
144 | };
|
145 | |
146 |
|
147 |
|
148 |
|
149 | WebhookNest.prototype.getName = function () {
|
150 | return this.name;
|
151 | };
|
152 | |
153 |
|
154 |
|
155 | WebhookNest.prototype.load = function () { };
|
156 | |
157 |
|
158 |
|
159 | WebhookNest.prototype.watch = function () {
|
160 | var wh = this;
|
161 | wh.e.addWebhook(wh);
|
162 | };
|
163 | |
164 |
|
165 |
|
166 |
|
167 | WebhookNest.prototype.arrive = function (job) {
|
168 | _super.prototype.arrive.call(this, job);
|
169 | };
|
170 | |
171 |
|
172 |
|
173 |
|
174 | WebhookNest.prototype.getInterfaceManager = function () {
|
175 | return this.im;
|
176 | };
|
177 | return WebhookNest;
|
178 | }(nest_1.Nest));
|
179 | exports.WebhookNest = WebhookNest;
|