@suprsend/node-sdk 0.1.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/attachment.js +26 -0
- package/dist/bulk_response.js +60 -0
- package/dist/constants.js +40 -0
- package/dist/event.js +153 -84
- package/dist/events_bulk.js +424 -0
- package/dist/index.js +89 -39
- package/dist/request_json/event.json +5 -0
- package/dist/request_json/workflow.json +116 -9
- package/dist/{identity.js → subscriber.js} +193 -48
- package/dist/{identity_helper.js → subscriber_helper.js} +191 -59
- package/dist/subscribers_bulk.js +447 -0
- package/dist/utils.js +210 -1
- package/dist/workflow.js +124 -59
- package/dist/workflows_bulk.js +424 -0
- package/package.json +2 -1
- package/src/attachment.js +12 -0
- package/src/bulk_response.js +35 -0
- package/src/constants.js +28 -0
- package/src/event.js +118 -75
- package/src/events_bulk.js +234 -0
- package/src/index.js +67 -33
- package/src/request_json/event.json +5 -0
- package/src/request_json/workflow.json +116 -9
- package/src/{identity.js → subscriber.js} +118 -34
- package/src/{identity_helper.js → subscriber_helper.js} +196 -53
- package/src/subscribers_bulk.js +235 -0
- package/src/utils.js +136 -0
- package/src/workflow.js +94 -45
- package/src/workflows_bulk.js +234 -0
- package/dist/config.js +0 -13
- package/src/config.js +0 -6
package/dist/workflow.js
CHANGED
|
@@ -5,74 +5,145 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", {
|
|
6
6
|
value: true
|
|
7
7
|
});
|
|
8
|
-
exports["default"] = void 0;
|
|
8
|
+
exports["default"] = exports._WorkflowTrigger = void 0;
|
|
9
9
|
|
|
10
10
|
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
|
|
11
11
|
|
|
12
12
|
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
|
|
13
13
|
|
|
14
|
+
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
|
15
|
+
|
|
14
16
|
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
|
15
17
|
|
|
16
18
|
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
|
17
19
|
|
|
18
|
-
var
|
|
20
|
+
var _axios = _interopRequireDefault(require("axios"));
|
|
19
21
|
|
|
20
|
-
var
|
|
22
|
+
var _signature = _interopRequireDefault(require("./signature"));
|
|
21
23
|
|
|
22
24
|
var _utils = require("./utils");
|
|
23
25
|
|
|
24
|
-
var
|
|
26
|
+
var _attachment = _interopRequireDefault(require("./attachment"));
|
|
25
27
|
|
|
26
|
-
var
|
|
28
|
+
var _constants = require("./constants");
|
|
27
29
|
|
|
28
30
|
var Workflow = /*#__PURE__*/function () {
|
|
29
|
-
function Workflow(
|
|
31
|
+
function Workflow(body, idempotency_key) {
|
|
30
32
|
(0, _classCallCheck2["default"])(this, Workflow);
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
33
|
+
|
|
34
|
+
if (!(body instanceof Object)) {
|
|
35
|
+
throw new _utils.SuprsendError("workflow body must be a json/dictionary");
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
this.body = body;
|
|
39
|
+
this.idempotency_key = idempotency_key;
|
|
34
40
|
}
|
|
35
41
|
|
|
36
42
|
(0, _createClass2["default"])(Workflow, [{
|
|
43
|
+
key: "add_attachment",
|
|
44
|
+
value: function add_attachment() {
|
|
45
|
+
var file_path = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
|
|
46
|
+
|
|
47
|
+
if (!this.body.data) {
|
|
48
|
+
this.body.data = {};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (!(this.body instanceof Object)) {
|
|
52
|
+
throw new _utils.SuprsendError("data must be a dictionary");
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
var attachment = (0, _attachment["default"])(file_path);
|
|
56
|
+
|
|
57
|
+
if (!this.body.data["$attachments"]) {
|
|
58
|
+
this.body["data"]["$attachments"] = [];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
this.body["data"]["$attachments"].push(attachment);
|
|
62
|
+
}
|
|
63
|
+
}, {
|
|
64
|
+
key: "get_final_json",
|
|
65
|
+
value: function get_final_json(config) {
|
|
66
|
+
var is_part_of_bulk = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
67
|
+
|
|
68
|
+
// add idempotency key in body if present
|
|
69
|
+
if (this.idempotency_key) {
|
|
70
|
+
this.body["$idempotency_key"] = this.idempotency_key;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
this.body = (0, _utils.validate_workflow_body_schema)(this.body);
|
|
74
|
+
var apparent_size = (0, _utils.get_apparent_workflow_body_size)(this.body, is_part_of_bulk); // review
|
|
75
|
+
|
|
76
|
+
if (apparent_size > _constants.BODY_MAX_APPARENT_SIZE_IN_BYTES) {
|
|
77
|
+
throw new _utils.SuprsendError("workflow body (discounting attachment if any) too big - ".concat(apparent_size, " Bytes, must not cross ").concat(_constants.BODY_MAX_APPARENT_SIZE_IN_BYTES_READABLE));
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return [this.body, apparent_size];
|
|
81
|
+
}
|
|
82
|
+
}]);
|
|
83
|
+
return Workflow;
|
|
84
|
+
}();
|
|
85
|
+
|
|
86
|
+
exports["default"] = Workflow;
|
|
87
|
+
|
|
88
|
+
var _WorkflowTrigger = /*#__PURE__*/function () {
|
|
89
|
+
function _WorkflowTrigger(config) {
|
|
90
|
+
(0, _classCallCheck2["default"])(this, _WorkflowTrigger);
|
|
91
|
+
this.config = config;
|
|
92
|
+
this.url = this._get_url();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
(0, _createClass2["default"])(_WorkflowTrigger, [{
|
|
37
96
|
key: "_get_url",
|
|
38
97
|
value: function _get_url() {
|
|
39
98
|
var url_template = "/trigger/";
|
|
40
99
|
|
|
41
|
-
if (this.
|
|
42
|
-
if (this.
|
|
100
|
+
if (this.config.include_signature_param) {
|
|
101
|
+
if (this.config.auth_enabled) {
|
|
43
102
|
url_template = url_template + "?verify=true";
|
|
44
103
|
} else {
|
|
45
104
|
url_template = url_template + "?verify=false";
|
|
46
105
|
}
|
|
47
106
|
}
|
|
48
107
|
|
|
49
|
-
var url_formatted = "".concat(this.
|
|
108
|
+
var url_formatted = "".concat(this.config.base_url).concat(this.config.workspace_key).concat(url_template);
|
|
50
109
|
return url_formatted;
|
|
51
110
|
}
|
|
52
111
|
}, {
|
|
53
112
|
key: "_get_headers",
|
|
54
113
|
value: function _get_headers() {
|
|
55
114
|
return {
|
|
56
|
-
"Content-Type": "application/json",
|
|
115
|
+
"Content-Type": "application/json; charset=utf-8",
|
|
57
116
|
Date: new Date().toUTCString(),
|
|
58
|
-
"User-Agent": this.
|
|
117
|
+
"User-Agent": this.config.user_agent
|
|
59
118
|
};
|
|
60
119
|
}
|
|
61
120
|
}, {
|
|
62
|
-
key: "
|
|
121
|
+
key: "trigger",
|
|
122
|
+
value: function trigger(workflow) {
|
|
123
|
+
var is_part_of_bulk = false;
|
|
124
|
+
|
|
125
|
+
var _workflow$get_final_j = workflow.get_final_json(this.config, is_part_of_bulk),
|
|
126
|
+
_workflow$get_final_j2 = (0, _slicedToArray2["default"])(_workflow$get_final_j, 2),
|
|
127
|
+
workflow_body = _workflow$get_final_j2[0],
|
|
128
|
+
body_size = _workflow$get_final_j2[1];
|
|
129
|
+
|
|
130
|
+
return this.send(workflow_body);
|
|
131
|
+
}
|
|
132
|
+
}, {
|
|
133
|
+
key: "send",
|
|
63
134
|
value: function () {
|
|
64
|
-
var
|
|
65
|
-
var headers, content_text, signature, response;
|
|
135
|
+
var _send = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(workflow_body) {
|
|
136
|
+
var headers, content_text, signature, response, ok_response;
|
|
66
137
|
return _regenerator["default"].wrap(function _callee$(_context) {
|
|
67
138
|
while (1) {
|
|
68
139
|
switch (_context.prev = _context.next) {
|
|
69
140
|
case 0:
|
|
70
141
|
headers = this._get_headers();
|
|
71
|
-
content_text = JSON.stringify(
|
|
142
|
+
content_text = JSON.stringify(workflow_body);
|
|
72
143
|
|
|
73
|
-
if (this.
|
|
74
|
-
signature = (0, _signature["default"])(this.url, "POST", content_text, headers, this.
|
|
75
|
-
headers["Authorization"] = "".concat(this.
|
|
144
|
+
if (this.config.auth_enabled) {
|
|
145
|
+
signature = (0, _signature["default"])(this.url, "POST", content_text, headers, this.config.workspace_secret);
|
|
146
|
+
headers["Authorization"] = "".concat(this.config.workspace_key, ":").concat(signature);
|
|
76
147
|
}
|
|
77
148
|
|
|
78
149
|
_context.prev = 3;
|
|
@@ -83,64 +154,58 @@ var Workflow = /*#__PURE__*/function () {
|
|
|
83
154
|
|
|
84
155
|
case 6:
|
|
85
156
|
response = _context.sent;
|
|
157
|
+
ok_response = Math.floor(response.status / 100) == 2;
|
|
158
|
+
|
|
159
|
+
if (!ok_response) {
|
|
160
|
+
_context.next = 12;
|
|
161
|
+
break;
|
|
162
|
+
}
|
|
163
|
+
|
|
86
164
|
return _context.abrupt("return", {
|
|
87
|
-
status_code: response.status,
|
|
88
165
|
success: true,
|
|
166
|
+
status: "success",
|
|
167
|
+
status_code: response.status,
|
|
89
168
|
message: response.statusText
|
|
90
169
|
});
|
|
91
170
|
|
|
92
|
-
case
|
|
93
|
-
_context.
|
|
171
|
+
case 12:
|
|
172
|
+
return _context.abrupt("return", {
|
|
173
|
+
success: false,
|
|
174
|
+
status: "fail",
|
|
175
|
+
status_code: response.status,
|
|
176
|
+
message: response.statusText
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
case 13:
|
|
180
|
+
_context.next = 18;
|
|
181
|
+
break;
|
|
182
|
+
|
|
183
|
+
case 15:
|
|
184
|
+
_context.prev = 15;
|
|
94
185
|
_context.t0 = _context["catch"](3);
|
|
95
186
|
return _context.abrupt("return", {
|
|
96
|
-
status_code: 400,
|
|
97
187
|
success: false,
|
|
188
|
+
status: "fail",
|
|
189
|
+
status_code: _context.t0.status || 500,
|
|
98
190
|
message: _context.t0.message
|
|
99
191
|
});
|
|
100
192
|
|
|
101
|
-
case
|
|
193
|
+
case 18:
|
|
102
194
|
case "end":
|
|
103
195
|
return _context.stop();
|
|
104
196
|
}
|
|
105
197
|
}
|
|
106
|
-
}, _callee, this, [[3,
|
|
198
|
+
}, _callee, this, [[3, 15]]);
|
|
107
199
|
}));
|
|
108
200
|
|
|
109
|
-
function
|
|
110
|
-
return
|
|
201
|
+
function send(_x) {
|
|
202
|
+
return _send.apply(this, arguments);
|
|
111
203
|
}
|
|
112
204
|
|
|
113
|
-
return
|
|
205
|
+
return send;
|
|
114
206
|
}()
|
|
115
|
-
}, {
|
|
116
|
-
key: "validate_data",
|
|
117
|
-
value: function validate_data() {
|
|
118
|
-
var _this$data;
|
|
119
|
-
|
|
120
|
-
if (!((_this$data = this.data) !== null && _this$data !== void 0 && _this$data.data)) {
|
|
121
|
-
this.data.data = {};
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
if (!(this.data.data instanceof Object)) {
|
|
125
|
-
throw new _utils.SuprsendError("data must be a object");
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
var schema = workflow_schema;
|
|
129
|
-
var v = new _jsonschema.Validator();
|
|
130
|
-
var validated_data = v.validate(this.data, schema);
|
|
131
|
-
|
|
132
|
-
if (validated_data.valid) {
|
|
133
|
-
return this.data;
|
|
134
|
-
} else {
|
|
135
|
-
var error_obj = validated_data.errors[0];
|
|
136
|
-
var error_msg = "".concat(error_obj.property, " ").concat(error_obj.message);
|
|
137
|
-
throw new _utils.SuprsendError(error_msg);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
207
|
}]);
|
|
141
|
-
return
|
|
208
|
+
return _WorkflowTrigger;
|
|
142
209
|
}();
|
|
143
210
|
|
|
144
|
-
|
|
145
|
-
exports["default"] = _default;
|
|
146
|
-
module.exports = exports.default;
|
|
211
|
+
exports._WorkflowTrigger = _WorkflowTrigger;
|
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.BulkWorkflowsFactory = void 0;
|
|
9
|
+
|
|
10
|
+
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
|
|
11
|
+
|
|
12
|
+
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
|
13
|
+
|
|
14
|
+
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
15
|
+
|
|
16
|
+
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
|
|
17
|
+
|
|
18
|
+
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
|
19
|
+
|
|
20
|
+
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
|
21
|
+
|
|
22
|
+
var _constants = require("./constants");
|
|
23
|
+
|
|
24
|
+
var _utils = require("./utils");
|
|
25
|
+
|
|
26
|
+
var _workflow = _interopRequireDefault(require("./workflow"));
|
|
27
|
+
|
|
28
|
+
var _lodash = require("lodash");
|
|
29
|
+
|
|
30
|
+
var _axios = _interopRequireDefault(require("axios"));
|
|
31
|
+
|
|
32
|
+
var _bulk_response = _interopRequireDefault(require("./bulk_response"));
|
|
33
|
+
|
|
34
|
+
var _signature = _interopRequireDefault(require("./signature"));
|
|
35
|
+
|
|
36
|
+
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
|
|
37
|
+
|
|
38
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
39
|
+
|
|
40
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
41
|
+
|
|
42
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
43
|
+
|
|
44
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
45
|
+
|
|
46
|
+
var BulkWorkflowsFactory = /*#__PURE__*/function () {
|
|
47
|
+
function BulkWorkflowsFactory(config) {
|
|
48
|
+
(0, _classCallCheck2["default"])(this, BulkWorkflowsFactory);
|
|
49
|
+
this.config = config;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
(0, _createClass2["default"])(BulkWorkflowsFactory, [{
|
|
53
|
+
key: "new_instance",
|
|
54
|
+
value: function new_instance() {
|
|
55
|
+
return new BulkWorkflows(this.config);
|
|
56
|
+
}
|
|
57
|
+
}]);
|
|
58
|
+
return BulkWorkflowsFactory;
|
|
59
|
+
}();
|
|
60
|
+
|
|
61
|
+
exports.BulkWorkflowsFactory = BulkWorkflowsFactory;
|
|
62
|
+
|
|
63
|
+
var _BulkWorkflowsChunk = /*#__PURE__*/function () {
|
|
64
|
+
function _BulkWorkflowsChunk(config) {
|
|
65
|
+
(0, _classCallCheck2["default"])(this, _BulkWorkflowsChunk);
|
|
66
|
+
this.config = config;
|
|
67
|
+
this.__chunk = [];
|
|
68
|
+
this.__url = this.__get_url();
|
|
69
|
+
this.__headers = this.__common_headers();
|
|
70
|
+
this.__running_size = 0;
|
|
71
|
+
this.__running_length = 0;
|
|
72
|
+
this.response;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
(0, _createClass2["default"])(_BulkWorkflowsChunk, [{
|
|
76
|
+
key: "__get_url",
|
|
77
|
+
value: function __get_url() {
|
|
78
|
+
var url_template = "/trigger/";
|
|
79
|
+
|
|
80
|
+
if (this.config.include_signature_param) {
|
|
81
|
+
if (this.config.auth_enabled) {
|
|
82
|
+
url_template = url_template + "?verify=true";
|
|
83
|
+
} else {
|
|
84
|
+
url_template = url_template + "?verify=false";
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
var url_formatted = "".concat(this.config.base_url).concat(this.config.workspace_key).concat(url_template);
|
|
89
|
+
return url_formatted;
|
|
90
|
+
}
|
|
91
|
+
}, {
|
|
92
|
+
key: "__common_headers",
|
|
93
|
+
value: function __common_headers() {
|
|
94
|
+
return {
|
|
95
|
+
"Content-Type": "application/json; charset=utf-8",
|
|
96
|
+
"User-Agent": this.config.user_agent
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
}, {
|
|
100
|
+
key: "__dynamic_headers",
|
|
101
|
+
value: function __dynamic_headers() {
|
|
102
|
+
return {
|
|
103
|
+
Date: new Date().toUTCString()
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
}, {
|
|
107
|
+
key: "__add_body_to_chunk",
|
|
108
|
+
value: function __add_body_to_chunk(body, body_size) {
|
|
109
|
+
// First add size, then body to reduce effects of race condition
|
|
110
|
+
this.__running_size += body_size;
|
|
111
|
+
|
|
112
|
+
this.__chunk.push(body);
|
|
113
|
+
|
|
114
|
+
this.__running_length += 1;
|
|
115
|
+
}
|
|
116
|
+
}, {
|
|
117
|
+
key: "__check_limit_reached",
|
|
118
|
+
value: function __check_limit_reached() {
|
|
119
|
+
if (this.__running_length >= _constants.MAX_WORKFLOWS_IN_BULK_API || this.__running_size >= _constants.BODY_MAX_APPARENT_SIZE_IN_BYTES) {
|
|
120
|
+
return true;
|
|
121
|
+
} else {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}, {
|
|
126
|
+
key: "try_to_add_into_chunk",
|
|
127
|
+
value: function try_to_add_into_chunk(body, body_size) {
|
|
128
|
+
if (!body) {
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (this.__check_limit_reached()) {
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (body_size > _constants.BODY_MAX_APPARENT_SIZE_IN_BYTES) {
|
|
137
|
+
throw new _utils.SuprsendError("workflow body (discounting attachment if any) too big - ".concat(body_size, " Bytes, must not cross ").concat(_constants.BODY_MAX_APPARENT_SIZE_IN_BYTES_READABLE));
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (this.__running_size + body_size > _constants.BODY_MAX_APPARENT_SIZE_IN_BYTES) {
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (!_constants.ALLOW_ATTACHMENTS_IN_BULK_API) {
|
|
145
|
+
delete body.data["$attachments"];
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
this.__add_body_to_chunk(body, body_size);
|
|
149
|
+
|
|
150
|
+
return true;
|
|
151
|
+
}
|
|
152
|
+
}, {
|
|
153
|
+
key: "trigger",
|
|
154
|
+
value: function () {
|
|
155
|
+
var _trigger = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() {
|
|
156
|
+
var headers, content_text, signature, response, ok_response, error_status;
|
|
157
|
+
return _regenerator["default"].wrap(function _callee$(_context) {
|
|
158
|
+
while (1) {
|
|
159
|
+
switch (_context.prev = _context.next) {
|
|
160
|
+
case 0:
|
|
161
|
+
headers = _objectSpread(_objectSpread({}, this.__headers), this.__dynamic_headers());
|
|
162
|
+
content_text = JSON.stringify(this.__chunk); // Based on whether signature is required or not, add Authorization header
|
|
163
|
+
|
|
164
|
+
if (this.config.auth_enabled) {
|
|
165
|
+
signature = (0, _signature["default"])(this.__url, "POST", content_text, headers, this.config.workspace_secret);
|
|
166
|
+
headers["Authorization"] = "".concat(this.config.workspace_key, ":").concat(signature);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
_context.prev = 3;
|
|
170
|
+
_context.next = 6;
|
|
171
|
+
return _axios["default"].post(this.__url, content_text, {
|
|
172
|
+
headers: headers
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
case 6:
|
|
176
|
+
response = _context.sent;
|
|
177
|
+
ok_response = Math.floor(response.status / 100) == 2;
|
|
178
|
+
|
|
179
|
+
if (ok_response) {
|
|
180
|
+
this.response = {
|
|
181
|
+
status: "success",
|
|
182
|
+
status_code: response.status,
|
|
183
|
+
total: this.__chunk.length,
|
|
184
|
+
success: this.__chunk.length,
|
|
185
|
+
failure: 0,
|
|
186
|
+
failed_records: []
|
|
187
|
+
};
|
|
188
|
+
} else {
|
|
189
|
+
this.response = {
|
|
190
|
+
status: "fail",
|
|
191
|
+
status_code: response.status,
|
|
192
|
+
total: this.__chunk.length,
|
|
193
|
+
success: 0,
|
|
194
|
+
failure: this.__chunk.length,
|
|
195
|
+
failed_records: this.__chunk.map(function (item) {
|
|
196
|
+
return {
|
|
197
|
+
record: item,
|
|
198
|
+
error: response.statusText,
|
|
199
|
+
code: response.status
|
|
200
|
+
};
|
|
201
|
+
})
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
_context.next = 15;
|
|
206
|
+
break;
|
|
207
|
+
|
|
208
|
+
case 11:
|
|
209
|
+
_context.prev = 11;
|
|
210
|
+
_context.t0 = _context["catch"](3);
|
|
211
|
+
error_status = _context.t0.status || 500;
|
|
212
|
+
return _context.abrupt("return", {
|
|
213
|
+
status: "fail",
|
|
214
|
+
status_code: error_status,
|
|
215
|
+
message: _context.t0.message,
|
|
216
|
+
total: this.__chunk.length,
|
|
217
|
+
success: 0,
|
|
218
|
+
failure: this.__chunk.length,
|
|
219
|
+
failed_records: this.__chunk.map(function (item) {
|
|
220
|
+
return {
|
|
221
|
+
record: item,
|
|
222
|
+
error: _context.t0.message,
|
|
223
|
+
code: error_status
|
|
224
|
+
};
|
|
225
|
+
})
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
case 15:
|
|
229
|
+
case "end":
|
|
230
|
+
return _context.stop();
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}, _callee, this, [[3, 11]]);
|
|
234
|
+
}));
|
|
235
|
+
|
|
236
|
+
function trigger() {
|
|
237
|
+
return _trigger.apply(this, arguments);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
return trigger;
|
|
241
|
+
}()
|
|
242
|
+
}]);
|
|
243
|
+
return _BulkWorkflowsChunk;
|
|
244
|
+
}();
|
|
245
|
+
|
|
246
|
+
var BulkWorkflows = /*#__PURE__*/function () {
|
|
247
|
+
function BulkWorkflows(config) {
|
|
248
|
+
(0, _classCallCheck2["default"])(this, BulkWorkflows);
|
|
249
|
+
this.config = config;
|
|
250
|
+
this.__workflows = [];
|
|
251
|
+
this.__pending_records = [];
|
|
252
|
+
this.chunks = [];
|
|
253
|
+
this.response = new _bulk_response["default"]();
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
(0, _createClass2["default"])(BulkWorkflows, [{
|
|
257
|
+
key: "__validate_workflows",
|
|
258
|
+
value: function __validate_workflows() {
|
|
259
|
+
if (!this.__workflows) {
|
|
260
|
+
throw new _utils.SuprsendError("workflow list is empty in bulk request");
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
var _iterator = _createForOfIteratorHelper(this.__workflows),
|
|
264
|
+
_step;
|
|
265
|
+
|
|
266
|
+
try {
|
|
267
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
268
|
+
var wf = _step.value;
|
|
269
|
+
var is_part_of_bulk = true;
|
|
270
|
+
|
|
271
|
+
var _wf$get_final_json = wf.get_final_json(this.config, is_part_of_bulk),
|
|
272
|
+
_wf$get_final_json2 = (0, _slicedToArray2["default"])(_wf$get_final_json, 2),
|
|
273
|
+
wf_body = _wf$get_final_json2[0],
|
|
274
|
+
body_size = _wf$get_final_json2[1];
|
|
275
|
+
|
|
276
|
+
this.__pending_records.push([wf_body, body_size]);
|
|
277
|
+
}
|
|
278
|
+
} catch (err) {
|
|
279
|
+
_iterator.e(err);
|
|
280
|
+
} finally {
|
|
281
|
+
_iterator.f();
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}, {
|
|
285
|
+
key: "__chunkify",
|
|
286
|
+
value: function __chunkify() {
|
|
287
|
+
var start_idx = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
|
|
288
|
+
var curr_chunk = new _BulkWorkflowsChunk(this.config);
|
|
289
|
+
this.chunks.push(curr_chunk);
|
|
290
|
+
|
|
291
|
+
var entries = this.__pending_records.slice(start_idx).entries();
|
|
292
|
+
|
|
293
|
+
var _iterator2 = _createForOfIteratorHelper(entries),
|
|
294
|
+
_step2;
|
|
295
|
+
|
|
296
|
+
try {
|
|
297
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
298
|
+
var _step2$value = (0, _slicedToArray2["default"])(_step2.value, 2),
|
|
299
|
+
rel_idx = _step2$value[0],
|
|
300
|
+
rec = _step2$value[1];
|
|
301
|
+
|
|
302
|
+
var is_added = curr_chunk.try_to_add_into_chunk(rec[0], rec[1]);
|
|
303
|
+
|
|
304
|
+
if (!is_added) {
|
|
305
|
+
// create chunks from remaining records
|
|
306
|
+
this.__chunkify(start_idx + rel_idx); // Don't forget to break. As current loop must not continue further
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
break;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
} catch (err) {
|
|
313
|
+
_iterator2.e(err);
|
|
314
|
+
} finally {
|
|
315
|
+
_iterator2.f();
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
}, {
|
|
319
|
+
key: "append",
|
|
320
|
+
value: function append() {
|
|
321
|
+
for (var _len = arguments.length, workflows = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
322
|
+
workflows[_key] = arguments[_key];
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
if (!workflows) {
|
|
326
|
+
throw new _utils.SuprsendError("workflow list empty. must pass one or more valid workflow instances");
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
for (var _i = 0, _workflows = workflows; _i < _workflows.length; _i++) {
|
|
330
|
+
var wf = _workflows[_i];
|
|
331
|
+
|
|
332
|
+
if (!wf) {
|
|
333
|
+
throw new _utils.SuprsendError("null/empty element found in bulk instance");
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
if (!(wf instanceof _workflow["default"])) {
|
|
337
|
+
throw new _utils.SuprsendError("element must be an instance of suprsend.Workflow");
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
var wf_copy = (0, _lodash.cloneDeep)(wf);
|
|
341
|
+
|
|
342
|
+
this.__workflows.push(wf_copy);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
}, {
|
|
346
|
+
key: "trigger",
|
|
347
|
+
value: function () {
|
|
348
|
+
var _trigger2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2() {
|
|
349
|
+
var _iterator3, _step3, _step3$value, c_idx, ch;
|
|
350
|
+
|
|
351
|
+
return _regenerator["default"].wrap(function _callee2$(_context2) {
|
|
352
|
+
while (1) {
|
|
353
|
+
switch (_context2.prev = _context2.next) {
|
|
354
|
+
case 0:
|
|
355
|
+
this.__validate_workflows();
|
|
356
|
+
|
|
357
|
+
this.__chunkify();
|
|
358
|
+
|
|
359
|
+
_iterator3 = _createForOfIteratorHelper(this.chunks.entries());
|
|
360
|
+
_context2.prev = 3;
|
|
361
|
+
|
|
362
|
+
_iterator3.s();
|
|
363
|
+
|
|
364
|
+
case 5:
|
|
365
|
+
if ((_step3 = _iterator3.n()).done) {
|
|
366
|
+
_context2.next = 13;
|
|
367
|
+
break;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
_step3$value = (0, _slicedToArray2["default"])(_step3.value, 2), c_idx = _step3$value[0], ch = _step3$value[1];
|
|
371
|
+
|
|
372
|
+
if (this.config.req_log_level > 0) {
|
|
373
|
+
console.log("DEBUG: triggering api call for chunk: ".concat(c_idx));
|
|
374
|
+
} // do api call
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
_context2.next = 10;
|
|
378
|
+
return ch.trigger();
|
|
379
|
+
|
|
380
|
+
case 10:
|
|
381
|
+
// merge response
|
|
382
|
+
this.response.merge_chunk_response(ch.response);
|
|
383
|
+
|
|
384
|
+
case 11:
|
|
385
|
+
_context2.next = 5;
|
|
386
|
+
break;
|
|
387
|
+
|
|
388
|
+
case 13:
|
|
389
|
+
_context2.next = 18;
|
|
390
|
+
break;
|
|
391
|
+
|
|
392
|
+
case 15:
|
|
393
|
+
_context2.prev = 15;
|
|
394
|
+
_context2.t0 = _context2["catch"](3);
|
|
395
|
+
|
|
396
|
+
_iterator3.e(_context2.t0);
|
|
397
|
+
|
|
398
|
+
case 18:
|
|
399
|
+
_context2.prev = 18;
|
|
400
|
+
|
|
401
|
+
_iterator3.f();
|
|
402
|
+
|
|
403
|
+
return _context2.finish(18);
|
|
404
|
+
|
|
405
|
+
case 21:
|
|
406
|
+
return _context2.abrupt("return", this.response);
|
|
407
|
+
|
|
408
|
+
case 22:
|
|
409
|
+
case "end":
|
|
410
|
+
return _context2.stop();
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
}, _callee2, this, [[3, 15, 18, 21]]);
|
|
414
|
+
}));
|
|
415
|
+
|
|
416
|
+
function trigger() {
|
|
417
|
+
return _trigger2.apply(this, arguments);
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
return trigger;
|
|
421
|
+
}()
|
|
422
|
+
}]);
|
|
423
|
+
return BulkWorkflows;
|
|
424
|
+
}();
|