UNPKG

5.19 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 job_1 = require("./job");
8var fileJob_1 = require("./fileJob");
9var fs = require("fs"), tmp = require("tmp"), url = require("url"), path = require("path"), _ = require("lodash");
10/**
11 * A job that is triggered when a webhook receives a request.
12 */
13var WebhookJob = (function (_super) {
14 __extends(WebhookJob, _super);
15 /**
16 * WebhookJob constructor
17 * @param e
18 * @param request
19 * @param response
20 */
21 function WebhookJob(e, request, response) {
22 _super.call(this, e, "Webhook Job");
23 this.request = request;
24 this.response = response;
25 this._responseSent = false;
26 }
27 Object.defineProperty(WebhookJob.prototype, "responseSent", {
28 /**
29 * Get if the response to the webhook was already sent or not.
30 * @returns {boolean}
31 */
32 get: function () {
33 return this._responseSent;
34 },
35 /**
36 * Set if the response to the webhook was already sent or not.
37 * @param sent
38 */
39 set: function (sent) {
40 this._responseSent = sent;
41 },
42 enumerable: true,
43 configurable: true
44 });
45 /**
46 * Get the HTTP response object.
47 * @returns {ClientResponse}
48 */
49 WebhookJob.prototype.getResponse = function () {
50 return this.response;
51 };
52 /**
53 * Get the HTTP request object.
54 * @returns {ClientRequest}
55 */
56 WebhookJob.prototype.getRequest = function () {
57 return this.request;
58 };
59 /**
60 * Return a specific URL parameter.
61 * #### Example
62 * ```js
63 * // Webhook URL: /hooks/my/hook?customer_id=MyCust
64 * var customer_id = webhookJob.getUrlParameter("customer_id");
65 * // customer_id => MyCust
66 * ```
67 * @param parameter
68 * @returns {any}
69 */
70 WebhookJob.prototype.getQueryStringValue = function (parameter) {
71 var wh = this;
72 var url_parts = url.parse(wh.getRequest().url, true);
73 return url_parts.query[parameter];
74 };
75 /**
76 * Return all URl parameters.
77 * * #### Example
78 * ```js
79 * // Webhook URL: /hooks/my/hook?customer_id=MyCust&file_name=MyFile.zip
80 * var query = webhookJob.getUrlParameters();
81 * // query => {customer_id: "MyCust", file_name: "MyFile.zip"}
82 * ```
83 * @returns {any}
84 */
85 WebhookJob.prototype.getQueryStringValues = function () {
86 var wh = this;
87 var url_parts = url.parse(wh.getRequest().url, true);
88 return url_parts.query;
89 };
90 /**
91 * Returns FileJobs made from files sent via FormData to the webhook.
92 * @returns {FileJob[]}
93 */
94 WebhookJob.prototype.getFormDataFiles = function () {
95 var wh = this;
96 var files = wh.getRequest().files;
97 var jobs = [];
98 if (files) {
99 files.forEach(function (file) {
100 var job = new fileJob_1.FileJob(wh.e, file.path);
101 job.rename(file.originalname);
102 jobs.push(job);
103 });
104 }
105 return jobs;
106 };
107 /**
108 * Get all FormData values.
109 * @returns {any}
110 */
111 WebhookJob.prototype.getFormDataValues = function () {
112 var wh = this;
113 var body = wh.getRequest().body;
114 return body;
115 };
116 /**
117 * Get a single FormData value.
118 * @param key
119 * @returns {any}
120 */
121 WebhookJob.prototype.getFormDataValue = function (key) {
122 var wh = this;
123 var formData = wh.getFormDataValues();
124 if (formData && key in formData) {
125 return formData[key];
126 }
127 else {
128 return false;
129 }
130 };
131 /**
132 * Get a string from the request body.
133 * The given callback is given a string parameter.
134 * #### Example
135 * ```js
136 * webhookJob.getDataAsString(function(requestBody){
137 * console.log(requestBody);
138 * });
139 * ```
140 * @param callback
141 */
142 WebhookJob.prototype.getDataAsString = function (callback) {
143 var wh = this;
144 var req = wh.getRequest();
145 var data = "";
146 req.on("data", function (chunk) {
147 data += chunk;
148 });
149 req.on("end", function () {
150 callback(data);
151 });
152 };
153 /**
154 * Returns an array of parameters from both the query string and form-data.
155 */
156 WebhookJob.prototype.getParameters = function () {
157 var wh = this;
158 return _.merge(wh.getQueryStringValues(), wh.getFormDataValues());
159 };
160 /**
161 * Returns a parameter from both the query string and form-data.
162 * @param key
163 * @returns {any}
164 */
165 WebhookJob.prototype.getParameter = function (key) {
166 var wh = this;
167 if (_.has(wh.getParameters(), key)) {
168 return wh.getParameters()[key];
169 }
170 else {
171 return false;
172 }
173 };
174 return WebhookJob;
175}(job_1.Job));
176exports.WebhookJob = WebhookJob;