@webuildbots/webuildbots-sdk 11.1.1 → 11.2.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.
@@ -46,100 +46,145 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
46
46
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
47
  }
48
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
+ };
49
60
  var __importDefault = (this && this.__importDefault) || function (mod) {
50
61
  return (mod && mod.__esModule) ? mod : { "default": mod };
51
62
  };
52
63
  Object.defineProperty(exports, "__esModule", { value: true });
53
64
  var __1 = require("..");
65
+ var block_enums_1 = require("../const/block-enums");
66
+ var form_data_builder_1 = require("../util/form-data-builder");
54
67
  var ApiService_1 = __importDefault(require("./ApiService"));
55
68
  var ResponseService = /** @class */ (function () {
56
69
  function ResponseService(token, client, rootURL) {
57
70
  this.apiService = new ApiService_1.default(token, rootURL);
58
71
  this.client = client;
59
72
  }
60
- ResponseService.prototype.createResponse = function (title, module, description, type, name, group, variable, formFieldCallbackArgs, next) {
61
- if (name === void 0) { name = __1.BlockBuilder.DEFAULT_NAME; }
62
- if (group === void 0) { group = __1.BlockBuilder.DEFAULT_GROUP; }
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;
63
90
  return __awaiter(this, void 0, void 0, function () {
64
- var block, res, options, e_1;
65
- var _a;
66
- return __generator(this, function (_b) {
67
- switch (_b.label) {
91
+ var block, builder, formatedBody, blockValues, payload, _b, res, options, e_1;
92
+ return __generator(this, function (_c) {
93
+ switch (_c.label) {
68
94
  case 0:
69
95
  block = {
70
- name: name,
71
- type: type,
72
- group: group,
73
96
  version: __1.BlockBuilder.VERSION,
74
- created: new Date(),
75
- content: {
76
- title: (_a = {}, _a[title.language] = title.value, _a)
77
- },
97
+ created: new Date().toISOString(),
98
+ content: content,
99
+ type: type,
78
100
  module: module,
79
101
  meta: {
80
102
  description: description
81
103
  },
82
- variables: [__assign({}, variable)],
104
+ name: name,
83
105
  formFieldCallback: formFieldCallbackArgs,
106
+ function: functionObj,
107
+ group: group,
84
108
  next: next
85
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;
86
126
  options = {
87
127
  method: 'POST',
88
- body: block,
128
+ data: payload,
89
129
  headers: { 'content-type': 'multipart/form-data' }
90
130
  };
91
- _b.label = 1;
92
- case 1:
93
- _b.trys.push([1, 3, , 4]);
131
+ _c.label = 5;
132
+ case 5:
133
+ _c.trys.push([5, 7, , 8]);
94
134
  return [4 /*yield*/, this.apiService.hitApi('api/agent/block', this.client, options)];
95
- case 2:
96
- res = _b.sent();
97
- return [3 /*break*/, 4];
98
- case 3:
99
- e_1 = _b.sent();
135
+ case 6:
136
+ res = _c.sent();
137
+ return [3 /*break*/, 8];
138
+ case 7:
139
+ e_1 = _c.sent();
100
140
  throw Error(e_1);
101
- case 4: return [2 /*return*/, res];
141
+ case 8: return [2 /*return*/, res];
102
142
  }
103
143
  });
104
144
  });
105
145
  };
106
- ResponseService.prototype.updateResponse = function (blockId, title, module, description, type, name, group, variable, formFieldCallbackArgs, next) {
146
+ ResponseService.prototype.updateResponse = function (blockId, body) {
147
+ var _a;
107
148
  return __awaiter(this, void 0, void 0, function () {
108
- var res, options, e_2;
109
- var _a;
110
- return __generator(this, function (_b) {
111
- switch (_b.label) {
112
- case 0:
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;
113
172
  options = {
114
173
  method: 'PUT',
115
- body: {
116
- name: name,
117
- type: type,
118
- group: group,
119
- content: {
120
- title: (_a = {}, _a[title.language] = title.value, _a)
121
- },
122
- module: module,
123
- meta: {
124
- description: description
125
- },
126
- variables: [__assign({}, variable)],
127
- formFieldCallback: formFieldCallbackArgs,
128
- next: next
129
- },
130
- headers: { 'content-type': 'multipart/form-data' }
174
+ data: payload,
175
+ headers: { 'Content-Type': 'multipart/form-data' }
131
176
  };
132
- _b.label = 1;
133
- case 1:
134
- _b.trys.push([1, 3, , 4]);
177
+ _c.label = 6;
178
+ case 6:
179
+ _c.trys.push([6, 8, , 9]);
135
180
  return [4 /*yield*/, this.apiService.hitApi('api/agent/block/' + blockId, this.client, options)];
136
- case 2:
137
- res = _b.sent();
138
- return [3 /*break*/, 4];
139
- case 3:
140
- e_2 = _b.sent();
181
+ case 7:
182
+ res = _c.sent();
183
+ return [3 /*break*/, 9];
184
+ case 8:
185
+ e_2 = _c.sent();
141
186
  throw Error(e_2);
142
- case 4: return [2 /*return*/, res];
187
+ case 9: return [2 /*return*/, res];
143
188
  }
144
189
  });
145
190
  });
@@ -173,41 +218,46 @@ var ResponseService = /** @class */ (function () {
173
218
  if (types === void 0) { types = []; }
174
219
  if (groups === void 0) { groups = []; }
175
220
  return __awaiter(this, void 0, void 0, function () {
176
- var res, query, _i, types_1, type, _a, groups_1, group, options, e_4;
177
- return __generator(this, function (_b) {
178
- switch (_b.label) {
221
+ var res, options, e_4;
222
+ return __generator(this, function (_a) {
223
+ switch (_a.label) {
179
224
  case 0:
180
- query = "?searchText=" + searchText;
181
- if (types.length) {
182
- for (_i = 0, types_1 = types; _i < types_1.length; _i++) {
183
- type = types_1[_i];
184
- query + ("&type[]=" + type);
185
- }
186
- }
187
- if (groups.length) {
188
- for (_a = 0, groups_1 = groups; _a < groups_1.length; _a++) {
189
- group = groups_1[_a];
190
- query + ("&group[]=" + group);
191
- }
192
- }
193
225
  options = {
194
- method: 'GET'
226
+ method: 'GET',
227
+ params: {
228
+ searchText: searchText,
229
+ types: types,
230
+ groups: groups
231
+ }
195
232
  };
196
- _b.label = 1;
233
+ _a.label = 1;
197
234
  case 1:
198
- _b.trys.push([1, 3, , 4]);
199
- return [4 /*yield*/, this.apiService.hitApi('api/agent/block/' + query, this.client, options)];
235
+ _a.trys.push([1, 3, , 4]);
236
+ return [4 /*yield*/, this.apiService.hitApi('api/agent/block', this.client, options)];
200
237
  case 2:
201
- res = _b.sent();
238
+ res = _a.sent();
202
239
  return [3 /*break*/, 4];
203
240
  case 3:
204
- e_4 = _b.sent();
241
+ e_4 = _a.sent();
205
242
  throw Error(e_4);
206
243
  case 4: return [2 /*return*/, res];
207
244
  }
208
245
  });
209
246
  });
210
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
+ };
211
261
  return ResponseService;
212
262
  }());
213
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.1",
3
+ "version": "11.2.0",
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",