bkper-js 2.7.0 → 2.7.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.
|
@@ -8,12 +8,15 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
10
|
import { HttpBooksApiV5Request } from "./http-api-request.js";
|
|
11
|
-
import
|
|
11
|
+
import { HttpRequest } from "./http-request.js";
|
|
12
12
|
export function getBalances(bookId, query) {
|
|
13
13
|
return __awaiter(this, void 0, void 0, function* () {
|
|
14
|
-
var
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
var _a;
|
|
15
|
+
let response = yield new HttpBooksApiV5Request(`${bookId}/balances`).addParam('query', query).addParam('time', Date.now()).fetch();
|
|
16
|
+
const balancesUrl = (_a = response.data) === null || _a === void 0 ? void 0 : _a.balancesUrl; // Expected for large payloads
|
|
17
|
+
if (balancesUrl) {
|
|
18
|
+
const balancesResponse = yield new HttpRequest(balancesUrl).setMethod('GET').execute();
|
|
19
|
+
response = { data: balancesResponse.data, status: balancesResponse.status };
|
|
17
20
|
}
|
|
18
21
|
return response.data;
|
|
19
22
|
});
|
|
@@ -21,59 +21,57 @@ export class HttpApiRequest extends HttpRequest {
|
|
|
21
21
|
this.addCustomHeaders();
|
|
22
22
|
this.setHeader('Authorization', `Bearer ${yield getAccessToken()}`);
|
|
23
23
|
this.addParam('key', yield getApiKey());
|
|
24
|
-
// this.httpRequest.setMuteHttpExceptions(true);
|
|
25
24
|
try {
|
|
26
25
|
let resp = yield _super.execute.call(this);
|
|
27
26
|
if (resp.status >= 200 && resp.status < 300) {
|
|
28
27
|
return resp;
|
|
29
28
|
}
|
|
29
|
+
else if (resp.status == 404) {
|
|
30
|
+
return { data: null, status: resp.status };
|
|
31
|
+
}
|
|
32
|
+
else if (resp.status != 400 && this.retry <= 3) {
|
|
33
|
+
this.retry++;
|
|
34
|
+
if (HttpApiRequest.config.requestRetryHandler) {
|
|
35
|
+
yield HttpApiRequest.config.requestRetryHandler(resp.status, resp.data, this.retry);
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
console.log(`${JSON.stringify(resp.data)} - Retrying... `);
|
|
39
|
+
}
|
|
40
|
+
return yield this.fetch();
|
|
41
|
+
}
|
|
30
42
|
else {
|
|
31
|
-
|
|
43
|
+
// Create an error object that matches axios error structure for compatibility
|
|
44
|
+
const errorObj = {
|
|
45
|
+
response: {
|
|
46
|
+
status: resp.status,
|
|
47
|
+
data: resp.data
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
throw this.handleError(errorObj);
|
|
32
51
|
}
|
|
33
52
|
}
|
|
34
53
|
catch (error) {
|
|
54
|
+
// If error already has response structure (from our code above), use it
|
|
35
55
|
if (error.response) {
|
|
36
|
-
|
|
37
|
-
// The request was made and the server responded with a status code
|
|
38
|
-
// that falls out of the range of 2xx
|
|
39
|
-
// console.log(error.response.data);
|
|
40
|
-
// console.log(error.response.status);
|
|
41
|
-
// console.log(error.response.headers);
|
|
42
|
-
if (errorResp.status == 404) {
|
|
43
|
-
return { data: null };
|
|
44
|
-
}
|
|
45
|
-
else if (errorResp.status != 400 && this.retry <= 3) {
|
|
46
|
-
this.retry++;
|
|
47
|
-
if (HttpApiRequest.config.requestRetryHandler) {
|
|
48
|
-
yield HttpApiRequest.config.requestRetryHandler(errorResp.status, errorResp.data, this.retry);
|
|
49
|
-
}
|
|
50
|
-
else {
|
|
51
|
-
console.log(`${JSON.stringify(errorResp.data)} - Retrying... `);
|
|
52
|
-
}
|
|
53
|
-
return yield this.fetch();
|
|
54
|
-
}
|
|
55
|
-
throw this.handleError(errorResp.data);
|
|
56
|
+
throw error;
|
|
56
57
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
//
|
|
60
|
-
// http.ClientRequest in node.js
|
|
58
|
+
// Network error or fetch failure
|
|
59
|
+
if (error instanceof TypeError && error.message.includes('fetch')) {
|
|
60
|
+
// Network error - retry if within retry limit
|
|
61
61
|
if (this.retry <= 3) {
|
|
62
62
|
this.retry++;
|
|
63
63
|
if (HttpApiRequest.config.requestRetryHandler) {
|
|
64
64
|
yield HttpApiRequest.config.requestRetryHandler(520, undefined, this.retry);
|
|
65
65
|
}
|
|
66
66
|
else {
|
|
67
|
-
console.log(`
|
|
67
|
+
console.log(`Network error - Retrying... `);
|
|
68
68
|
}
|
|
69
69
|
return yield this.fetch();
|
|
70
70
|
}
|
|
71
71
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
}
|
|
76
|
-
throw this.handleError(error.toJSON ? error.toJSON() : error);
|
|
72
|
+
// Other errors
|
|
73
|
+
console.log('Error', error.message);
|
|
74
|
+
throw this.handleError(error);
|
|
77
75
|
}
|
|
78
76
|
});
|
|
79
77
|
}
|
|
@@ -7,9 +7,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
import https from 'https';
|
|
11
|
-
import axios from 'axios';
|
|
12
|
-
const httpsAgent = https && https.Agent ? new https.Agent({ keepAlive: true }) : undefined;
|
|
13
10
|
export class HttpRequest {
|
|
14
11
|
constructor(url) {
|
|
15
12
|
this.params = [];
|
|
@@ -64,14 +61,39 @@ export class HttpRequest {
|
|
|
64
61
|
execute() {
|
|
65
62
|
return __awaiter(this, void 0, void 0, function* () {
|
|
66
63
|
const url = this.getUrl();
|
|
67
|
-
|
|
68
|
-
url: url,
|
|
64
|
+
const fetchOptions = {
|
|
69
65
|
method: this.method,
|
|
70
66
|
headers: this.headers,
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
67
|
+
};
|
|
68
|
+
// Add body for non-GET requests
|
|
69
|
+
if (this.payload && this.method !== 'GET') {
|
|
70
|
+
if (typeof this.payload === 'string') {
|
|
71
|
+
fetchOptions.body = this.payload;
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
fetchOptions.body = JSON.stringify(this.payload);
|
|
75
|
+
// Ensure content-type is set for JSON payloads
|
|
76
|
+
if (!this.headers['Content-Type'] && !this.headers['content-type']) {
|
|
77
|
+
fetchOptions.headers = Object.assign(Object.assign({}, this.headers), { 'Content-Type': 'application/json' });
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
const response = yield fetch(url, fetchOptions);
|
|
82
|
+
// Parse response body
|
|
83
|
+
let data;
|
|
84
|
+
const contentType = response.headers.get('content-type');
|
|
85
|
+
if (contentType && contentType.includes('application/json')) {
|
|
86
|
+
data = yield response.json();
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
data = yield response.text();
|
|
90
|
+
}
|
|
91
|
+
// Return axios-compatible response structure
|
|
92
|
+
return {
|
|
93
|
+
data: data,
|
|
94
|
+
status: response.status,
|
|
95
|
+
headers: response.headers
|
|
96
|
+
};
|
|
75
97
|
});
|
|
76
98
|
}
|
|
77
99
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bkper-js",
|
|
3
|
-
"version": "2.7.
|
|
3
|
+
"version": "2.7.1",
|
|
4
4
|
"description": "Javascript client for Bkper REST API",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"module": "./lib/index.js",
|
|
@@ -37,7 +37,6 @@
|
|
|
37
37
|
"@bkper/bkper-api-types": "^5.25.0"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"axios": "^1.7.7",
|
|
41
40
|
"big.js": "^6.0.3",
|
|
42
41
|
"dayjs": "^1.10.3",
|
|
43
42
|
"luxon": "^1.25.0"
|