d5-api-initializer 0.0.2-alpha.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.
@@ -0,0 +1,801 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
+ var __publicField = (obj, key, value) => {
4
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
+ return value;
6
+ };
7
+
8
+ // src/classes/ApiHttpResponse.ts
9
+ var ApiHttpResponse = class {
10
+ constructor(jsWapiHttpResponse) {
11
+ __publicField(this, "_jsWapiHttpResponse");
12
+ this._jsWapiHttpResponse = jsWapiHttpResponse;
13
+ }
14
+ get statusCode() {
15
+ return this._jsWapiHttpResponse.getStatusCode();
16
+ }
17
+ get body() {
18
+ return this._jsWapiHttpResponse.getBody();
19
+ }
20
+ get bodyAsStream() {
21
+ return this._jsWapiHttpResponse.getBodyAsInputStream();
22
+ }
23
+ getHeader(name) {
24
+ return this._jsWapiHttpResponse.getHeader(name);
25
+ }
26
+ isSuccessful() {
27
+ return this._jsWapiHttpResponse.isSuccessful();
28
+ }
29
+ };
30
+
31
+ // src/classes/ApiHttpRequest.ts
32
+ var ApiHttpRequest = class {
33
+ constructor(jsWapiHttp) {
34
+ __publicField(this, "_jsWapiHttp");
35
+ this._jsWapiHttp = jsWapiHttp;
36
+ }
37
+ set headers(headers) {
38
+ for (const headersKey in headers) {
39
+ this._jsWapiHttp = this._jsWapiHttp.setHeader(headersKey, JSON.stringify(headers[headersKey]));
40
+ }
41
+ }
42
+ set timeout(milliseconds) {
43
+ this._jsWapiHttp = this._jsWapiHttp.setTimeout(milliseconds);
44
+ }
45
+ setHeader(name, value) {
46
+ this._jsWapiHttp = this._jsWapiHttp.setHeader(name, "".concat(value));
47
+ return this;
48
+ }
49
+ setHeaders(headers) {
50
+ for (const headersKey in headers) {
51
+ this._jsWapiHttp = this._jsWapiHttp.setHeader(headersKey, "".concat(headers[headersKey]));
52
+ }
53
+ return this;
54
+ }
55
+ setTimeout(milliseconds) {
56
+ this._jsWapiHttp = this._jsWapiHttp.setTimeout(milliseconds);
57
+ return this;
58
+ }
59
+ GET(callback) {
60
+ return this._jsWapiHttp.GET(this._getWrappedCallback(callback));
61
+ }
62
+ POST(requestBody, callback) {
63
+ return this._jsWapiHttp.POST(JSON.stringify(requestBody), this._getWrappedCallback(callback));
64
+ }
65
+ PUT(requestBody, callback) {
66
+ return this._jsWapiHttp.PUT(JSON.stringify(requestBody), this._getWrappedCallback(callback));
67
+ }
68
+ PATCH(requestBody, callback) {
69
+ return this._jsWapiHttp.PATCH(JSON.stringify(requestBody), this._getWrappedCallback(callback));
70
+ }
71
+ _getWrappedCallback(callback) {
72
+ return (res) => {
73
+ if (callback) {
74
+ return callback(new ApiHttpResponse(res));
75
+ }
76
+ };
77
+ }
78
+ };
79
+
80
+ // src/classes/ApiResponse.ts
81
+ var ApiResponse = class {
82
+ constructor(jsWapiResponse, objectName) {
83
+ __publicField(this, "_jsWapiResponse");
84
+ __publicField(this, "_objectName");
85
+ this._jsWapiResponse = jsWapiResponse;
86
+ this._objectName = objectName;
87
+ }
88
+ get objectName() {
89
+ return this._objectName;
90
+ }
91
+ set response(response) {
92
+ this.setResponse(response);
93
+ }
94
+ getResponse(responseName) {
95
+ return this._jsWapiResponse.getResponse(responseName || this._objectName);
96
+ }
97
+ setResponse(response, fieldsMap) {
98
+ if (Array.isArray(response)) {
99
+ const data = !fieldsMap ? response : response.map((row) => {
100
+ const res = {};
101
+ for (const externalKey in fieldsMap) {
102
+ res[fieldsMap[externalKey]] = row[externalKey];
103
+ }
104
+ return res;
105
+ });
106
+ this._jsWapiResponse.putResponse(this._objectName, data);
107
+ } else {
108
+ this._jsWapiResponse.setResponse(response);
109
+ }
110
+ return this;
111
+ }
112
+ sendHttpResponse(headers, body) {
113
+ this._jsWapiResponse.sendHttpResponse(headers, body);
114
+ return this;
115
+ }
116
+ sendHttpBody(body) {
117
+ this._jsWapiResponse.sendHttpResponse({}, body);
118
+ return this;
119
+ }
120
+ getPage() {
121
+ return this._jsWapiResponse.getPage();
122
+ }
123
+ setPage(page) {
124
+ this._jsWapiResponse.setPage(page);
125
+ return this;
126
+ }
127
+ getPagesPredict() {
128
+ return this._jsWapiResponse.getPagesPredict();
129
+ }
130
+ setPagesPredict(pagesPredict) {
131
+ this._jsWapiResponse.setPagesPredict(pagesPredict);
132
+ return this;
133
+ }
134
+ };
135
+
136
+ // src/classes/ApiInvoker.ts
137
+ var ApiInvoker = class {
138
+ constructor(jsWapiInvoker, objectName) {
139
+ __publicField(this, "_jsWapiInvoker");
140
+ __publicField(this, "_objectName");
141
+ this._jsWapiInvoker = jsWapiInvoker;
142
+ this._objectName = objectName;
143
+ }
144
+ /* ----------------------------Accessors---------------------------- */
145
+ get objectName() {
146
+ return this._objectName;
147
+ }
148
+ set filters(filters) {
149
+ this._jsWapiInvoker = this._jsWapiInvoker.setFilters(filters);
150
+ }
151
+ set params(params) {
152
+ this._jsWapiInvoker = this._jsWapiInvoker.setParams(params);
153
+ }
154
+ set rowsPerPage(amount) {
155
+ this._jsWapiInvoker = this._jsWapiInvoker.setRowsPerPage(amount);
156
+ }
157
+ set page(number) {
158
+ this._jsWapiInvoker = this._jsWapiInvoker.setPage(number);
159
+ }
160
+ set pagesPredict(amount) {
161
+ this._jsWapiInvoker = this._jsWapiInvoker.setPagesPredict(amount);
162
+ }
163
+ set request(request) {
164
+ if (Array.isArray(request)) {
165
+ this._jsWapiInvoker = this._jsWapiInvoker.putRequest(this._objectName, request);
166
+ } else {
167
+ this._jsWapiInvoker = this._jsWapiInvoker.setRequest(request);
168
+ }
169
+ }
170
+ get request() {
171
+ return this._jsWapiInvoker.getRequest();
172
+ }
173
+ set columns(columns) {
174
+ this._jsWapiInvoker = this._jsWapiInvoker.setColumns(columns);
175
+ }
176
+ set sorts(sorts) {
177
+ this._jsWapiInvoker = this._jsWapiInvoker.setSorts(sorts);
178
+ }
179
+ set aggregatedColumns(aggregatedColumns) {
180
+ this._jsWapiInvoker = this._jsWapiInvoker.setAggregatedColumns(aggregatedColumns);
181
+ }
182
+ set nestedColumns(nestedColumns) {
183
+ this._jsWapiInvoker = this._jsWapiInvoker.setNestedColumns(nestedColumns);
184
+ }
185
+ /* -----------------------------Setters----------------------------- */
186
+ setHierarchy(parentColumn, requestKind) {
187
+ this._jsWapiInvoker = this._jsWapiInvoker.setHierarchy(parentColumn, requestKind);
188
+ return this;
189
+ }
190
+ setFilters(filters) {
191
+ this.filters = filters;
192
+ return this;
193
+ }
194
+ setParams(params) {
195
+ this.params = params;
196
+ return this;
197
+ }
198
+ setPage(number) {
199
+ this.page = number;
200
+ return this;
201
+ }
202
+ setRowsPerPage(amount) {
203
+ this.rowsPerPage = amount;
204
+ return this;
205
+ }
206
+ setPagesPredict(amount) {
207
+ this.pagesPredict = amount;
208
+ return this;
209
+ }
210
+ setRequest(request) {
211
+ this.request = request;
212
+ return this;
213
+ }
214
+ getRequest(requestName) {
215
+ return this._jsWapiInvoker.getRequest(requestName);
216
+ }
217
+ setSrcRequest(srcRequest) {
218
+ this._jsWapiInvoker = this._jsWapiInvoker.setSrcRequest(srcRequest);
219
+ return this;
220
+ }
221
+ getSrcRequest() {
222
+ throw new Error("not implemented");
223
+ return this._jsWapiInvoker.getSrcRequest();
224
+ }
225
+ setColumns(columns) {
226
+ this.columns = columns;
227
+ return this;
228
+ }
229
+ setAggregatedColumns(aggregatedColumns) {
230
+ this.aggregatedColumns = aggregatedColumns;
231
+ return this;
232
+ }
233
+ setNestedColumns(nestedColumns) {
234
+ this.nestedColumns = nestedColumns;
235
+ return this;
236
+ }
237
+ setSorts(sorts) {
238
+ this.sorts = sorts;
239
+ return this;
240
+ }
241
+ setFirstRows(firstRows) {
242
+ this._jsWapiInvoker.setFirstRows(firstRows);
243
+ return this;
244
+ }
245
+ setOperRightCheck(check) {
246
+ this._jsWapiInvoker.setOperRightCheck(check);
247
+ return this;
248
+ }
249
+ getFirstRows() {
250
+ return this._jsWapiInvoker.getFirstRows();
251
+ }
252
+ /* -----------------------------Adders------------------------------ */
253
+ addColumns(columns) {
254
+ this._jsWapiInvoker = this._jsWapiInvoker.addColumns(columns);
255
+ return this;
256
+ }
257
+ addAggregatedColumns(aggregatedColumns) {
258
+ this._jsWapiInvoker = this._jsWapiInvoker.putAggregatedColumns(aggregatedColumns);
259
+ return this;
260
+ }
261
+ addNestedColumns(nestedColumns) {
262
+ this._jsWapiInvoker = this._jsWapiInvoker.putNestedColumns(nestedColumns);
263
+ return this;
264
+ }
265
+ addSorts(sorts) {
266
+ this._jsWapiInvoker = this._jsWapiInvoker.addSorts(sorts);
267
+ return this;
268
+ }
269
+ /* ------------------------------Other------------------------------ */
270
+ invoke() {
271
+ return new ApiResponse(this._jsWapiInvoker.invoke(), this._objectName);
272
+ }
273
+ };
274
+
275
+ // src/classes/UserMessages.ts
276
+ function formatMessage(text, args = []) {
277
+ let str = text;
278
+ for (let v in args) {
279
+ str = str.replace(`{${v}}`, args[v] == null ? "" : args[v]);
280
+ }
281
+ return str;
282
+ }
283
+ var UserMessages = class {
284
+ constructor() {
285
+ __publicField(this, "_messages", {});
286
+ __publicField(this, "_lang", "ua" /* ua */);
287
+ }
288
+ appendDictionary(newDictionary) {
289
+ if (Array.isArray(newDictionary) || !newDictionary || typeof newDictionary !== "object")
290
+ return;
291
+ for (const key in newDictionary) {
292
+ if (!this._messages.hasOwnProperty(key)) {
293
+ this._messages[key] = {};
294
+ }
295
+ this._messages[key] = Object.assign(this._messages[key], newDictionary[key]);
296
+ }
297
+ }
298
+ format(msgKey, params) {
299
+ const msg = this._messages[this._lang]?.[msgKey] ?? msgKey;
300
+ return formatMessage(msg, params);
301
+ }
302
+ set lang(value) {
303
+ this._lang = value;
304
+ }
305
+ };
306
+ var UserMessages_default = new UserMessages();
307
+
308
+ // src/classes/ApiObjectCollectionIterator.ts
309
+ var ApiObjectCollectionIterator = class {
310
+ constructor({ invoker }) {
311
+ __publicField(this, "_invoker");
312
+ this._invoker = invoker;
313
+ }
314
+ get objectName() {
315
+ return this._invoker.objectName;
316
+ }
317
+ /* -----------------------------Setters----------------------------- */
318
+ setColumns(columns) {
319
+ this._invoker.setColumns(columns);
320
+ return this;
321
+ }
322
+ setFilters(filters) {
323
+ this._invoker.setFilters(filters);
324
+ return this;
325
+ }
326
+ setSorts(sorts) {
327
+ this._invoker.setSorts(sorts);
328
+ return this;
329
+ }
330
+ /* -----------------------------Adders------------------------------ */
331
+ addSorts(sorts) {
332
+ this._invoker.addSorts(sorts);
333
+ return this;
334
+ }
335
+ addColumns(columns) {
336
+ this._invoker.addColumns(columns);
337
+ return this;
338
+ }
339
+ /* ------------------------------Other------------------------------ */
340
+ *invoke() {
341
+ let data = null, pageCount = 1;
342
+ let lastPage = false;
343
+ const rowsPerPage = 100;
344
+ while (!lastPage) {
345
+ this._invoker.page = pageCount;
346
+ this._invoker.rowsPerPage = rowsPerPage;
347
+ data = new ApiResponse(this._invoker.invoke(), this.objectName).getResponse();
348
+ pageCount++;
349
+ if (Array.isArray(data)) {
350
+ for (let i = 0; i < data.length; i++) {
351
+ yield data[i];
352
+ }
353
+ }
354
+ lastPage = data.length < rowsPerPage;
355
+ }
356
+ }
357
+ };
358
+
359
+ // src/classes/ApiCore.ts
360
+ var ApiCore = class {
361
+ constructor({ http, request }) {
362
+ __publicField(this, "httpSender");
363
+ __publicField(this, "_request");
364
+ this.httpSender = http;
365
+ this._request = request;
366
+ }
367
+ /**
368
+ * Переводит и форматирует строку из словаря переводов согласно текущего языка приложения.
369
+ * @param msgKey
370
+ * @param params
371
+ *
372
+ * @example
373
+ * {
374
+ * ...
375
+ * "myKey": 'some text with {0}'
376
+ * ...
377
+ * }
378
+ * t('myKey', ['placeholder text']) // 'some text with placeholder text'
379
+ */
380
+ t(msgKey, params) {
381
+ UserMessages_default.lang = this._request.lang || "ua" /* ua */;
382
+ return UserMessages_default.format(msgKey, params);
383
+ }
384
+ newHttpRequest(url) {
385
+ return new ApiHttpRequest(this.httpSender.newJSWapiHttp(url));
386
+ }
387
+ newApiInvoker(objectName, operName) {
388
+ return new ApiInvoker(this._request.newJSWapiInvoker(objectName, operName), objectName);
389
+ }
390
+ newApiObjectCollectionIterator(objectName) {
391
+ return new ApiObjectCollectionIterator({
392
+ invoker: this.newApiInvoker(objectName, "List")
393
+ });
394
+ }
395
+ newApiException(message, code) {
396
+ if (code) {
397
+ return new JSWapiException(code.toString(), message);
398
+ }
399
+ return new JSWapiException(message);
400
+ }
401
+ /*
402
+ * Will be implemented later
403
+ *
404
+ newApiJoinInvoker(alias: string, request: ApiRequest | ApiInvoker): ApiJoinInvoker {
405
+ if (request instanceof ApiRequest) {
406
+ return new ApiJoinInvoker(this._jsWapiRequest.newJSWapiJoinInvoker(alias, request._jsWapiRequest));
407
+ } else if (request instanceof ApiInvoker) {
408
+ return new ApiJoinInvoker(this._jsWapiRequest.newJSWapiJoinInvoker(alias, request._jsWapiInvoker));
409
+ }
410
+ }
411
+ */
412
+ };
413
+
414
+ // src/classes/ApiRequest.ts
415
+ var ApiRequest = class {
416
+ constructor(jsWapiRequest, objectName) {
417
+ __publicField(this, "_jsWapiRequest");
418
+ __publicField(this, "_objectName");
419
+ this._jsWapiRequest = jsWapiRequest;
420
+ this._objectName = objectName;
421
+ }
422
+ /* ----------------------------Accessors---------------------------- */
423
+ get objectName() {
424
+ return this._objectName;
425
+ }
426
+ get columns() {
427
+ return this._jsWapiRequest.getColumns();
428
+ }
429
+ get aggregatedColumns() {
430
+ return this._jsWapiRequest.getAggregatedColumns();
431
+ }
432
+ get nestedColumns() {
433
+ return this._jsWapiRequest.getNestedColumns();
434
+ }
435
+ get filters() {
436
+ return this._jsWapiRequest.getFilters();
437
+ }
438
+ get params() {
439
+ return this._jsWapiRequest.getParams();
440
+ }
441
+ get request() {
442
+ return this._jsWapiRequest.getRequest(this._objectName);
443
+ }
444
+ get userId() {
445
+ return this._jsWapiRequest.getUserId();
446
+ }
447
+ get page() {
448
+ return this._jsWapiRequest.getPage();
449
+ }
450
+ get rowsPerPage() {
451
+ return this._jsWapiRequest.getRowsPerPage();
452
+ }
453
+ get sorts() {
454
+ return this._jsWapiRequest.getSorts();
455
+ }
456
+ get lang() {
457
+ return this._jsWapiRequest.getLanguage();
458
+ }
459
+ set columns(columns) {
460
+ this._jsWapiRequest = this._jsWapiRequest.setColumns(columns);
461
+ }
462
+ set aggregatedColumns(columns) {
463
+ this._jsWapiRequest = this._jsWapiRequest.setAggregatedColumns(columns);
464
+ }
465
+ set nestedColumns(columns) {
466
+ this._jsWapiRequest = this._jsWapiRequest.setNestedColumns(columns);
467
+ }
468
+ set filters(filters) {
469
+ this._jsWapiRequest = this._jsWapiRequest.setFilters(filters);
470
+ }
471
+ set params(params) {
472
+ this._jsWapiRequest = this._jsWapiRequest.setParams(params);
473
+ }
474
+ set sorts(sorts) {
475
+ this._jsWapiRequest = this._jsWapiRequest.setSorts(sorts);
476
+ }
477
+ set page(number) {
478
+ this._jsWapiRequest = this._jsWapiRequest.setPage(number);
479
+ }
480
+ set rowsPerPage(amount) {
481
+ this._jsWapiRequest = this._jsWapiRequest.setRowsPerPage(amount);
482
+ }
483
+ /* -----------------------------Getters----------------------------- */
484
+ getFilter(filterName) {
485
+ return this._jsWapiRequest.getFilter(filterName);
486
+ }
487
+ /**
488
+ * Вернуть значение фильтра типа "равно".
489
+ * Возможные варианты:
490
+ * "поле": "значение"
491
+ * "поле": {"=": "значение"}
492
+ * "поле": ["значение"]
493
+ * "поле": {"=": [123]}
494
+ * Это чтобы не парсить каждый раз все эти варианты.
495
+ *
496
+ * Работает только для условия "равно", для други возвращает null!!!!
497
+ */
498
+ getFilterValue(fieldName) {
499
+ return this._jsWapiRequest.getFilterEqualOneValue(fieldName);
500
+ }
501
+ getParam(paramName) {
502
+ return this._jsWapiRequest.getParam(paramName);
503
+ }
504
+ getExcelFileAsJson(fileRequestId) {
505
+ return this._jsWapiRequest.getExcelFileAsJson(fileRequestId);
506
+ }
507
+ getFileAsString(fileRequestId) {
508
+ return this._jsWapiRequest.getFileAsString(fileRequestId);
509
+ }
510
+ getFilesListAsString(filesRequestIds) {
511
+ return this._jsWapiRequest.getMultiFileAsString(...this._getWithStringifiedElements(filesRequestIds));
512
+ }
513
+ getExcelFilesListAsJson(filesRequestIds) {
514
+ return this._jsWapiRequest.getMultiExcelFileAsJson(...this._getWithStringifiedElements(filesRequestIds));
515
+ }
516
+ getRequest(requestName) {
517
+ return this._jsWapiRequest.getRequest(requestName);
518
+ }
519
+ /* -----------------------------Setters----------------------------- */
520
+ setHierarchy(parentColumn, requestKind) {
521
+ this._jsWapiRequest = this._jsWapiRequest.setHierarchy(parentColumn, requestKind);
522
+ return this;
523
+ }
524
+ setColumns(columns) {
525
+ this.columns = columns;
526
+ return this;
527
+ }
528
+ setNestedColumns(nestedColumns) {
529
+ this.nestedColumns = nestedColumns;
530
+ return this;
531
+ }
532
+ setAggregatedColumns(aggregatedColumns) {
533
+ this.aggregatedColumns = aggregatedColumns;
534
+ return this;
535
+ }
536
+ setFilters(filters) {
537
+ this.filters = filters;
538
+ return this;
539
+ }
540
+ setSorts(sorts) {
541
+ this.sorts = sorts;
542
+ return this;
543
+ }
544
+ setParams(params) {
545
+ this.params = params;
546
+ return this;
547
+ }
548
+ setPage(number) {
549
+ this.page = number;
550
+ return this;
551
+ }
552
+ setRowsPerPage(amount) {
553
+ this.rowsPerPage = amount;
554
+ return this;
555
+ }
556
+ setRequest(request) {
557
+ if (Array.isArray(request)) {
558
+ this._jsWapiRequest = this._jsWapiRequest.putRequest(this._objectName, request);
559
+ } else {
560
+ this._jsWapiRequest = this._jsWapiRequest.setRequest(request);
561
+ }
562
+ return this;
563
+ }
564
+ setFirstRows(firstRows) {
565
+ this._jsWapiRequest.setFirstRows(firstRows);
566
+ return this;
567
+ }
568
+ getFirstRows() {
569
+ return this._jsWapiRequest.getFirstRows();
570
+ }
571
+ addSorts(sorts) {
572
+ this._jsWapiRequest = this._jsWapiRequest.addSorts(sorts);
573
+ return this;
574
+ }
575
+ addColumns(columns) {
576
+ this._jsWapiRequest = this._jsWapiRequest.addColumns(columns);
577
+ return this;
578
+ }
579
+ addAggregatedColumns(columns) {
580
+ this._jsWapiRequest = this._jsWapiRequest.putAggregatedColumns(columns);
581
+ return this;
582
+ }
583
+ /**
584
+ * @link http://d5.develop.erp-director.com/userscript-docs/docs/server_scripts/api-request/api-request#addnestedcolumnscolumns
585
+ * @example
586
+ * apiRequest.addNestedColumns({
587
+ * 'AuthorsNested': {
588
+ * 'Columns': ['ID', 'Name']
589
+ * }
590
+ * });
591
+ */
592
+ addNestedColumns(columns) {
593
+ this._jsWapiRequest = this._jsWapiRequest.putNestedColumns(columns);
594
+ return this;
595
+ }
596
+ addFilters(filters) {
597
+ this._jsWapiRequest = this._jsWapiRequest.putFilters(filters);
598
+ return this;
599
+ }
600
+ addParams(params) {
601
+ for (const name in params) {
602
+ this._jsWapiRequest = this._jsWapiRequest.putParam(name, params[name]);
603
+ }
604
+ return this;
605
+ }
606
+ /* ----------------------------Removers----------------------------- */
607
+ removeSorts(sortsNames) {
608
+ this._jsWapiRequest = this._jsWapiRequest.removeSorts(sortsNames);
609
+ return this;
610
+ }
611
+ removeColumns(columnsNames) {
612
+ this._jsWapiRequest = this._jsWapiRequest.removeColumns(columnsNames);
613
+ return this;
614
+ }
615
+ removeAggregatedColumns(aggregatedColumnsNames) {
616
+ this._jsWapiRequest = this._jsWapiRequest.removeAggregatedColumns(aggregatedColumnsNames);
617
+ return this;
618
+ }
619
+ removeNestedColumns(nestedColumnsNames) {
620
+ this._jsWapiRequest = this._jsWapiRequest.removeNestedColumns(nestedColumnsNames);
621
+ return this;
622
+ }
623
+ removeParams(paramsNames) {
624
+ this._jsWapiRequest = this._jsWapiRequest.removeParams(paramsNames);
625
+ return this;
626
+ }
627
+ removeFilters(filtersNames) {
628
+ this._jsWapiRequest = this._jsWapiRequest.removeFilters(filtersNames);
629
+ return this;
630
+ }
631
+ /* ------------------------------Other------------------------------ */
632
+ hasFilter(filterName) {
633
+ return this._jsWapiRequest.isFilterExists(filterName);
634
+ }
635
+ invoke() {
636
+ return new ApiResponse(this._jsWapiRequest.invoke(), this._objectName);
637
+ }
638
+ newJSWapiInvoker(objectName, operName) {
639
+ return this._jsWapiRequest.newJSWapiInvoker(objectName, operName);
640
+ }
641
+ newApiInvoker(objectName, operName) {
642
+ return new ApiInvoker(this.newJSWapiInvoker(objectName, operName), objectName);
643
+ }
644
+ /**
645
+ * Переводит и форматирует строку из словаря переводов согласно текущего языка приложения.
646
+ * @param msgKey
647
+ * @param params
648
+ *
649
+ * @example
650
+ * {
651
+ * ...
652
+ * "myKey": 'some text with {0}'
653
+ * ...
654
+ * }
655
+ * t('myKey', ['placeholder text']) // 'some text with placeholder text'
656
+ */
657
+ t(msgKey, params) {
658
+ UserMessages_default.lang = this.lang || "ua" /* ua */;
659
+ return UserMessages_default.format(msgKey, params);
660
+ }
661
+ /* -------------------------Support methods------------------------- */
662
+ _getWithStringifiedElements(array) {
663
+ return array.map((element) => element.toString());
664
+ }
665
+ };
666
+
667
+ // src/classes/ApiObjectInitializer.ts
668
+ var ApiInitializer = class {
669
+ constructor(settings, operations, options) {
670
+ __publicField(this, "_api");
671
+ __publicField(this, "_operations");
672
+ this._api = jsWapi;
673
+ const throwError = (msg) => {
674
+ throw new Error(msg);
675
+ };
676
+ if (typeof settings === "string") {
677
+ if (!operations)
678
+ throwError('The second argument "operations" is required');
679
+ this._operations = operations;
680
+ this.internalCreate(settings);
681
+ options?.translations && this.initTranslation(options.translations);
682
+ return;
683
+ }
684
+ const { objectName, operations: opers, translations } = settings;
685
+ if (!opers)
686
+ throwError('Property "operations" in the settings is required');
687
+ this._operations = opers;
688
+ this.internalCreate(objectName);
689
+ translations && this.initTranslation(translations);
690
+ }
691
+ _initOperations(objectName) {
692
+ for (const operationName in this._operations) {
693
+ this.putOperation(operationName, objectName);
694
+ }
695
+ }
696
+ /**
697
+ * Инициализация словаря переводов.
698
+ * @param translations
699
+ * @example Пример строки перевода
700
+ * { ...
701
+ * "ru": {"key": 'some text {0} {1}'}
702
+ * ...
703
+ * }
704
+ *
705
+ * core.t('key', ['test1', 'test2']); // 'some text test1 test2'
706
+ */
707
+ initTranslation(translations) {
708
+ UserMessages_default.appendDictionary(translations);
709
+ }
710
+ };
711
+ var ApiObjectInitializer = class extends ApiInitializer {
712
+ constructor() {
713
+ super(...arguments);
714
+ __publicField(this, "jsWapiObject");
715
+ }
716
+ internalCreate(objectName) {
717
+ this._api.putJSWapiObject(objectName, (jsWapiObject) => {
718
+ this.jsWapiObject = jsWapiObject;
719
+ this._initOperations(objectName);
720
+ });
721
+ }
722
+ putOperation(operationName, objectName) {
723
+ this.jsWapiObject.putJSWapiOper(operationName, (jsWapiRequest, jsWapiResponse) => {
724
+ const request = new ApiRequest(jsWapiRequest, objectName);
725
+ return this._operations[operationName](
726
+ new ApiCore({ http: this.jsWapiObject, request }),
727
+ request,
728
+ new ApiResponse(jsWapiResponse, objectName)
729
+ );
730
+ });
731
+ }
732
+ };
733
+
734
+ // src/classes/EngineInitializer.ts
735
+ var EngineInitializer = class {
736
+ constructor(arg1, arg2) {
737
+ __publicField(this, "engine");
738
+ __publicField(this, "_operations");
739
+ let operations = arg1.operations || arg1;
740
+ let translations = arg1.translations || arg2?.translations;
741
+ const throwError = (msg) => {
742
+ throw new Error(msg);
743
+ };
744
+ if (!operations)
745
+ throwError('Property "operations" in the settings is required');
746
+ this._operations = operations;
747
+ this.internalCreate();
748
+ translations && this.initTranslation(translations);
749
+ }
750
+ internalCreate() {
751
+ jsWapi.setJSWapiEngine((engine) => {
752
+ this.engine = engine;
753
+ for (const operationName in this._operations) {
754
+ this.putOperation(operationName);
755
+ }
756
+ });
757
+ }
758
+ putOperation(operationName) {
759
+ this.engine.putJSWapiOper(operationName, (jsWapiRequest, jsWapiResponse) => {
760
+ const request = new ApiRequest(jsWapiRequest, this.engine.getName());
761
+ const apiCore = new ApiCore({ http: this.engine, request });
762
+ return this._operations[operationName](
763
+ apiCore,
764
+ request,
765
+ new ApiResponse(jsWapiResponse, this.engine.getName()),
766
+ {
767
+ parameters: this.engine.getExternalSystemParameters(),
768
+ objectName: this.engine.getName(),
769
+ externalObject: this.engine.getExternalName(),
770
+ externalFieldsMap: this.engine.getExternalObjectFields()
771
+ }
772
+ );
773
+ });
774
+ }
775
+ /**
776
+ * Инициализация словаря переводов.
777
+ * @param translations
778
+ * @example Пример строки перевода
779
+ * { ...
780
+ * "ru": {"key": 'some text {0} {1}'}
781
+ * ...
782
+ * }
783
+ *
784
+ * core.t('key', ['test1', 'test2']); // 'some text test1 test2'
785
+ */
786
+ initTranslation(translations) {
787
+ UserMessages_default.appendDictionary(translations);
788
+ }
789
+ call({ apiCore, apiRequest, apiResponse, operation, meta }) {
790
+ return this._operations[operation](
791
+ apiCore,
792
+ apiRequest,
793
+ apiResponse,
794
+ meta
795
+ );
796
+ }
797
+ };
798
+ export {
799
+ ApiObjectInitializer,
800
+ EngineInitializer
801
+ };