@2bad/bitrix 2.5.0 → 3.0.0-beta.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/README.md +10 -9
- package/build/bitrix.d.ts +65 -0
- package/build/bitrix.js +55 -0
- package/build/client/hooks/addAccessToken.js +9 -0
- package/build/{module/client → client}/index.d.ts +3 -3
- package/build/{module/client → client}/index.js +21 -16
- package/build/{module/client → client}/methods/batch.d.ts +6 -6
- package/build/client/methods/batch.js +75 -0
- package/build/{module/client → client}/methods/call.d.ts +4 -4
- package/build/{module/client → client}/methods/call.js +8 -9
- package/build/{module/client → client}/methods/list.d.ts +10 -10
- package/build/client/methods/list.js +58 -0
- package/build/{module/commands.d.ts → commands.d.ts} +3 -3
- package/build/commands.js +1 -0
- package/build/{module/methods.d.ts → methods.d.ts} +19 -19
- package/build/{module/methods.js → methods.js} +5 -5
- package/build/{main/payloads.d.ts → payloads.d.ts} +5 -5
- package/build/payloads.js +1 -0
- package/build/{main/services → services}/common.d.ts +7 -7
- package/build/services/common.js +2 -0
- package/build/{main/services → services}/companies/entities.d.ts +2 -2
- package/build/services/companies/entities.js +1 -0
- package/build/services/companies/index.d.ts +15 -0
- package/build/services/companies/index.js +17 -0
- package/build/{module/services → services}/companies/methods.d.ts +5 -5
- package/build/services/companies/methods.js +1 -0
- package/build/{module/services → services}/contacts/entities.d.ts +2 -2
- package/build/services/contacts/entities.js +1 -0
- package/build/services/contacts/index.d.ts +15 -0
- package/build/services/contacts/index.js +17 -0
- package/build/{module/services → services}/contacts/methods.d.ts +5 -5
- package/build/services/contacts/methods.js +1 -0
- package/build/{module/services → services}/currencies/entities.d.ts +2 -2
- package/build/services/currencies/entities.js +1 -0
- package/build/{module/services → services}/deals/entities.d.ts +2 -2
- package/build/services/deals/entities.js +1 -0
- package/build/services/deals/index.d.ts +15 -0
- package/build/services/deals/index.js +17 -0
- package/build/{main/services → services}/deals/methods.d.ts +5 -5
- package/build/services/deals/methods.js +1 -0
- package/build/{module/services → services}/leads/entities.d.ts +2 -2
- package/build/services/leads/entities.js +1 -0
- package/build/services/leads/index.d.ts +15 -0
- package/build/services/leads/index.js +17 -0
- package/build/{main/services → services}/leads/methods.d.ts +5 -5
- package/build/services/leads/methods.js +1 -0
- package/build/{main/services → services}/statuses/entities.d.ts +3 -3
- package/build/services/statuses/entities.js +1 -0
- package/build/services/statuses/index.d.ts +14 -0
- package/build/services/statuses/index.js +19 -0
- package/build/{main/services → services}/statuses/methods.d.ts +5 -5
- package/build/services/statuses/methods.js +1 -0
- package/build/{module/services → services}/users/entities.d.ts +2 -2
- package/build/services/users/entities.js +1 -0
- package/build/services/users/index.d.ts +9 -0
- package/build/services/users/index.js +7 -0
- package/build/{main/services → services}/users/methods.d.ts +4 -4
- package/build/services/users/methods.js +1 -0
- package/build/utils/Diff.d.ts +1 -0
- package/build/utils/Diff.js +7 -0
- package/build/{module/utils → utils}/ExtractValue.d.ts +1 -1
- package/build/{main/utils/ExtractValue.d.ts → utils/ExtractValue.js} +1 -2
- package/build/utils/isArray.d.ts +2 -0
- package/build/{module/utils → utils}/isArray.js +1 -1
- package/package.json +43 -49
- package/build/main/bitrix.d.ts +0 -114
- package/build/main/bitrix.integration.test.d.ts +0 -0
- package/build/main/bitrix.integration.test.js +0 -31
- package/build/main/bitrix.js +0 -58
- package/build/main/bitrix.unit.test.d.ts +0 -1
- package/build/main/bitrix.unit.test.js +0 -44
- package/build/main/client/client.unit.test.d.ts +0 -1
- package/build/main/client/client.unit.test.js +0 -70
- package/build/main/client/hooks/addAccessToken.js +0 -13
- package/build/main/client/hooks/addAccessToken.unit.test.d.ts +0 -1
- package/build/main/client/hooks/addAccessToken.unit.test.js +0 -31
- package/build/main/client/index.d.ts +0 -15
- package/build/main/client/index.js +0 -53
- package/build/main/client/methods/batch.d.ts +0 -40
- package/build/main/client/methods/batch.js +0 -85
- package/build/main/client/methods/batch.unit.test.d.ts +0 -1
- package/build/main/client/methods/batch.unit.test.js +0 -494
- package/build/main/client/methods/call.d.ts +0 -17
- package/build/main/client/methods/call.js +0 -36
- package/build/main/client/methods/call.unit.test.d.ts +0 -1
- package/build/main/client/methods/call.unit.test.js +0 -183
- package/build/main/client/methods/list.d.ts +0 -30
- package/build/main/client/methods/list.js +0 -66
- package/build/main/client/methods/list.unit.test.d.ts +0 -1
- package/build/main/client/methods/list.unit.test.js +0 -277
- package/build/main/commands.d.ts +0 -10
- package/build/main/commands.js +0 -2
- package/build/main/methods.d.ts +0 -92
- package/build/main/methods.js +0 -41
- package/build/main/payloads.js +0 -2
- package/build/main/services/common.js +0 -2
- package/build/main/services/companies/entities.js +0 -2
- package/build/main/services/companies/index.d.ts +0 -26
- package/build/main/services/companies/index.integration.test.d.ts +0 -1
- package/build/main/services/companies/index.integration.test.js +0 -19
- package/build/main/services/companies/index.js +0 -10
- package/build/main/services/companies/index.unit.test.d.ts +0 -1
- package/build/main/services/companies/index.unit.test.js +0 -72
- package/build/main/services/companies/methods.d.ts +0 -44
- package/build/main/services/companies/methods.js +0 -3
- package/build/main/services/contacts/entities.d.ts +0 -45
- package/build/main/services/contacts/entities.js +0 -2
- package/build/main/services/contacts/index.d.ts +0 -26
- package/build/main/services/contacts/index.integration.test.d.ts +0 -1
- package/build/main/services/contacts/index.integration.test.js +0 -19
- package/build/main/services/contacts/index.js +0 -10
- package/build/main/services/contacts/index.unit.test.d.ts +0 -1
- package/build/main/services/contacts/index.unit.test.js +0 -72
- package/build/main/services/contacts/methods.d.ts +0 -44
- package/build/main/services/contacts/methods.js +0 -3
- package/build/main/services/currencies/entities.d.ts +0 -15
- package/build/main/services/currencies/entities.js +0 -2
- package/build/main/services/deals/entities.d.ts +0 -43
- package/build/main/services/deals/entities.js +0 -2
- package/build/main/services/deals/index.d.ts +0 -26
- package/build/main/services/deals/index.integration.test.d.ts +0 -1
- package/build/main/services/deals/index.integration.test.js +0 -19
- package/build/main/services/deals/index.js +0 -10
- package/build/main/services/deals/index.unit.test.d.ts +0 -1
- package/build/main/services/deals/index.unit.test.js +0 -72
- package/build/main/services/deals/methods.js +0 -3
- package/build/main/services/leads/entities.d.ts +0 -49
- package/build/main/services/leads/entities.js +0 -2
- package/build/main/services/leads/index.d.ts +0 -26
- package/build/main/services/leads/index.integration.test.d.ts +0 -1
- package/build/main/services/leads/index.integration.test.js +0 -19
- package/build/main/services/leads/index.js +0 -10
- package/build/main/services/leads/index.unit.test.d.ts +0 -1
- package/build/main/services/leads/index.unit.test.js +0 -72
- package/build/main/services/leads/methods.js +0 -3
- package/build/main/services/statuses/entities.js +0 -2
- package/build/main/services/statuses/index.d.ts +0 -14
- package/build/main/services/statuses/index.integration.test.d.ts +0 -1
- package/build/main/services/statuses/index.integration.test.js +0 -90
- package/build/main/services/statuses/index.js +0 -11
- package/build/main/services/statuses/index.unit.test.d.ts +0 -1
- package/build/main/services/statuses/index.unit.test.js +0 -60
- package/build/main/services/statuses/methods.js +0 -3
- package/build/main/services/users/entities.d.ts +0 -40
- package/build/main/services/users/entities.js +0 -2
- package/build/main/services/users/index.d.ts +0 -9
- package/build/main/services/users/index.integration.test.d.ts +0 -1
- package/build/main/services/users/index.integration.test.js +0 -33
- package/build/main/services/users/index.js +0 -7
- package/build/main/services/users/index.unit.test.d.ts +0 -1
- package/build/main/services/users/index.unit.test.js +0 -25
- package/build/main/services/users/methods.js +0 -3
- package/build/main/utils/Diff.d.ts +0 -1
- package/build/main/utils/Diff.js +0 -2
- package/build/main/utils/ExtractValue.js +0 -2
- package/build/main/utils/isArray.d.ts +0 -2
- package/build/main/utils/isArray.js +0 -4
- package/build/module/bitrix.d.ts +0 -114
- package/build/module/bitrix.integration.test.d.ts +0 -0
- package/build/module/bitrix.integration.test.js +0 -31
- package/build/module/bitrix.js +0 -39
- package/build/module/bitrix.unit.test.d.ts +0 -1
- package/build/module/bitrix.unit.test.js +0 -16
- package/build/module/client/client.unit.test.d.ts +0 -1
- package/build/module/client/client.unit.test.js +0 -65
- package/build/module/client/hooks/addAccessToken.d.ts +0 -6
- package/build/module/client/hooks/addAccessToken.js +0 -11
- package/build/module/client/hooks/addAccessToken.unit.test.d.ts +0 -1
- package/build/module/client/hooks/addAccessToken.unit.test.js +0 -26
- package/build/module/client/methods/batch.js +0 -76
- package/build/module/client/methods/batch.unit.test.d.ts +0 -1
- package/build/module/client/methods/batch.unit.test.js +0 -466
- package/build/module/client/methods/call.unit.test.d.ts +0 -1
- package/build/module/client/methods/call.unit.test.js +0 -155
- package/build/module/client/methods/list.js +0 -55
- package/build/module/client/methods/list.unit.test.d.ts +0 -1
- package/build/module/client/methods/list.unit.test.js +0 -252
- package/build/module/commands.js +0 -1
- package/build/module/payloads.d.ts +0 -40
- package/build/module/payloads.js +0 -1
- package/build/module/services/common.d.ts +0 -22
- package/build/module/services/common.js +0 -1
- package/build/module/services/companies/entities.d.ts +0 -54
- package/build/module/services/companies/entities.js +0 -1
- package/build/module/services/companies/index.d.ts +0 -26
- package/build/module/services/companies/index.integration.test.d.ts +0 -1
- package/build/module/services/companies/index.integration.test.js +0 -14
- package/build/module/services/companies/index.js +0 -8
- package/build/module/services/companies/index.unit.test.d.ts +0 -1
- package/build/module/services/companies/index.unit.test.js +0 -67
- package/build/module/services/companies/methods.js +0 -1
- package/build/module/services/contacts/entities.js +0 -1
- package/build/module/services/contacts/index.d.ts +0 -26
- package/build/module/services/contacts/index.integration.test.d.ts +0 -1
- package/build/module/services/contacts/index.integration.test.js +0 -14
- package/build/module/services/contacts/index.js +0 -8
- package/build/module/services/contacts/index.unit.test.d.ts +0 -1
- package/build/module/services/contacts/index.unit.test.js +0 -67
- package/build/module/services/contacts/methods.js +0 -1
- package/build/module/services/currencies/entities.js +0 -1
- package/build/module/services/deals/entities.js +0 -1
- package/build/module/services/deals/index.d.ts +0 -26
- package/build/module/services/deals/index.integration.test.d.ts +0 -1
- package/build/module/services/deals/index.integration.test.js +0 -14
- package/build/module/services/deals/index.js +0 -8
- package/build/module/services/deals/index.unit.test.d.ts +0 -1
- package/build/module/services/deals/index.unit.test.js +0 -67
- package/build/module/services/deals/methods.d.ts +0 -44
- package/build/module/services/deals/methods.js +0 -1
- package/build/module/services/leads/entities.js +0 -1
- package/build/module/services/leads/index.d.ts +0 -26
- package/build/module/services/leads/index.integration.test.d.ts +0 -1
- package/build/module/services/leads/index.integration.test.js +0 -14
- package/build/module/services/leads/index.js +0 -8
- package/build/module/services/leads/index.unit.test.d.ts +0 -1
- package/build/module/services/leads/index.unit.test.js +0 -67
- package/build/module/services/leads/methods.d.ts +0 -44
- package/build/module/services/leads/methods.js +0 -1
- package/build/module/services/statuses/entities.d.ts +0 -16
- package/build/module/services/statuses/entities.js +0 -1
- package/build/module/services/statuses/index.d.ts +0 -14
- package/build/module/services/statuses/index.integration.test.d.ts +0 -1
- package/build/module/services/statuses/index.integration.test.js +0 -85
- package/build/module/services/statuses/index.js +0 -9
- package/build/module/services/statuses/index.unit.test.d.ts +0 -1
- package/build/module/services/statuses/index.unit.test.js +0 -55
- package/build/module/services/statuses/methods.d.ts +0 -51
- package/build/module/services/statuses/methods.js +0 -1
- package/build/module/services/users/entities.js +0 -1
- package/build/module/services/users/index.d.ts +0 -9
- package/build/module/services/users/index.integration.test.d.ts +0 -1
- package/build/module/services/users/index.integration.test.js +0 -28
- package/build/module/services/users/index.js +0 -5
- package/build/module/services/users/index.unit.test.d.ts +0 -1
- package/build/module/services/users/index.unit.test.js +0 -20
- package/build/module/services/users/methods.d.ts +0 -17
- package/build/module/services/users/methods.js +0 -1
- package/build/module/utils/Diff.d.ts +0 -1
- package/build/module/utils/Diff.js +0 -1
- package/build/module/utils/ExtractValue.js +0 -1
- package/build/module/utils/isArray.d.ts +0 -2
- /package/build/{main/client → client}/hooks/addAccessToken.d.ts +0 -0
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.mergeBatchPayloads = exports.prepareCommandsQueries = exports.chunkCommands = exports.MAX_COMMANDS_PER_BATCH = void 0;
|
|
7
|
-
const lodash_chunk_1 = __importDefault(require("lodash.chunk"));
|
|
8
|
-
const lodash_frompairs_1 = __importDefault(require("lodash.frompairs"));
|
|
9
|
-
const qs_1 = require("qs");
|
|
10
|
-
const methods_1 = require("../../methods");
|
|
11
|
-
const isArray_1 = __importDefault(require("../../utils/isArray"));
|
|
12
|
-
const call_1 = require("./call");
|
|
13
|
-
exports.MAX_COMMANDS_PER_BATCH = 50;
|
|
14
|
-
/**
|
|
15
|
-
* Split any number of commands into the chunks with max commands per batch in each chunk
|
|
16
|
-
*/
|
|
17
|
-
const chunkCommands = (commands, size = exports.MAX_COMMANDS_PER_BATCH) => {
|
|
18
|
-
const commandsIsArray = (0, isArray_1.default)(commands);
|
|
19
|
-
const chunks = (0, lodash_chunk_1.default)(Object.entries(commands), size);
|
|
20
|
-
// @todo Generated return type isn't very accurate since it says that it will make array of <C>, while
|
|
21
|
-
// in fact <C> will be distributed among chunks. That gives wrong hints with named commands and arrays as const.
|
|
22
|
-
return chunks.map((c) => commandsIsArray
|
|
23
|
-
// @todo Figure out how to avoid unsafe casting to <C> here
|
|
24
|
-
// Without `<C>` generic it won't work with complex functions like `batch`
|
|
25
|
-
? c.map(([_key, command]) => command)
|
|
26
|
-
: (0, lodash_frompairs_1.default)(c));
|
|
27
|
-
};
|
|
28
|
-
exports.chunkCommands = chunkCommands;
|
|
29
|
-
/**
|
|
30
|
-
* Prepares list of commands to be used in requests to the Bitrix
|
|
31
|
-
* @note We could avoid that function... if only Bitrix API would support posting of the batch commands as
|
|
32
|
-
* plain objects, like it does with other methods. Instead, it should be a dict or array of string queries
|
|
33
|
-
*/
|
|
34
|
-
const prepareCommandsQueries = (commands) => Object.entries(commands).reduce((calls, [name, { method, params }]) => {
|
|
35
|
-
const stringifiedParams = params ? `?${(0, qs_1.stringify)(params)}` : '';
|
|
36
|
-
return {
|
|
37
|
-
...calls,
|
|
38
|
-
[`cmd[${name}]`]: `${method}${stringifiedParams}`
|
|
39
|
-
};
|
|
40
|
-
// eslint-disable-next-line @typescript-eslint/prefer-reduce-type-parameter
|
|
41
|
-
}, {});
|
|
42
|
-
exports.prepareCommandsQueries = prepareCommandsQueries;
|
|
43
|
-
/**
|
|
44
|
-
* Merges list of batch responses into a single batch
|
|
45
|
-
* @todo Generics inference is complicated here and might be not super accurate
|
|
46
|
-
*/
|
|
47
|
-
const mergeBatchPayloads = (payloads) => {
|
|
48
|
-
const merge = (a, b) => (0, isArray_1.default)(a) && (0, isArray_1.default)(b)
|
|
49
|
-
// @todo Gotta find way to avoid unsafe casting here
|
|
50
|
-
? [...a, ...b]
|
|
51
|
-
: ({ ...a, ...b });
|
|
52
|
-
return payloads.reduce((merged, payload) => ({
|
|
53
|
-
result: {
|
|
54
|
-
result: merge(merged.result.result || [], payload.result.result),
|
|
55
|
-
result_error: merge(merged.result.result_error || [], payload.result.result_error),
|
|
56
|
-
result_next: merge(merged.result.result_next || [], payload.result.result_next),
|
|
57
|
-
result_time: merge(merged.result.result_time || [], payload.result.result_time),
|
|
58
|
-
result_total: merge(merged.result.result_total || [], payload.result.result_total)
|
|
59
|
-
},
|
|
60
|
-
time: { ...merged.time, ...payload.time }
|
|
61
|
-
// eslint-disable-next-line @typescript-eslint/prefer-reduce-type-parameter
|
|
62
|
-
}), { result: {}, time: {} });
|
|
63
|
-
};
|
|
64
|
-
exports.mergeBatchPayloads = mergeBatchPayloads;
|
|
65
|
-
/**
|
|
66
|
-
* Dispatches a batch request with specified commands. Will fill figure out payload type based on the Methods.
|
|
67
|
-
* Supports unlimited number of commands. If they do exceed max amount of commands per batch, will dispatch
|
|
68
|
-
* multiple request and merge result into single batch payload
|
|
69
|
-
* @todo For now due to issues with types it won't check `params` of command, so any Method can be used
|
|
70
|
-
* with any (even wrong) params
|
|
71
|
-
*/
|
|
72
|
-
exports.default = ({ get }) => {
|
|
73
|
-
const batch = async (commands, commandsPerRequest = exports.MAX_COMMANDS_PER_BATCH) => {
|
|
74
|
-
const call = (c) => get(methods_1.Method.BATCH, { searchParams: (0, exports.prepareCommandsQueries)(c) })
|
|
75
|
-
.then(({ body }) => body);
|
|
76
|
-
const calls = (0, exports.chunkCommands)(commands, commandsPerRequest)
|
|
77
|
-
.map(call);
|
|
78
|
-
return await Promise.all(calls)
|
|
79
|
-
.then((chunkedPayloads) => {
|
|
80
|
-
const payloads = (0, exports.mergeBatchPayloads)(chunkedPayloads);
|
|
81
|
-
return (0, call_1.handlePayload)(payloads);
|
|
82
|
-
});
|
|
83
|
-
};
|
|
84
|
-
return batch;
|
|
85
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,494 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
-
};
|
|
28
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
const got_1 = __importDefault(require("got"));
|
|
30
|
-
const lodash_range_1 = __importDefault(require("lodash.range"));
|
|
31
|
-
const nock_1 = __importDefault(require("nock"));
|
|
32
|
-
const methods_1 = require("../../methods");
|
|
33
|
-
const batch_1 = __importStar(require("./batch"));
|
|
34
|
-
describe('Client `chunkCommands` method', () => {
|
|
35
|
-
it('should chunk named commands', () => {
|
|
36
|
-
const chunkSize = 2;
|
|
37
|
-
const commands = {
|
|
38
|
-
a: { method: methods_1.Method.CRM_DEAL_GET },
|
|
39
|
-
b: { method: methods_1.Method.CRM_LEAD_GET },
|
|
40
|
-
c: { method: methods_1.Method.CRM_DEAL_LIST },
|
|
41
|
-
d: { method: methods_1.Method.CRM_LEAD_LIST },
|
|
42
|
-
e: { method: methods_1.Method.CRM_STATUS_LIST }
|
|
43
|
-
};
|
|
44
|
-
expect((0, batch_1.chunkCommands)(commands, chunkSize)).toMatchSnapshot();
|
|
45
|
-
});
|
|
46
|
-
it('should chunk array of commands', () => {
|
|
47
|
-
const chunkSize = 2;
|
|
48
|
-
const commands = [
|
|
49
|
-
{ method: methods_1.Method.CRM_DEAL_GET },
|
|
50
|
-
{ method: methods_1.Method.CRM_LEAD_GET },
|
|
51
|
-
{ method: methods_1.Method.CRM_DEAL_LIST },
|
|
52
|
-
{ method: methods_1.Method.CRM_LEAD_LIST },
|
|
53
|
-
{ method: methods_1.Method.CRM_STATUS_LIST }
|
|
54
|
-
];
|
|
55
|
-
expect((0, batch_1.chunkCommands)(commands, chunkSize)).toMatchSnapshot();
|
|
56
|
-
});
|
|
57
|
-
it('should by default chunk with a size of `MAX_COMMANDS_PER_BATCH`', () => {
|
|
58
|
-
const commandsSets = 2;
|
|
59
|
-
const commands = (0, lodash_range_1.default)(0, batch_1.MAX_COMMANDS_PER_BATCH * commandsSets).map(() => ({
|
|
60
|
-
method: methods_1.Method.CRM_DEAL_GET
|
|
61
|
-
}));
|
|
62
|
-
const chunked = (0, batch_1.chunkCommands)(commands);
|
|
63
|
-
expect(chunked.length).toBe(commandsSets);
|
|
64
|
-
// @ts-ignore we have asserted it length
|
|
65
|
-
expect(chunked[0].length).toBe(batch_1.MAX_COMMANDS_PER_BATCH);
|
|
66
|
-
});
|
|
67
|
-
});
|
|
68
|
-
describe('Client `prepareCommandsQueries` method', () => {
|
|
69
|
-
it('should transform dict of the commands into the query object', () => {
|
|
70
|
-
const testDealId = 11111;
|
|
71
|
-
const commands = {
|
|
72
|
-
one: {
|
|
73
|
-
method: methods_1.Method.CRM_DEAL_GET,
|
|
74
|
-
params: { ID: testDealId }
|
|
75
|
-
},
|
|
76
|
-
two: {
|
|
77
|
-
method: methods_1.Method.CRM_DEAL_LIST,
|
|
78
|
-
params: {
|
|
79
|
-
filter: { '>PROBABILITY': 50 },
|
|
80
|
-
order: { STAGE_ID: 'ASC' },
|
|
81
|
-
select: ['ID', 'TITLE'],
|
|
82
|
-
start: 200
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
expect((0, batch_1.prepareCommandsQueries)(commands)).toMatchSnapshot();
|
|
87
|
-
});
|
|
88
|
-
it('should work with numbered commands', () => {
|
|
89
|
-
const commands = {
|
|
90
|
-
0: { method: methods_1.Method.CRM_DEAL_GET },
|
|
91
|
-
1: { method: methods_1.Method.CRM_DEAL_LIST }
|
|
92
|
-
};
|
|
93
|
-
expect((0, batch_1.prepareCommandsQueries)(commands)).toMatchSnapshot();
|
|
94
|
-
});
|
|
95
|
-
it('should work with array of commands', () => {
|
|
96
|
-
const commands = [{ method: methods_1.Method.CRM_DEAL_GET }, { method: methods_1.Method.CRM_DEAL_LIST }];
|
|
97
|
-
expect((0, batch_1.prepareCommandsQueries)(commands)).toMatchSnapshot();
|
|
98
|
-
});
|
|
99
|
-
it('should return empty query object when no commands provided', () => {
|
|
100
|
-
expect((0, batch_1.prepareCommandsQueries)({})).toMatchSnapshot();
|
|
101
|
-
});
|
|
102
|
-
});
|
|
103
|
-
describe('Client `mergeBatchPayloads` method', () => {
|
|
104
|
-
it('should merge named payloads', () => {
|
|
105
|
-
const batch1 = {
|
|
106
|
-
result: {
|
|
107
|
-
result: {
|
|
108
|
-
a: [{ ID: '1' }, { ID: '2' }],
|
|
109
|
-
b: [{ ID: '3' }, { ID: '4' }]
|
|
110
|
-
},
|
|
111
|
-
result_error: [],
|
|
112
|
-
result_total: { a: 8, b: 8 },
|
|
113
|
-
result_next: { a: 2, b: 4 },
|
|
114
|
-
result_time: {
|
|
115
|
-
a: {
|
|
116
|
-
start: 1567196891.008149,
|
|
117
|
-
finish: 1567196891.022234,
|
|
118
|
-
duration: 0.014085054397583008,
|
|
119
|
-
processing: 0.013998985290527344,
|
|
120
|
-
date_start: '2019-08-30T23:28:11+03:00',
|
|
121
|
-
date_finish: '2019-08-30T23:28:11+03:00'
|
|
122
|
-
},
|
|
123
|
-
b: {
|
|
124
|
-
start: 1567196891.022316,
|
|
125
|
-
finish: 1567196891.03225,
|
|
126
|
-
duration: 0.009933948516845703,
|
|
127
|
-
processing: 0.009846210479736328,
|
|
128
|
-
date_start: '2019-08-30T23:28:11+03:00',
|
|
129
|
-
date_finish: '2019-08-30T23:28:11+03:00'
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
},
|
|
133
|
-
time: {
|
|
134
|
-
start: 1567196890.959017,
|
|
135
|
-
finish: 1567196891.223739,
|
|
136
|
-
duration: 0.2647218704223633,
|
|
137
|
-
processing: 0.21567082405090332,
|
|
138
|
-
date_start: '2019-08-30T23:28:10+03:00',
|
|
139
|
-
date_finish: '2019-08-30T23:28:11+03:00'
|
|
140
|
-
}
|
|
141
|
-
};
|
|
142
|
-
const batch2 = {
|
|
143
|
-
result: {
|
|
144
|
-
result: {
|
|
145
|
-
c: [{ ID: '5' }, { ID: '6' }],
|
|
146
|
-
d: [{ ID: '7' }, { ID: '8' }]
|
|
147
|
-
},
|
|
148
|
-
result_error: [],
|
|
149
|
-
result_total: { c: 8, d: 8 },
|
|
150
|
-
result_next: { c: 6, d: 8 },
|
|
151
|
-
result_time: {
|
|
152
|
-
c: {
|
|
153
|
-
start: 1567196891.032315,
|
|
154
|
-
finish: 1567196891.035297,
|
|
155
|
-
duration: 0.002981901168823242,
|
|
156
|
-
processing: 0.002897024154663086,
|
|
157
|
-
date_start: '2019-08-30T23:28:11+03:00',
|
|
158
|
-
date_finish: '2019-08-30T23:28:11+03:00'
|
|
159
|
-
},
|
|
160
|
-
d: {
|
|
161
|
-
start: 1567196891.03536,
|
|
162
|
-
finish: 1567196891.039936,
|
|
163
|
-
duration: 0.004575967788696289,
|
|
164
|
-
processing: 0.00450897216796875,
|
|
165
|
-
date_start: '2019-08-30T23:28:11+03:00',
|
|
166
|
-
date_finish: '2019-08-30T23:28:11+03:00'
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
},
|
|
170
|
-
time: {
|
|
171
|
-
start: 1567196890.959517,
|
|
172
|
-
finish: 1567196891.225739,
|
|
173
|
-
duration: 0.2647218704223683,
|
|
174
|
-
processing: 0.21567082405090832,
|
|
175
|
-
date_start: '2019-08-30T23:28:12+03:00',
|
|
176
|
-
date_finish: '2019-08-30T23:28:13+03:00'
|
|
177
|
-
}
|
|
178
|
-
};
|
|
179
|
-
expect((0, batch_1.mergeBatchPayloads)([batch1, batch2])).toMatchSnapshot();
|
|
180
|
-
});
|
|
181
|
-
it('should merge array payloads', () => {
|
|
182
|
-
const batch1 = {
|
|
183
|
-
result: {
|
|
184
|
-
result: [
|
|
185
|
-
[{ ID: '1' }, { ID: '2' }],
|
|
186
|
-
[{ ID: '3' }, { ID: '4' }]
|
|
187
|
-
],
|
|
188
|
-
result_error: [],
|
|
189
|
-
result_total: [8, 8],
|
|
190
|
-
result_next: [2, 4],
|
|
191
|
-
result_time: [
|
|
192
|
-
{
|
|
193
|
-
start: 1567196891.008149,
|
|
194
|
-
finish: 1567196891.022234,
|
|
195
|
-
duration: 0.014085054397583008,
|
|
196
|
-
processing: 0.013998985290527344,
|
|
197
|
-
date_start: '2019-08-30T23:28:11+03:00',
|
|
198
|
-
date_finish: '2019-08-30T23:28:11+03:00'
|
|
199
|
-
},
|
|
200
|
-
{
|
|
201
|
-
start: 1567196891.022316,
|
|
202
|
-
finish: 1567196891.03225,
|
|
203
|
-
duration: 0.009933948516845703,
|
|
204
|
-
processing: 0.009846210479736328,
|
|
205
|
-
date_start: '2019-08-30T23:28:11+03:00',
|
|
206
|
-
date_finish: '2019-08-30T23:28:11+03:00'
|
|
207
|
-
}
|
|
208
|
-
]
|
|
209
|
-
},
|
|
210
|
-
time: {
|
|
211
|
-
start: 1567196890.959017,
|
|
212
|
-
finish: 1567196891.223739,
|
|
213
|
-
duration: 0.2647218704223633,
|
|
214
|
-
processing: 0.21567082405090332,
|
|
215
|
-
date_start: '2019-08-30T23:28:10+03:00',
|
|
216
|
-
date_finish: '2019-08-30T23:28:11+03:00'
|
|
217
|
-
}
|
|
218
|
-
};
|
|
219
|
-
const batch2 = {
|
|
220
|
-
result: {
|
|
221
|
-
result: [
|
|
222
|
-
[{ ID: '5' }, { ID: '6' }],
|
|
223
|
-
[{ ID: '7' }, { ID: '8' }]
|
|
224
|
-
],
|
|
225
|
-
result_error: [],
|
|
226
|
-
result_total: [8, 8],
|
|
227
|
-
result_next: [6, 8],
|
|
228
|
-
result_time: [
|
|
229
|
-
{
|
|
230
|
-
start: 1567196891.032315,
|
|
231
|
-
finish: 1567196891.035297,
|
|
232
|
-
duration: 0.002981901168823242,
|
|
233
|
-
processing: 0.002897024154663086,
|
|
234
|
-
date_start: '2019-08-30T23:28:11+03:00',
|
|
235
|
-
date_finish: '2019-08-30T23:28:11+03:00'
|
|
236
|
-
},
|
|
237
|
-
{
|
|
238
|
-
start: 1567196891.03536,
|
|
239
|
-
finish: 1567196891.039936,
|
|
240
|
-
duration: 0.004575967788696289,
|
|
241
|
-
processing: 0.00450897216796875,
|
|
242
|
-
date_start: '2019-08-30T23:28:11+03:00',
|
|
243
|
-
date_finish: '2019-08-30T23:28:11+03:00'
|
|
244
|
-
}
|
|
245
|
-
]
|
|
246
|
-
},
|
|
247
|
-
time: {
|
|
248
|
-
start: 1567196890.959517,
|
|
249
|
-
finish: 1567196891.225739,
|
|
250
|
-
duration: 0.2647218704223683,
|
|
251
|
-
processing: 0.21567082405090832,
|
|
252
|
-
date_start: '2019-08-30T23:28:12+03:00',
|
|
253
|
-
date_finish: '2019-08-30T23:28:13+03:00'
|
|
254
|
-
}
|
|
255
|
-
};
|
|
256
|
-
expect((0, batch_1.mergeBatchPayloads)([batch1, batch2])).toMatchSnapshot();
|
|
257
|
-
});
|
|
258
|
-
});
|
|
259
|
-
const TEST_URI = 'https://test.com/rest';
|
|
260
|
-
const batch = (0, batch_1.default)({
|
|
261
|
-
get: got_1.default.extend({ prefixUrl: TEST_URI, responseType: 'json' }).get
|
|
262
|
-
});
|
|
263
|
-
const RESPONSE_200 = 200;
|
|
264
|
-
describe('Client `batch` method', () => {
|
|
265
|
-
it('should form a proper request', async () => {
|
|
266
|
-
const payload = { result: { result: { one: 'done', two: 'done' }, result_error: [] } };
|
|
267
|
-
const dealId = 999;
|
|
268
|
-
const commands = {
|
|
269
|
-
one: { method: methods_1.Method.CRM_DEAL_GET, params: { ID: dealId } },
|
|
270
|
-
two: { method: methods_1.Method.CRM_DEAL_LIST }
|
|
271
|
-
};
|
|
272
|
-
const scope = (0, nock_1.default)(TEST_URI)
|
|
273
|
-
// @todo We'd want to use `query` object here as it is much more readable, but nock for some reason
|
|
274
|
-
// fails to match request when it contains `cmd[someName]`. The issue definitely connected
|
|
275
|
-
// to the `[]`, since it does not appear when only one bracket present
|
|
276
|
-
.get(`/${methods_1.Method.BATCH}?cmd%5Bone%5D=${commands.one.method}%3FID%3D${dealId}&cmd%5Btwo%5D=${commands.two.method}`)
|
|
277
|
-
.reply(RESPONSE_200, payload);
|
|
278
|
-
await batch(commands);
|
|
279
|
-
expect(scope.done()).toBe(undefined);
|
|
280
|
-
});
|
|
281
|
-
it('should form a proper request with numbered commands', async () => {
|
|
282
|
-
const payload = { result: { result: ['done1', 'done2'], result_error: [] } };
|
|
283
|
-
const dealId = 999;
|
|
284
|
-
const commands = {
|
|
285
|
-
0: { method: methods_1.Method.CRM_DEAL_GET, params: { ID: dealId } },
|
|
286
|
-
1: { method: methods_1.Method.CRM_DEAL_LIST }
|
|
287
|
-
};
|
|
288
|
-
const scope = (0, nock_1.default)(TEST_URI)
|
|
289
|
-
// @todo We'd want to use `query` object here as it is much more readable, but nock for some reason
|
|
290
|
-
// fails to match request when it contains `cmd[someName]`. The issue definitely
|
|
291
|
-
// connected to the `[]` since it does not appear when only one bracket present
|
|
292
|
-
.get(`/${methods_1.Method.BATCH}?cmd%5B0%5D=${commands[0].method}%3FID%3D${dealId}&cmd%5B1%5D=${commands[1].method}`)
|
|
293
|
-
.reply(RESPONSE_200, payload);
|
|
294
|
-
await batch(commands);
|
|
295
|
-
expect(scope.done()).toBe(undefined);
|
|
296
|
-
});
|
|
297
|
-
it('should form a proper request with array of commands', async () => {
|
|
298
|
-
const payload = { result: { result: ['done1', 'done2'], result_error: [] } };
|
|
299
|
-
const dealId = 999;
|
|
300
|
-
const commands = [
|
|
301
|
-
{ method: methods_1.Method.CRM_DEAL_GET, params: { ID: dealId } },
|
|
302
|
-
{ method: methods_1.Method.CRM_DEAL_LIST }
|
|
303
|
-
];
|
|
304
|
-
const scope = (0, nock_1.default)(TEST_URI)
|
|
305
|
-
// @todo We'd want to use `query` object here as it is much more readable, but nock for some reason
|
|
306
|
-
// fails to match request when it contains `cmd[someName]`. The issue definitely
|
|
307
|
-
// connected to the `[]` since it does not appear when only one bracket present
|
|
308
|
-
.get(`/${methods_1.Method.BATCH}?cmd%5B0%5D=${commands[0].method}%3FID%3D${dealId}&cmd%5B1%5D=${commands[1].method}`)
|
|
309
|
-
.reply(RESPONSE_200, payload);
|
|
310
|
-
await batch(commands);
|
|
311
|
-
expect(scope.done()).toBe(undefined);
|
|
312
|
-
});
|
|
313
|
-
it('should form a proper requests with more than max allowed commands per batch', async () => {
|
|
314
|
-
const maxCommandsPerBatch = 1;
|
|
315
|
-
const payload = { result: { result: { one: 'done', two: 'done' }, result_error: [] } };
|
|
316
|
-
const dealId = 999;
|
|
317
|
-
const commands = {
|
|
318
|
-
one: { method: methods_1.Method.CRM_DEAL_GET, params: { ID: dealId } },
|
|
319
|
-
two: { method: methods_1.Method.CRM_DEAL_LIST }
|
|
320
|
-
};
|
|
321
|
-
const scope = (0, nock_1.default)(TEST_URI)
|
|
322
|
-
// @todo We'd want to use `query` object here as it is much more readable, but nock for some reason
|
|
323
|
-
// fails to match request when it contains `cmd[someName]`. The issue definitely connected
|
|
324
|
-
// to the `[]`, since it does not appear when only one bracket present
|
|
325
|
-
.get(`/${methods_1.Method.BATCH}?cmd%5Bone%5D=${commands.one.method}%3FID%3D${dealId}`)
|
|
326
|
-
.reply(RESPONSE_200, payload)
|
|
327
|
-
.get(`/${methods_1.Method.BATCH}?cmd%5Btwo%5D=${commands.two.method}`)
|
|
328
|
-
.reply(RESPONSE_200, payload);
|
|
329
|
-
await batch(commands, maxCommandsPerBatch);
|
|
330
|
-
expect(scope.done()).toBe(undefined);
|
|
331
|
-
});
|
|
332
|
-
it('should form a proper request with more than allowed max numbered commands', async () => {
|
|
333
|
-
const maxCommandsPerBatch = 1;
|
|
334
|
-
const payload = { result: { result: ['done1', 'done2'], result_error: [] } };
|
|
335
|
-
const dealId = 999;
|
|
336
|
-
const commands = {
|
|
337
|
-
0: { method: methods_1.Method.CRM_DEAL_GET, params: { ID: dealId } },
|
|
338
|
-
1: { method: methods_1.Method.CRM_DEAL_LIST }
|
|
339
|
-
};
|
|
340
|
-
const scope = (0, nock_1.default)(TEST_URI)
|
|
341
|
-
// @todo We'd want to use `query` object here as it is much more readable, but nock for some reason
|
|
342
|
-
// fails to match request when it contains `cmd[someName]`. The issue definitely
|
|
343
|
-
// connected to the `[]` since it does not appear when only one bracket present
|
|
344
|
-
.get(`/${methods_1.Method.BATCH}?cmd%5B0%5D=${commands[0].method}%3FID%3D${dealId}`)
|
|
345
|
-
.reply(RESPONSE_200, payload)
|
|
346
|
-
.get(`/${methods_1.Method.BATCH}?cmd%5B1%5D=${commands[1].method}`)
|
|
347
|
-
.reply(RESPONSE_200, payload);
|
|
348
|
-
await batch(commands, maxCommandsPerBatch);
|
|
349
|
-
expect(scope.done()).toBe(undefined);
|
|
350
|
-
});
|
|
351
|
-
it('should form a proper request with more than max allowed array of commands', async () => {
|
|
352
|
-
const maxCommandsPerBatch = 1;
|
|
353
|
-
const payload = { result: { result: ['done1', 'done2'], result_error: [] } };
|
|
354
|
-
const dealId = 999;
|
|
355
|
-
const commands = [
|
|
356
|
-
{ method: methods_1.Method.CRM_DEAL_GET, params: { ID: dealId } },
|
|
357
|
-
{ method: methods_1.Method.CRM_DEAL_LIST }
|
|
358
|
-
];
|
|
359
|
-
const scope = (0, nock_1.default)(TEST_URI)
|
|
360
|
-
// @todo We'd want to use `query` object here as it is much more readable, but nock for some reason
|
|
361
|
-
// fails to match request when it contains `cmd[someName]`. The issue definitely
|
|
362
|
-
// connected to the `[]` since it does not appear when only one bracket present
|
|
363
|
-
.get(`/${methods_1.Method.BATCH}?cmd%5B0%5D=${commands[0].method}%3FID%3D${dealId}`)
|
|
364
|
-
.reply(RESPONSE_200, payload)
|
|
365
|
-
.get(`/${methods_1.Method.BATCH}?cmd%5B0%5D=${commands[1].method}`)
|
|
366
|
-
.reply(RESPONSE_200, payload);
|
|
367
|
-
await batch(commands, maxCommandsPerBatch);
|
|
368
|
-
expect(scope.done()).toBe(undefined);
|
|
369
|
-
});
|
|
370
|
-
it('should return body as payload', async () => {
|
|
371
|
-
const commands = {
|
|
372
|
-
0: { method: methods_1.Method.CRM_DEAL_GET },
|
|
373
|
-
1: { method: methods_1.Method.CRM_DEAL_LIST }
|
|
374
|
-
};
|
|
375
|
-
const payload = {
|
|
376
|
-
result: {
|
|
377
|
-
result: ['done'],
|
|
378
|
-
result_error: [],
|
|
379
|
-
result_next: [],
|
|
380
|
-
result_time: [],
|
|
381
|
-
result_total: []
|
|
382
|
-
},
|
|
383
|
-
time: {}
|
|
384
|
-
};
|
|
385
|
-
(0, nock_1.default)(TEST_URI)
|
|
386
|
-
// @todo We'd want to use `query` object here as it is much more readable, but nock for some reason
|
|
387
|
-
// fails to match request when it contains `cmd[someName]`. The issue definitely connected
|
|
388
|
-
// to the `[]`, since it does not appear when only one bracket present
|
|
389
|
-
.get(`/${methods_1.Method.BATCH}?cmd%5B0%5D=${commands[0].method}&cmd%5B1%5D=${commands[1].method}`)
|
|
390
|
-
.reply(RESPONSE_200, payload);
|
|
391
|
-
expect(await batch(commands)).toEqual(payload);
|
|
392
|
-
});
|
|
393
|
-
// @todo Test this also for named and numbered commands
|
|
394
|
-
it('should merge payloads when more then max allowed commands provided', async () => {
|
|
395
|
-
const maxAllowedCommands = 1;
|
|
396
|
-
const commands = {
|
|
397
|
-
0: { method: methods_1.Method.CRM_DEAL_GET },
|
|
398
|
-
1: { method: methods_1.Method.CRM_DEAL_LIST }
|
|
399
|
-
};
|
|
400
|
-
const payload1 = {
|
|
401
|
-
result: {
|
|
402
|
-
result: ['done1'],
|
|
403
|
-
result_error: [],
|
|
404
|
-
result_next: [1],
|
|
405
|
-
result_time: [],
|
|
406
|
-
result_total: [2]
|
|
407
|
-
},
|
|
408
|
-
time: {}
|
|
409
|
-
};
|
|
410
|
-
const payload2 = {
|
|
411
|
-
result: {
|
|
412
|
-
result: ['done2'],
|
|
413
|
-
result_error: [],
|
|
414
|
-
result_next: [2],
|
|
415
|
-
result_time: [],
|
|
416
|
-
result_total: [2]
|
|
417
|
-
},
|
|
418
|
-
time: {}
|
|
419
|
-
};
|
|
420
|
-
(0, nock_1.default)(TEST_URI)
|
|
421
|
-
// @todo We'd want to use `query` object here as it is much more readable, but nock for some reason
|
|
422
|
-
// fails to match request when it contains `cmd[someName]`. The issue definitely connected
|
|
423
|
-
// to the `[]`, since it does not appear when only one bracket present
|
|
424
|
-
.get(`/${methods_1.Method.BATCH}?cmd%5B0%5D=${commands[0].method}`)
|
|
425
|
-
.reply(RESPONSE_200, payload1)
|
|
426
|
-
.get(`/${methods_1.Method.BATCH}?cmd%5B1%5D=${commands[1].method}`)
|
|
427
|
-
.reply(RESPONSE_200, payload2);
|
|
428
|
-
expect(await batch(commands, maxAllowedCommands)).toMatchSnapshot();
|
|
429
|
-
});
|
|
430
|
-
it.todo('should cast payload to the <P>');
|
|
431
|
-
it('should throw when getting errors in payload', () => {
|
|
432
|
-
const payload = {
|
|
433
|
-
result: {
|
|
434
|
-
result: { one: 'done', two: 'done' },
|
|
435
|
-
result_error: {
|
|
436
|
-
one: 'Expected error from numbered `batch` one',
|
|
437
|
-
two: 'Expected error from numbered `batch` two'
|
|
438
|
-
}
|
|
439
|
-
}
|
|
440
|
-
};
|
|
441
|
-
const commands = {
|
|
442
|
-
one: { method: methods_1.Method.CRM_DEAL_GET },
|
|
443
|
-
two: { method: methods_1.Method.CRM_DEAL_LIST }
|
|
444
|
-
};
|
|
445
|
-
(0, nock_1.default)(TEST_URI)
|
|
446
|
-
// @todo We'd want to use `query` object here as it is much more readable, but nock for some reason
|
|
447
|
-
// fails to match request when it contains `cmd[someName]`. The issue definitely connected
|
|
448
|
-
// to the `[]`, since it does not appear when only one bracket present
|
|
449
|
-
.get(`/${methods_1.Method.BATCH}?cmd%5Bone%5D=${commands.one.method}&cmd%5Btwo%5D=${commands.two.method}`)
|
|
450
|
-
.reply(RESPONSE_200, payload);
|
|
451
|
-
return expect(batch(commands)).rejects.toMatchSnapshot();
|
|
452
|
-
});
|
|
453
|
-
it('should throw when getting errors in numbered commands payload', () => {
|
|
454
|
-
const payload = {
|
|
455
|
-
result: {
|
|
456
|
-
result: ['done'],
|
|
457
|
-
result_error: [
|
|
458
|
-
'Expected error from numbered `batch` 0',
|
|
459
|
-
'Expected error from numbered `batch` 1'
|
|
460
|
-
]
|
|
461
|
-
}
|
|
462
|
-
};
|
|
463
|
-
const commands = {
|
|
464
|
-
0: { method: methods_1.Method.CRM_DEAL_GET },
|
|
465
|
-
1: { method: methods_1.Method.CRM_DEAL_LIST }
|
|
466
|
-
};
|
|
467
|
-
(0, nock_1.default)(TEST_URI)
|
|
468
|
-
// @todo We'd want to use `query` object here as it is much more readable, but nock for some reason
|
|
469
|
-
// fails to match request when it contains `cmd[someName]`. The issue definitely connected
|
|
470
|
-
// to the `[]`, since it does not appear when only one bracket present
|
|
471
|
-
.get(`/${methods_1.Method.BATCH}?cmd%5B0%5D=${commands[0].method}&cmd%5B1%5D=${commands[1].method}`)
|
|
472
|
-
.reply(RESPONSE_200, payload);
|
|
473
|
-
return expect(batch(commands)).rejects.toMatchSnapshot();
|
|
474
|
-
});
|
|
475
|
-
it('should throw when getting errors in array of commands payload', () => {
|
|
476
|
-
const payload = {
|
|
477
|
-
result: {
|
|
478
|
-
result: ['done'],
|
|
479
|
-
result_error: [
|
|
480
|
-
'Expected error from array of command `batch` 0',
|
|
481
|
-
'Expected error from array of command `batch` 1'
|
|
482
|
-
]
|
|
483
|
-
}
|
|
484
|
-
};
|
|
485
|
-
const commands = [{ method: methods_1.Method.CRM_DEAL_GET }, { method: methods_1.Method.CRM_DEAL_LIST }];
|
|
486
|
-
(0, nock_1.default)(TEST_URI)
|
|
487
|
-
// @todo We'd want to use `query` object here as it is much more readable, but nock for some reason
|
|
488
|
-
// fails to match request when it contains `cmd[someName]`. The issue definitely connected
|
|
489
|
-
// to the `[]`, since it does not appear when only one bracket present
|
|
490
|
-
.get(`/${methods_1.Method.BATCH}?cmd%5B0%5D=${commands[0].method}&cmd%5B1%5D=${commands[1].method}`)
|
|
491
|
-
.reply(RESPONSE_200, payload);
|
|
492
|
-
return expect(batch(commands)).rejects.toMatchSnapshot();
|
|
493
|
-
});
|
|
494
|
-
});
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import got from 'got';
|
|
2
|
-
import { Method, MethodParams, MethodPayload } from '../../methods';
|
|
3
|
-
import { Payload } from '../../payloads';
|
|
4
|
-
/**
|
|
5
|
-
* Checks wether payload have any errors and if it does — throws them
|
|
6
|
-
* Bitrix payload types do not provide discriminators, so we're forced to type cast them
|
|
7
|
-
*/
|
|
8
|
-
export declare const handlePayload: <P extends Payload<unknown>>(payload: P) => P;
|
|
9
|
-
export declare type Call = <M extends Method>(method: M, params: MethodParams<M>) => Promise<MethodPayload<M>>;
|
|
10
|
-
declare type Dependencies = {
|
|
11
|
-
readonly get: typeof got.get;
|
|
12
|
-
};
|
|
13
|
-
/**
|
|
14
|
-
* Dispatches a request with specified method and params. Will fill figure out payload type based on the Method
|
|
15
|
-
*/
|
|
16
|
-
declare const _default: ({ get }: Dependencies) => Call;
|
|
17
|
-
export default _default;
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.handlePayload = void 0;
|
|
7
|
-
const qs_1 = require("qs");
|
|
8
|
-
const isArray_1 = __importDefault(require("../../utils/isArray"));
|
|
9
|
-
/**
|
|
10
|
-
* Checks wether payload have any errors and if it does — throws them
|
|
11
|
-
* Bitrix payload types do not provide discriminators, so we're forced to type cast them
|
|
12
|
-
*/
|
|
13
|
-
const handlePayload = (payload) => {
|
|
14
|
-
var _a;
|
|
15
|
-
if (payload.error) {
|
|
16
|
-
throw new Error(`[call] failed to get the resource: ${(_a = payload.error) !== null && _a !== void 0 ? _a : ''}.`);
|
|
17
|
-
}
|
|
18
|
-
if (payload.result && payload.result.result_error) {
|
|
19
|
-
const resultErrors = payload.result.result_error;
|
|
20
|
-
const errors = (0, isArray_1.default)(resultErrors) ? resultErrors : Object.values(resultErrors);
|
|
21
|
-
if (errors.length > 0) {
|
|
22
|
-
// @todo We can give better formatting to display errored commands. But it's not important for now
|
|
23
|
-
throw new Error(`[batch] failed to process. Received errors in ${errors.length} commands:\n${errors.join('\n')}`);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
return payload;
|
|
27
|
-
};
|
|
28
|
-
exports.handlePayload = handlePayload;
|
|
29
|
-
/**
|
|
30
|
-
* Dispatches a request with specified method and params. Will fill figure out payload type based on the Method
|
|
31
|
-
*/
|
|
32
|
-
exports.default = ({ get }) => {
|
|
33
|
-
const call = (method, params) => get(method, { searchParams: (0, qs_1.stringify)(params) })
|
|
34
|
-
.then(({ body }) => (0, exports.handlePayload)(body));
|
|
35
|
-
return call;
|
|
36
|
-
};
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|