@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.
Files changed (242) hide show
  1. package/README.md +10 -9
  2. package/build/bitrix.d.ts +65 -0
  3. package/build/bitrix.js +55 -0
  4. package/build/client/hooks/addAccessToken.js +9 -0
  5. package/build/{module/client → client}/index.d.ts +3 -3
  6. package/build/{module/client → client}/index.js +21 -16
  7. package/build/{module/client → client}/methods/batch.d.ts +6 -6
  8. package/build/client/methods/batch.js +75 -0
  9. package/build/{module/client → client}/methods/call.d.ts +4 -4
  10. package/build/{module/client → client}/methods/call.js +8 -9
  11. package/build/{module/client → client}/methods/list.d.ts +10 -10
  12. package/build/client/methods/list.js +58 -0
  13. package/build/{module/commands.d.ts → commands.d.ts} +3 -3
  14. package/build/commands.js +1 -0
  15. package/build/{module/methods.d.ts → methods.d.ts} +19 -19
  16. package/build/{module/methods.js → methods.js} +5 -5
  17. package/build/{main/payloads.d.ts → payloads.d.ts} +5 -5
  18. package/build/payloads.js +1 -0
  19. package/build/{main/services → services}/common.d.ts +7 -7
  20. package/build/services/common.js +2 -0
  21. package/build/{main/services → services}/companies/entities.d.ts +2 -2
  22. package/build/services/companies/entities.js +1 -0
  23. package/build/services/companies/index.d.ts +15 -0
  24. package/build/services/companies/index.js +17 -0
  25. package/build/{module/services → services}/companies/methods.d.ts +5 -5
  26. package/build/services/companies/methods.js +1 -0
  27. package/build/{module/services → services}/contacts/entities.d.ts +2 -2
  28. package/build/services/contacts/entities.js +1 -0
  29. package/build/services/contacts/index.d.ts +15 -0
  30. package/build/services/contacts/index.js +17 -0
  31. package/build/{module/services → services}/contacts/methods.d.ts +5 -5
  32. package/build/services/contacts/methods.js +1 -0
  33. package/build/{module/services → services}/currencies/entities.d.ts +2 -2
  34. package/build/services/currencies/entities.js +1 -0
  35. package/build/{module/services → services}/deals/entities.d.ts +2 -2
  36. package/build/services/deals/entities.js +1 -0
  37. package/build/services/deals/index.d.ts +15 -0
  38. package/build/services/deals/index.js +17 -0
  39. package/build/{main/services → services}/deals/methods.d.ts +5 -5
  40. package/build/services/deals/methods.js +1 -0
  41. package/build/{module/services → services}/leads/entities.d.ts +2 -2
  42. package/build/services/leads/entities.js +1 -0
  43. package/build/services/leads/index.d.ts +15 -0
  44. package/build/services/leads/index.js +17 -0
  45. package/build/{main/services → services}/leads/methods.d.ts +5 -5
  46. package/build/services/leads/methods.js +1 -0
  47. package/build/{main/services → services}/statuses/entities.d.ts +3 -3
  48. package/build/services/statuses/entities.js +1 -0
  49. package/build/services/statuses/index.d.ts +14 -0
  50. package/build/services/statuses/index.js +19 -0
  51. package/build/{main/services → services}/statuses/methods.d.ts +5 -5
  52. package/build/services/statuses/methods.js +1 -0
  53. package/build/{module/services → services}/users/entities.d.ts +2 -2
  54. package/build/services/users/entities.js +1 -0
  55. package/build/services/users/index.d.ts +9 -0
  56. package/build/services/users/index.js +7 -0
  57. package/build/{main/services → services}/users/methods.d.ts +4 -4
  58. package/build/services/users/methods.js +1 -0
  59. package/build/utils/Diff.d.ts +1 -0
  60. package/build/utils/Diff.js +7 -0
  61. package/build/{module/utils → utils}/ExtractValue.d.ts +1 -1
  62. package/build/{main/utils/ExtractValue.d.ts → utils/ExtractValue.js} +1 -2
  63. package/build/utils/isArray.d.ts +2 -0
  64. package/build/{module/utils → utils}/isArray.js +1 -1
  65. package/package.json +43 -49
  66. package/build/main/bitrix.d.ts +0 -114
  67. package/build/main/bitrix.integration.test.d.ts +0 -0
  68. package/build/main/bitrix.integration.test.js +0 -31
  69. package/build/main/bitrix.js +0 -58
  70. package/build/main/bitrix.unit.test.d.ts +0 -1
  71. package/build/main/bitrix.unit.test.js +0 -44
  72. package/build/main/client/client.unit.test.d.ts +0 -1
  73. package/build/main/client/client.unit.test.js +0 -70
  74. package/build/main/client/hooks/addAccessToken.js +0 -13
  75. package/build/main/client/hooks/addAccessToken.unit.test.d.ts +0 -1
  76. package/build/main/client/hooks/addAccessToken.unit.test.js +0 -31
  77. package/build/main/client/index.d.ts +0 -15
  78. package/build/main/client/index.js +0 -53
  79. package/build/main/client/methods/batch.d.ts +0 -40
  80. package/build/main/client/methods/batch.js +0 -85
  81. package/build/main/client/methods/batch.unit.test.d.ts +0 -1
  82. package/build/main/client/methods/batch.unit.test.js +0 -494
  83. package/build/main/client/methods/call.d.ts +0 -17
  84. package/build/main/client/methods/call.js +0 -36
  85. package/build/main/client/methods/call.unit.test.d.ts +0 -1
  86. package/build/main/client/methods/call.unit.test.js +0 -183
  87. package/build/main/client/methods/list.d.ts +0 -30
  88. package/build/main/client/methods/list.js +0 -66
  89. package/build/main/client/methods/list.unit.test.d.ts +0 -1
  90. package/build/main/client/methods/list.unit.test.js +0 -277
  91. package/build/main/commands.d.ts +0 -10
  92. package/build/main/commands.js +0 -2
  93. package/build/main/methods.d.ts +0 -92
  94. package/build/main/methods.js +0 -41
  95. package/build/main/payloads.js +0 -2
  96. package/build/main/services/common.js +0 -2
  97. package/build/main/services/companies/entities.js +0 -2
  98. package/build/main/services/companies/index.d.ts +0 -26
  99. package/build/main/services/companies/index.integration.test.d.ts +0 -1
  100. package/build/main/services/companies/index.integration.test.js +0 -19
  101. package/build/main/services/companies/index.js +0 -10
  102. package/build/main/services/companies/index.unit.test.d.ts +0 -1
  103. package/build/main/services/companies/index.unit.test.js +0 -72
  104. package/build/main/services/companies/methods.d.ts +0 -44
  105. package/build/main/services/companies/methods.js +0 -3
  106. package/build/main/services/contacts/entities.d.ts +0 -45
  107. package/build/main/services/contacts/entities.js +0 -2
  108. package/build/main/services/contacts/index.d.ts +0 -26
  109. package/build/main/services/contacts/index.integration.test.d.ts +0 -1
  110. package/build/main/services/contacts/index.integration.test.js +0 -19
  111. package/build/main/services/contacts/index.js +0 -10
  112. package/build/main/services/contacts/index.unit.test.d.ts +0 -1
  113. package/build/main/services/contacts/index.unit.test.js +0 -72
  114. package/build/main/services/contacts/methods.d.ts +0 -44
  115. package/build/main/services/contacts/methods.js +0 -3
  116. package/build/main/services/currencies/entities.d.ts +0 -15
  117. package/build/main/services/currencies/entities.js +0 -2
  118. package/build/main/services/deals/entities.d.ts +0 -43
  119. package/build/main/services/deals/entities.js +0 -2
  120. package/build/main/services/deals/index.d.ts +0 -26
  121. package/build/main/services/deals/index.integration.test.d.ts +0 -1
  122. package/build/main/services/deals/index.integration.test.js +0 -19
  123. package/build/main/services/deals/index.js +0 -10
  124. package/build/main/services/deals/index.unit.test.d.ts +0 -1
  125. package/build/main/services/deals/index.unit.test.js +0 -72
  126. package/build/main/services/deals/methods.js +0 -3
  127. package/build/main/services/leads/entities.d.ts +0 -49
  128. package/build/main/services/leads/entities.js +0 -2
  129. package/build/main/services/leads/index.d.ts +0 -26
  130. package/build/main/services/leads/index.integration.test.d.ts +0 -1
  131. package/build/main/services/leads/index.integration.test.js +0 -19
  132. package/build/main/services/leads/index.js +0 -10
  133. package/build/main/services/leads/index.unit.test.d.ts +0 -1
  134. package/build/main/services/leads/index.unit.test.js +0 -72
  135. package/build/main/services/leads/methods.js +0 -3
  136. package/build/main/services/statuses/entities.js +0 -2
  137. package/build/main/services/statuses/index.d.ts +0 -14
  138. package/build/main/services/statuses/index.integration.test.d.ts +0 -1
  139. package/build/main/services/statuses/index.integration.test.js +0 -90
  140. package/build/main/services/statuses/index.js +0 -11
  141. package/build/main/services/statuses/index.unit.test.d.ts +0 -1
  142. package/build/main/services/statuses/index.unit.test.js +0 -60
  143. package/build/main/services/statuses/methods.js +0 -3
  144. package/build/main/services/users/entities.d.ts +0 -40
  145. package/build/main/services/users/entities.js +0 -2
  146. package/build/main/services/users/index.d.ts +0 -9
  147. package/build/main/services/users/index.integration.test.d.ts +0 -1
  148. package/build/main/services/users/index.integration.test.js +0 -33
  149. package/build/main/services/users/index.js +0 -7
  150. package/build/main/services/users/index.unit.test.d.ts +0 -1
  151. package/build/main/services/users/index.unit.test.js +0 -25
  152. package/build/main/services/users/methods.js +0 -3
  153. package/build/main/utils/Diff.d.ts +0 -1
  154. package/build/main/utils/Diff.js +0 -2
  155. package/build/main/utils/ExtractValue.js +0 -2
  156. package/build/main/utils/isArray.d.ts +0 -2
  157. package/build/main/utils/isArray.js +0 -4
  158. package/build/module/bitrix.d.ts +0 -114
  159. package/build/module/bitrix.integration.test.d.ts +0 -0
  160. package/build/module/bitrix.integration.test.js +0 -31
  161. package/build/module/bitrix.js +0 -39
  162. package/build/module/bitrix.unit.test.d.ts +0 -1
  163. package/build/module/bitrix.unit.test.js +0 -16
  164. package/build/module/client/client.unit.test.d.ts +0 -1
  165. package/build/module/client/client.unit.test.js +0 -65
  166. package/build/module/client/hooks/addAccessToken.d.ts +0 -6
  167. package/build/module/client/hooks/addAccessToken.js +0 -11
  168. package/build/module/client/hooks/addAccessToken.unit.test.d.ts +0 -1
  169. package/build/module/client/hooks/addAccessToken.unit.test.js +0 -26
  170. package/build/module/client/methods/batch.js +0 -76
  171. package/build/module/client/methods/batch.unit.test.d.ts +0 -1
  172. package/build/module/client/methods/batch.unit.test.js +0 -466
  173. package/build/module/client/methods/call.unit.test.d.ts +0 -1
  174. package/build/module/client/methods/call.unit.test.js +0 -155
  175. package/build/module/client/methods/list.js +0 -55
  176. package/build/module/client/methods/list.unit.test.d.ts +0 -1
  177. package/build/module/client/methods/list.unit.test.js +0 -252
  178. package/build/module/commands.js +0 -1
  179. package/build/module/payloads.d.ts +0 -40
  180. package/build/module/payloads.js +0 -1
  181. package/build/module/services/common.d.ts +0 -22
  182. package/build/module/services/common.js +0 -1
  183. package/build/module/services/companies/entities.d.ts +0 -54
  184. package/build/module/services/companies/entities.js +0 -1
  185. package/build/module/services/companies/index.d.ts +0 -26
  186. package/build/module/services/companies/index.integration.test.d.ts +0 -1
  187. package/build/module/services/companies/index.integration.test.js +0 -14
  188. package/build/module/services/companies/index.js +0 -8
  189. package/build/module/services/companies/index.unit.test.d.ts +0 -1
  190. package/build/module/services/companies/index.unit.test.js +0 -67
  191. package/build/module/services/companies/methods.js +0 -1
  192. package/build/module/services/contacts/entities.js +0 -1
  193. package/build/module/services/contacts/index.d.ts +0 -26
  194. package/build/module/services/contacts/index.integration.test.d.ts +0 -1
  195. package/build/module/services/contacts/index.integration.test.js +0 -14
  196. package/build/module/services/contacts/index.js +0 -8
  197. package/build/module/services/contacts/index.unit.test.d.ts +0 -1
  198. package/build/module/services/contacts/index.unit.test.js +0 -67
  199. package/build/module/services/contacts/methods.js +0 -1
  200. package/build/module/services/currencies/entities.js +0 -1
  201. package/build/module/services/deals/entities.js +0 -1
  202. package/build/module/services/deals/index.d.ts +0 -26
  203. package/build/module/services/deals/index.integration.test.d.ts +0 -1
  204. package/build/module/services/deals/index.integration.test.js +0 -14
  205. package/build/module/services/deals/index.js +0 -8
  206. package/build/module/services/deals/index.unit.test.d.ts +0 -1
  207. package/build/module/services/deals/index.unit.test.js +0 -67
  208. package/build/module/services/deals/methods.d.ts +0 -44
  209. package/build/module/services/deals/methods.js +0 -1
  210. package/build/module/services/leads/entities.js +0 -1
  211. package/build/module/services/leads/index.d.ts +0 -26
  212. package/build/module/services/leads/index.integration.test.d.ts +0 -1
  213. package/build/module/services/leads/index.integration.test.js +0 -14
  214. package/build/module/services/leads/index.js +0 -8
  215. package/build/module/services/leads/index.unit.test.d.ts +0 -1
  216. package/build/module/services/leads/index.unit.test.js +0 -67
  217. package/build/module/services/leads/methods.d.ts +0 -44
  218. package/build/module/services/leads/methods.js +0 -1
  219. package/build/module/services/statuses/entities.d.ts +0 -16
  220. package/build/module/services/statuses/entities.js +0 -1
  221. package/build/module/services/statuses/index.d.ts +0 -14
  222. package/build/module/services/statuses/index.integration.test.d.ts +0 -1
  223. package/build/module/services/statuses/index.integration.test.js +0 -85
  224. package/build/module/services/statuses/index.js +0 -9
  225. package/build/module/services/statuses/index.unit.test.d.ts +0 -1
  226. package/build/module/services/statuses/index.unit.test.js +0 -55
  227. package/build/module/services/statuses/methods.d.ts +0 -51
  228. package/build/module/services/statuses/methods.js +0 -1
  229. package/build/module/services/users/entities.js +0 -1
  230. package/build/module/services/users/index.d.ts +0 -9
  231. package/build/module/services/users/index.integration.test.d.ts +0 -1
  232. package/build/module/services/users/index.integration.test.js +0 -28
  233. package/build/module/services/users/index.js +0 -5
  234. package/build/module/services/users/index.unit.test.d.ts +0 -1
  235. package/build/module/services/users/index.unit.test.js +0 -20
  236. package/build/module/services/users/methods.d.ts +0 -17
  237. package/build/module/services/users/methods.js +0 -1
  238. package/build/module/utils/Diff.d.ts +0 -1
  239. package/build/module/utils/Diff.js +0 -1
  240. package/build/module/utils/ExtractValue.js +0 -1
  241. package/build/module/utils/isArray.d.ts +0 -2
  242. /package/build/{main/client → client}/hooks/addAccessToken.d.ts +0 -0
@@ -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 +0,0 @@
1
- export {};
@@ -1,155 +0,0 @@
1
- import got from 'got';
2
- import nock from 'nock';
3
- import { Method } from '../../methods';
4
- import Call, { handlePayload } from './call';
5
- const TEST_URI = 'https://test.com/rest';
6
- const call = Call({
7
- get: got.extend({ prefixUrl: TEST_URI, responseType: 'json' }).get
8
- });
9
- const RESPONSE_200 = 200;
10
- describe('Client `handlePayload` method', () => {
11
- it('should return `GetPayload`', () => {
12
- const payload = {
13
- result: 'done',
14
- time: { start: 1, finish: 1, duration: 1, processing: 1, date_start: 'date', date_finish: 'date' }
15
- };
16
- expect(handlePayload(payload)).toBe(payload);
17
- });
18
- it('should return `ListPayload`', () => {
19
- const payload = {
20
- error: undefined,
21
- next: undefined,
22
- result: ['done'],
23
- time: { start: 1, finish: 1, duration: 1, processing: 1, date_start: 'date', date_finish: 'date' },
24
- total: 1
25
- };
26
- expect(handlePayload(payload)).toBe(payload);
27
- });
28
- it('should throw when getting error in `ListPayload`', () => {
29
- const payload = {
30
- error: 'Expected error for `handlePayload`',
31
- next: undefined,
32
- result: ['done'],
33
- time: { start: 1, finish: 1, duration: 1, processing: 1, date_start: 'date', date_finish: 'date' },
34
- total: 1
35
- };
36
- expect(() => handlePayload(payload)).toThrowErrorMatchingSnapshot();
37
- });
38
- it('should return `BatchPayload` with numbered commands', () => {
39
- const payload = {
40
- result: {
41
- result: { one: 'done' },
42
- result_error: [],
43
- result_next: [],
44
- result_time: [],
45
- result_total: []
46
- },
47
- time: { start: 1, finish: 1, duration: 1, processing: 1, date_start: 'date', date_finish: 'date' }
48
- };
49
- expect(handlePayload(payload)).toBe(payload);
50
- });
51
- it('should return `BatchPayload` with result from array of commands', () => {
52
- const payload = {
53
- result: {
54
- result: ['done'],
55
- result_error: [],
56
- result_next: [],
57
- result_time: [],
58
- result_total: []
59
- },
60
- time: { start: 1, finish: 1, duration: 1, processing: 1, date_start: 'date', date_finish: 'date' }
61
- };
62
- expect(handlePayload(payload)).toBe(payload);
63
- });
64
- it('should throw when getting errors in `BatchPayload` with result from named commands', () => {
65
- const payload = {
66
- result: {
67
- result: { one: 'done' },
68
- result_error: { one: 'Expected error for `handlePayload`' },
69
- result_next: [],
70
- result_time: [],
71
- result_total: []
72
- },
73
- time: { start: 1, finish: 1, duration: 1, processing: 1, date_start: 'date', date_finish: 'date' }
74
- };
75
- expect(() => handlePayload(payload)).toThrowErrorMatchingSnapshot();
76
- });
77
- it('should throw when getting errors in `BatchPayload` with result from array of commands', () => {
78
- const payload = {
79
- result: {
80
- result: ['done'],
81
- result_error: ['Expected error for `handlePayload`'],
82
- result_next: [],
83
- result_time: [],
84
- result_total: []
85
- },
86
- time: { start: 1, finish: 1, duration: 1, processing: 1, date_start: 'date', date_finish: 'date' }
87
- };
88
- expect(() => handlePayload(payload)).toThrowErrorMatchingSnapshot();
89
- });
90
- });
91
- describe('Client `call` method', () => {
92
- it('should form a proper request', async () => {
93
- const params = { id: '1' };
94
- const scope = nock(TEST_URI)
95
- .get(`/${Method.CRM_DEAL_GET}`)
96
- .query(params)
97
- .reply(RESPONSE_200);
98
- await call(Method.CRM_DEAL_GET, params);
99
- expect(scope.done()).toBe(undefined);
100
- });
101
- it('should form a proper request with list methods', async () => {
102
- const params = {
103
- filter: { '>PROBABILITY': 50 },
104
- order: { STAGE_ID: 'ASC' },
105
- select: ['ID', 'TITLE'],
106
- start: 50
107
- };
108
- const scope = nock(TEST_URI)
109
- .get(`/${Method.CRM_DEAL_LIST}`)
110
- .query(params)
111
- .reply(RESPONSE_200);
112
- await call(Method.CRM_DEAL_LIST, params);
113
- expect(scope.done()).toBe(undefined);
114
- });
115
- it('should return body as payload', async () => {
116
- const params = { id: '1' };
117
- const payload = { test: 'value' };
118
- nock(TEST_URI)
119
- .get(`/${Method.CRM_DEAL_GET}`)
120
- .query(params)
121
- .reply(RESPONSE_200, payload);
122
- expect(await call(Method.CRM_DEAL_GET, params)).toEqual(payload);
123
- });
124
- it('should return body as payload with list methods', async () => {
125
- const payload = { result: ['done'] };
126
- nock(TEST_URI)
127
- .get(`/${Method.CRM_DEAL_LIST}`)
128
- .reply(RESPONSE_200, payload);
129
- expect(await call(Method.CRM_DEAL_LIST, {})).toEqual(payload);
130
- });
131
- it('should throw when getting errors in payload', () => {
132
- const payload = { result: ['done'], error: 'Expected error for `getList`' };
133
- nock(TEST_URI)
134
- .get(`/${Method.CRM_DEAL_LIST}`)
135
- .reply(RESPONSE_200, payload);
136
- return expect(call(Method.CRM_DEAL_LIST, {})).rejects.toMatchSnapshot();
137
- });
138
- it('should throw when getting errors in batch payload', () => {
139
- const payload = {
140
- result: {
141
- result: ['done'],
142
- result_error: ['Expected error'],
143
- result_next: [],
144
- result_time: [],
145
- result_total: []
146
- },
147
- time: { start: 1, finish: 1, duration: 1, processing: 1, date_start: 'date', date_finish: 'date' }
148
- };
149
- nock(TEST_URI)
150
- .get(`/${Method.BATCH}`)
151
- .reply(RESPONSE_200, payload);
152
- return expect(call(Method.BATCH, {})).rejects.toMatchSnapshot();
153
- });
154
- it.todo('should cast payload to the <P>');
155
- });
@@ -1,55 +0,0 @@
1
- import range from 'lodash.range';
2
- const MAX_ENTRIES_PER_COMMAND = 50;
3
- /**
4
- * Generates required amount of commands to process specified amount of entries
5
- */
6
- export const fillWithCommands = ({ method, params }, start, toProcess, entriesPerCommand) => {
7
- const requiresCommands = Math.ceil((toProcess - start) / entriesPerCommand);
8
- return range(0, requiresCommands)
9
- .map((i) => ({ method, params: { ...params, start: start + (entriesPerCommand * i) } }));
10
- };
11
- /**
12
- * Get the highest value from object or an array if there's any
13
- */
14
- export const highest = (input) => Object.values(input).reduce((a, b) => a !== undefined && b !== undefined
15
- ? a > b ? a : b
16
- : a !== undefined && b === undefined
17
- ? a
18
- : a === undefined && b !== undefined
19
- ? b
20
- : undefined, undefined);
21
- /**
22
- * Converts batch payload to a list payload
23
- */
24
- export const batchToListPayload = (payload) => {
25
- // eslint-disable-next-line @typescript-eslint/naming-convention
26
- const { result: { result, result_total, result_error, result_next }, time } = payload;
27
- const flattenResult = Object.entries(result).reduce(
28
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
29
- // @ts-expect-error ignore this for now
30
- (flatten, [_key, r]) => !r ? flatten : [...flatten, ...r], []);
31
- return {
32
- error: Object.values(result_error).join('\n'),
33
- next: highest(result_next),
34
- result: flattenResult,
35
- // @todo Not accurate, we do not care
36
- time,
37
- total: highest(result_total) ?? 0
38
- };
39
- };
40
- /**
41
- * Gets all entries by dispatching required amount of requests. Will fill figure out payload type based on the Method.
42
- */
43
- export default ({ call, batch }) => {
44
- const list = async (method, params) => {
45
- const start = params.start ?? 0;
46
- const listAll = async () => {
47
- const batchCommands = fillWithCommands({ method, params }, start, firstCall.total, MAX_ENTRIES_PER_COMMAND);
48
- const payload = await batch(batchCommands);
49
- return batchToListPayload(payload);
50
- };
51
- const firstCall = await call(method, { ...params, start });
52
- return !firstCall.next ? firstCall : await listAll();
53
- };
54
- return list;
55
- };
@@ -1 +0,0 @@
1
- export {};