@2bad/bitrix 2.4.0 → 2.5.1

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 (229) hide show
  1. package/LICENSE +0 -0
  2. package/README.md +54 -49
  3. package/build/main/bitrix.d.ts +10 -2
  4. package/build/main/bitrix.integration.test.d.ts +0 -0
  5. package/build/main/bitrix.integration.test.js +31 -0
  6. package/build/main/bitrix.js +24 -10
  7. package/build/main/bitrix.unit.test.d.ts +1 -0
  8. package/build/main/bitrix.unit.test.js +44 -0
  9. package/build/main/client/client.unit.test.d.ts +1 -0
  10. package/build/main/client/client.unit.test.js +70 -0
  11. package/build/main/client/hooks/addAccessToken.d.ts +4 -1
  12. package/build/main/client/hooks/addAccessToken.js +3 -1
  13. package/build/main/client/hooks/addAccessToken.unit.test.d.ts +1 -0
  14. package/build/main/client/hooks/addAccessToken.unit.test.js +31 -0
  15. package/build/main/client/index.d.ts +9 -1
  16. package/build/main/client/index.js +17 -6
  17. package/build/main/client/methods/batch.d.ts +19 -0
  18. package/build/main/client/methods/batch.js +35 -10
  19. package/build/main/client/methods/batch.unit.test.d.ts +1 -0
  20. package/build/main/client/methods/batch.unit.test.js +494 -0
  21. package/build/main/client/methods/call.d.ts +7 -0
  22. package/build/main/client/methods/call.js +11 -4
  23. package/build/main/client/methods/call.unit.test.d.ts +1 -0
  24. package/build/main/client/methods/call.unit.test.js +183 -0
  25. package/build/main/client/methods/list.d.ts +12 -0
  26. package/build/main/client/methods/list.js +23 -7
  27. package/build/main/client/methods/list.unit.test.d.ts +1 -0
  28. package/build/main/client/methods/list.unit.test.js +277 -0
  29. package/build/main/commands.js +0 -1
  30. package/build/main/methods.d.ts +19 -0
  31. package/build/main/methods.js +0 -1
  32. package/build/main/payloads.js +0 -1
  33. package/build/main/services/common.d.ts +1 -0
  34. package/build/main/services/common.js +0 -1
  35. package/build/main/services/companies/entities.js +0 -1
  36. package/build/main/services/companies/index.integration.test.d.ts +1 -0
  37. package/build/main/services/companies/index.integration.test.js +19 -0
  38. package/build/main/services/companies/index.js +0 -1
  39. package/build/main/services/companies/index.unit.test.d.ts +1 -0
  40. package/build/main/services/companies/index.unit.test.js +72 -0
  41. package/build/main/services/companies/methods.js +0 -1
  42. package/build/main/services/contacts/entities.d.ts +2 -2
  43. package/build/main/services/contacts/entities.js +0 -1
  44. package/build/main/services/contacts/index.integration.test.d.ts +1 -0
  45. package/build/main/services/contacts/index.integration.test.js +19 -0
  46. package/build/main/services/contacts/index.js +0 -1
  47. package/build/main/services/contacts/index.unit.test.d.ts +1 -0
  48. package/build/main/services/contacts/index.unit.test.js +72 -0
  49. package/build/main/services/contacts/methods.js +0 -1
  50. package/build/main/services/currencies/entities.js +0 -1
  51. package/build/main/services/deals/entities.js +0 -1
  52. package/build/main/services/deals/index.integration.test.d.ts +1 -0
  53. package/build/main/services/deals/index.integration.test.js +19 -0
  54. package/build/main/services/deals/index.js +0 -1
  55. package/build/main/services/deals/index.unit.test.d.ts +1 -0
  56. package/build/main/services/deals/index.unit.test.js +72 -0
  57. package/build/main/services/deals/methods.js +0 -1
  58. package/build/main/services/leads/entities.js +0 -1
  59. package/build/main/services/leads/index.integration.test.d.ts +1 -0
  60. package/build/main/services/leads/index.integration.test.js +19 -0
  61. package/build/main/services/leads/index.js +0 -1
  62. package/build/main/services/leads/index.unit.test.d.ts +1 -0
  63. package/build/main/services/leads/index.unit.test.js +72 -0
  64. package/build/main/services/leads/methods.js +0 -1
  65. package/build/main/services/statuses/entities.js +0 -1
  66. package/build/main/services/statuses/index.integration.test.d.ts +1 -0
  67. package/build/main/services/statuses/index.integration.test.js +90 -0
  68. package/build/main/services/statuses/index.js +0 -1
  69. package/build/main/services/statuses/index.unit.test.d.ts +1 -0
  70. package/build/main/services/statuses/index.unit.test.js +60 -0
  71. package/build/main/services/statuses/methods.js +0 -1
  72. package/build/main/services/users/entities.js +0 -1
  73. package/build/main/services/users/index.d.ts +1 -1
  74. package/build/main/services/users/index.integration.test.d.ts +1 -0
  75. package/build/main/services/users/index.integration.test.js +33 -0
  76. package/build/main/services/users/index.js +0 -1
  77. package/build/main/services/users/index.unit.test.d.ts +1 -0
  78. package/build/main/services/users/index.unit.test.js +25 -0
  79. package/build/main/services/users/methods.js +0 -1
  80. package/build/main/utils/Diff.js +0 -1
  81. package/build/main/utils/ExtractValue.d.ts +5 -0
  82. package/build/main/utils/ExtractValue.js +0 -1
  83. package/build/main/utils/isArray.js +1 -1
  84. package/build/module/bitrix.d.ts +10 -2
  85. package/build/module/bitrix.integration.test.d.ts +0 -0
  86. package/build/module/bitrix.integration.test.js +31 -0
  87. package/build/module/bitrix.js +10 -2
  88. package/build/module/bitrix.unit.test.d.ts +1 -0
  89. package/build/module/bitrix.unit.test.js +16 -0
  90. package/build/module/client/client.unit.test.d.ts +1 -0
  91. package/build/module/client/client.unit.test.js +65 -0
  92. package/build/module/client/hooks/addAccessToken.d.ts +4 -1
  93. package/build/module/client/hooks/addAccessToken.js +3 -1
  94. package/build/module/client/hooks/addAccessToken.unit.test.d.ts +1 -0
  95. package/build/module/client/hooks/addAccessToken.unit.test.js +26 -0
  96. package/build/module/client/index.d.ts +9 -1
  97. package/build/module/client/index.js +14 -3
  98. package/build/module/client/methods/batch.d.ts +19 -0
  99. package/build/module/client/methods/batch.js +26 -1
  100. package/build/module/client/methods/batch.unit.test.d.ts +1 -0
  101. package/build/module/client/methods/batch.unit.test.js +466 -0
  102. package/build/module/client/methods/call.d.ts +7 -0
  103. package/build/module/client/methods/call.js +9 -3
  104. package/build/module/client/methods/call.unit.test.d.ts +1 -0
  105. package/build/module/client/methods/call.unit.test.js +155 -0
  106. package/build/module/client/methods/list.d.ts +12 -0
  107. package/build/module/client/methods/list.js +20 -6
  108. package/build/module/client/methods/list.unit.test.d.ts +1 -0
  109. package/build/module/client/methods/list.unit.test.js +252 -0
  110. package/build/module/commands.js +0 -1
  111. package/build/module/methods.d.ts +19 -0
  112. package/build/module/methods.js +0 -1
  113. package/build/module/payloads.js +0 -1
  114. package/build/module/services/common.d.ts +1 -0
  115. package/build/module/services/common.js +0 -1
  116. package/build/module/services/companies/entities.js +0 -1
  117. package/build/module/services/companies/index.integration.test.d.ts +1 -0
  118. package/build/module/services/companies/index.integration.test.js +14 -0
  119. package/build/module/services/companies/index.js +0 -1
  120. package/build/module/services/companies/index.unit.test.d.ts +1 -0
  121. package/build/module/services/companies/index.unit.test.js +67 -0
  122. package/build/module/services/companies/methods.js +0 -1
  123. package/build/module/services/contacts/entities.d.ts +2 -2
  124. package/build/module/services/contacts/entities.js +0 -1
  125. package/build/module/services/contacts/index.integration.test.d.ts +1 -0
  126. package/build/module/services/contacts/index.integration.test.js +14 -0
  127. package/build/module/services/contacts/index.js +0 -1
  128. package/build/module/services/contacts/index.unit.test.d.ts +1 -0
  129. package/build/module/services/contacts/index.unit.test.js +67 -0
  130. package/build/module/services/contacts/methods.js +0 -1
  131. package/build/module/services/currencies/entities.js +0 -1
  132. package/build/module/services/deals/entities.js +0 -1
  133. package/build/module/services/deals/index.integration.test.d.ts +1 -0
  134. package/build/module/services/deals/index.integration.test.js +14 -0
  135. package/build/module/services/deals/index.js +0 -1
  136. package/build/module/services/deals/index.unit.test.d.ts +1 -0
  137. package/build/module/services/deals/index.unit.test.js +67 -0
  138. package/build/module/services/deals/methods.js +0 -1
  139. package/build/module/services/leads/entities.js +0 -1
  140. package/build/module/services/leads/index.integration.test.d.ts +1 -0
  141. package/build/module/services/leads/index.integration.test.js +14 -0
  142. package/build/module/services/leads/index.js +0 -1
  143. package/build/module/services/leads/index.unit.test.d.ts +1 -0
  144. package/build/module/services/leads/index.unit.test.js +67 -0
  145. package/build/module/services/leads/methods.js +0 -1
  146. package/build/module/services/statuses/entities.js +0 -1
  147. package/build/module/services/statuses/index.integration.test.d.ts +1 -0
  148. package/build/module/services/statuses/index.integration.test.js +85 -0
  149. package/build/module/services/statuses/index.js +0 -1
  150. package/build/module/services/statuses/index.unit.test.d.ts +1 -0
  151. package/build/module/services/statuses/index.unit.test.js +55 -0
  152. package/build/module/services/statuses/methods.js +0 -1
  153. package/build/module/services/users/entities.js +0 -1
  154. package/build/module/services/users/index.d.ts +1 -1
  155. package/build/module/services/users/index.integration.test.d.ts +1 -0
  156. package/build/module/services/users/index.integration.test.js +28 -0
  157. package/build/module/services/users/index.js +0 -1
  158. package/build/module/services/users/index.unit.test.d.ts +1 -0
  159. package/build/module/services/users/index.unit.test.js +20 -0
  160. package/build/module/services/users/methods.js +0 -1
  161. package/build/module/utils/Diff.js +0 -1
  162. package/build/module/utils/ExtractValue.d.ts +5 -0
  163. package/build/module/utils/ExtractValue.js +0 -1
  164. package/build/module/utils/isArray.js +1 -1
  165. package/package.json +21 -22
  166. package/build/main/bitrix.js.map +0 -1
  167. package/build/main/client/hooks/addAccessToken.js.map +0 -1
  168. package/build/main/client/index.js.map +0 -1
  169. package/build/main/client/methods/batch.js.map +0 -1
  170. package/build/main/client/methods/call.js.map +0 -1
  171. package/build/main/client/methods/list.js.map +0 -1
  172. package/build/main/commands.js.map +0 -1
  173. package/build/main/methods.js.map +0 -1
  174. package/build/main/payloads.js.map +0 -1
  175. package/build/main/services/common.js.map +0 -1
  176. package/build/main/services/companies/entities.js.map +0 -1
  177. package/build/main/services/companies/index.js.map +0 -1
  178. package/build/main/services/companies/methods.js.map +0 -1
  179. package/build/main/services/contacts/entities.js.map +0 -1
  180. package/build/main/services/contacts/index.js.map +0 -1
  181. package/build/main/services/contacts/methods.js.map +0 -1
  182. package/build/main/services/currencies/entities.js.map +0 -1
  183. package/build/main/services/deals/entities.js.map +0 -1
  184. package/build/main/services/deals/index.js.map +0 -1
  185. package/build/main/services/deals/methods.js.map +0 -1
  186. package/build/main/services/leads/entities.js.map +0 -1
  187. package/build/main/services/leads/index.js.map +0 -1
  188. package/build/main/services/leads/methods.js.map +0 -1
  189. package/build/main/services/statuses/entities.js.map +0 -1
  190. package/build/main/services/statuses/index.js.map +0 -1
  191. package/build/main/services/statuses/methods.js.map +0 -1
  192. package/build/main/services/users/entities.js.map +0 -1
  193. package/build/main/services/users/index.js.map +0 -1
  194. package/build/main/services/users/methods.js.map +0 -1
  195. package/build/main/utils/Diff.js.map +0 -1
  196. package/build/main/utils/ExtractValue.js.map +0 -1
  197. package/build/main/utils/isArray.js.map +0 -1
  198. package/build/module/bitrix.js.map +0 -1
  199. package/build/module/client/hooks/addAccessToken.js.map +0 -1
  200. package/build/module/client/index.js.map +0 -1
  201. package/build/module/client/methods/batch.js.map +0 -1
  202. package/build/module/client/methods/call.js.map +0 -1
  203. package/build/module/client/methods/list.js.map +0 -1
  204. package/build/module/commands.js.map +0 -1
  205. package/build/module/methods.js.map +0 -1
  206. package/build/module/payloads.js.map +0 -1
  207. package/build/module/services/common.js.map +0 -1
  208. package/build/module/services/companies/entities.js.map +0 -1
  209. package/build/module/services/companies/index.js.map +0 -1
  210. package/build/module/services/companies/methods.js.map +0 -1
  211. package/build/module/services/contacts/entities.js.map +0 -1
  212. package/build/module/services/contacts/index.js.map +0 -1
  213. package/build/module/services/contacts/methods.js.map +0 -1
  214. package/build/module/services/currencies/entities.js.map +0 -1
  215. package/build/module/services/deals/entities.js.map +0 -1
  216. package/build/module/services/deals/index.js.map +0 -1
  217. package/build/module/services/deals/methods.js.map +0 -1
  218. package/build/module/services/leads/entities.js.map +0 -1
  219. package/build/module/services/leads/index.js.map +0 -1
  220. package/build/module/services/leads/methods.js.map +0 -1
  221. package/build/module/services/statuses/entities.js.map +0 -1
  222. package/build/module/services/statuses/index.js.map +0 -1
  223. package/build/module/services/statuses/methods.js.map +0 -1
  224. package/build/module/services/users/entities.js.map +0 -1
  225. package/build/module/services/users/index.js.map +0 -1
  226. package/build/module/services/users/methods.js.map +0 -1
  227. package/build/module/utils/Diff.js.map +0 -1
  228. package/build/module/utils/ExtractValue.js.map +0 -1
  229. package/build/module/utils/isArray.js.map +0 -1
@@ -0,0 +1,466 @@
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,10 +1,17 @@
1
1
  import got from 'got';
2
2
  import { Method, MethodParams, MethodPayload } from '../../methods';
3
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
+ */
4
8
  export declare const handlePayload: <P extends Payload<unknown>>(payload: P) => P;
5
9
  export declare type Call = <M extends Method>(method: M, params: MethodParams<M>) => Promise<MethodPayload<M>>;
6
10
  declare type Dependencies = {
7
11
  readonly get: typeof got.get;
8
12
  };
13
+ /**
14
+ * Dispatches a request with specified method and params. Will fill figure out payload type based on the Method
15
+ */
9
16
  declare const _default: ({ get }: Dependencies) => Call;
10
17
  export default _default;
@@ -1,22 +1,28 @@
1
1
  import { stringify as toQuery } from 'qs';
2
2
  import isArray from '../../utils/isArray';
3
+ /**
4
+ * Checks wether payload have any errors and if it does — throws them
5
+ * Bitrix payload types do not provide discriminators, so we're forced to type cast them
6
+ */
3
7
  export const handlePayload = (payload) => {
4
- var _a;
5
8
  if (payload.error) {
6
- throw new Error(`[call] failed to get the resource: ${(_a = payload.error) !== null && _a !== void 0 ? _a : ''}.`);
9
+ throw new Error(`[call] failed to get the resource: ${payload.error ?? ''}.`);
7
10
  }
8
11
  if (payload.result && payload.result.result_error) {
9
12
  const resultErrors = payload.result.result_error;
10
13
  const errors = isArray(resultErrors) ? resultErrors : Object.values(resultErrors);
11
14
  if (errors.length > 0) {
15
+ // @todo We can give better formatting to display errored commands. But it's not important for now
12
16
  throw new Error(`[batch] failed to process. Received errors in ${errors.length} commands:\n${errors.join('\n')}`);
13
17
  }
14
18
  }
15
19
  return payload;
16
20
  };
21
+ /**
22
+ * Dispatches a request with specified method and params. Will fill figure out payload type based on the Method
23
+ */
17
24
  export default ({ get }) => {
18
25
  const call = (method, params) => get(method, { searchParams: toQuery(params) })
19
26
  .then(({ body }) => handlePayload(body));
20
27
  return call;
21
28
  };
22
- //# sourceMappingURL=call.js.map
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,155 @@
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
+ });