@2bad/bitrix 2.4.0 → 2.5.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 (229) hide show
  1. package/LICENSE +0 -0
  2. package/README.md +52 -49
  3. package/build/main/bitrix.d.ts +9 -1
  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 +20 -9
  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 +9 -1
  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 +8 -1
  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
@@ -3,16 +3,28 @@ import { ListableMethod, MethodParams, MethodPayloadType } from '../../methods';
3
3
  import { BatchPayload, ListPayload } from '../../payloads';
4
4
  import { Batch } from './batch';
5
5
  import { Call } from './call';
6
+ /**
7
+ * Generates required amount of commands to process specified amount of entries
8
+ */
6
9
  export declare const fillWithCommands: <C extends Command>({ method, params }: C, start: number, toProcess: number, entriesPerCommand: number) => readonly {
7
10
  readonly method: C["method"];
8
11
  readonly params?: C["params"] | undefined;
9
12
  }[];
13
+ /**
14
+ * Get the highest value from object or an array if there's any
15
+ */
10
16
  export declare const highest: (input: ReadonlyArray<number | undefined> | Record<string, number | undefined>) => number | undefined;
17
+ /**
18
+ * Converts batch payload to a list payload
19
+ */
11
20
  export declare const batchToListPayload: <P>(payload: BatchPayload<Record<string, P> | readonly P[]>) => ListPayload<P>;
12
21
  declare type Dependencies = {
13
22
  readonly call: Call;
14
23
  readonly batch: Batch;
15
24
  };
16
25
  export declare type List = <M extends ListableMethod>(method: M, params: MethodParams<M>) => Promise<ListPayload<MethodPayloadType<M>>>;
26
+ /**
27
+ * Gets all entries by dispatching required amount of requests. Will fill figure out payload type based on the Method.
28
+ */
17
29
  declare const _default: ({ call, batch }: Dependencies) => List;
18
30
  export default _default;
@@ -1,10 +1,16 @@
1
1
  import range from 'lodash.range';
2
2
  const MAX_ENTRIES_PER_COMMAND = 50;
3
+ /**
4
+ * Generates required amount of commands to process specified amount of entries
5
+ */
3
6
  export const fillWithCommands = ({ method, params }, start, toProcess, entriesPerCommand) => {
4
7
  const requiresCommands = Math.ceil((toProcess - start) / entriesPerCommand);
5
8
  return range(0, requiresCommands)
6
9
  .map((i) => ({ method, params: { ...params, start: start + (entriesPerCommand * i) } }));
7
10
  };
11
+ /**
12
+ * Get the highest value from object or an array if there's any
13
+ */
8
14
  export const highest = (input) => Object.values(input).reduce((a, b) => a !== undefined && b !== undefined
9
15
  ? a > b ? a : b
10
16
  : a !== undefined && b === undefined
@@ -12,22 +18,31 @@ export const highest = (input) => Object.values(input).reduce((a, b) => a !== un
12
18
  : a === undefined && b !== undefined
13
19
  ? b
14
20
  : undefined, undefined);
21
+ /**
22
+ * Converts batch payload to a list payload
23
+ */
15
24
  export const batchToListPayload = (payload) => {
16
- var _a;
25
+ // eslint-disable-next-line @typescript-eslint/naming-convention
17
26
  const { result: { result, result_total, result_error, result_next }, time } = payload;
18
- const flattenResult = Object.entries(result).reduce((flatten, [_key, r]) => !r ? flatten : [...flatten, ...r], []);
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], []);
19
31
  return {
20
32
  error: Object.values(result_error).join('\n'),
21
33
  next: highest(result_next),
22
34
  result: flattenResult,
35
+ // @todo Not accurate, we do not care
23
36
  time,
24
- total: (_a = highest(result_total)) !== null && _a !== void 0 ? _a : 0
37
+ total: highest(result_total) ?? 0
25
38
  };
26
39
  };
40
+ /**
41
+ * Gets all entries by dispatching required amount of requests. Will fill figure out payload type based on the Method.
42
+ */
27
43
  export default ({ call, batch }) => {
28
44
  const list = async (method, params) => {
29
- var _a;
30
- const start = (_a = params.start) !== null && _a !== void 0 ? _a : 0;
45
+ const start = params.start ?? 0;
31
46
  const listAll = async () => {
32
47
  const batchCommands = fillWithCommands({ method, params }, start, firstCall.total, MAX_ENTRIES_PER_COMMAND);
33
48
  const payload = await batch(batchCommands);
@@ -38,4 +53,3 @@ export default ({ call, batch }) => {
38
53
  };
39
54
  return list;
40
55
  };
41
- //# sourceMappingURL=list.js.map
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,252 @@
1
+ import { Method } from '../../methods';
2
+ import List, { batchToListPayload, fillWithCommands, highest } from './list';
3
+ describe('Client `fillWithCommands` method', () => {
4
+ it('should fill array with required amount of commands to process all entries', () => {
5
+ const command = { method: Method.CRM_DEAL_LIST, params: { select: ['*'] } };
6
+ const start = 0;
7
+ const toProcess = 7;
8
+ const entriesPerCommand = 2;
9
+ expect(fillWithCommands(command, start, toProcess, entriesPerCommand)).toMatchSnapshot();
10
+ });
11
+ it('should properly take into account start', () => {
12
+ const command = { method: Method.CRM_DEAL_LIST, params: { select: ['*'] } };
13
+ const start = 2;
14
+ const toProcess = 7;
15
+ const entriesPerCommand = 2;
16
+ expect(fillWithCommands(command, start, toProcess, entriesPerCommand)).toMatchSnapshot();
17
+ });
18
+ it('should override `params.start`', () => {
19
+ const wrongStart = 99;
20
+ const command = { method: Method.CRM_DEAL_LIST, params: { start: wrongStart } };
21
+ const start = 0;
22
+ const toProcess = 2;
23
+ const entriesPerCommand = 1;
24
+ const commands = fillWithCommands(command, start, toProcess, entriesPerCommand);
25
+ expect(Object.values(commands).map((c) => c.params)).toMatchSnapshot();
26
+ });
27
+ });
28
+ describe('Client `highest` method', () => {
29
+ it('should get highest value from object', () => {
30
+ expect(highest({ a: undefined })).toBe(undefined);
31
+ expect(highest({ a: undefined, b: undefined })).toBe(undefined);
32
+ expect(highest({ a: 1, b: undefined })).toBe(1);
33
+ expect(highest({ a: undefined, b: 1 })).toBe(1);
34
+ expect(highest({ a: 1, b: 2 })).toBe(2);
35
+ expect(highest({ a: 2, b: 1 })).toBe(2);
36
+ });
37
+ it('should get highest value from array', () => {
38
+ expect(highest([undefined])).toBe(undefined);
39
+ expect(highest([undefined, undefined])).toBe(undefined);
40
+ expect(highest([1, undefined])).toBe(1);
41
+ expect(highest([undefined, 1])).toBe(1);
42
+ expect(highest([1, 2])).toBe(2);
43
+ expect(highest([2, 1])).toBe(2);
44
+ });
45
+ });
46
+ describe('Client `batchToListPayload` method', () => {
47
+ it('should convert batch with named payloads to a list payload', () => {
48
+ const payload = {
49
+ result: {
50
+ result: {
51
+ a: [{ ID: '1' }, { ID: '2' }],
52
+ b: [{ ID: '3' }, { ID: '4' }]
53
+ },
54
+ result_error: { a: 'Expected error A', b: 'Expected error B' },
55
+ result_total: { a: 4, b: 4 },
56
+ result_next: { a: 2, b: 4 },
57
+ result_time: {
58
+ a: {
59
+ start: 1567196891.008149,
60
+ finish: 1567196891.022234,
61
+ duration: 0.014085054397583008,
62
+ processing: 0.013998985290527344,
63
+ date_start: '2019-08-30T23:28:11+03:00',
64
+ date_finish: '2019-08-30T23:28:11+03:00'
65
+ },
66
+ b: {
67
+ start: 1567196891.022316,
68
+ finish: 1567196891.03225,
69
+ duration: 0.009933948516845703,
70
+ processing: 0.009846210479736328,
71
+ date_start: '2019-08-30T23:28:11+03:00',
72
+ date_finish: '2019-08-30T23:28:11+03:00'
73
+ }
74
+ }
75
+ },
76
+ time: {
77
+ start: 1567196890.959017,
78
+ finish: 1567196891.223739,
79
+ duration: 0.2647218704223633,
80
+ processing: 0.21567082405090332,
81
+ date_start: '2019-08-30T23:28:10+03:00',
82
+ date_finish: '2019-08-30T23:28:11+03:00'
83
+ }
84
+ };
85
+ expect(batchToListPayload(payload)).toMatchSnapshot();
86
+ });
87
+ it('should convert batch with payload of arrays to a list payload', () => {
88
+ const payload = {
89
+ result: {
90
+ result: [
91
+ [{ ID: '1' }, { ID: '2' }],
92
+ [{ ID: '3' }, { ID: '4' }]
93
+ ],
94
+ result_error: ['Expected error 1', 'Expected error 2'],
95
+ result_total: [4, 4],
96
+ result_next: [2, 4],
97
+ result_time: [{
98
+ start: 1567196891.008149,
99
+ finish: 1567196891.022234,
100
+ duration: 0.014085054397583008,
101
+ processing: 0.013998985290527344,
102
+ date_start: '2019-08-30T23:28:11+03:00',
103
+ date_finish: '2019-08-30T23:28:11+03:00'
104
+ }, {
105
+ start: 1567196891.022316,
106
+ finish: 1567196891.03225,
107
+ duration: 0.009933948516845703,
108
+ processing: 0.009846210479736328,
109
+ date_start: '2019-08-30T23:28:11+03:00',
110
+ date_finish: '2019-08-30T23:28:11+03:00'
111
+ }]
112
+ },
113
+ time: {
114
+ start: 1567196890.959017,
115
+ finish: 1567196891.223739,
116
+ duration: 0.2647218704223633,
117
+ processing: 0.21567082405090332,
118
+ date_start: '2019-08-30T23:28:10+03:00',
119
+ date_finish: '2019-08-30T23:28:11+03:00'
120
+ }
121
+ };
122
+ expect(batchToListPayload(payload)).toMatchSnapshot();
123
+ });
124
+ it('should handle undefined in `result`', () => {
125
+ const payload = {
126
+ result: {
127
+ result: [undefined, [{ ID: '1' }], undefined],
128
+ result_error: [],
129
+ result_total: [],
130
+ result_next: [],
131
+ result_time: [{
132
+ start: 1567196891.008149,
133
+ finish: 1567196891.022234,
134
+ duration: 0.014085054397583008,
135
+ processing: 0.013998985290527344,
136
+ date_start: '2019-08-30T23:28:11+03:00',
137
+ date_finish: '2019-08-30T23:28:11+03:00'
138
+ }, {
139
+ start: 1567196891.022316,
140
+ finish: 1567196891.03225,
141
+ duration: 0.009933948516845703,
142
+ processing: 0.009846210479736328,
143
+ date_start: '2019-08-30T23:28:11+03:00',
144
+ date_finish: '2019-08-30T23:28:11+03:00'
145
+ }]
146
+ },
147
+ time: {
148
+ start: 1567196890.959017,
149
+ finish: 1567196891.223739,
150
+ duration: 0.2647218704223633,
151
+ processing: 0.21567082405090332,
152
+ date_start: '2019-08-30T23:28:10+03:00',
153
+ date_finish: '2019-08-30T23:28:11+03:00'
154
+ }
155
+ };
156
+ expect(batchToListPayload(payload)).toMatchSnapshot();
157
+ });
158
+ });
159
+ describe('Client `list` method', () => {
160
+ it('should make one request when entries can be fetched in one go', async () => {
161
+ const callMock = jest.fn(() => Promise.resolve({ next: false }));
162
+ const batchMock = jest.fn(() => Promise.resolve({}));
163
+ const list = List({ call: callMock, batch: batchMock });
164
+ await list(Method.CRM_DEAL_LIST, {});
165
+ expect(callMock.mock.calls).toMatchSnapshot();
166
+ expect(batchMock).not.toBeCalled();
167
+ });
168
+ it('should make multiple requests when entries can not be fetched in one go', async () => {
169
+ const callMock = jest.fn(() => Promise.resolve({ next: 2, total: 120 }));
170
+ const batchMock = jest.fn(() => Promise.resolve({
171
+ result: {
172
+ result: ['done'],
173
+ result_error: [],
174
+ result_next: [],
175
+ result_time: [],
176
+ result_total: []
177
+ },
178
+ time: {}
179
+ }));
180
+ const list = List({ call: callMock, batch: batchMock });
181
+ await list(Method.CRM_DEAL_LIST, {});
182
+ expect(callMock.mock.calls).toMatchSnapshot();
183
+ expect(batchMock.mock.calls).toMatchSnapshot();
184
+ });
185
+ it('should return a first request payload when entities can be fetched in a single request', async () => {
186
+ const mockPayload = { next: false, result: 'test' };
187
+ const callMock = jest.fn(() => Promise.resolve(mockPayload));
188
+ const batchMock = jest.fn(() => Promise.resolve({}));
189
+ const list = List({ call: callMock, batch: batchMock });
190
+ const payload = await list(Method.CRM_DEAL_LIST, {});
191
+ expect(payload).toEqual(mockPayload);
192
+ });
193
+ it('should return a flatten payload when entities can not be fetched in a single request', async () => {
194
+ const mockPayload = {
195
+ result: {
196
+ result: {
197
+ a: [{ ID: '1' }, { ID: '2' }],
198
+ b: [{ ID: '3' }, { ID: '4' }]
199
+ },
200
+ result_error: { a: 'Expected error A', b: 'Expected error B' },
201
+ result_total: { a: 4, b: 4 },
202
+ result_next: { a: 2, b: 4 },
203
+ result_time: {
204
+ a: { start: 1567196891.008149 },
205
+ b: { start: 1567196891.022316 }
206
+ }
207
+ },
208
+ time: { start: 1567196890.959017 }
209
+ };
210
+ const callMock = jest.fn(() => Promise.resolve({ next: 2 }));
211
+ const batchMock = jest.fn(() => Promise.resolve(mockPayload));
212
+ const list = List({ call: callMock, batch: batchMock });
213
+ const payload = await list(Method.CRM_DEAL_LIST, {});
214
+ expect(payload).toMatchSnapshot();
215
+ });
216
+ it('should default start to 0', async () => {
217
+ const callMock = jest.fn(() => Promise.resolve({ next: 2, total: 20 }));
218
+ const batchMock = jest.fn(() => Promise.resolve({
219
+ result: {
220
+ result: ['done'],
221
+ result_error: [],
222
+ result_next: [],
223
+ result_time: [],
224
+ result_total: []
225
+ },
226
+ time: {}
227
+ }));
228
+ const list = List({ call: callMock, batch: batchMock });
229
+ await list(Method.CRM_DEAL_LIST, {});
230
+ expect(callMock.mock.calls[0]).toMatchSnapshot();
231
+ expect(batchMock.mock.calls[0]).toMatchSnapshot();
232
+ });
233
+ it('should properly take into account start', async () => {
234
+ const callMock = jest.fn(() => Promise.resolve({ next: 2, total: 120 }));
235
+ const batchMock = jest.fn(() => Promise.resolve({
236
+ result: {
237
+ result: ['done'],
238
+ result_error: [],
239
+ result_next: [],
240
+ result_time: [],
241
+ result_total: []
242
+ },
243
+ time: {}
244
+ }));
245
+ const list = List({ call: callMock, batch: batchMock });
246
+ await list(Method.CRM_DEAL_LIST, { start: 27 });
247
+ expect(callMock.mock.calls).toMatchSnapshot();
248
+ expect(batchMock.mock.calls).toMatchSnapshot();
249
+ });
250
+ it.todo('should cast first request payload to the <P>');
251
+ it.todo('should cast payload results to the <P>');
252
+ });
@@ -1,2 +1 @@
1
1
  export {};
2
- //# sourceMappingURL=commands.js.map
@@ -59,6 +59,13 @@ export declare type ListParams = {
59
59
  };
60
60
  readonly select?: readonly string[];
61
61
  };
62
+ /**
63
+ * A mega map of all supported Bitrix methods to their parameters. Used to resolve
64
+ * low-level client method required params and payload types.
65
+ * - `type` — a type that method associated with
66
+ * - `payload` — a payload that method returns
67
+ * - `params` — params that method accepts
68
+ */
62
69
  export declare type Methods = MethodsMap & CompaniesMethods & ContactsMethods & DealsMethods & LeadsMethods & StatusesMethods & UsersMethods & {
63
70
  readonly [Method.BATCH]: {
64
71
  readonly type: unknown;
@@ -66,8 +73,20 @@ export declare type Methods = MethodsMap & CompaniesMethods & ContactsMethods &
66
73
  readonly params: Commands;
67
74
  };
68
75
  };
76
+ /**
77
+ * Retrieve specified method data
78
+ */
69
79
  export declare type MethodData<M extends Method> = ExtractValue<Methods, M>;
80
+ /**
81
+ * Retrieve associated with specified method type
82
+ */
70
83
  export declare type MethodPayloadType<M extends Method> = MethodData<M>['type'];
84
+ /**
85
+ * Retrieve specified method payload type
86
+ */
71
87
  export declare type MethodPayload<M extends Method> = MethodData<M>['payload'];
88
+ /**
89
+ * Retrieve specified method params
90
+ */
72
91
  export declare type MethodParams<M extends Method> = MethodData<M>['params'];
73
92
  export {};
@@ -36,4 +36,3 @@ const LISTABLE_METHODS = [
36
36
  Method.CRM_DEAL_LIST,
37
37
  Method.CRM_LEAD_LIST
38
38
  ];
39
- //# sourceMappingURL=methods.js.map
@@ -1,2 +1 @@
1
1
  export {};
2
- //# sourceMappingURL=payloads.js.map
@@ -19,3 +19,4 @@ export declare type MultiField = {
19
19
  readonly VALUE: string;
20
20
  readonly TYPE_ID: string;
21
21
  };
22
+ export declare type MultiFieldArray = ReadonlyArray<Pick<MultiField, 'VALUE' | 'VALUE_TYPE'>>;
@@ -1,2 +1 @@
1
1
  export {};
2
- //# sourceMappingURL=common.js.map
@@ -1,2 +1 @@
1
1
  export {};
2
- //# sourceMappingURL=entities.js.map
@@ -0,0 +1,14 @@
1
+ import Bitrix from './../../bitrix';
2
+ const WEBHOOK_URL = process.env['WEBHOOK_URL'];
3
+ if (!WEBHOOK_URL) {
4
+ throw Error('Integration tests require environmental variable `WEBHOOK_URL` to be set');
5
+ }
6
+ const { companies } = Bitrix(WEBHOOK_URL);
7
+ describe('Companies', () => {
8
+ describe('fields', () => {
9
+ it('should get all fields', async () => {
10
+ const { result } = await companies.fields();
11
+ expect(result).toMatchSnapshot();
12
+ });
13
+ });
14
+ });
@@ -6,4 +6,3 @@ export default ({ call, list }) => ({
6
6
  list: (params = {}) => list(Method.CRM_COMPANY_LIST, params),
7
7
  update: (id, fields, params) => call(Method.CRM_COMPANY_UPDATE, { id, fields, params })
8
8
  });
9
- //# sourceMappingURL=index.js.map
@@ -0,0 +1,67 @@
1
+ import Companies from '.';
2
+ const mockCall = jest.fn(() => Promise.resolve());
3
+ const mockList = jest.fn(() => Promise.resolve());
4
+ const companies = Companies({ call: mockCall, list: mockList });
5
+ const ID = '77';
6
+ describe('Companies', () => {
7
+ beforeEach(() => {
8
+ mockCall.mockClear();
9
+ mockList.mockClear();
10
+ });
11
+ it('should expose API methods', () => {
12
+ expect(companies).toMatchSnapshot();
13
+ });
14
+ describe('`create`', () => {
15
+ it('should invoke `call`', async () => {
16
+ await companies.create({
17
+ COMMENTS: 'Some comment'
18
+ }, {
19
+ REGISTER_SONET_EVENT: 'Y'
20
+ });
21
+ expect(mockCall.mock.calls).toMatchSnapshot();
22
+ });
23
+ it('should work without optional params', async () => {
24
+ await companies.create({
25
+ COMMENTS: 'Some comment'
26
+ });
27
+ expect(mockCall.mock.calls).toMatchSnapshot();
28
+ });
29
+ });
30
+ describe('`get`', () => {
31
+ it('should invoke `call`', async () => {
32
+ await companies.get(ID);
33
+ expect(mockCall.mock.calls).toMatchSnapshot();
34
+ });
35
+ });
36
+ describe('`list`', () => {
37
+ it('should invoke `list`', async () => {
38
+ await companies.list({
39
+ start: 0,
40
+ order: { COMMENTS: 'ASC' },
41
+ filter: { '>PROBABILITY': 50 },
42
+ select: ['*', 'UF_*']
43
+ });
44
+ expect(mockList.mock.calls).toMatchSnapshot();
45
+ });
46
+ it('should work without optional params', async () => {
47
+ await companies.list();
48
+ expect(mockList.mock.calls).toMatchSnapshot();
49
+ });
50
+ });
51
+ describe('`update`', () => {
52
+ it('should invoke `call`', async () => {
53
+ await companies.update(ID, {
54
+ COMMENTS: 'Some comment'
55
+ }, {
56
+ REGISTER_SONET_EVENT: 'Y'
57
+ });
58
+ expect(mockCall.mock.calls).toMatchSnapshot();
59
+ });
60
+ it('should work without optional params', async () => {
61
+ await companies.update(ID, {
62
+ COMMENTS: 'Some comment'
63
+ });
64
+ expect(mockCall.mock.calls).toMatchSnapshot();
65
+ });
66
+ });
67
+ });
@@ -1,2 +1 @@
1
1
  import { Method } from '../../methods';
2
- //# sourceMappingURL=methods.js.map
@@ -1,6 +1,6 @@
1
- import { BoolString, ISODate, NumberString } from '../common';
1
+ import { BoolString, ISODate, MultiFieldArray, NumberString } from '../common';
2
2
  export declare type Contact = {
3
- readonly [key: string]: string | null;
3
+ readonly [key: string]: MultiFieldArray | string | null;
4
4
  readonly ID: NumberString;
5
5
  readonly POST: string | null;
6
6
  readonly COMMENTS: string | null;
@@ -1,2 +1 @@
1
1
  export {};
2
- //# sourceMappingURL=entities.js.map
@@ -0,0 +1,14 @@
1
+ import Bitrix from './../../bitrix';
2
+ const WEBHOOK_URL = process.env['WEBHOOK_URL'];
3
+ if (!WEBHOOK_URL) {
4
+ throw Error('Integration tests require environmental variable `WEBHOOK_URL` to be set');
5
+ }
6
+ const { contacts } = Bitrix(WEBHOOK_URL);
7
+ describe('Contacts', () => {
8
+ describe('fields', () => {
9
+ it('should get all fields', async () => {
10
+ const { result } = await contacts.fields();
11
+ expect(result).toMatchSnapshot();
12
+ });
13
+ });
14
+ });
@@ -6,4 +6,3 @@ export default ({ call, list }) => ({
6
6
  list: (params = {}) => list(Method.CRM_CONTACT_LIST, params),
7
7
  update: (id, fields, params) => call(Method.CRM_CONTACT_UPDATE, { id, fields, params })
8
8
  });
9
- //# sourceMappingURL=index.js.map
@@ -0,0 +1,67 @@
1
+ import Contacts from '.';
2
+ const mockCall = jest.fn(() => Promise.resolve());
3
+ const mockList = jest.fn(() => Promise.resolve());
4
+ const contacts = Contacts({ call: mockCall, list: mockList });
5
+ const ID = '77';
6
+ describe('Contacts', () => {
7
+ beforeEach(() => {
8
+ mockCall.mockClear();
9
+ mockList.mockClear();
10
+ });
11
+ it('should expose API methods', () => {
12
+ expect(contacts).toMatchSnapshot();
13
+ });
14
+ describe('`create`', () => {
15
+ it('should invoke `call`', async () => {
16
+ await contacts.create({
17
+ COMMENTS: 'Some comment'
18
+ }, {
19
+ REGISTER_SONET_EVENT: 'Y'
20
+ });
21
+ expect(mockCall.mock.calls).toMatchSnapshot();
22
+ });
23
+ it('should work without optional params', async () => {
24
+ await contacts.create({
25
+ COMMENTS: 'Some comment'
26
+ });
27
+ expect(mockCall.mock.calls).toMatchSnapshot();
28
+ });
29
+ });
30
+ describe('`get`', () => {
31
+ it('should invoke `call`', async () => {
32
+ await contacts.get(ID);
33
+ expect(mockCall.mock.calls).toMatchSnapshot();
34
+ });
35
+ });
36
+ describe('`list`', () => {
37
+ it('should invoke `list`', async () => {
38
+ await contacts.list({
39
+ start: 0,
40
+ order: { COMMENTS: 'ASC' },
41
+ filter: { '>PROBABILITY': 50 },
42
+ select: ['*', 'UF_*']
43
+ });
44
+ expect(mockList.mock.calls).toMatchSnapshot();
45
+ });
46
+ it('should work without optional params', async () => {
47
+ await contacts.list();
48
+ expect(mockList.mock.calls).toMatchSnapshot();
49
+ });
50
+ });
51
+ describe('`update`', () => {
52
+ it('should invoke `call`', async () => {
53
+ await contacts.update(ID, {
54
+ COMMENTS: 'Some comment'
55
+ }, {
56
+ REGISTER_SONET_EVENT: 'Y'
57
+ });
58
+ expect(mockCall.mock.calls).toMatchSnapshot();
59
+ });
60
+ it('should work without optional params', async () => {
61
+ await contacts.update(ID, {
62
+ COMMENTS: 'Some comment'
63
+ });
64
+ expect(mockCall.mock.calls).toMatchSnapshot();
65
+ });
66
+ });
67
+ });
@@ -1,2 +1 @@
1
1
  import { Method } from '../../methods';
2
- //# sourceMappingURL=methods.js.map
@@ -1,2 +1 @@
1
1
  export {};
2
- //# sourceMappingURL=entities.js.map
@@ -1,2 +1 @@
1
1
  export {};
2
- //# sourceMappingURL=entities.js.map
@@ -0,0 +1,14 @@
1
+ import Bitrix from './../../bitrix';
2
+ const WEBHOOK_URL = process.env['WEBHOOK_URL'];
3
+ if (!WEBHOOK_URL) {
4
+ throw Error('Integration tests require environmental variable `WEBHOOK_URL` to be set');
5
+ }
6
+ const { deals } = Bitrix(WEBHOOK_URL);
7
+ describe('Deals', () => {
8
+ describe('fields', () => {
9
+ it('should get all fields', async () => {
10
+ const { result } = await deals.fields();
11
+ expect(result).toMatchSnapshot();
12
+ });
13
+ });
14
+ });
@@ -6,4 +6,3 @@ export default ({ call, list }) => ({
6
6
  list: (params = {}) => list(Method.CRM_DEAL_LIST, params),
7
7
  update: (id, fields, params) => call(Method.CRM_DEAL_UPDATE, { id, fields, params })
8
8
  });
9
- //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ export {};