@2bad/bitrix 2.5.1 → 3.0.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (242) hide show
  1. package/README.md +1 -2
  2. package/build/bitrix.d.ts +65 -0
  3. package/build/bitrix.js +55 -0
  4. package/build/client/hooks/addAccessToken.js +9 -0
  5. package/build/{module/client → client}/index.d.ts +3 -3
  6. package/build/{module/client → client}/index.js +21 -16
  7. package/build/{module/client → client}/methods/batch.d.ts +6 -6
  8. package/build/client/methods/batch.js +75 -0
  9. package/build/{module/client → client}/methods/call.d.ts +4 -4
  10. package/build/{module/client → client}/methods/call.js +8 -9
  11. package/build/{module/client → client}/methods/list.d.ts +10 -10
  12. package/build/client/methods/list.js +58 -0
  13. package/build/{module/commands.d.ts → commands.d.ts} +3 -3
  14. package/build/commands.js +1 -0
  15. package/build/{module/methods.d.ts → methods.d.ts} +19 -19
  16. package/build/{module/methods.js → methods.js} +5 -5
  17. package/build/{main/payloads.d.ts → payloads.d.ts} +5 -5
  18. package/build/payloads.js +1 -0
  19. package/build/{main/services → services}/common.d.ts +7 -7
  20. package/build/services/common.js +2 -0
  21. package/build/{main/services → services}/companies/entities.d.ts +2 -2
  22. package/build/services/companies/entities.js +1 -0
  23. package/build/services/companies/index.d.ts +15 -0
  24. package/build/services/companies/index.js +17 -0
  25. package/build/{module/services → services}/companies/methods.d.ts +5 -5
  26. package/build/services/companies/methods.js +1 -0
  27. package/build/{module/services → services}/contacts/entities.d.ts +2 -2
  28. package/build/services/contacts/entities.js +1 -0
  29. package/build/services/contacts/index.d.ts +15 -0
  30. package/build/services/contacts/index.js +17 -0
  31. package/build/{module/services → services}/contacts/methods.d.ts +5 -5
  32. package/build/services/contacts/methods.js +1 -0
  33. package/build/{module/services → services}/currencies/entities.d.ts +2 -2
  34. package/build/services/currencies/entities.js +1 -0
  35. package/build/{module/services → services}/deals/entities.d.ts +2 -2
  36. package/build/services/deals/entities.js +1 -0
  37. package/build/services/deals/index.d.ts +15 -0
  38. package/build/services/deals/index.js +17 -0
  39. package/build/{main/services → services}/deals/methods.d.ts +5 -5
  40. package/build/services/deals/methods.js +1 -0
  41. package/build/{module/services → services}/leads/entities.d.ts +2 -2
  42. package/build/services/leads/entities.js +1 -0
  43. package/build/services/leads/index.d.ts +15 -0
  44. package/build/services/leads/index.js +17 -0
  45. package/build/{main/services → services}/leads/methods.d.ts +5 -5
  46. package/build/services/leads/methods.js +1 -0
  47. package/build/{main/services → services}/statuses/entities.d.ts +3 -3
  48. package/build/services/statuses/entities.js +1 -0
  49. package/build/services/statuses/index.d.ts +14 -0
  50. package/build/services/statuses/index.js +19 -0
  51. package/build/{main/services → services}/statuses/methods.d.ts +5 -5
  52. package/build/services/statuses/methods.js +1 -0
  53. package/build/{module/services → services}/users/entities.d.ts +2 -2
  54. package/build/services/users/entities.js +1 -0
  55. package/build/services/users/index.d.ts +9 -0
  56. package/build/services/users/index.js +7 -0
  57. package/build/{main/services → services}/users/methods.d.ts +4 -4
  58. package/build/services/users/methods.js +1 -0
  59. package/build/utils/Diff.d.ts +1 -0
  60. package/build/utils/Diff.js +7 -0
  61. package/build/{module/utils → utils}/ExtractValue.d.ts +1 -1
  62. package/build/{main/utils/ExtractValue.d.ts → utils/ExtractValue.js} +1 -2
  63. package/build/utils/isArray.d.ts +2 -0
  64. package/build/{module/utils → utils}/isArray.js +1 -1
  65. package/package.json +43 -49
  66. package/build/main/bitrix.d.ts +0 -114
  67. package/build/main/bitrix.integration.test.d.ts +0 -0
  68. package/build/main/bitrix.integration.test.js +0 -31
  69. package/build/main/bitrix.js +0 -61
  70. package/build/main/bitrix.unit.test.d.ts +0 -1
  71. package/build/main/bitrix.unit.test.js +0 -44
  72. package/build/main/client/client.unit.test.d.ts +0 -1
  73. package/build/main/client/client.unit.test.js +0 -70
  74. package/build/main/client/hooks/addAccessToken.js +0 -13
  75. package/build/main/client/hooks/addAccessToken.unit.test.d.ts +0 -1
  76. package/build/main/client/hooks/addAccessToken.unit.test.js +0 -31
  77. package/build/main/client/index.d.ts +0 -15
  78. package/build/main/client/index.js +0 -53
  79. package/build/main/client/methods/batch.d.ts +0 -40
  80. package/build/main/client/methods/batch.js +0 -85
  81. package/build/main/client/methods/batch.unit.test.d.ts +0 -1
  82. package/build/main/client/methods/batch.unit.test.js +0 -494
  83. package/build/main/client/methods/call.d.ts +0 -17
  84. package/build/main/client/methods/call.js +0 -36
  85. package/build/main/client/methods/call.unit.test.d.ts +0 -1
  86. package/build/main/client/methods/call.unit.test.js +0 -183
  87. package/build/main/client/methods/list.d.ts +0 -30
  88. package/build/main/client/methods/list.js +0 -66
  89. package/build/main/client/methods/list.unit.test.d.ts +0 -1
  90. package/build/main/client/methods/list.unit.test.js +0 -277
  91. package/build/main/commands.d.ts +0 -10
  92. package/build/main/commands.js +0 -2
  93. package/build/main/methods.d.ts +0 -92
  94. package/build/main/methods.js +0 -41
  95. package/build/main/payloads.js +0 -2
  96. package/build/main/services/common.js +0 -2
  97. package/build/main/services/companies/entities.js +0 -2
  98. package/build/main/services/companies/index.d.ts +0 -26
  99. package/build/main/services/companies/index.integration.test.d.ts +0 -1
  100. package/build/main/services/companies/index.integration.test.js +0 -19
  101. package/build/main/services/companies/index.js +0 -10
  102. package/build/main/services/companies/index.unit.test.d.ts +0 -1
  103. package/build/main/services/companies/index.unit.test.js +0 -72
  104. package/build/main/services/companies/methods.d.ts +0 -44
  105. package/build/main/services/companies/methods.js +0 -3
  106. package/build/main/services/contacts/entities.d.ts +0 -45
  107. package/build/main/services/contacts/entities.js +0 -2
  108. package/build/main/services/contacts/index.d.ts +0 -26
  109. package/build/main/services/contacts/index.integration.test.d.ts +0 -1
  110. package/build/main/services/contacts/index.integration.test.js +0 -19
  111. package/build/main/services/contacts/index.js +0 -10
  112. package/build/main/services/contacts/index.unit.test.d.ts +0 -1
  113. package/build/main/services/contacts/index.unit.test.js +0 -72
  114. package/build/main/services/contacts/methods.d.ts +0 -44
  115. package/build/main/services/contacts/methods.js +0 -3
  116. package/build/main/services/currencies/entities.d.ts +0 -15
  117. package/build/main/services/currencies/entities.js +0 -2
  118. package/build/main/services/deals/entities.d.ts +0 -43
  119. package/build/main/services/deals/entities.js +0 -2
  120. package/build/main/services/deals/index.d.ts +0 -26
  121. package/build/main/services/deals/index.integration.test.d.ts +0 -1
  122. package/build/main/services/deals/index.integration.test.js +0 -19
  123. package/build/main/services/deals/index.js +0 -10
  124. package/build/main/services/deals/index.unit.test.d.ts +0 -1
  125. package/build/main/services/deals/index.unit.test.js +0 -72
  126. package/build/main/services/deals/methods.js +0 -3
  127. package/build/main/services/leads/entities.d.ts +0 -49
  128. package/build/main/services/leads/entities.js +0 -2
  129. package/build/main/services/leads/index.d.ts +0 -26
  130. package/build/main/services/leads/index.integration.test.d.ts +0 -1
  131. package/build/main/services/leads/index.integration.test.js +0 -19
  132. package/build/main/services/leads/index.js +0 -10
  133. package/build/main/services/leads/index.unit.test.d.ts +0 -1
  134. package/build/main/services/leads/index.unit.test.js +0 -72
  135. package/build/main/services/leads/methods.js +0 -3
  136. package/build/main/services/statuses/entities.js +0 -2
  137. package/build/main/services/statuses/index.d.ts +0 -14
  138. package/build/main/services/statuses/index.integration.test.d.ts +0 -1
  139. package/build/main/services/statuses/index.integration.test.js +0 -90
  140. package/build/main/services/statuses/index.js +0 -11
  141. package/build/main/services/statuses/index.unit.test.d.ts +0 -1
  142. package/build/main/services/statuses/index.unit.test.js +0 -60
  143. package/build/main/services/statuses/methods.js +0 -3
  144. package/build/main/services/users/entities.d.ts +0 -40
  145. package/build/main/services/users/entities.js +0 -2
  146. package/build/main/services/users/index.d.ts +0 -9
  147. package/build/main/services/users/index.integration.test.d.ts +0 -1
  148. package/build/main/services/users/index.integration.test.js +0 -33
  149. package/build/main/services/users/index.js +0 -7
  150. package/build/main/services/users/index.unit.test.d.ts +0 -1
  151. package/build/main/services/users/index.unit.test.js +0 -25
  152. package/build/main/services/users/methods.js +0 -3
  153. package/build/main/utils/Diff.d.ts +0 -1
  154. package/build/main/utils/Diff.js +0 -2
  155. package/build/main/utils/ExtractValue.js +0 -2
  156. package/build/main/utils/isArray.d.ts +0 -2
  157. package/build/main/utils/isArray.js +0 -4
  158. package/build/module/bitrix.d.ts +0 -114
  159. package/build/module/bitrix.integration.test.d.ts +0 -0
  160. package/build/module/bitrix.integration.test.js +0 -31
  161. package/build/module/bitrix.js +0 -40
  162. package/build/module/bitrix.unit.test.d.ts +0 -1
  163. package/build/module/bitrix.unit.test.js +0 -16
  164. package/build/module/client/client.unit.test.d.ts +0 -1
  165. package/build/module/client/client.unit.test.js +0 -65
  166. package/build/module/client/hooks/addAccessToken.d.ts +0 -6
  167. package/build/module/client/hooks/addAccessToken.js +0 -11
  168. package/build/module/client/hooks/addAccessToken.unit.test.d.ts +0 -1
  169. package/build/module/client/hooks/addAccessToken.unit.test.js +0 -26
  170. package/build/module/client/methods/batch.js +0 -76
  171. package/build/module/client/methods/batch.unit.test.d.ts +0 -1
  172. package/build/module/client/methods/batch.unit.test.js +0 -466
  173. package/build/module/client/methods/call.unit.test.d.ts +0 -1
  174. package/build/module/client/methods/call.unit.test.js +0 -155
  175. package/build/module/client/methods/list.js +0 -55
  176. package/build/module/client/methods/list.unit.test.d.ts +0 -1
  177. package/build/module/client/methods/list.unit.test.js +0 -252
  178. package/build/module/commands.js +0 -1
  179. package/build/module/payloads.d.ts +0 -40
  180. package/build/module/payloads.js +0 -1
  181. package/build/module/services/common.d.ts +0 -22
  182. package/build/module/services/common.js +0 -1
  183. package/build/module/services/companies/entities.d.ts +0 -54
  184. package/build/module/services/companies/entities.js +0 -1
  185. package/build/module/services/companies/index.d.ts +0 -26
  186. package/build/module/services/companies/index.integration.test.d.ts +0 -1
  187. package/build/module/services/companies/index.integration.test.js +0 -14
  188. package/build/module/services/companies/index.js +0 -8
  189. package/build/module/services/companies/index.unit.test.d.ts +0 -1
  190. package/build/module/services/companies/index.unit.test.js +0 -67
  191. package/build/module/services/companies/methods.js +0 -1
  192. package/build/module/services/contacts/entities.js +0 -1
  193. package/build/module/services/contacts/index.d.ts +0 -26
  194. package/build/module/services/contacts/index.integration.test.d.ts +0 -1
  195. package/build/module/services/contacts/index.integration.test.js +0 -14
  196. package/build/module/services/contacts/index.js +0 -8
  197. package/build/module/services/contacts/index.unit.test.d.ts +0 -1
  198. package/build/module/services/contacts/index.unit.test.js +0 -67
  199. package/build/module/services/contacts/methods.js +0 -1
  200. package/build/module/services/currencies/entities.js +0 -1
  201. package/build/module/services/deals/entities.js +0 -1
  202. package/build/module/services/deals/index.d.ts +0 -26
  203. package/build/module/services/deals/index.integration.test.d.ts +0 -1
  204. package/build/module/services/deals/index.integration.test.js +0 -14
  205. package/build/module/services/deals/index.js +0 -8
  206. package/build/module/services/deals/index.unit.test.d.ts +0 -1
  207. package/build/module/services/deals/index.unit.test.js +0 -67
  208. package/build/module/services/deals/methods.d.ts +0 -44
  209. package/build/module/services/deals/methods.js +0 -1
  210. package/build/module/services/leads/entities.js +0 -1
  211. package/build/module/services/leads/index.d.ts +0 -26
  212. package/build/module/services/leads/index.integration.test.d.ts +0 -1
  213. package/build/module/services/leads/index.integration.test.js +0 -14
  214. package/build/module/services/leads/index.js +0 -8
  215. package/build/module/services/leads/index.unit.test.d.ts +0 -1
  216. package/build/module/services/leads/index.unit.test.js +0 -67
  217. package/build/module/services/leads/methods.d.ts +0 -44
  218. package/build/module/services/leads/methods.js +0 -1
  219. package/build/module/services/statuses/entities.d.ts +0 -16
  220. package/build/module/services/statuses/entities.js +0 -1
  221. package/build/module/services/statuses/index.d.ts +0 -14
  222. package/build/module/services/statuses/index.integration.test.d.ts +0 -1
  223. package/build/module/services/statuses/index.integration.test.js +0 -85
  224. package/build/module/services/statuses/index.js +0 -9
  225. package/build/module/services/statuses/index.unit.test.d.ts +0 -1
  226. package/build/module/services/statuses/index.unit.test.js +0 -55
  227. package/build/module/services/statuses/methods.d.ts +0 -51
  228. package/build/module/services/statuses/methods.js +0 -1
  229. package/build/module/services/users/entities.js +0 -1
  230. package/build/module/services/users/index.d.ts +0 -9
  231. package/build/module/services/users/index.integration.test.d.ts +0 -1
  232. package/build/module/services/users/index.integration.test.js +0 -28
  233. package/build/module/services/users/index.js +0 -5
  234. package/build/module/services/users/index.unit.test.d.ts +0 -1
  235. package/build/module/services/users/index.unit.test.js +0 -20
  236. package/build/module/services/users/methods.d.ts +0 -17
  237. package/build/module/services/users/methods.js +0 -1
  238. package/build/module/utils/Diff.d.ts +0 -1
  239. package/build/module/utils/Diff.js +0 -1
  240. package/build/module/utils/ExtractValue.js +0 -1
  241. package/build/module/utils/isArray.d.ts +0 -2
  242. /package/build/{main/client → client}/hooks/addAccessToken.d.ts +0 -0
package/README.md CHANGED
@@ -5,8 +5,7 @@
5
5
  [![NPM version](https://img.shields.io/npm/v/@2bad/bitrix)](https://www.npmjs.com/package/@2bad/bitrix)
6
6
  [![License](https://img.shields.io/npm/l/@2bad/bitrix)](https://www.npmjs.com/package/@2bad/bitrix)
7
7
  [![Code coverage](https://img.shields.io/codecov/c/github/2BAD/bitrix)](https://codecov.io/gh/2BAD/bitrix)
8
- [![Travis Build Status](https://img.shields.io/travis/2BAD/bitrix?logo=Travis)](https://travis-ci.org/github/2BAD/bitrix)
9
- [![GitHub Build Status](https://img.shields.io/github/workflow/status/2BAD/bitrix/Integration%20testing?logo=GitHub)](https://github.com/2BAD/bitrix/actions?query=workflow%3A%22Integration+testing%22)
8
+ [![GitHub Build Status](https://img.shields.io/github/actions/workflow/status/2BAD/bitrix/build.yml)](https://github.com/2BAD/bitrix/actions/workflows/build.yml)
10
9
  [![Written in TypeScript](https://img.shields.io/github/languages/top/2BAD/bitrix)](https://github.com/2BAD/bitrix/search?l=typescript)
11
10
 
12
11
  - 🔥 No bullshit
@@ -0,0 +1,65 @@
1
+ import { ExtendOptions } from 'got';
2
+ /**
3
+ * Construct a Bitrix client with generic methods
4
+ * @param restURI REST endpoint, like a `https://hello.bitrix24.ua/rest` or an inbound webhook endpoint,
5
+ * like a `https://hello.bitrix24.ua/rest/1/WEBHOOK_TOKEN`.
6
+ * @param accessToken Bitrix application Access Token. Do not specify in case inbound webhook endpoint used.
7
+ * @param clientOptions an object that will overwrite underlying configuration for HTTP client,
8
+ * see `https://github.com/sindresorhus/got/blob/main/documentation/2-options.md`.
9
+ */
10
+ export declare const Bitrix: (restURI: string, accessToken?: string, clientOptions?: ExtendOptions) => {
11
+ call: import("./client/methods/call.ts").Call;
12
+ batch: import("./client/methods/batch.ts").Batch;
13
+ list: import("./client/methods/list.ts").List;
14
+ companies: {
15
+ fields: () => Promise<import("./payloads.ts").GetPayload<import("./services/common.js").Fields>>;
16
+ create: <D extends import("./methods.js").MethodParams<import("./methods.js").Method.CRM_COMPANY_ADD>>(fields: D["fields"], params?: D["params"]) => Promise<import("./payloads.ts").GetPayload<number>>;
17
+ get: (id: string) => Promise<import("./payloads.ts").GetPayload<import("./services/companies/entities.js").Company>>;
18
+ list: (params?: import("./methods.js").MethodParams<import("./methods.js").Method.CRM_COMPANY_LIST>) => Promise<import("./payloads.ts").ListPayload<import("./services/companies/entities.js").Company>>;
19
+ update: <D extends import("./methods.js").MethodParams<import("./methods.js").Method.CRM_COMPANY_UPDATE>>(id: string, fields: D["fields"], params?: D["params"]) => Promise<import("./payloads.ts").GetPayload<boolean>>;
20
+ };
21
+ contacts: {
22
+ fields: () => Promise<import("./payloads.ts").GetPayload<import("./services/common.js").Fields>>;
23
+ create: <D extends import("./methods.js").MethodParams<import("./methods.js").Method.CRM_CONTACT_ADD>>(fields: D["fields"], params?: D["params"]) => Promise<import("./payloads.ts").GetPayload<number>>;
24
+ get: (id: string) => Promise<import("./payloads.ts").GetPayload<import("./services/contacts/entities.js").Contact>>;
25
+ list: (params?: import("./methods.js").MethodParams<import("./methods.js").Method.CRM_CONTACT_LIST>) => Promise<import("./payloads.ts").ListPayload<import("./services/contacts/entities.js").Contact>>;
26
+ update: <D extends import("./methods.js").MethodParams<import("./methods.js").Method.CRM_CONTACT_UPDATE>>(id: string, fields: D["fields"], params?: D["params"]) => Promise<import("./payloads.ts").GetPayload<boolean>>;
27
+ };
28
+ deals: {
29
+ fields: () => Promise<import("./payloads.ts").GetPayload<import("./services/common.js").Fields>>;
30
+ create: <D extends import("./methods.js").MethodParams<import("./methods.js").Method.CRM_DEAL_ADD>>(fields: D["fields"], params?: D["params"]) => Promise<import("./payloads.ts").GetPayload<number>>;
31
+ get: (id: string) => Promise<import("./payloads.ts").GetPayload<import("./services/deals/entities.js").Deal>>;
32
+ list: (params?: import("./methods.js").MethodParams<import("./methods.js").Method.CRM_DEAL_LIST>) => Promise<import("./payloads.ts").ListPayload<import("./services/deals/entities.js").Deal>>;
33
+ update: <D extends import("./methods.js").MethodParams<import("./methods.js").Method.CRM_DEAL_UPDATE>>(id: string, fields: D["fields"], params?: D["params"]) => Promise<import("./payloads.ts").GetPayload<boolean>>;
34
+ };
35
+ leads: {
36
+ fields: () => Promise<import("./payloads.ts").GetPayload<import("./services/common.js").Fields>>;
37
+ create: <D extends import("./methods.js").MethodParams<import("./methods.js").Method.CRM_LEAD_ADD>>(fields: D["fields"], params?: D["params"]) => Promise<import("./payloads.ts").GetPayload<number>>;
38
+ get: (id: string) => Promise<import("./payloads.ts").GetPayload<import("./services/leads/entities.js").Lead>>;
39
+ list: (params?: import("./methods.js").MethodParams<import("./methods.js").Method.CRM_LEAD_LIST>) => Promise<import("./payloads.ts").ListPayload<import("./services/leads/entities.js").Lead>>;
40
+ update: <D extends import("./methods.js").MethodParams<import("./methods.js").Method.CRM_LEAD_UPDATE>>(id: string, fields: D["fields"], params?: D["params"]) => Promise<import("./payloads.ts").GetPayload<boolean>>;
41
+ };
42
+ statuses: {
43
+ fields: () => Promise<import("./payloads.ts").GetPayload<import("./services/common.js").Fields>>;
44
+ create: (fields: import("./methods.js").MethodParams<import("./methods.js").Method.CRM_STATUS_ADD>["fields"]) => Promise<import("./payloads.ts").GetPayload<number>>;
45
+ delete: (id: string | number, params?: import("./methods.js").MethodParams<import("./methods.js").Method.CRM_STATUS_DELETE>["params"]) => Promise<import("./payloads.ts").GetPayload<boolean>>;
46
+ get: (id: string | number) => Promise<import("./payloads.ts").GetPayload<import("./services/statuses/entities.js").Status>>;
47
+ list: (params?: import("./methods.js").MethodParams<import("./methods.js").Method.CRM_STATUS_LIST>) => Promise<import("./payloads.ts").ListPayload<import("./services/statuses/entities.js").Status>>;
48
+ update: (id: string | number, fields: import("./methods.js").MethodParams<import("./methods.js").Method.CRM_STATUS_UPDATE>["fields"]) => Promise<import("./payloads.ts").GetPayload<boolean>>;
49
+ };
50
+ users: {
51
+ fields: () => Promise<import("./payloads.ts").GetPayload<import("./services/users/entities.js").User>>;
52
+ get: (id?: string) => Promise<import("./payloads.ts").GetPayload<readonly import("./services/users/entities.js").User[]>>;
53
+ };
54
+ };
55
+ export default Bitrix;
56
+ export * from './methods.js';
57
+ export * from './commands.js';
58
+ export * from './services/common.js';
59
+ export * from './services/companies/entities.js';
60
+ export * from './services/contacts/entities.js';
61
+ export * from './services/currencies/entities.js';
62
+ export * from './services/deals/entities.js';
63
+ export * from './services/leads/entities.js';
64
+ export * from './services/statuses/entities.js';
65
+ export * from './services/users/entities.js';
@@ -0,0 +1,55 @@
1
+ import Client from "./client/index.js";
2
+ import CompaniesService from "./services/companies/index.js";
3
+ import ContactsService from "./services/contacts/index.js";
4
+ import DealsService from "./services/deals/index.js";
5
+ import LeadsService from "./services/leads/index.js";
6
+ import StatusesService from "./services/statuses/index.js";
7
+ import UsersService from "./services/users/index.js";
8
+ /**
9
+ * Construct a Bitrix client with generic methods
10
+ * @param restURI REST endpoint, like a `https://hello.bitrix24.ua/rest` or an inbound webhook endpoint,
11
+ * like a `https://hello.bitrix24.ua/rest/1/WEBHOOK_TOKEN`.
12
+ * @param accessToken Bitrix application Access Token. Do not specify in case inbound webhook endpoint used.
13
+ * @param clientOptions an object that will overwrite underlying configuration for HTTP client,
14
+ * see `https://github.com/sindresorhus/got/blob/main/documentation/2-options.md`.
15
+ */ export const Bitrix = (restURI, accessToken, clientOptions)=>{
16
+ const { call, batch, list } = Client(restURI, accessToken, clientOptions);
17
+ return {
18
+ call,
19
+ batch,
20
+ list,
21
+ companies: CompaniesService({
22
+ call,
23
+ list
24
+ }),
25
+ contacts: ContactsService({
26
+ call,
27
+ list
28
+ }),
29
+ deals: DealsService({
30
+ call,
31
+ list
32
+ }),
33
+ leads: LeadsService({
34
+ call,
35
+ list
36
+ }),
37
+ statuses: StatusesService({
38
+ call
39
+ }),
40
+ users: UsersService({
41
+ call
42
+ })
43
+ };
44
+ };
45
+ export default Bitrix;
46
+ export * from "./methods.js";
47
+ export * from "./commands.js";
48
+ export * from "./services/common.js";
49
+ export * from "./services/companies/entities.js";
50
+ export * from "./services/contacts/entities.js";
51
+ export * from "./services/currencies/entities.js";
52
+ export * from "./services/deals/entities.js";
53
+ export * from "./services/leads/entities.js";
54
+ export * from "./services/statuses/entities.js";
55
+ export * from "./services/users/entities.js";
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Got can't merge `query` option with other queries if they are string. But that hook can.
3
+ */ export default ((accessToken)=>(options)=>{
4
+ if (!accessToken) return;
5
+ if (!options.url) return;
6
+ if (typeof options.url.search === 'string') {
7
+ options.url.search = `${options.url.search}${options.url.search ? '&' : '?'}access_token=${accessToken}`;
8
+ }
9
+ });
@@ -8,8 +8,8 @@ import { ExtendOptions } from 'got';
8
8
  * see `https://github.com/sindresorhus/got/blob/main/documentation/2-options.md`.
9
9
  */
10
10
  declare const _default: (restURI: string, accessToken?: string, options?: ExtendOptions) => {
11
- call: import("./methods/call").Call;
12
- batch: import("./methods/batch").Batch;
13
- list: import("./methods/list").List;
11
+ call: import("./methods/call.js").Call;
12
+ batch: import("./methods/batch.js").Batch;
13
+ list: import("./methods/list.js").List;
14
14
  };
15
15
  export default _default;
@@ -1,11 +1,12 @@
1
- import got from 'got';
2
- import Queue from 'p-queue';
3
- import addAccessToken from './hooks/addAccessToken';
4
- import Batch from './methods/batch';
5
- import Call from './methods/call';
6
- import List from './methods/list';
1
+ import got from "got";
2
+ import Queue from "p-queue";
3
+ import addAccessToken from "./hooks/addAccessToken.js";
4
+ import Batch from "./methods/batch.js";
5
+ import Call from "./methods/call.js";
6
+ import List from "./methods/list.js";
7
7
  const BITRIX_API_RATE_LIMIT = 2;
8
- const BITRIX_API_RATE_INTERVAL = 1000; // 1 second
8
+ const BITRIX_API_RATE_INTERVAL = 1000 // 1 second
9
+ ;
9
10
  /**
10
11
  * Construct a Bitrix client with generic methods
11
12
  * @param restURI REST endpoint, like a `https://hello.bitrix24.ru/rest` or an inbound webhook endpoint,
@@ -13,8 +14,7 @@ const BITRIX_API_RATE_INTERVAL = 1000; // 1 second
13
14
  * @param accessToken Bitrix application Access Token. Do not specify in case inbound webhook endpoint used.
14
15
  * @param options an object that will overwrite underlying configuration for HTTP client,
15
16
  * see `https://github.com/sindresorhus/got/blob/main/documentation/2-options.md`.
16
- */
17
- export default (restURI, accessToken, options) => {
17
+ */ export default ((restURI, accessToken, options)=>{
18
18
  const client = got.extend({
19
19
  prefixUrl: restURI,
20
20
  headers: {
@@ -32,17 +32,22 @@ export default (restURI, accessToken, options) => {
32
32
  intervalCap: BITRIX_API_RATE_LIMIT,
33
33
  interval: BITRIX_API_RATE_INTERVAL
34
34
  });
35
- const queuedGet = (...args) => queue.add(() => client.get(...args));
36
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
35
+ const queuedGet = (...args)=>queue.add(()=>client.get(...args));
37
36
  // @ts-expect-error @todo remove after issue is resolved (https://github.com/sindresorhus/got/issues/954)
38
- const call = Call({ get: queuedGet });
39
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
37
+ const call = Call({
38
+ get: queuedGet
39
+ });
40
40
  // @ts-expect-error @todo remove after issue is resolved (https://github.com/sindresorhus/got/issues/954)
41
- const batch = Batch({ get: queuedGet });
42
- const list = List({ call, batch });
41
+ const batch = Batch({
42
+ get: queuedGet
43
+ });
44
+ const list = List({
45
+ call,
46
+ batch
47
+ });
43
48
  return {
44
49
  call,
45
50
  batch,
46
51
  list
47
52
  };
48
- };
53
+ });
@@ -1,7 +1,7 @@
1
1
  import got from 'got';
2
- import { Commands } from '../../commands';
3
- import { Method, MethodPayloadType } from '../../methods';
4
- import { BatchPayload } from '../../payloads';
2
+ import { Commands } from '../../commands.js';
3
+ import { Method, MethodPayloadType } from '../../methods.js';
4
+ import { BatchPayload } from '../../payloads.js';
5
5
  export declare const MAX_COMMANDS_PER_BATCH = 50;
6
6
  /**
7
7
  * Split any number of commands into the chunks with max commands per batch in each chunk
@@ -18,15 +18,15 @@ export declare const prepareCommandsQueries: <C extends Commands, R = { [K in ke
18
18
  * @todo Generics inference is complicated here and might be not super accurate
19
19
  */
20
20
  export declare const mergeBatchPayloads: <B extends BatchPayload<unknown>, P = B extends BatchPayload<infer U> ? U : never>(payloads: readonly B[]) => BatchPayload<P>;
21
- export declare type CommandsPayloads<C extends Commands, CM = {
21
+ export type CommandsPayloads<C extends Commands, CM = {
22
22
  [K in keyof C]: C[K] extends {
23
23
  readonly method: infer M;
24
24
  } ? M : never;
25
25
  }> = {
26
26
  [K in keyof CM]: MethodPayloadType<CM[K] extends Method ? CM[K] : never>;
27
27
  };
28
- export declare type Batch = <C extends Commands, P = CommandsPayloads<C>>(commands: C, commandsPerRequest?: number) => Promise<BatchPayload<P>>;
29
- declare type Dependencies = {
28
+ export type Batch = <C extends Commands, P = CommandsPayloads<C>>(commands: C, commandsPerRequest?: number) => Promise<BatchPayload<P>>;
29
+ type Dependencies = {
30
30
  readonly get: typeof got.get;
31
31
  };
32
32
  /**
@@ -0,0 +1,75 @@
1
+ import chunk from "lodash.chunk";
2
+ import fromPairs from "lodash.frompairs";
3
+ import { stringify as toQuery } from "qs";
4
+ import { Method } from "../../methods.js";
5
+ import isArray from "../../utils/isArray.js";
6
+ import { handlePayload } from "./call.js";
7
+ export const MAX_COMMANDS_PER_BATCH = 50;
8
+ /**
9
+ * Split any number of commands into the chunks with max commands per batch in each chunk
10
+ */ export const chunkCommands = (commands, size = MAX_COMMANDS_PER_BATCH)=>{
11
+ const commandsIsArray = isArray(commands);
12
+ const chunks = chunk(Object.entries(commands), size);
13
+ // @todo Generated return type isn't very accurate since it says that it will make array of <C>, while
14
+ // in fact <C> will be distributed among chunks. That gives wrong hints with named commands and arrays as const.
15
+ return chunks.map((c)=>commandsIsArray ? c.map(([_key, command])=>command) : fromPairs(c));
16
+ };
17
+ /**
18
+ * Prepares list of commands to be used in requests to the Bitrix
19
+ * @note We could avoid that function... if only Bitrix API would support posting of the batch commands as
20
+ * plain objects, like it does with other methods. Instead, it should be a dict or array of string queries
21
+ */ export const prepareCommandsQueries = (commands)=>Object.entries(commands).reduce((calls, [name, { method, params }])=>{
22
+ const stringifiedParams = params ? `?${toQuery(params)}` : '';
23
+ return {
24
+ ...calls,
25
+ [`cmd[${name}]`]: `${method}${stringifiedParams}`
26
+ };
27
+ // eslint-disable-next-line @typescript-eslint/prefer-reduce-type-parameter
28
+ }, {});
29
+ /**
30
+ * Merges list of batch responses into a single batch
31
+ * @todo Generics inference is complicated here and might be not super accurate
32
+ */ export const mergeBatchPayloads = (payloads)=>{
33
+ const merge = (a, b)=>isArray(a) && isArray(b) ? [
34
+ ...a,
35
+ ...b
36
+ ] : {
37
+ ...a,
38
+ ...b
39
+ };
40
+ return payloads.reduce((merged, payload)=>({
41
+ result: {
42
+ result: merge(merged.result.result || [], payload.result.result),
43
+ result_error: merge(merged.result.result_error || [], payload.result.result_error),
44
+ result_next: merge(merged.result.result_next || [], payload.result.result_next),
45
+ result_time: merge(merged.result.result_time || [], payload.result.result_time),
46
+ result_total: merge(merged.result.result_total || [], payload.result.result_total)
47
+ },
48
+ time: {
49
+ ...merged.time,
50
+ ...payload.time
51
+ }
52
+ }), {
53
+ result: {},
54
+ time: {}
55
+ });
56
+ };
57
+ /**
58
+ * Dispatches a batch request with specified commands. Will fill figure out payload type based on the Methods.
59
+ * Supports unlimited number of commands. If they do exceed max amount of commands per batch, will dispatch
60
+ * multiple request and merge result into single batch payload
61
+ * @todo For now due to issues with types it won't check `params` of command, so any Method can be used
62
+ * with any (even wrong) params
63
+ */ export default (({ get })=>{
64
+ const batch = async (commands, commandsPerRequest = MAX_COMMANDS_PER_BATCH)=>{
65
+ const call = (c)=>get(Method.BATCH, {
66
+ searchParams: prepareCommandsQueries(c)
67
+ }).then(({ body })=>body);
68
+ const calls = chunkCommands(commands, commandsPerRequest).map(call);
69
+ return await Promise.all(calls).then((chunkedPayloads)=>{
70
+ const payloads = mergeBatchPayloads(chunkedPayloads);
71
+ return handlePayload(payloads);
72
+ });
73
+ };
74
+ return batch;
75
+ });
@@ -1,13 +1,13 @@
1
1
  import got from 'got';
2
- import { Method, MethodParams, MethodPayload } from '../../methods';
3
- import { Payload } from '../../payloads';
2
+ import { Method, MethodParams, MethodPayload } from '../../methods.js';
3
+ import { Payload } from '../../payloads.js';
4
4
  /**
5
5
  * Checks wether payload have any errors and if it does — throws them
6
6
  * Bitrix payload types do not provide discriminators, so we're forced to type cast them
7
7
  */
8
8
  export declare const handlePayload: <P extends Payload<unknown>>(payload: P) => P;
9
- export declare type Call = <M extends Method>(method: M, params: MethodParams<M>) => Promise<MethodPayload<M>>;
10
- declare type Dependencies = {
9
+ export type Call = <M extends Method>(method: M, params: MethodParams<M>) => Promise<MethodPayload<M>>;
10
+ type Dependencies = {
11
11
  readonly get: typeof got.get;
12
12
  };
13
13
  /**
@@ -1,10 +1,9 @@
1
- import { stringify as toQuery } from 'qs';
2
- import isArray from '../../utils/isArray';
1
+ import { stringify as toQuery } from "qs";
2
+ import isArray from "../../utils/isArray.js";
3
3
  /**
4
4
  * Checks wether payload have any errors and if it does — throws them
5
5
  * Bitrix payload types do not provide discriminators, so we're forced to type cast them
6
- */
7
- export const handlePayload = (payload) => {
6
+ */ export const handlePayload = (payload)=>{
8
7
  if (payload.error) {
9
8
  throw new Error(`[call] failed to get the resource: ${payload.error ?? ''}.`);
10
9
  }
@@ -20,9 +19,9 @@ export const handlePayload = (payload) => {
20
19
  };
21
20
  /**
22
21
  * 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));
22
+ */ export default (({ get })=>{
23
+ const call = (method, params)=>get(method, {
24
+ searchParams: toQuery(params)
25
+ }).then(({ body })=>handlePayload(body));
27
26
  return call;
28
- };
27
+ });
@@ -1,15 +1,15 @@
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';
1
+ import { Command } from '../../commands.js';
2
+ import { ListableMethod, MethodParams, MethodPayloadType } from '../../methods.js';
3
+ import { BatchPayload, ListPayload } from '../../payloads.js';
4
+ import { Batch } from './batch.js';
5
+ import { Call } from './call.js';
6
6
  /**
7
7
  * Generates required amount of commands to process specified amount of entries
8
8
  */
9
- export declare const fillWithCommands: <C extends Command>({ method, params }: C, start: number, toProcess: number, entriesPerCommand: number) => readonly {
9
+ export declare const fillWithCommands: <C extends Command>({ method, params }: C, start: number, toProcess: number, entriesPerCommand: number) => ReadonlyArray<{
10
10
  readonly method: C["method"];
11
- readonly params?: C["params"] | undefined;
12
- }[];
11
+ readonly params?: C["params"];
12
+ }>;
13
13
  /**
14
14
  * Get the highest value from object or an array if there's any
15
15
  */
@@ -18,11 +18,11 @@ export declare const highest: (input: ReadonlyArray<number | undefined> | Record
18
18
  * Converts batch payload to a list payload
19
19
  */
20
20
  export declare const batchToListPayload: <P>(payload: BatchPayload<Record<string, P> | readonly P[]>) => ListPayload<P>;
21
- declare type Dependencies = {
21
+ type Dependencies = {
22
22
  readonly call: Call;
23
23
  readonly batch: Batch;
24
24
  };
25
- export declare type List = <M extends ListableMethod>(method: M, params: MethodParams<M>) => Promise<ListPayload<MethodPayloadType<M>>>;
25
+ export type List = <M extends ListableMethod>(method: M, params: MethodParams<M>) => Promise<ListPayload<MethodPayloadType<M>>>;
26
26
  /**
27
27
  * Gets all entries by dispatching required amount of requests. Will fill figure out payload type based on the Method.
28
28
  */
@@ -0,0 +1,58 @@
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
+ */ export const fillWithCommands = ({ method, params }, start, toProcess, entriesPerCommand)=>{
6
+ const requiresCommands = Math.ceil((toProcess - start) / entriesPerCommand);
7
+ return range(0, requiresCommands).map((i)=>({
8
+ method,
9
+ params: {
10
+ ...params,
11
+ start: start + entriesPerCommand * i
12
+ }
13
+ }));
14
+ };
15
+ /**
16
+ * Get the highest value from object or an array if there's any
17
+ */ export const highest = (input)=>Object.values(input).reduce((a, b)=>a !== undefined && b !== undefined ? a > b ? a : b : a !== undefined && b === undefined ? a : a === undefined && b !== undefined ? b : undefined, undefined);
18
+ /**
19
+ * Converts batch payload to a list payload
20
+ */ export const batchToListPayload = (payload)=>{
21
+ // eslint-disable-next-line @typescript-eslint/naming-convention
22
+ const { result: { result, result_total, result_error, result_next }, time } = payload;
23
+ const flattenResult = Object.entries(result).reduce(// @ts-expect-error ignore this for now
24
+ (flatten, [_key, r])=>!r ? flatten : [
25
+ ...flatten,
26
+ ...r
27
+ ], []);
28
+ return {
29
+ error: Object.values(result_error).join('\n'),
30
+ next: highest(result_next),
31
+ result: flattenResult,
32
+ // @todo Not accurate, we do not care
33
+ time,
34
+ total: highest(result_total) ?? 0
35
+ };
36
+ };
37
+ /**
38
+ * Gets all entries by dispatching required amount of requests. Will fill figure out payload type based on the Method.
39
+ */ export default (({ call, batch })=>{
40
+ const list = async (method, params)=>{
41
+ const start = params.start ?? 0;
42
+ const listAll = async ()=>{
43
+ const batchCommands = fillWithCommands({
44
+ method,
45
+ params
46
+ }, start, firstCall.total, MAX_ENTRIES_PER_COMMAND);
47
+ const payload = await batch(batchCommands);
48
+ // @ts-expect-error ignore this for now
49
+ return batchToListPayload(payload);
50
+ };
51
+ const firstCall = await call(method, {
52
+ ...params,
53
+ start
54
+ });
55
+ return !firstCall.next ? firstCall : await listAll();
56
+ };
57
+ return list;
58
+ });
@@ -1,9 +1,9 @@
1
- import { Method } from './methods';
2
- export declare type Command = {
1
+ import { Method } from './methods.js';
2
+ export type Command = {
3
3
  readonly method: Method;
4
4
  readonly params?: Record<string, any>;
5
5
  };
6
- export declare type Commands = {
6
+ export type Commands = {
7
7
  readonly [key: string]: Command;
8
8
  } | {
9
9
  readonly [index: number]: Command;
@@ -0,0 +1 @@
1
+ export { };
@@ -1,13 +1,13 @@
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';
1
+ import { Commands } from './commands.js';
2
+ import { BatchPayload } from './payloads.js';
3
+ import { CompaniesMethods } from './services/companies/methods.js';
4
+ import { ContactsMethods } from './services/contacts/methods.js';
5
+ import { DealsMethods } from './services/deals/methods.js';
6
+ import { LeadsMethods } from './services/leads/methods.js';
7
+ import { StatusesMethods } from './services/statuses/methods.js';
8
+ import { UsersMethods } from './services/users/methods.js';
9
+ import { Diff } from './utils/Diff.js';
10
+ import { ExtractValue } from './utils/ExtractValue.js';
11
11
  export declare enum Method {
12
12
  BATCH = "batch",
13
13
  CRM_COMPANY_FIELDS = "crm.company.fields",
@@ -40,16 +40,16 @@ export declare enum Method {
40
40
  USER_GET = "user.get"
41
41
  }
42
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 = {
43
+ export type ListableMethod = typeof LISTABLE_METHODS[number];
44
+ export type GettableMethod = Diff<Method, ListableMethod>;
45
+ type MethodsMap = {
46
46
  readonly [key: string]: {
47
47
  readonly type: unknown;
48
48
  readonly payload: unknown;
49
49
  readonly params: Record<string, unknown>;
50
50
  };
51
51
  };
52
- export declare type ListParams = {
52
+ export type ListParams = {
53
53
  readonly start?: number;
54
54
  readonly order?: {
55
55
  readonly [key: string]: string;
@@ -66,7 +66,7 @@ export declare type ListParams = {
66
66
  * - `payload` — a payload that method returns
67
67
  * - `params` — params that method accepts
68
68
  */
69
- export declare type Methods = MethodsMap & CompaniesMethods & ContactsMethods & DealsMethods & LeadsMethods & StatusesMethods & UsersMethods & {
69
+ export type Methods = MethodsMap & CompaniesMethods & ContactsMethods & DealsMethods & LeadsMethods & StatusesMethods & UsersMethods & {
70
70
  readonly [Method.BATCH]: {
71
71
  readonly type: unknown;
72
72
  readonly payload: BatchPayload<unknown>;
@@ -76,17 +76,17 @@ export declare type Methods = MethodsMap & CompaniesMethods & ContactsMethods &
76
76
  /**
77
77
  * Retrieve specified method data
78
78
  */
79
- export declare type MethodData<M extends Method> = ExtractValue<Methods, M>;
79
+ export type MethodData<M extends Method> = ExtractValue<Methods, M>;
80
80
  /**
81
81
  * Retrieve associated with specified method type
82
82
  */
83
- export declare type MethodPayloadType<M extends Method> = MethodData<M>['type'];
83
+ export type MethodPayloadType<M extends Method> = MethodData<M>['type'];
84
84
  /**
85
85
  * Retrieve specified method payload type
86
86
  */
87
- export declare type MethodPayload<M extends Method> = MethodData<M>['payload'];
87
+ export type MethodPayload<M extends Method> = MethodData<M>['payload'];
88
88
  /**
89
89
  * Retrieve specified method params
90
90
  */
91
- export declare type MethodParams<M extends Method> = MethodData<M>['params'];
91
+ export type MethodParams<M extends Method> = MethodData<M>['params'];
92
92
  export {};
@@ -1,5 +1,5 @@
1
1
  export var Method;
2
- (function (Method) {
2
+ (function(Method) {
3
3
  Method["BATCH"] = "batch";
4
4
  Method["CRM_COMPANY_FIELDS"] = "crm.company.fields";
5
5
  Method["CRM_COMPANY_ADD"] = "crm.company.add";
@@ -31,8 +31,8 @@ export var Method;
31
31
  Method["USER_GET"] = "user.get";
32
32
  })(Method || (Method = {}));
33
33
  const LISTABLE_METHODS = [
34
- Method.CRM_COMPANY_LIST,
35
- Method.CRM_CONTACT_LIST,
36
- Method.CRM_DEAL_LIST,
37
- Method.CRM_LEAD_LIST
34
+ "crm.company.list",
35
+ "crm.contact.list",
36
+ "crm.deal.list",
37
+ "crm.lead.list"
38
38
  ];
@@ -1,4 +1,4 @@
1
- export declare type PayloadTime = {
1
+ export type PayloadTime = {
2
2
  readonly start: number;
3
3
  readonly finish: number;
4
4
  readonly duration: number;
@@ -6,18 +6,18 @@ export declare type PayloadTime = {
6
6
  readonly date_start: string;
7
7
  readonly date_finish: string;
8
8
  };
9
- export declare type GetPayload<P> = {
9
+ export type GetPayload<P> = {
10
10
  readonly result: P;
11
11
  readonly time: PayloadTime;
12
12
  };
13
- export declare type ListPayload<P> = {
13
+ export type ListPayload<P> = {
14
14
  readonly result: readonly P[];
15
15
  readonly error?: string;
16
16
  readonly total: number;
17
17
  readonly next?: number;
18
18
  readonly time: PayloadTime;
19
19
  };
20
- export declare type BatchPayload<C> = {
20
+ export type BatchPayload<C> = {
21
21
  readonly result: {
22
22
  readonly result: {
23
23
  readonly [P in keyof C]?: C[P];
@@ -37,4 +37,4 @@ export declare type BatchPayload<C> = {
37
37
  };
38
38
  readonly time: PayloadTime;
39
39
  };
40
- export declare type Payload<P> = GetPayload<P> | ListPayload<P> | BatchPayload<P>;
40
+ export type Payload<P> = GetPayload<P> | ListPayload<P> | BatchPayload<P>;
@@ -0,0 +1 @@
1
+ export { };