@webuildbots/webuildbots-sdk 11.1.2 → 11.2.1

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.
@@ -0,0 +1,263 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (_) try {
29
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
49
+ var __rest = (this && this.__rest) || function (s, e) {
50
+ var t = {};
51
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
52
+ t[p] = s[p];
53
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
54
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
55
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
56
+ t[p[i]] = s[p[i]];
57
+ }
58
+ return t;
59
+ };
60
+ var __importDefault = (this && this.__importDefault) || function (mod) {
61
+ return (mod && mod.__esModule) ? mod : { "default": mod };
62
+ };
63
+ Object.defineProperty(exports, "__esModule", { value: true });
64
+ var __1 = require("..");
65
+ var block_enums_1 = require("../const/block-enums");
66
+ var form_data_builder_1 = require("../util/form-data-builder");
67
+ var ApiService_1 = __importDefault(require("./ApiService"));
68
+ var ResponseService = /** @class */ (function () {
69
+ function ResponseService(token, client, rootURL) {
70
+ this.apiService = new ApiService_1.default(token, rootURL);
71
+ this.client = client;
72
+ }
73
+ ResponseService.prototype.formatBlockValues = function (block) {
74
+ var _a;
75
+ var functionObject = block.function;
76
+ var allFormValuesFlag = functionObject &&
77
+ (functionObject.useAllFormValues ||
78
+ functionObject.type === block_enums_1.FunctionTypes.LOGIC_BLOCK);
79
+ if ((_a = block === null || block === void 0 ? void 0 : block.function) === null || _a === void 0 ? void 0 : _a.name) {
80
+ var _b = block.function, name_1 = _b.name, otherValues = __rest(_b, ["name"]);
81
+ return __assign(__assign({}, block), { function: __assign({ name: name_1, useAllFormValues: allFormValuesFlag }, otherValues) });
82
+ }
83
+ if (functionObject) {
84
+ return __assign(__assign({}, block), { function: __assign(__assign({}, block.function), { useAllFormValues: allFormValuesFlag }) });
85
+ }
86
+ return block;
87
+ };
88
+ ResponseService.prototype.createResponse = function (content, module, description, type, name, group, formFieldCallbackArgs, next, functionObj) {
89
+ var _a;
90
+ return __awaiter(this, void 0, void 0, function () {
91
+ var block, builder, formatedBody, blockValues, payload, _b, res, options, e_1;
92
+ return __generator(this, function (_c) {
93
+ switch (_c.label) {
94
+ case 0:
95
+ block = {
96
+ version: __1.BlockBuilder.VERSION,
97
+ created: new Date().toISOString(),
98
+ content: content,
99
+ type: type,
100
+ module: module,
101
+ meta: {
102
+ description: description
103
+ },
104
+ name: name,
105
+ formFieldCallback: formFieldCallbackArgs,
106
+ function: functionObj,
107
+ group: group,
108
+ next: next
109
+ };
110
+ builder = form_data_builder_1.FormDataBuilder();
111
+ formatedBody = __assign(__assign({}, block), { content: block.content ? block.content : {}, name: block.name ? block.name : __1.BlockBuilder.DEFAULT_NAME, group: block.group ? block.group : __1.BlockBuilder.DEFAULT_GROUP });
112
+ blockValues = block.function
113
+ ? this.formatBlockValues(formatedBody)
114
+ : formatedBody;
115
+ if (!((_a = block.content) === null || _a === void 0 ? void 0 : _a.items)) return [3 /*break*/, 2];
116
+ return [4 /*yield*/, builder.buildItems(blockValues)];
117
+ case 1:
118
+ _b = _c.sent();
119
+ return [3 /*break*/, 4];
120
+ case 2: return [4 /*yield*/, builder.buildBlock(blockValues)];
121
+ case 3:
122
+ _b = _c.sent();
123
+ _c.label = 4;
124
+ case 4:
125
+ payload = _b;
126
+ options = {
127
+ method: 'POST',
128
+ data: payload,
129
+ headers: { 'content-type': 'multipart/form-data' }
130
+ };
131
+ _c.label = 5;
132
+ case 5:
133
+ _c.trys.push([5, 7, , 8]);
134
+ return [4 /*yield*/, this.apiService.hitApi('api/agent/block', this.client, options)];
135
+ case 6:
136
+ res = _c.sent();
137
+ return [3 /*break*/, 8];
138
+ case 7:
139
+ e_1 = _c.sent();
140
+ throw Error(e_1);
141
+ case 8: return [2 /*return*/, res];
142
+ }
143
+ });
144
+ });
145
+ };
146
+ ResponseService.prototype.updateResponse = function (blockId, body) {
147
+ var _a;
148
+ return __awaiter(this, void 0, void 0, function () {
149
+ var oldResponse, builder, content, formatedBody, blockValues, payload, _b, res, options, e_2;
150
+ return __generator(this, function (_c) {
151
+ switch (_c.label) {
152
+ case 0: return [4 /*yield*/, this.getResponse(blockId)];
153
+ case 1:
154
+ oldResponse = _c.sent();
155
+ builder = form_data_builder_1.FormDataBuilder();
156
+ content = body.content ? body.content : {};
157
+ formatedBody = __assign(__assign({}, body), { _id: blockId, created: body.created || oldResponse[0].created, version: body.version || oldResponse[0].version, type: body.type || oldResponse[0].type, content: content });
158
+ blockValues = body.function
159
+ ? this.formatBlockValues(formatedBody)
160
+ : formatedBody;
161
+ if (!((_a = body.content) === null || _a === void 0 ? void 0 : _a.items)) return [3 /*break*/, 3];
162
+ return [4 /*yield*/, builder.buildItems(blockValues)];
163
+ case 2:
164
+ _b = _c.sent();
165
+ return [3 /*break*/, 5];
166
+ case 3: return [4 /*yield*/, builder.buildBlock(blockValues)];
167
+ case 4:
168
+ _b = _c.sent();
169
+ _c.label = 5;
170
+ case 5:
171
+ payload = _b;
172
+ options = {
173
+ method: 'PUT',
174
+ data: payload,
175
+ headers: { 'Content-Type': 'multipart/form-data' }
176
+ };
177
+ _c.label = 6;
178
+ case 6:
179
+ _c.trys.push([6, 8, , 9]);
180
+ return [4 /*yield*/, this.apiService.hitApi('api/agent/block/' + blockId, this.client, options)];
181
+ case 7:
182
+ res = _c.sent();
183
+ return [3 /*break*/, 9];
184
+ case 8:
185
+ e_2 = _c.sent();
186
+ throw Error(e_2);
187
+ case 9: return [2 /*return*/, res];
188
+ }
189
+ });
190
+ });
191
+ };
192
+ ResponseService.prototype.deleteResponse = function (blockId) {
193
+ return __awaiter(this, void 0, void 0, function () {
194
+ var res, options, e_3;
195
+ return __generator(this, function (_a) {
196
+ switch (_a.label) {
197
+ case 0:
198
+ options = {
199
+ method: 'DELETE'
200
+ };
201
+ _a.label = 1;
202
+ case 1:
203
+ _a.trys.push([1, 3, , 4]);
204
+ return [4 /*yield*/, this.apiService.hitApi('api/agent/block/' + blockId, this.client, options)];
205
+ case 2:
206
+ res = _a.sent();
207
+ return [3 /*break*/, 4];
208
+ case 3:
209
+ e_3 = _a.sent();
210
+ throw Error(e_3);
211
+ case 4: return [2 /*return*/, res];
212
+ }
213
+ });
214
+ });
215
+ };
216
+ ResponseService.prototype.getResponses = function (searchText, types, groups) {
217
+ if (searchText === void 0) { searchText = ''; }
218
+ if (types === void 0) { types = []; }
219
+ if (groups === void 0) { groups = []; }
220
+ return __awaiter(this, void 0, void 0, function () {
221
+ var res, options, e_4;
222
+ return __generator(this, function (_a) {
223
+ switch (_a.label) {
224
+ case 0:
225
+ options = {
226
+ method: 'GET',
227
+ params: {
228
+ searchText: searchText,
229
+ types: types,
230
+ groups: groups
231
+ }
232
+ };
233
+ _a.label = 1;
234
+ case 1:
235
+ _a.trys.push([1, 3, , 4]);
236
+ return [4 /*yield*/, this.apiService.hitApi('api/agent/block', this.client, options)];
237
+ case 2:
238
+ res = _a.sent();
239
+ return [3 /*break*/, 4];
240
+ case 3:
241
+ e_4 = _a.sent();
242
+ throw Error(e_4);
243
+ case 4: return [2 /*return*/, res];
244
+ }
245
+ });
246
+ });
247
+ };
248
+ ResponseService.prototype.getResponse = function (id) {
249
+ return __awaiter(this, void 0, void 0, function () {
250
+ var allResponses;
251
+ return __generator(this, function (_a) {
252
+ switch (_a.label) {
253
+ case 0: return [4 /*yield*/, this.getResponses()];
254
+ case 1:
255
+ allResponses = _a.sent();
256
+ return [2 /*return*/, allResponses.data.filter(function (response) { return response._id == id; })];
257
+ }
258
+ });
259
+ });
260
+ };
261
+ return ResponseService;
262
+ }());
263
+ exports.default = ResponseService;
@@ -0,0 +1,9 @@
1
+ import BlockObj from '../interfaces/block';
2
+ import { BlockFormState } from '../interfaces/block-form-state';
3
+ import FormData from 'form-data';
4
+ export declare const FormDataBuilder: () => {
5
+ buildSingleImageForm: (obj: any, shouldUpdateAvatar?: boolean) => Promise<FormData>;
6
+ buildBlock: (blk: BlockObj | BlockFormState | any) => Promise<FormData>;
7
+ buildItems: (blk: BlockObj | BlockFormState) => Promise<FormData>;
8
+ buildSettings: (settings: any) => Promise<FormData>;
9
+ };
@@ -0,0 +1,183 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (_) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ var __importDefault = (this && this.__importDefault) || function (mod) {
39
+ return (mod && mod.__esModule) ? mod : { "default": mod };
40
+ };
41
+ Object.defineProperty(exports, "__esModule", { value: true });
42
+ exports.FormDataBuilder = void 0;
43
+ var json_stringify_safe_1 = __importDefault(require("json-stringify-safe"));
44
+ var form_data_1 = __importDefault(require("form-data"));
45
+ var FormDataBuilder = function () {
46
+ var formData = new form_data_1.default();
47
+ var createBlob = function (imgData) { return __awaiter(void 0, void 0, void 0, function () {
48
+ return __generator(this, function (_a) {
49
+ switch (_a.label) {
50
+ case 0:
51
+ if (!(typeof imgData === 'string')) return [3 /*break*/, 2];
52
+ return [4 /*yield*/, fetch(imgData)];
53
+ case 1: return [2 /*return*/, (_a.sent()).blob()];
54
+ case 2: return [2 /*return*/, imgData];
55
+ }
56
+ });
57
+ }); };
58
+ var appendBlob = function (key, obj) { return __awaiter(void 0, void 0, void 0, function () {
59
+ var blob;
60
+ var _a;
61
+ return __generator(this, function (_b) {
62
+ switch (_b.label) {
63
+ case 0:
64
+ if (!obj.imageBlob) return [3 /*break*/, 3];
65
+ return [4 /*yield*/, createBlob(obj.imageBlob)];
66
+ case 1:
67
+ blob = _b.sent();
68
+ return [4 /*yield*/, formData.append(key, blob)];
69
+ case 2:
70
+ _b.sent();
71
+ _b.label = 3;
72
+ case 3:
73
+ if (!((_a = obj.media) === null || _a === void 0 ? void 0 : _a.mediaFiles)) return [3 /*break*/, 5];
74
+ // append media files
75
+ return [4 /*yield*/, formData.append(key, obj.media.mediaFiles[0])];
76
+ case 4:
77
+ // append media files
78
+ _b.sent();
79
+ _b.label = 5;
80
+ case 5: return [2 /*return*/];
81
+ }
82
+ });
83
+ }); };
84
+ var buildSingleImageForm = function (obj, shouldUpdateAvatar) {
85
+ if (shouldUpdateAvatar === void 0) { shouldUpdateAvatar = true; }
86
+ return __awaiter(void 0, void 0, void 0, function () {
87
+ return __generator(this, function (_a) {
88
+ switch (_a.label) {
89
+ case 0:
90
+ if (!shouldUpdateAvatar) return [3 /*break*/, 2];
91
+ return [4 /*yield*/, appendBlob('upload', obj)];
92
+ case 1:
93
+ _a.sent();
94
+ _a.label = 2;
95
+ case 2:
96
+ delete obj.imageBlob;
97
+ formData.append('body', json_stringify_safe_1.default(obj));
98
+ return [2 /*return*/, formData];
99
+ }
100
+ });
101
+ });
102
+ };
103
+ var buildSettings = function (settings) { return __awaiter(void 0, void 0, void 0, function () {
104
+ var blob, blob;
105
+ return __generator(this, function (_a) {
106
+ switch (_a.label) {
107
+ case 0:
108
+ if (!settings.company.iconImageBlob) return [3 /*break*/, 3];
109
+ return [4 /*yield*/, createBlob(settings.company.iconImageBlob)];
110
+ case 1:
111
+ blob = _a.sent();
112
+ return [4 /*yield*/, formData.append('uploadIcon', blob)];
113
+ case 2:
114
+ _a.sent();
115
+ delete settings.company.iconImageBlob;
116
+ _a.label = 3;
117
+ case 3:
118
+ if (!settings.company.logoImageBlob) return [3 /*break*/, 6];
119
+ return [4 /*yield*/, createBlob(settings.company.logoImageBlob)];
120
+ case 4:
121
+ blob = _a.sent();
122
+ return [4 /*yield*/, formData.append('uploadLogo', blob)];
123
+ case 5:
124
+ _a.sent();
125
+ delete settings.company.logoImageBlob;
126
+ _a.label = 6;
127
+ case 6:
128
+ formData.append('body', json_stringify_safe_1.default(settings));
129
+ return [2 /*return*/, formData];
130
+ }
131
+ });
132
+ }); };
133
+ var buildBlock = function (blk) { return __awaiter(void 0, void 0, void 0, function () {
134
+ var content;
135
+ return __generator(this, function (_a) {
136
+ switch (_a.label) {
137
+ case 0:
138
+ content = blk.content;
139
+ return [4 /*yield*/, appendBlob('mediaFile', content)];
140
+ case 1:
141
+ _a.sent();
142
+ formData.append('body', json_stringify_safe_1.default(blk));
143
+ return [2 /*return*/, formData];
144
+ }
145
+ });
146
+ }); };
147
+ var buildItems = function (blk) { return __awaiter(void 0, void 0, void 0, function () {
148
+ var content, _a, _b, _i, i, item;
149
+ return __generator(this, function (_c) {
150
+ switch (_c.label) {
151
+ case 0:
152
+ content = blk.content;
153
+ _a = [];
154
+ for (_b in content.items)
155
+ _a.push(_b);
156
+ _i = 0;
157
+ _c.label = 1;
158
+ case 1:
159
+ if (!(_i < _a.length)) return [3 /*break*/, 4];
160
+ i = _a[_i];
161
+ if (!content.items.hasOwnProperty(i)) return [3 /*break*/, 3];
162
+ item = content.items[i];
163
+ return [4 /*yield*/, appendBlob("uploadItemMedia" + i, item)];
164
+ case 2:
165
+ _c.sent();
166
+ _c.label = 3;
167
+ case 3:
168
+ _i++;
169
+ return [3 /*break*/, 1];
170
+ case 4:
171
+ formData.append('body', json_stringify_safe_1.default(blk));
172
+ return [2 /*return*/, formData];
173
+ }
174
+ });
175
+ }); };
176
+ return {
177
+ buildSingleImageForm: buildSingleImageForm,
178
+ buildBlock: buildBlock,
179
+ buildItems: buildItems,
180
+ buildSettings: buildSettings
181
+ };
182
+ };
183
+ exports.FormDataBuilder = FormDataBuilder;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webuildbots/webuildbots-sdk",
3
- "version": "11.1.2",
3
+ "version": "11.2.1",
4
4
  "description": "webuildbots sdk",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -53,10 +53,11 @@
53
53
  },
54
54
  "dependencies": {
55
55
  "@types/json-logic-js": "^1.2.1",
56
+ "axios": "^1.3.5",
56
57
  "bson-objectid": "^1.3.1",
57
- "cross-fetch": "^3.1.5",
58
58
  "deep-equal": "^2.0.4",
59
59
  "flatted": "^3.1.0",
60
+ "form-data": "^3.0.1",
60
61
  "http-status-codes": "^2.1.4",
61
62
  "jsonwebtoken": "^8.5.1",
62
63
  "md5": "^2.3.0",