bx-rest 0.10.7 → 0.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. package/README.md +388 -263
  2. package/consts/part-name-methods.d.ts +4 -0
  3. package/esm2022/consts/part-name-methods.mjs +5 -1
  4. package/esm2022/request-navvy/bizproc/activity.mjs +64 -0
  5. package/esm2022/request-navvy/bizproc/event.mjs +24 -0
  6. package/esm2022/request-navvy/bizproc/robot.mjs +51 -0
  7. package/esm2022/request-navvy/bizproc/task.mjs +25 -7
  8. package/esm2022/request-navvy/bizproc/workflow/template.mjs +54 -0
  9. package/esm2022/request-navvy/bizproc/workflow.mjs +31 -9
  10. package/esm2022/request-navvy/bizproc.mjs +13 -1
  11. package/esm2022/request-navvy/event/offline.mjs +61 -0
  12. package/esm2022/request-navvy/event.mjs +69 -0
  13. package/esm2022/rest/navvy.mjs +4 -1
  14. package/esm2022/typification/rest/bizproc/activity.mjs +2 -0
  15. package/esm2022/typification/rest/bizproc/base/registration.mjs +2 -0
  16. package/esm2022/typification/rest/bizproc/event.mjs +2 -0
  17. package/esm2022/typification/rest/bizproc/robot.mjs +2 -0
  18. package/esm2022/typification/rest/bizproc/task/complete.mjs +1 -1
  19. package/esm2022/typification/rest/bizproc/task/delegate.mjs +1 -1
  20. package/esm2022/typification/rest/bizproc/workflow/kill.mjs +1 -1
  21. package/esm2022/typification/rest/bizproc/workflow/start.mjs +1 -1
  22. package/esm2022/typification/rest/bizproc/workflow/template/add.mjs +2 -0
  23. package/esm2022/typification/rest/bizproc/workflow/template/delete.mjs +2 -0
  24. package/esm2022/typification/rest/bizproc/workflow/template/list.mjs +1 -1
  25. package/esm2022/typification/rest/bizproc/workflow/template/update.mjs +2 -0
  26. package/esm2022/typification/rest/bizproc/workflow/terminate.mjs +1 -1
  27. package/esm2022/typification/rest/bizproc.mjs +8 -1
  28. package/esm2022/typification/rest/event.mjs +2 -0
  29. package/esm2022/typification/rest/user/user.mjs +1 -1
  30. package/fesm2022/bx-rest.mjs +382 -14
  31. package/fesm2022/bx-rest.mjs.map +1 -1
  32. package/package.json +4 -1
  33. package/request-navvy/bizproc/activity.d.ts +50 -0
  34. package/request-navvy/bizproc/event.d.ts +18 -0
  35. package/request-navvy/bizproc/robot.d.ts +39 -0
  36. package/request-navvy/bizproc/task.d.ts +21 -3
  37. package/request-navvy/bizproc/workflow/template.d.ts +42 -0
  38. package/request-navvy/bizproc/workflow.d.ts +26 -4
  39. package/request-navvy/bizproc.d.ts +12 -0
  40. package/request-navvy/event/offline.d.ts +49 -0
  41. package/request-navvy/event.d.ts +57 -0
  42. package/rest/navvy.d.ts +3 -0
  43. package/typification/rest/bizproc/activity.d.ts +25 -0
  44. package/typification/rest/bizproc/base/registration.d.ts +66 -0
  45. package/typification/rest/bizproc/event.d.ts +9 -0
  46. package/typification/rest/bizproc/robot.d.ts +18 -0
  47. package/typification/rest/bizproc/task/complete.d.ts +6 -0
  48. package/typification/rest/bizproc/task/delegate.d.ts +4 -0
  49. package/typification/rest/bizproc/workflow/kill.d.ts +3 -1
  50. package/typification/rest/bizproc/workflow/start.d.ts +9 -1
  51. package/typification/rest/bizproc/workflow/template/add.d.ts +21 -0
  52. package/typification/rest/bizproc/workflow/template/delete.d.ts +5 -0
  53. package/typification/rest/bizproc/workflow/template/list.d.ts +32 -6
  54. package/typification/rest/bizproc/workflow/template/update.d.ts +19 -0
  55. package/typification/rest/bizproc/workflow/terminate.d.ts +3 -0
  56. package/typification/rest/bizproc.d.ts +7 -0
  57. package/typification/rest/event.d.ts +178 -0
  58. package/typification/rest/user/user.d.ts +32 -32
package/README.md CHANGED
@@ -1,68 +1,28 @@
1
- # bx-rest - Bitrix24 REST API client for TypeScript, Angular and Vue
1
+ # bx-rest - Bitrix24 REST API client for TypeScript, Angular, Vue and React
2
2
 
3
3
  `bx-rest` is a TypeScript SDK for the Bitrix24 REST API. It helps call BX24 REST methods, work with auth tokens, sessid, OAuth2, pagination, mappers and typed API requests.
4
4
 
5
5
  Also known as: Bitrix24 REST client, BX24 REST SDK, Bitrix24 API client, Bitrix REST TypeScript library.
6
-
7
- ### Install
8
-
9
- ```shell
10
- npm install bx-rest
11
- ```
12
- # Usage in Angular
13
- ```typescript
14
- import { BXRestSettings } from 'bx-rest'
15
-
16
- BXRestSettings.update({
17
- auth: {
18
- source: 'cookies',
19
- key: 'auth'
20
- },
21
- urls: {
22
- source: 'string',
23
- key: environment.urls.home,
24
- additional_part: 'rest'
25
- }
26
- })
27
-
28
- export const BXRestNavvyProvider: Provider = {
29
- provide: BXRestNavvy,
30
- useClass: BXRestNavvy,
31
- };
32
-
33
- export const BXRestMapProvider: Provider = {
34
- provide: BXRestMap,
35
- useFactory: () => new BXRestMap(),
36
- }
37
-
38
- export const BXRestRequestProvider: Provider = {
39
- provide: BXRestRequest,
40
- useFactory: () => new BXRestRequest(),
41
- }
42
-
43
- export const appConfig: ApplicationConfig = {
44
- providers: [
45
- provideRouter(routes),
46
- provideHttpClient(),
47
- BXRestNavvyProvider,
48
- BXRestMapProvider,
49
- BXRestRequestProvider,
50
- BXRestMapProvider
51
- ]
52
- }
53
- document.cookie ='auth=ACCESS_TOKEN; max-age=99999'
6
+
7
+ ## Install
8
+
9
+ ```shell
10
+ npm install bx-rest
54
11
  ```
55
12
 
56
- # SessionKeyServices
13
+ ## Quick Start
57
14
 
58
- `SessionKeyServices` is available from the public API and can be used when you need to inspect the current authorization data before making REST requests.
15
+ Configure the portal URL and an access-token source once, then create a client and subscribe to the returned RxJS `Observable`. Use a complete URL including `https://`.
59
16
 
60
17
  ```typescript
61
- import { BXRestSettings, SessionKeyServices, SessionKeyError } from 'bx-rest'
18
+ import { firstValueFrom } from 'rxjs'
19
+ import { BXRestNavvy, BXRestSettings } from 'bx-rest'
20
+
21
+ const accessToken = 'YOUR_ACCESS_TOKEN'
62
22
 
63
23
  BXRestSettings.update({
64
24
  auth: {
65
- source: 'cookies',
25
+ source: () => accessToken,
66
26
  key: 'auth'
67
27
  },
68
28
  urls: {
@@ -72,242 +32,407 @@ BXRestSettings.update({
72
32
  }
73
33
  })
74
34
 
75
- const session = new SessionKeyServices()
35
+ const bxRest = new BXRestNavvy()
36
+
37
+ const deals = await firstValueFrom(
38
+ bxRest.crm.deal.list({
39
+ select: ['ID', 'TITLE'],
40
+ filter: {},
41
+ start: 0
42
+ }).res()
43
+ )
44
+
45
+ console.log(deals)
46
+ ```
47
+
48
+ Do not hardcode or commit a production access token. Pass it from your application's authorization flow, server response or secure runtime configuration.
49
+
50
+ ### Result modes
51
+
52
+ REST calls are lazy: the request starts when the returned `Observable` is subscribed to or passed to `firstValueFrom`.
76
53
 
77
- const auth = session.getAuthParams()
78
- const authKey = session.getKeyAuth()
54
+ | Method | Result |
55
+ | --- | --- |
56
+ | `.res()` | Unwraps the Bitrix response and applies the configured mapper. For paginated methods it returns the current page. |
57
+ | `.resAll()` | Loads and combines every page. Available on paginated Navvy helpers. |
58
+ | `.resVanilla()` | Returns the raw Bitrix response, including fields such as `result`, `total` and `next`. |
59
+ | `.mapForVanilla()` | Keeps the raw response envelope but applies the mapper to its `result`. |
79
60
 
80
- if (!auth && session.getCheckAuthParamsIsOn()) {
81
- const error = session.getAuthError('manual auth check')
82
- if (error instanceof SessionKeyError) {
83
- console.error(error.message)
61
+ ## Authentication and OAuth2
62
+
63
+ `auth.source` controls where credentials are read from. `auth.key` controls the request parameter name, except for the special `OAuth2` cookie-credentials mode.
64
+
65
+ | Scenario | `auth.source` | `auth.key` | Behaviour |
66
+ | --- | --- | --- | --- |
67
+ | Access token from application state | `() => accessToken` | `auth` | Calls the function before each request and sends its value in the `auth` parameter. Recommended for OAuth2 access tokens managed by the application. |
68
+ | Access token in local storage | `localStorage` | `auth` | Reads `localStorage["auth"]` and sends it as `auth`. |
69
+ | Access token in a cookie | `cookies` | `auth` | Reads the `auth` cookie and sends it as `auth`. |
70
+ | Embedded Bitrix page / local session | `cookies` | `sessid` | Tries `window.BX.bitrix_sessid()`, the `sessid` query parameter, `localStorage.sessid`, then the `auth` cookie. Sends the resolved value as `sessid`. |
71
+ | Incoming webhook or URL that already contains authorization | `off` | Any value | Skips credential lookup and does not add an authorization parameter. |
72
+ | Cookie-based credential request | Any source returning a non-empty value | `OAuth2` | Does not append the resolved value to request params and enables Axios `withCredentials`. Use only when the portal and CORS policy support credential cookies. |
73
+
74
+ For a regular Bitrix OAuth2 `access_token`, use `auth.key: 'auth'` and provide the current token through a function or `localStorage`. The special `auth.key: 'OAuth2'` mode is for credential cookies; it does not send an OAuth access token by itself.
75
+
76
+ For an incoming webhook URL, keep the full webhook path in `urls.key`, set `additional_part` to an empty string and disable additional authorization:
77
+
78
+ ```typescript
79
+ BXRestSettings.update({
80
+ auth: {
81
+ source: 'off',
82
+ key: 'auth'
83
+ },
84
+ urls: {
85
+ source: 'string',
86
+ key: 'https://example.bitrix24.com/rest/1/WEBHOOK_CODE',
87
+ additional_part: ''
84
88
  }
85
- throw error
89
+ })
90
+ ```
91
+
92
+ ## Usage in Angular
93
+
94
+ Register the public clients and `Navvy` when you want to inject them into Angular services or components:
95
+
96
+ ```typescript
97
+ import { ApplicationConfig, Provider } from '@angular/core'
98
+ import {
99
+ BXRestMap,
100
+ BXRestNavvy,
101
+ BXRestRequest,
102
+ Navvy
103
+ } from 'bx-rest'
104
+
105
+ export const BXRestNavvyProvider: Provider = {
106
+ provide: BXRestNavvy,
107
+ useFactory: () => new BXRestNavvy()
86
108
  }
87
109
 
88
- session.getBaseUrl().subscribe((baseUrl) => {
89
- if (!baseUrl) {
90
- throw session.getBaseUrlError('manual base url check')
91
- }
110
+ export const BXRestMapProvider: Provider = {
111
+ provide: BXRestMap,
112
+ useFactory: () => new BXRestMap()
113
+ }
92
114
 
93
- console.log({ authKey, auth, baseUrl })
94
- })
115
+ export const BXRestRequestProvider: Provider = {
116
+ provide: BXRestRequest,
117
+ useFactory: () => new BXRestRequest()
118
+ }
119
+
120
+ export const NavvyProvider: Provider = {
121
+ provide: Navvy,
122
+ useFactory: () => new Navvy()
123
+ }
124
+
125
+ export const appConfig: ApplicationConfig = {
126
+ providers: [
127
+ BXRestNavvyProvider,
128
+ BXRestMapProvider,
129
+ BXRestRequestProvider,
130
+ NavvyProvider
131
+ ]
132
+ }
95
133
  ```
96
134
 
97
- For `auth.key: 'auth'`, the service reads the `auth` cookie. For `auth.key: 'sessid'`, it tries `window.BX.bitrix_sessid()`, then the `sessid` query parameter, then `localStorage.sessid`, and finally the `auth` cookie. If `auth.source` is `localStorage`, the value is read by `auth.key`. If `auth.source` is `off`, authorization params are not added to requests.
135
+ Example component:
98
136
 
99
137
  ```typescript
138
+ import { Component, inject } from '@angular/core'
100
139
  import { BXRestNavvy } from 'bx-rest'
101
140
 
102
141
  @Component({
103
- selector: 'app-any',
104
- templateUrl: './any.component.html',
105
- styleUrls: ['./any.component.scss']
106
- })
107
- export class AnyComponent {
108
-
109
- listElements$ = this.BXRestNavvy.lists.element.get({
110
- IBLOCK_TYPE_ID: 'lists',
111
- IBLOCK_ID: 150,
112
- FILTER: {
113
- ['>' + vacancies.del]: 0
114
- }
115
- }).res() // or .resAll() - to get all elements or .resVanilla() - to get all elements
116
-
117
- private readonly BXRestNavvy = inject(BXRestNavvy)
118
- }
119
- ```
120
- # Usage in Vue
121
- ```typescript
122
- import { BXRestSettings, BXRestMap, BXRestNavvy, BXRestRequest } from 'bx-rest';
123
-
124
- const bxRestPlugin = {
125
- install(Vue) {
126
- // Settings bx-rest
127
- BXRestSettings.update({
128
- auth: {
129
- source: 'cookies',
130
- key: 'auth',
131
- },
132
- urls: {
133
- source: 'string',
134
- key: 'b24.trace-studio.com',
135
- additional_part: 'rest',
136
- },
137
- });
138
-
139
- // Adding instances to global access
140
- Vue.config.globalProperties.$bxRestNavvy = new BXRestNavvy();
141
- Vue.config.globalProperties.$bxRestMap = new BXRestMap();
142
- Vue.config.globalProperties.$bxRestRequest = new BXRestRequest();
143
- },
144
- };
145
-
146
- export default bxRestPlugin;
147
- ```
148
-
149
- ```vue
150
- import './assets/main.css'
151
-
152
- import { createApp } from 'vue'
153
- import App from './App.vue'
154
- import bxRestPlugin from './bxRestPlugin';
155
-
156
- const app = createApp(App);
157
-
158
- app.use(bxRestPlugin);
159
-
160
- app.mount('#app');
161
- ```
162
-
163
- # Extension
164
- You can add your own methods to the rest API; you can read more about this here:
165
- https://dev.1c-bitrix.ru/learning/course/index.php?COURSE_ID=99&LESSON_ID=7985
142
+ selector: 'app-list',
143
+ template: ''
144
+ })
145
+ export class ListComponent {
146
+ private readonly bxRest = inject(BXRestNavvy)
147
+
148
+ readonly listElements$ = this.bxRest.lists.element.get({
149
+ IBLOCK_TYPE_ID: 'lists',
150
+ IBLOCK_ID: 150,
151
+ FILTER: {
152
+ '>ID': 0
153
+ }
154
+ }).res()
155
+ }
156
+ ```
157
+
158
+ ## SessionKeyServices
159
+
160
+ `SessionKeyServices` is available from the public API and can be used when you need to inspect the current authorization data before making REST requests.
166
161
 
167
- And after creating the methods, we can add functions to call them in the same style as in the current plugin:
168
162
  ```typescript
169
- import { Injectable } from '@angular/core'
170
- import { partNameMethods as PNM } from 'bx-rest' // part name methods for saving add size
171
- import { BXRestRequest } from 'bx-rest'
172
- import {
173
- iBXRestCustomBlogpostGetViewParam,
174
- iBXRestCustomHttpBlogpostGetView
175
- } from '@/lib/typification/bitrix/api/custom/blogpost/get/view'
176
- import {
177
- iBXRestCustomHttpBlogpostGetRating,
178
- iBXRestCustomHttpBlogpostGetRatingParam
179
- } from '@/lib/typification/bitrix/api/custom/blogpost/get/rating'
180
-
181
- @Injectable({
182
- providedIn: 'root'
183
- })
184
- export class BXRestCustomBlogpostGet {
185
-
186
- url = {
187
- view: [PNM.$log, PNM.$blogpost, PNM.$get, 'view'],
188
- rating: [PNM.$log, PNM.$blogpost, PNM.$get, 'rating'],
189
- }
190
-
191
- private readonly http = inject(BXRestRequest)
192
-
193
- view(param: iBXRestCustomBlogpostGetViewParam) {
194
- return this.http.post<iBXRestCustomHttpBlogpostGetView[]>(this.url.view, param)
195
- }
163
+ import { BXRestSettings, SessionKeyServices, SessionKeyError } from 'bx-rest'
196
164
 
197
- rating(param: iBXRestCustomHttpBlogpostGetRatingParam) {
198
- return this.http.post<iBXRestCustomHttpBlogpostGetRating[]>(this.url.rating, param)
165
+ BXRestSettings.update({
166
+ auth: {
167
+ source: 'cookies',
168
+ key: 'auth'
169
+ },
170
+ urls: {
171
+ source: 'string',
172
+ key: 'https://example.bitrix24.com',
173
+ additional_part: 'rest'
199
174
  }
200
- }
201
- ```
202
- in navvy:
203
- ```typescript
204
- import { Injectable } from '@angular/core'
205
- import { Navvy } from 'bx-rest'
206
- import { BXRestCustomBlogpostGet } from '../../blogpost/get'
207
- import { BXRestCustomMapBlogpostGet } from '../../map/blogpost/get'
208
- import { iBXRestCustomBlogpostGetViewParam } from '@/lib/typification/bitrix/api/custom/blogpost/get/view'
209
- import {
210
- iBXRestCustomHttpBlogpostGetRatingParam
211
- } from '@/lib/typification/bitrix/api/custom/blogpost/get/rating'
212
-
213
- @Injectable({
214
- providedIn: 'root'
215
175
  })
216
- export class BXRestCustomNavvyBlogpostGet {
217
176
 
218
- Navvy: Navvy<BXRestCustomBlogpostGet, BXRestCustomMapBlogpostGet>
177
+ const session = new SessionKeyServices()
219
178
 
220
- url = {
221
- view: [PNM.$log, PNM.$blogpost, PNM.$get, 'view'],
222
- rating: [PNM.$log, PNM.$blogpost, PNM.$get, 'rating'],
223
- }
224
-
225
- private readonly http = inject(BXRestRequest)
226
- private readonly BXRestCustomBlogpostGet = inject(BXRestCustomBlogpostGet)
227
- private readonly BXRestCustomMapBlogpostGet = inject(BXRestCustomMapBlogpostGet)
228
- private readonly Navvy = new Navvy(this.BXRestCustomBlogpostGet, this.BXRestCustomMapBlogpostGet)
229
-
230
- view(param: iBXRestCustomBlogpostGetViewParam) {
231
- return this.Navvy.PagNav(this.url.view, param, this.BXRestCustomMapBlogpostGet.view)
232
- }
179
+ const auth = session.getAuthParams()
180
+ const authKey = session.getKeyAuth()
233
181
 
234
- rating(param: iBXRestCustomHttpBlogpostGetRatingParam) {
235
- return this.Navvy.PagNav(this.url.rating, param, this.BXRestCustomMapBlogpostGet.rating)
182
+ if (!auth && session.getCheckAuthParamsIsOn()) {
183
+ const error = session.getAuthError('manual auth check')
184
+ if (error instanceof SessionKeyError) {
185
+ console.error(error.message)
236
186
  }
187
+ throw error
237
188
  }
238
- ```
239
- mapper:
240
- ```typescript
241
- import { Injectable } from '@angular/core'
242
- import {
243
- iBXRestCustomBlogpostGetView,
244
- iBXRestCustomHttpBlogpostGetView
245
- } from '@/lib/typification/bitrix/api/custom/blogpost/get/view'
246
- import {
247
- iBXRestCustomBlogpostGetRating,
248
- iBXRestCustomHttpBlogpostGetRating,
249
- } from '@/lib/typification/bitrix/api/custom/blogpost/get/rating'
250
- import { BXRestMapBase } from 'bx-rest'
251
-
252
- @Injectable({
253
- providedIn: 'root'
254
- })
255
- export class BXRestCustomMapBlogpostGet extends BXRestMapBase {
256
189
 
257
- view(value: iBXRestCustomHttpBlogpostGetView[] | undefined): iBXRestCustomBlogpostGetView[] {
258
- return (value) ? value.map(i => {
259
- return {
260
- ID: toNum(i.ID),
261
- USER_ID: toNum(i.USER_ID),
262
- DATE: toDate(i.DATE, 'dd.MM.yyyy HH:mm:ss'),
263
- }
264
- })
265
- : []
190
+ session.getBaseUrl().subscribe((baseUrl) => {
191
+ if (!baseUrl) {
192
+ throw session.getBaseUrlError('manual base url check')
266
193
  }
267
194
 
268
- rating(value: iBXRestCustomHttpBlogpostGetRating[] | undefined): iBXRestCustomBlogpostGetRating[] {
269
- return (value) ? value.map(i => {
270
- return {
271
- ID: toNum(i.ID),
272
- REACTION: i.REACTION,
273
- TOTAL_VOTES: toNum(i.TOTAL_VOTES),
274
- }
275
- }) as iBXRestCustomBlogpostGetRating[]
276
- : [] as iBXRestCustomBlogpostGetRating[]
277
- }
278
- }
279
- ```
280
- # Specific methods
281
-
282
- tasks.task.list:
283
- ```typescript
284
-
285
- // your's select defult fields
286
- export type selectTaskFieldsType = Extract<
287
- iBXRestTaskFieldsName,
288
- 'ID' | 'TITLE' | 'GROUP_ID' | 'TIME_ESTIMATE' | 'CREATED_DATE' | 'CLOSED_DATE' | 'DURATION_FACT' | 'TIME_SPENT_IN_LOGS'
289
- >
290
-
291
- // your's custom fields tasks
292
- export type customTaskField = { ufListDiscipline: string, ufUfListSubdiscipline: string, ufList1: string }
293
-
294
- return this.BXRestNavvy.tasks.task.list<selectTaskFieldsType[], customTaskField>(
295
- {
296
- order: {
297
- ID: 'DESC'
298
- },
299
- filter: {
300
- CREATED_BY: [v.ID],
301
- // '<REAL_STATUS': 5,
302
- TITLE: (this.formControl.controls.textSearch.value) ? this.formControl.controls.textSearch.value : ''
303
- },
304
- select: [...selectTaskFields, ...selectTaskFieldsCustom],
305
- start: (this.formControl.controls.pageNumber.value) ? this.formControl.controls.pageNumber.value * 50 : 0
306
- }
307
- )
308
- ```
195
+ console.log({ authKey, auth, baseUrl })
196
+ })
197
+ ```
198
+
199
+ ## Usage in Vue
200
+
201
+ ```typescript
202
+ import type { Plugin } from 'vue'
203
+ import { BXRestSettings, BXRestMap, BXRestNavvy, BXRestRequest } from 'bx-rest'
204
+
205
+ const bxRestPlugin: Plugin = {
206
+ install(app) {
207
+ BXRestSettings.update({
208
+ auth: {
209
+ source: 'cookies',
210
+ key: 'auth'
211
+ },
212
+ urls: {
213
+ source: 'string',
214
+ key: 'https://example.bitrix24.com',
215
+ additional_part: 'rest'
216
+ }
217
+ })
218
+
219
+ app.config.globalProperties.$bxRestNavvy = new BXRestNavvy()
220
+ app.config.globalProperties.$bxRestMap = new BXRestMap()
221
+ app.config.globalProperties.$bxRestRequest = new BXRestRequest()
222
+ }
223
+ }
224
+
225
+ export default bxRestPlugin
226
+ ```
227
+
228
+ ```vue
229
+ import './assets/main.css'
230
+
231
+ import { createApp } from 'vue'
232
+ import App from './App.vue'
233
+ import bxRestPlugin from './bxRestPlugin'
234
+
235
+ const app = createApp(App)
236
+
237
+ app.use(bxRestPlugin)
238
+
239
+ app.mount('#app')
240
+ ```
241
+
242
+ ## Usage in React
243
+
244
+ Create the client outside the component and unsubscribe when the component is unmounted:
245
+
246
+ ```tsx
247
+ import { useEffect, useState } from 'react'
248
+ import { BXRestNavvy } from 'bx-rest'
249
+
250
+ const bxRest = new BXRestNavvy()
251
+
252
+ export function Deals() {
253
+ const [deals, setDeals] = useState<unknown[]>([])
254
+
255
+ useEffect(() => {
256
+ const subscription = bxRest.crm.deal.list({
257
+ select: ['ID', 'TITLE'],
258
+ filter: {},
259
+ start: 0
260
+ }).res().subscribe({
261
+ next: value => setDeals(value ?? []),
262
+ error: error => console.error(error)
263
+ })
264
+
265
+ return () => subscription.unsubscribe()
266
+ }, [])
267
+
268
+ return <pre>{JSON.stringify(deals, null, 2)}</pre>
269
+ }
270
+ ```
271
+
272
+ ## Custom REST methods
273
+
274
+ `Navvy` is exported from `bx-rest`. In Angular, register the `NavvyProvider` shown above and inject `Navvy` into your custom API service. A method URL is represented as an array and is joined with dots before the request is sent.
275
+
276
+ See the [Bitrix24 documentation](https://dev.1c-bitrix.ru/learning/course/index.php?COURSE_ID=99&LESSON_ID=7985) for registering custom REST methods.
277
+
278
+ ### Mapper
279
+
280
+ A mapper is a regular function. It does not need to extend `BXRestMap`.
281
+
282
+ ```typescript
283
+ import {
284
+ iBXRestCustomBlogGet,
285
+ iBXRestCustomBlogGetHttp
286
+ } from '../type/blog'
287
+
288
+ export class BXCustomBlogMap {
289
+ static get(
290
+ value: iBXRestCustomBlogGetHttp[] | undefined
291
+ ): iBXRestCustomBlogGet[] {
292
+ return (value ?? []).map(item => ({
293
+ id: Number(item.ID),
294
+ title: item.TITLE
295
+ }))
296
+ }
297
+ }
298
+ ```
299
+
300
+ ### A custom method with `simple()`
301
+
302
+ Use `simple()` when the method returns one response without automatic pagination:
303
+
304
+ ```typescript
305
+ import { inject, Injectable } from '@angular/core'
306
+ import { Navvy, partNameMethods as PNM } from 'bx-rest'
307
+ import { BXCustomBlogMap } from '../map/blog'
308
+ import {
309
+ iBXRestCustomBlogGet,
310
+ iBXRestCustomBlogGetHttp,
311
+ iBXRestCustomParamBlogGet
312
+ } from '../type/blog'
313
+
314
+ @Injectable({
315
+ providedIn: 'root'
316
+ })
317
+ export class BXRestNavvyCustomApiBlog {
318
+ private readonly url = {
319
+ get: ['blog', PNM.$get]
320
+ }
321
+
322
+ private readonly navvy = inject(Navvy)
323
+
324
+ get(param: iBXRestCustomParamBlogGet) {
325
+ return this.navvy.simple<
326
+ iBXRestCustomBlogGetHttp[],
327
+ iBXRestCustomBlogGet[],
328
+ iBXRestCustomParamBlogGet
329
+ >(
330
+ this.url.get,
331
+ param,
332
+ BXCustomBlogMap.get
333
+ )
334
+ }
335
+ }
336
+ ```
337
+
338
+ The returned helper supports `.res()`, `.resVanilla()` and `.mapForVanilla()`.
339
+
340
+ ### A paginated custom method with `pagNav()`
341
+
342
+ Use `pagNav()` when the Bitrix response contains an array and standard `start`, `next` and `total` pagination fields. The parameter type must support `start?: number`.
343
+
344
+ ```typescript
345
+ import { inject, Injectable } from '@angular/core'
346
+ import { Navvy, partNameMethods as PNM } from 'bx-rest'
347
+ import type { iBXRestPagination } from 'bx-rest/typification/base'
348
+ import { BXCustomBlogMap } from '../map/blog'
349
+ import {
350
+ iBXRestCustomBlogGet,
351
+ iBXRestCustomBlogGetHttp
352
+ } from '../type/blog'
353
+
354
+ export interface iBXRestCustomParamBlogList extends iBXRestPagination {
355
+ filter?: Record<string, unknown>
356
+ }
357
+
358
+ @Injectable({
359
+ providedIn: 'root'
360
+ })
361
+ export class BXRestNavvyCustomApiBlogList {
362
+ private readonly url = {
363
+ list: ['blog', PNM.$list]
364
+ }
365
+
366
+ private readonly navvy = inject(Navvy)
367
+
368
+ list(param: iBXRestCustomParamBlogList) {
369
+ return this.navvy.pagNav<
370
+ iBXRestCustomBlogGetHttp,
371
+ iBXRestCustomBlogGet,
372
+ iBXRestCustomParamBlogList
373
+ >(
374
+ this.url.list,
375
+ param,
376
+ BXCustomBlogMap.get
377
+ )
378
+ }
379
+ }
380
+ ```
381
+
382
+ The returned helper additionally supports `.resAll()` for loading and combining all pages.
383
+
384
+ ## Specific methods
385
+
386
+ ### `tasks.task.list`
387
+
388
+ The first generic argument selects built-in task fields. The second adds project-specific user fields:
389
+
390
+ ```typescript
391
+ import { BXRestNavvy } from 'bx-rest'
392
+ import type { iBXRestTaskFieldsName } from 'bx-rest/typification/tasks'
393
+
394
+ type SelectedTaskField = Extract<
395
+ iBXRestTaskFieldsName,
396
+ 'ID' | 'TITLE' | 'GROUP_ID' | 'TIME_ESTIMATE' | 'CREATED_DATE' | 'CLOSED_DATE' | 'DURATION_FACT' | 'TIME_SPENT_IN_LOGS'
397
+ >
398
+
399
+ type CustomTaskFields = {
400
+ ufListDiscipline: string
401
+ ufListSubdiscipline: string
402
+ ufList1: string
403
+ }
404
+
405
+ const selectedTaskFields: SelectedTaskField[] = [
406
+ 'ID',
407
+ 'TITLE',
408
+ 'GROUP_ID',
409
+ 'CREATED_DATE'
410
+ ]
411
+
412
+ const selectedCustomFields = [
413
+ 'UF_LIST_DISCIPLINE',
414
+ 'UF_LIST_SUBDISCIPLINE',
415
+ 'UF_LIST_1'
416
+ ] as const
417
+
418
+ const bxRest = new BXRestNavvy()
419
+
420
+ const tasks$ = bxRest.tasks.task.list<
421
+ SelectedTaskField[],
422
+ CustomTaskFields
423
+ >({
424
+ order: {
425
+ ID: 'DESC'
426
+ },
427
+ filter: {
428
+ CREATED_BY: [1]
429
+ },
430
+ select: [...selectedTaskFields, ...selectedCustomFields],
431
+ start: 0
432
+ }).res()
433
+ ```
309
434
 
310
- # Future features
435
+ ## Future features
311
436
  - Auto get token
312
437
  - Mappers for normalization types
313
- - 100% coverage
438
+ - 100% coverage