@2bad/bitrix 2.5.1 → 3.0.0

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