UNPKG

6.26 kBJavaScriptView Raw
1"use strict";
2var __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};
7var nest_1 = require("./nest");
8var interfaceManager_1 = require("../ui/interfaceManager");
9var http = require("http");
10var WebhookNest = (function (_super) {
11 __extends(WebhookNest, _super);
12 /**
13 * Webhook Nest constructor
14 * @param e
15 * @param path
16 * @param httpMethod
17 * @param handleRequest Custom request handler function.
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 * Get the holdResponse flag.
33 * @returns {boolean}
34 */
35 get: function () {
36 return this._holdResponse;
37 },
38 /**
39 * Set hold response flag. This allows you to run tunnel logic and send the response after completion.
40 * You must call _releaseResponse_ later if you use this.
41 * @param holdResponse
42 */
43 set: function (holdResponse) {
44 this._holdResponse = holdResponse;
45 },
46 enumerable: true,
47 configurable: true
48 });
49 /**
50 * Releases the webhook response when tunnel run logic is complete.
51 * @param job {WebhookJob} The webhook job that triggered the webhook nest.
52 * @param message {string} The optional response message, if not using a custom request handler.
53 * #### Example
54 * ```js
55 * var webhook = af.createWebhookNest(["jobs", "submit"], "post");
56 * webhook.holdResponse = true; // Keeps the response from being sent immediately
57 * var tunnel = af.createTunnel("Dwight's test tunnel");
58 * tunnel.watch(webhook);
59 * tunnel.run(function(job, nest){
60 * setTimeout(function(){
61 * nest.releaseResponse(job, "Worked!"); // Sends response
62 * }, 1500); // After 1.5 seconds
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 * Get the custom handleRequest function.
80 * @returns {any}
81 */
82 WebhookNest.prototype.getCustomHandleRequest = function () {
83 return this.handleRequest;
84 };
85 /**
86 * Set the custom handlerRequest function.
87 * @param handleRequest
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 * Set the path as a string or a string array. All parts are URI encoded.
97 * Create directory structures with an array: ["one", "two"] results in "/one/two".
98 * @param path
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 * Get the path.
121 * @returns {string}
122 */
123 WebhookNest.prototype.getPath = function () {
124 return this.path;
125 };
126 /**
127 * Get the HTTP method.
128 * @returns {string}
129 */
130 WebhookNest.prototype.getHttpMethod = function () {
131 return this.httpMethod;
132 };
133 /**
134 * Set the HTTP method.
135 * @param httpMethod
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 * Get the name.
147 * @returns {string}
148 */
149 WebhookNest.prototype.getName = function () {
150 return this.name;
151 };
152 /**
153 * On load, do nothing.
154 */
155 WebhookNest.prototype.load = function () { };
156 /**
157 * Add webhook to server watch list.
158 */
159 WebhookNest.prototype.watch = function () {
160 var wh = this;
161 wh.e.addWebhook(wh);
162 };
163 /**
164 * Creates a new job
165 * @param job
166 */
167 WebhookNest.prototype.arrive = function (job) {
168 _super.prototype.arrive.call(this, job);
169 };
170 /**
171 * Get the interface manager. Used to manage interface instances for session handling.
172 * @returns {InterfaceManager}
173 */
174 WebhookNest.prototype.getInterfaceManager = function () {
175 return this.im;
176 };
177 return WebhookNest;
178}(nest_1.Nest));
179exports.WebhookNest = WebhookNest;