@2bad/bitrix 2.5.1 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (211) hide show
  1. package/README.md +94 -188
  2. package/build/bin.d.ts +1 -0
  3. package/build/bin.js +160 -0
  4. package/build/client-Bk76ySik.js +769 -0
  5. package/build/index.d.ts +2881 -0
  6. package/build/index.js +1 -0
  7. package/package.json +46 -73
  8. package/build/main/bitrix.d.ts +0 -114
  9. package/build/main/bitrix.integration.test.d.ts +0 -0
  10. package/build/main/bitrix.integration.test.js +0 -31
  11. package/build/main/bitrix.js +0 -61
  12. package/build/main/bitrix.unit.test.d.ts +0 -1
  13. package/build/main/bitrix.unit.test.js +0 -44
  14. package/build/main/client/client.unit.test.d.ts +0 -1
  15. package/build/main/client/client.unit.test.js +0 -70
  16. package/build/main/client/hooks/addAccessToken.d.ts +0 -6
  17. package/build/main/client/hooks/addAccessToken.js +0 -13
  18. package/build/main/client/hooks/addAccessToken.unit.test.d.ts +0 -1
  19. package/build/main/client/hooks/addAccessToken.unit.test.js +0 -31
  20. package/build/main/client/index.d.ts +0 -15
  21. package/build/main/client/index.js +0 -53
  22. package/build/main/client/methods/batch.d.ts +0 -40
  23. package/build/main/client/methods/batch.js +0 -85
  24. package/build/main/client/methods/batch.unit.test.d.ts +0 -1
  25. package/build/main/client/methods/batch.unit.test.js +0 -494
  26. package/build/main/client/methods/call.d.ts +0 -17
  27. package/build/main/client/methods/call.js +0 -36
  28. package/build/main/client/methods/call.unit.test.d.ts +0 -1
  29. package/build/main/client/methods/call.unit.test.js +0 -183
  30. package/build/main/client/methods/list.d.ts +0 -30
  31. package/build/main/client/methods/list.js +0 -66
  32. package/build/main/client/methods/list.unit.test.d.ts +0 -1
  33. package/build/main/client/methods/list.unit.test.js +0 -277
  34. package/build/main/commands.d.ts +0 -10
  35. package/build/main/commands.js +0 -2
  36. package/build/main/methods.d.ts +0 -92
  37. package/build/main/methods.js +0 -41
  38. package/build/main/payloads.d.ts +0 -40
  39. package/build/main/payloads.js +0 -2
  40. package/build/main/services/common.d.ts +0 -22
  41. package/build/main/services/common.js +0 -2
  42. package/build/main/services/companies/entities.d.ts +0 -54
  43. package/build/main/services/companies/entities.js +0 -2
  44. package/build/main/services/companies/index.d.ts +0 -26
  45. package/build/main/services/companies/index.integration.test.d.ts +0 -1
  46. package/build/main/services/companies/index.integration.test.js +0 -19
  47. package/build/main/services/companies/index.js +0 -10
  48. package/build/main/services/companies/index.unit.test.d.ts +0 -1
  49. package/build/main/services/companies/index.unit.test.js +0 -72
  50. package/build/main/services/companies/methods.d.ts +0 -44
  51. package/build/main/services/companies/methods.js +0 -3
  52. package/build/main/services/contacts/entities.d.ts +0 -45
  53. package/build/main/services/contacts/entities.js +0 -2
  54. package/build/main/services/contacts/index.d.ts +0 -26
  55. package/build/main/services/contacts/index.integration.test.d.ts +0 -1
  56. package/build/main/services/contacts/index.integration.test.js +0 -19
  57. package/build/main/services/contacts/index.js +0 -10
  58. package/build/main/services/contacts/index.unit.test.d.ts +0 -1
  59. package/build/main/services/contacts/index.unit.test.js +0 -72
  60. package/build/main/services/contacts/methods.d.ts +0 -44
  61. package/build/main/services/contacts/methods.js +0 -3
  62. package/build/main/services/currencies/entities.d.ts +0 -15
  63. package/build/main/services/currencies/entities.js +0 -2
  64. package/build/main/services/deals/entities.d.ts +0 -43
  65. package/build/main/services/deals/entities.js +0 -2
  66. package/build/main/services/deals/index.d.ts +0 -26
  67. package/build/main/services/deals/index.integration.test.d.ts +0 -1
  68. package/build/main/services/deals/index.integration.test.js +0 -19
  69. package/build/main/services/deals/index.js +0 -10
  70. package/build/main/services/deals/index.unit.test.d.ts +0 -1
  71. package/build/main/services/deals/index.unit.test.js +0 -72
  72. package/build/main/services/deals/methods.d.ts +0 -44
  73. package/build/main/services/deals/methods.js +0 -3
  74. package/build/main/services/leads/entities.d.ts +0 -49
  75. package/build/main/services/leads/entities.js +0 -2
  76. package/build/main/services/leads/index.d.ts +0 -26
  77. package/build/main/services/leads/index.integration.test.d.ts +0 -1
  78. package/build/main/services/leads/index.integration.test.js +0 -19
  79. package/build/main/services/leads/index.js +0 -10
  80. package/build/main/services/leads/index.unit.test.d.ts +0 -1
  81. package/build/main/services/leads/index.unit.test.js +0 -72
  82. package/build/main/services/leads/methods.d.ts +0 -44
  83. package/build/main/services/leads/methods.js +0 -3
  84. package/build/main/services/statuses/entities.d.ts +0 -16
  85. package/build/main/services/statuses/entities.js +0 -2
  86. package/build/main/services/statuses/index.d.ts +0 -14
  87. package/build/main/services/statuses/index.integration.test.d.ts +0 -1
  88. package/build/main/services/statuses/index.integration.test.js +0 -90
  89. package/build/main/services/statuses/index.js +0 -11
  90. package/build/main/services/statuses/index.unit.test.d.ts +0 -1
  91. package/build/main/services/statuses/index.unit.test.js +0 -60
  92. package/build/main/services/statuses/methods.d.ts +0 -51
  93. package/build/main/services/statuses/methods.js +0 -3
  94. package/build/main/services/users/entities.d.ts +0 -40
  95. package/build/main/services/users/entities.js +0 -2
  96. package/build/main/services/users/index.d.ts +0 -9
  97. package/build/main/services/users/index.integration.test.d.ts +0 -1
  98. package/build/main/services/users/index.integration.test.js +0 -33
  99. package/build/main/services/users/index.js +0 -7
  100. package/build/main/services/users/index.unit.test.d.ts +0 -1
  101. package/build/main/services/users/index.unit.test.js +0 -25
  102. package/build/main/services/users/methods.d.ts +0 -17
  103. package/build/main/services/users/methods.js +0 -3
  104. package/build/main/utils/Diff.d.ts +0 -1
  105. package/build/main/utils/Diff.js +0 -2
  106. package/build/main/utils/ExtractValue.d.ts +0 -6
  107. package/build/main/utils/ExtractValue.js +0 -2
  108. package/build/main/utils/isArray.d.ts +0 -2
  109. package/build/main/utils/isArray.js +0 -4
  110. package/build/module/bitrix.d.ts +0 -114
  111. package/build/module/bitrix.integration.test.d.ts +0 -0
  112. package/build/module/bitrix.integration.test.js +0 -31
  113. package/build/module/bitrix.js +0 -40
  114. package/build/module/bitrix.unit.test.d.ts +0 -1
  115. package/build/module/bitrix.unit.test.js +0 -16
  116. package/build/module/client/client.unit.test.d.ts +0 -1
  117. package/build/module/client/client.unit.test.js +0 -65
  118. package/build/module/client/hooks/addAccessToken.d.ts +0 -6
  119. package/build/module/client/hooks/addAccessToken.js +0 -11
  120. package/build/module/client/hooks/addAccessToken.unit.test.d.ts +0 -1
  121. package/build/module/client/hooks/addAccessToken.unit.test.js +0 -26
  122. package/build/module/client/index.d.ts +0 -15
  123. package/build/module/client/index.js +0 -48
  124. package/build/module/client/methods/batch.d.ts +0 -40
  125. package/build/module/client/methods/batch.js +0 -76
  126. package/build/module/client/methods/batch.unit.test.d.ts +0 -1
  127. package/build/module/client/methods/batch.unit.test.js +0 -466
  128. package/build/module/client/methods/call.d.ts +0 -17
  129. package/build/module/client/methods/call.js +0 -28
  130. package/build/module/client/methods/call.unit.test.d.ts +0 -1
  131. package/build/module/client/methods/call.unit.test.js +0 -155
  132. package/build/module/client/methods/list.d.ts +0 -30
  133. package/build/module/client/methods/list.js +0 -55
  134. package/build/module/client/methods/list.unit.test.d.ts +0 -1
  135. package/build/module/client/methods/list.unit.test.js +0 -252
  136. package/build/module/commands.d.ts +0 -10
  137. package/build/module/commands.js +0 -1
  138. package/build/module/methods.d.ts +0 -92
  139. package/build/module/methods.js +0 -38
  140. package/build/module/payloads.d.ts +0 -40
  141. package/build/module/payloads.js +0 -1
  142. package/build/module/services/common.d.ts +0 -22
  143. package/build/module/services/common.js +0 -1
  144. package/build/module/services/companies/entities.d.ts +0 -54
  145. package/build/module/services/companies/entities.js +0 -1
  146. package/build/module/services/companies/index.d.ts +0 -26
  147. package/build/module/services/companies/index.integration.test.d.ts +0 -1
  148. package/build/module/services/companies/index.integration.test.js +0 -14
  149. package/build/module/services/companies/index.js +0 -8
  150. package/build/module/services/companies/index.unit.test.d.ts +0 -1
  151. package/build/module/services/companies/index.unit.test.js +0 -67
  152. package/build/module/services/companies/methods.d.ts +0 -44
  153. package/build/module/services/companies/methods.js +0 -1
  154. package/build/module/services/contacts/entities.d.ts +0 -45
  155. package/build/module/services/contacts/entities.js +0 -1
  156. package/build/module/services/contacts/index.d.ts +0 -26
  157. package/build/module/services/contacts/index.integration.test.d.ts +0 -1
  158. package/build/module/services/contacts/index.integration.test.js +0 -14
  159. package/build/module/services/contacts/index.js +0 -8
  160. package/build/module/services/contacts/index.unit.test.d.ts +0 -1
  161. package/build/module/services/contacts/index.unit.test.js +0 -67
  162. package/build/module/services/contacts/methods.d.ts +0 -44
  163. package/build/module/services/contacts/methods.js +0 -1
  164. package/build/module/services/currencies/entities.d.ts +0 -15
  165. package/build/module/services/currencies/entities.js +0 -1
  166. package/build/module/services/deals/entities.d.ts +0 -43
  167. package/build/module/services/deals/entities.js +0 -1
  168. package/build/module/services/deals/index.d.ts +0 -26
  169. package/build/module/services/deals/index.integration.test.d.ts +0 -1
  170. package/build/module/services/deals/index.integration.test.js +0 -14
  171. package/build/module/services/deals/index.js +0 -8
  172. package/build/module/services/deals/index.unit.test.d.ts +0 -1
  173. package/build/module/services/deals/index.unit.test.js +0 -67
  174. package/build/module/services/deals/methods.d.ts +0 -44
  175. package/build/module/services/deals/methods.js +0 -1
  176. package/build/module/services/leads/entities.d.ts +0 -49
  177. package/build/module/services/leads/entities.js +0 -1
  178. package/build/module/services/leads/index.d.ts +0 -26
  179. package/build/module/services/leads/index.integration.test.d.ts +0 -1
  180. package/build/module/services/leads/index.integration.test.js +0 -14
  181. package/build/module/services/leads/index.js +0 -8
  182. package/build/module/services/leads/index.unit.test.d.ts +0 -1
  183. package/build/module/services/leads/index.unit.test.js +0 -67
  184. package/build/module/services/leads/methods.d.ts +0 -44
  185. package/build/module/services/leads/methods.js +0 -1
  186. package/build/module/services/statuses/entities.d.ts +0 -16
  187. package/build/module/services/statuses/entities.js +0 -1
  188. package/build/module/services/statuses/index.d.ts +0 -14
  189. package/build/module/services/statuses/index.integration.test.d.ts +0 -1
  190. package/build/module/services/statuses/index.integration.test.js +0 -85
  191. package/build/module/services/statuses/index.js +0 -9
  192. package/build/module/services/statuses/index.unit.test.d.ts +0 -1
  193. package/build/module/services/statuses/index.unit.test.js +0 -55
  194. package/build/module/services/statuses/methods.d.ts +0 -51
  195. package/build/module/services/statuses/methods.js +0 -1
  196. package/build/module/services/users/entities.d.ts +0 -40
  197. package/build/module/services/users/entities.js +0 -1
  198. package/build/module/services/users/index.d.ts +0 -9
  199. package/build/module/services/users/index.integration.test.d.ts +0 -1
  200. package/build/module/services/users/index.integration.test.js +0 -28
  201. package/build/module/services/users/index.js +0 -5
  202. package/build/module/services/users/index.unit.test.d.ts +0 -1
  203. package/build/module/services/users/index.unit.test.js +0 -20
  204. package/build/module/services/users/methods.d.ts +0 -17
  205. package/build/module/services/users/methods.js +0 -1
  206. package/build/module/utils/Diff.d.ts +0 -1
  207. package/build/module/utils/Diff.js +0 -1
  208. package/build/module/utils/ExtractValue.d.ts +0 -6
  209. package/build/module/utils/ExtractValue.js +0 -1
  210. package/build/module/utils/isArray.d.ts +0 -2
  211. package/build/module/utils/isArray.js +0 -2
@@ -1,28 +0,0 @@
1
- import { stringify as toQuery } from 'qs';
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
- */
7
- export const handlePayload = (payload) => {
8
- if (payload.error) {
9
- throw new Error(`[call] failed to get the resource: ${payload.error ?? ''}.`);
10
- }
11
- if (payload.result && payload.result.result_error) {
12
- const resultErrors = payload.result.result_error;
13
- const errors = isArray(resultErrors) ? resultErrors : Object.values(resultErrors);
14
- if (errors.length > 0) {
15
- // @todo We can give better formatting to display errored commands. But it's not important for now
16
- throw new Error(`[batch] failed to process. Received errors in ${errors.length} commands:\n${errors.join('\n')}`);
17
- }
18
- }
19
- return payload;
20
- };
21
- /**
22
- * Dispatches a request with specified method and params. Will fill figure out payload type based on the Method
23
- */
24
- export default ({ get }) => {
25
- const call = (method, params) => get(method, { searchParams: toQuery(params) })
26
- .then(({ body }) => handlePayload(body));
27
- return call;
28
- };
@@ -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,30 +0,0 @@
1
- import { Command } from '../../commands';
2
- import { ListableMethod, MethodParams, MethodPayloadType } from '../../methods';
3
- import { BatchPayload, ListPayload } from '../../payloads';
4
- import { Batch } from './batch';
5
- import { Call } from './call';
6
- /**
7
- * Generates required amount of commands to process specified amount of entries
8
- */
9
- export declare const fillWithCommands: <C extends Command>({ method, params }: C, start: number, toProcess: number, entriesPerCommand: number) => readonly {
10
- readonly method: C["method"];
11
- readonly params?: C["params"] | undefined;
12
- }[];
13
- /**
14
- * Get the highest value from object or an array if there's any
15
- */
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
- */
20
- export declare const batchToListPayload: <P>(payload: BatchPayload<Record<string, P> | readonly P[]>) => ListPayload<P>;
21
- declare type Dependencies = {
22
- readonly call: Call;
23
- readonly batch: Batch;
24
- };
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
- */
29
- declare const _default: ({ call, batch }: Dependencies) => List;
30
- export default _default;
@@ -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 {};
@@ -1,252 +0,0 @@
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,10 +0,0 @@
1
- import { Method } from './methods';
2
- export declare type Command = {
3
- readonly method: Method;
4
- readonly params?: Record<string, any>;
5
- };
6
- export declare type Commands = {
7
- readonly [key: string]: Command;
8
- } | {
9
- readonly [index: number]: Command;
10
- };
@@ -1 +0,0 @@
1
- export {};
@@ -1,92 +0,0 @@
1
- import { Commands } from './commands';
2
- import { BatchPayload } from './payloads';
3
- import { CompaniesMethods } from './services/companies/methods';
4
- import { ContactsMethods } from './services/contacts/methods';
5
- import { DealsMethods } from './services/deals/methods';
6
- import { LeadsMethods } from './services/leads/methods';
7
- import { StatusesMethods } from './services/statuses/methods';
8
- import { UsersMethods } from './services/users/methods';
9
- import { Diff } from './utils/Diff';
10
- import { ExtractValue } from './utils/ExtractValue';
11
- export declare enum Method {
12
- BATCH = "batch",
13
- CRM_COMPANY_FIELDS = "crm.company.fields",
14
- CRM_COMPANY_ADD = "crm.company.add",
15
- CRM_COMPANY_UPDATE = "crm.company.update",
16
- CRM_COMPANY_GET = "crm.company.get",
17
- CRM_COMPANY_LIST = "crm.company.list",
18
- CRM_CONTACT_FIELDS = "crm.contact.fields",
19
- CRM_CONTACT_ADD = "crm.contact.add",
20
- CRM_CONTACT_UPDATE = "crm.contact.update",
21
- CRM_CONTACT_GET = "crm.contact.get",
22
- CRM_CONTACT_LIST = "crm.contact.list",
23
- CRM_DEAL_FIELDS = "crm.deal.fields",
24
- CRM_DEAL_ADD = "crm.deal.add",
25
- CRM_DEAL_UPDATE = "crm.deal.update",
26
- CRM_DEAL_GET = "crm.deal.get",
27
- CRM_DEAL_LIST = "crm.deal.list",
28
- CRM_LEAD_FIELDS = "crm.lead.fields",
29
- CRM_LEAD_ADD = "crm.lead.add",
30
- CRM_LEAD_UPDATE = "crm.lead.update",
31
- CRM_LEAD_GET = "crm.lead.get",
32
- CRM_LEAD_LIST = "crm.lead.list",
33
- CRM_STATUS_FIELDS = "crm.status.fields",
34
- CRM_STATUS_ADD = "crm.status.add",
35
- CRM_STATUS_DELETE = "crm.status.delete",
36
- CRM_STATUS_GET = "crm.status.get",
37
- CRM_STATUS_LIST = "crm.status.list",
38
- CRM_STATUS_UPDATE = "crm.status.update",
39
- USER_FIELDS = "user.fields",
40
- USER_GET = "user.get"
41
- }
42
- declare const LISTABLE_METHODS: readonly [Method.CRM_COMPANY_LIST, Method.CRM_CONTACT_LIST, Method.CRM_DEAL_LIST, Method.CRM_LEAD_LIST];
43
- export declare type ListableMethod = typeof LISTABLE_METHODS[number];
44
- export declare type GettableMethod = Diff<Method, ListableMethod>;
45
- declare type MethodsMap = {
46
- readonly [key: string]: {
47
- readonly type: unknown;
48
- readonly payload: unknown;
49
- readonly params: Record<string, unknown>;
50
- };
51
- };
52
- export declare type ListParams = {
53
- readonly start?: number;
54
- readonly order?: {
55
- readonly [key: string]: string;
56
- };
57
- readonly filter?: {
58
- readonly [key: string]: string | number;
59
- };
60
- readonly select?: readonly string[];
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
- */
69
- export declare type Methods = MethodsMap & CompaniesMethods & ContactsMethods & DealsMethods & LeadsMethods & StatusesMethods & UsersMethods & {
70
- readonly [Method.BATCH]: {
71
- readonly type: unknown;
72
- readonly payload: BatchPayload<unknown>;
73
- readonly params: Commands;
74
- };
75
- };
76
- /**
77
- * Retrieve specified method data
78
- */
79
- export declare type MethodData<M extends Method> = ExtractValue<Methods, M>;
80
- /**
81
- * Retrieve associated with specified method type
82
- */
83
- export declare type MethodPayloadType<M extends Method> = MethodData<M>['type'];
84
- /**
85
- * Retrieve specified method payload type
86
- */
87
- export declare type MethodPayload<M extends Method> = MethodData<M>['payload'];
88
- /**
89
- * Retrieve specified method params
90
- */
91
- export declare type MethodParams<M extends Method> = MethodData<M>['params'];
92
- export {};
@@ -1,38 +0,0 @@
1
- export var Method;
2
- (function (Method) {
3
- Method["BATCH"] = "batch";
4
- Method["CRM_COMPANY_FIELDS"] = "crm.company.fields";
5
- Method["CRM_COMPANY_ADD"] = "crm.company.add";
6
- Method["CRM_COMPANY_UPDATE"] = "crm.company.update";
7
- Method["CRM_COMPANY_GET"] = "crm.company.get";
8
- Method["CRM_COMPANY_LIST"] = "crm.company.list";
9
- Method["CRM_CONTACT_FIELDS"] = "crm.contact.fields";
10
- Method["CRM_CONTACT_ADD"] = "crm.contact.add";
11
- Method["CRM_CONTACT_UPDATE"] = "crm.contact.update";
12
- Method["CRM_CONTACT_GET"] = "crm.contact.get";
13
- Method["CRM_CONTACT_LIST"] = "crm.contact.list";
14
- Method["CRM_DEAL_FIELDS"] = "crm.deal.fields";
15
- Method["CRM_DEAL_ADD"] = "crm.deal.add";
16
- Method["CRM_DEAL_UPDATE"] = "crm.deal.update";
17
- Method["CRM_DEAL_GET"] = "crm.deal.get";
18
- Method["CRM_DEAL_LIST"] = "crm.deal.list";
19
- Method["CRM_LEAD_FIELDS"] = "crm.lead.fields";
20
- Method["CRM_LEAD_ADD"] = "crm.lead.add";
21
- Method["CRM_LEAD_UPDATE"] = "crm.lead.update";
22
- Method["CRM_LEAD_GET"] = "crm.lead.get";
23
- Method["CRM_LEAD_LIST"] = "crm.lead.list";
24
- Method["CRM_STATUS_FIELDS"] = "crm.status.fields";
25
- Method["CRM_STATUS_ADD"] = "crm.status.add";
26
- Method["CRM_STATUS_DELETE"] = "crm.status.delete";
27
- Method["CRM_STATUS_GET"] = "crm.status.get";
28
- Method["CRM_STATUS_LIST"] = "crm.status.list";
29
- Method["CRM_STATUS_UPDATE"] = "crm.status.update";
30
- Method["USER_FIELDS"] = "user.fields";
31
- Method["USER_GET"] = "user.get";
32
- })(Method || (Method = {}));
33
- const LISTABLE_METHODS = [
34
- Method.CRM_COMPANY_LIST,
35
- Method.CRM_CONTACT_LIST,
36
- Method.CRM_DEAL_LIST,
37
- Method.CRM_LEAD_LIST
38
- ];