bx-rest 1.1.0 → 1.2.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.
- package/CHANGELOG.md +21 -0
- package/README.en.md +25 -22
- package/README.md +25 -22
- package/esm2022/functions/errors.mjs +64 -8
- package/esm2022/services/navvy/extends/navvy-pag-base.mjs +12 -3
- package/fesm2022/bx-rest.mjs +72 -8
- package/fesm2022/bx-rest.mjs.map +1 -1
- package/functions/errors.d.ts +46 -6
- package/package.json +1 -1
- package/services/navvy/extends/navvy-pag-base.d.ts +7 -4
package/functions/errors.d.ts
CHANGED
|
@@ -1,13 +1,53 @@
|
|
|
1
|
+
export interface BXRestApiErrorDetails {
|
|
2
|
+
/**
|
|
3
|
+
* HTTP status of the portal response. `undefined` when the error came in
|
|
4
|
+
* the body of a successful (2xx) response.
|
|
5
|
+
*/
|
|
6
|
+
status?: number;
|
|
7
|
+
/** REST method that failed, e.g. `crm.deal.get`. */
|
|
8
|
+
method?: string;
|
|
9
|
+
/**
|
|
10
|
+
* The original error, e.g. the `AxiosError` — its `response.data` holds
|
|
11
|
+
* the full response body (including REST v3 `validation`).
|
|
12
|
+
*/
|
|
13
|
+
cause?: unknown;
|
|
14
|
+
}
|
|
1
15
|
/**
|
|
2
|
-
*
|
|
3
|
-
* Bitrix REST call resolves as a Bitrix-level API error, e.g.
|
|
4
|
-
* `INSUFFICIENT_SCOPE` or `expired_token`.
|
|
16
|
+
* A Bitrix-level API error, e.g. `INSUFFICIENT_SCOPE` or `expired_token`.
|
|
5
17
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
18
|
+
* `.res()` fails with it whenever the portal answers with a Bitrix error and
|
|
19
|
+
* a non-2xx HTTP status — which is how Bitrix sends most of its errors. When
|
|
20
|
+
* the error comes in the body of a 2xx response, `.res()` resolves as
|
|
21
|
+
* `undefined` by default and fails with it only with
|
|
22
|
+
* `{ throwOnApiError: true }` — see the "Error handling" section of the README.
|
|
8
23
|
*/
|
|
9
24
|
export declare class BXRestApiError extends Error {
|
|
10
25
|
error: string | undefined;
|
|
11
26
|
error_description: string | undefined;
|
|
12
|
-
|
|
27
|
+
readonly status: number | undefined;
|
|
28
|
+
readonly method: string | undefined;
|
|
29
|
+
constructor(error: string | undefined, error_description: string | undefined, details?: BXRestApiErrorDetails);
|
|
13
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* Recognises a Bitrix error body: REST v2 `{ error, error_description }` and
|
|
33
|
+
* REST v3 `{ error: { code, message } }`. Returns `undefined` for anything
|
|
34
|
+
* else — a success payload, an HTML page from a proxy, an empty body.
|
|
35
|
+
*
|
|
36
|
+
* An empty `error` still counts: Bitrix answers e.g. `crm.deal.get` for a
|
|
37
|
+
* missing ID with `{ error: '', error_description: 'Not found' }`.
|
|
38
|
+
*/
|
|
39
|
+
export declare function parseBXRestErrorBody(body: unknown): {
|
|
40
|
+
error: string;
|
|
41
|
+
error_description: string | undefined;
|
|
42
|
+
} | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* Turns an HTTP failure into a `BXRestApiError` when the portal answered with
|
|
45
|
+
* a Bitrix error body. Bitrix sends most errors with a non-2xx status
|
|
46
|
+
* (401 `expired_token`, 404 `ERROR_METHOD_NOT_FOUND`, 503
|
|
47
|
+
* `QUERY_LIMIT_EXCEEDED`, 400 "Not found" from `crm.*.get`, ...), and axios
|
|
48
|
+
* rejects on those.
|
|
49
|
+
*
|
|
50
|
+
* Returns `undefined` for network errors and for responses that are not a
|
|
51
|
+
* Bitrix error body, so the caller can keep its own handling for them.
|
|
52
|
+
*/
|
|
53
|
+
export declare function bxRestApiErrorFromHttp(err: unknown, method: string): BXRestApiError | undefined;
|
package/package.json
CHANGED
|
@@ -4,10 +4,13 @@ import { HttpBXServices } from '../../http/HttpBX';
|
|
|
4
4
|
import { ReturnTypeNavvy } from '../navvy-support';
|
|
5
5
|
export interface BXRestResOptions {
|
|
6
6
|
/**
|
|
7
|
-
* When `true`, a Bitrix-level API error
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
7
|
+
* When `true`, a Bitrix-level API error that comes in the body of a 2xx
|
|
8
|
+
* response makes the `Observable` returned by `.res()` error out with a
|
|
9
|
+
* `BXRestApiError` instead of resolving with `undefined`.
|
|
10
|
+
*
|
|
11
|
+
* Errors that Bitrix sends with a non-2xx HTTP status (most of them, e.g.
|
|
12
|
+
* `expired_token`, `INSUFFICIENT_SCOPE`) fail with `BXRestApiError`
|
|
13
|
+
* regardless of this option.
|
|
11
14
|
*
|
|
12
15
|
* Default: `false` (preserves the historical `.res()` behaviour).
|
|
13
16
|
*/
|