oneentry 1.0.123 → 1.0.125
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/README.md +24 -22
- package/dist/admins/adminsApi.d.ts +30 -9
- package/dist/admins/adminsApi.js +30 -9
- package/dist/admins/adminsInterfaces.d.ts +77 -32
- package/dist/attribute-sets/attributeSetsApi.d.ts +36 -22
- package/dist/attribute-sets/attributeSetsApi.js +41 -25
- package/dist/attribute-sets/attributeSetsInterfaces.d.ts +133 -39
- package/dist/auth-provider/authProviderApi.d.ts +209 -88
- package/dist/auth-provider/authProviderApi.js +226 -87
- package/dist/auth-provider/authProvidersInterfaces.d.ts +345 -18
- package/dist/base/asyncModules.d.ts +38 -28
- package/dist/base/asyncModules.js +178 -47
- package/dist/base/result.js +23 -0
- package/dist/base/syncModules.d.ts +48 -44
- package/dist/base/syncModules.js +84 -64
- package/dist/base/utils.d.ts +1 -1
- package/dist/blocks/blocksApi.d.ts +48 -27
- package/dist/blocks/blocksApi.js +48 -27
- package/dist/blocks/blocksInterfaces.d.ts +126 -32
- package/dist/events/eventsApi.d.ts +45 -12
- package/dist/events/eventsApi.js +47 -12
- package/dist/events/eventsInterfaces.d.ts +50 -3
- package/dist/file-uploading/fileUploadingApi.d.ts +57 -29
- package/dist/file-uploading/fileUploadingApi.js +57 -30
- package/dist/file-uploading/fileUploadingInterfaces.d.ts +90 -17
- package/dist/forms/formsApi.d.ts +20 -9
- package/dist/forms/formsApi.js +19 -8
- package/dist/forms/formsInterfaces.d.ts +86 -23
- package/dist/formsData/formsDataApi.d.ts +33 -21
- package/dist/formsData/formsDataApi.js +33 -21
- package/dist/formsData/formsDataInterfaces.d.ts +212 -41
- package/dist/general-types/generalTypesApi.d.ts +11 -2
- package/dist/general-types/generalTypesApi.js +10 -1
- package/dist/general-types/generalTypesInterfaces.d.ts +17 -6
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/integration-collections/integrationCollectionsApi.d.ts +134 -81
- package/dist/integration-collections/integrationCollectionsApi.js +135 -83
- package/dist/integration-collections/integrationCollectionsInterfaces.d.ts +246 -43
- package/dist/locales/localesApi.d.ts +5 -1
- package/dist/locales/localesApi.js +5 -1
- package/dist/locales/localesInterfaces.d.ts +19 -12
- package/dist/menus/menusApi.d.ts +6 -3
- package/dist/menus/menusApi.js +6 -3
- package/dist/menus/menusInterfaces.d.ts +60 -21
- package/dist/orders/ordersApi.d.ts +84 -66
- package/dist/orders/ordersApi.js +91 -71
- package/dist/orders/ordersInterfaces.d.ts +282 -86
- package/dist/pages/pagesApi.d.ts +101 -42
- package/dist/pages/pagesApi.js +93 -39
- package/dist/pages/pagesInterfaces.d.ts +218 -75
- package/dist/payments/paymentsApi.d.ts +53 -39
- package/dist/payments/paymentsApi.js +55 -40
- package/dist/payments/paymentsInterfaces.d.ts +121 -56
- package/dist/product-statuses/productStatusesApi.d.ts +17 -17
- package/dist/product-statuses/productStatusesApi.js +16 -19
- package/dist/product-statuses/productStatusesInterfaces.d.ts +43 -14
- package/dist/products/productsApi.d.ts +309 -177
- package/dist/products/productsApi.js +313 -167
- package/dist/products/productsInterfaces.d.ts +447 -81
- package/dist/system/systemApi.d.ts +42 -12
- package/dist/system/systemApi.js +43 -10
- package/dist/system/systemInterfaces.d.ts +7 -0
- package/dist/templates/templatesApi.d.ts +24 -20
- package/dist/templates/templatesApi.js +22 -21
- package/dist/templates/templatesInterfaces.d.ts +48 -22
- package/dist/templates-preview/templatesPreviewApi.d.ts +17 -17
- package/dist/templates-preview/templatesPreviewApi.js +16 -19
- package/dist/templates-preview/templatesPreviewInterfaces.d.ts +85 -32
- package/dist/users/usersApi.d.ts +59 -35
- package/dist/users/usersApi.js +62 -35
- package/dist/users/usersInterfaces.d.ts +136 -14
- package/dist/web-socket/wsApi.d.ts +4 -5
- package/dist/web-socket/wsApi.js +4 -5
- package/dist/web-socket/wsInterfaces.d.ts +2 -1
- package/package.json +20 -18
|
@@ -5,7 +5,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const syncModules_1 = __importDefault(require("./syncModules"));
|
|
7
7
|
// import { IError } from './utils';
|
|
8
|
+
/**
|
|
9
|
+
* Abstract class AsyncModules extends SyncModules to provide asynchronous HTTP request functionalities.
|
|
10
|
+
*/
|
|
8
11
|
class AsyncModules extends syncModules_1.default {
|
|
12
|
+
/**
|
|
13
|
+
* Constructor initializes the AsyncModules with a given state.
|
|
14
|
+
* @param state - Instance of StateModule containing configuration and state data.
|
|
15
|
+
*/
|
|
9
16
|
constructor(state) {
|
|
10
17
|
var _a;
|
|
11
18
|
super(state);
|
|
@@ -15,162 +22,252 @@ class AsyncModules extends syncModules_1.default {
|
|
|
15
22
|
this._https = (_a = state._https) !== null && _a !== void 0 ? _a : null;
|
|
16
23
|
}
|
|
17
24
|
/**
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
* @
|
|
21
|
-
* @
|
|
25
|
+
* Performs an HTTP GET request.
|
|
26
|
+
* @param path - The path to append to the base URL.
|
|
27
|
+
* @return A promise resolving to the response data.
|
|
28
|
+
* @description Define a protected asynchronous method '_fetchGet' that performs a GET request
|
|
22
29
|
*/
|
|
23
30
|
async _fetchGet(path) {
|
|
31
|
+
// Create options for the GET request using the 'makeOptions' method
|
|
24
32
|
const options = this.makeOptions('GET');
|
|
33
|
+
// Check if the fetch operation should be performed using the Fetch API or an alternative method
|
|
25
34
|
if (!this._NO_FETCH) {
|
|
35
|
+
// If Fetch API is allowed, use 'browserResponse' to perform the request and return the result
|
|
26
36
|
return await this.browserResponse(path, options);
|
|
27
37
|
}
|
|
28
38
|
else {
|
|
39
|
+
// If Fetch API is not allowed, perform the request using Node.js HTTPS module
|
|
40
|
+
// Return a new promise to handle the asynchronous nature of the HTTPS request
|
|
29
41
|
return new Promise((resolve, reject) => {
|
|
30
|
-
|
|
31
|
-
|
|
42
|
+
// Use the HTTPS module to send a GET request to the specified path with the given options
|
|
43
|
+
const req = this._https.get(this._getFullPath(path), // Get the full URL path for the request
|
|
44
|
+
options, // Pass the options for the request
|
|
45
|
+
(res) => {
|
|
46
|
+
// Handle the response from the server
|
|
47
|
+
let responseData = ''; // Initialize a variable to accumulate response data
|
|
48
|
+
// Listen for 'data' events to receive chunks of data from the response
|
|
32
49
|
res.on('data', (chunk) => {
|
|
33
|
-
|
|
50
|
+
responseData += chunk; // Append each chunk to the accumulated response data
|
|
34
51
|
});
|
|
52
|
+
// Listen for the 'end' event to know when the response has been fully received
|
|
35
53
|
res.on('end', () => {
|
|
36
|
-
|
|
54
|
+
try {
|
|
55
|
+
// Attempt to parse the accumulated response data as JSON and resolve the promise with it
|
|
56
|
+
resolve(JSON.parse(responseData));
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
// If parsing fails, resolve the promise with the raw response data
|
|
60
|
+
resolve(responseData);
|
|
61
|
+
}
|
|
37
62
|
});
|
|
38
63
|
});
|
|
64
|
+
// Listen for 'error' events on the request to handle any errors that occur
|
|
39
65
|
req.on('error', (error) => {
|
|
66
|
+
// Reject the promise with the error
|
|
40
67
|
reject(error);
|
|
41
68
|
});
|
|
42
69
|
});
|
|
43
70
|
}
|
|
44
71
|
}
|
|
45
72
|
/**
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
* @param
|
|
49
|
-
* @
|
|
50
|
-
* @
|
|
73
|
+
* Performs an HTTP POST request.
|
|
74
|
+
* @param path - The path to append to the base URL.
|
|
75
|
+
* @param data - The data to send in the request body.
|
|
76
|
+
* @return A promise resolving to the response data.
|
|
77
|
+
* @description Define a protected asynchronous method '_fetchPost' that performs a POST request
|
|
51
78
|
*/
|
|
52
79
|
async _fetchPost(path, data) {
|
|
80
|
+
// Create options for the POST request using the 'makeOptions' method, including the data to be sent
|
|
53
81
|
const options = this.makeOptions('POST', data);
|
|
82
|
+
// Check if the fetch operation should be performed using the Fetch API or an alternative method
|
|
54
83
|
if (!this._NO_FETCH) {
|
|
84
|
+
// If Fetch API is allowed, use 'browserResponse' to perform the request and return the result
|
|
55
85
|
return await this.browserResponse(path, options);
|
|
56
86
|
}
|
|
57
87
|
else {
|
|
88
|
+
// If Fetch API is not allowed, perform the request using Node.js HTTPS module
|
|
89
|
+
// Return a new promise to handle the asynchronous nature of the HTTPS request
|
|
58
90
|
return new Promise((resolve, reject) => {
|
|
59
|
-
|
|
60
|
-
|
|
91
|
+
// Use the HTTPS module to send a POST request to the specified path with the given options
|
|
92
|
+
const req = this._https.request(this._getFullPath(path), // Get the full URL path for the request
|
|
93
|
+
options, // Pass the options for the request
|
|
94
|
+
(res) => {
|
|
95
|
+
// Handle the response from the server
|
|
96
|
+
let responseData = ''; // Initialize a variable to accumulate response data
|
|
97
|
+
// Listen for 'data' events to receive chunks of data from the response
|
|
61
98
|
res.on('data', (chunk) => {
|
|
62
|
-
responseData += chunk;
|
|
99
|
+
responseData += chunk; // Append each chunk to the accumulated response data
|
|
63
100
|
});
|
|
101
|
+
// Listen for the 'end' event to know when the response has been fully received
|
|
64
102
|
res.on('end', () => {
|
|
65
103
|
try {
|
|
104
|
+
// Attempt to parse the accumulated response data as JSON and resolve the promise with it
|
|
66
105
|
resolve(JSON.parse(responseData));
|
|
67
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
68
106
|
}
|
|
69
107
|
catch (error) {
|
|
108
|
+
// If parsing fails, resolve the promise with the raw response data
|
|
70
109
|
resolve(responseData);
|
|
71
110
|
}
|
|
72
111
|
});
|
|
73
112
|
});
|
|
113
|
+
// Listen for 'error' events on the request to handle any errors that occur
|
|
74
114
|
req.on('error', (error) => {
|
|
115
|
+
// Reject the promise with the error
|
|
75
116
|
reject(error);
|
|
76
117
|
});
|
|
118
|
+
// Write the provided data to the request body in JSON format
|
|
77
119
|
req.write(JSON.stringify(data));
|
|
120
|
+
// End the request to signal that all data has been sent
|
|
78
121
|
req.end();
|
|
79
122
|
});
|
|
80
123
|
}
|
|
81
124
|
}
|
|
82
125
|
/**
|
|
83
|
-
*
|
|
84
|
-
* @param path
|
|
85
|
-
* @param data
|
|
86
|
-
* @
|
|
126
|
+
* Performs an HTTP PUT request.
|
|
127
|
+
* @param path - The path to append to the base URL.
|
|
128
|
+
* @param data - The data to send in the request body.
|
|
129
|
+
* @return A promise resolving to the response data.
|
|
130
|
+
* @description Define a protected asynchronous method '_fetchPut' that performs a PUT request
|
|
87
131
|
*/
|
|
88
132
|
async _fetchPut(path, data) {
|
|
133
|
+
// Create options for the PUT request using the 'makeOptions' method, including the data to be sent
|
|
89
134
|
const options = this.makeOptions('PUT', data);
|
|
135
|
+
// Check if the fetch operation should be performed using the Fetch API or an alternative method
|
|
90
136
|
if (!this._NO_FETCH) {
|
|
137
|
+
// If Fetch API is allowed, use 'browserResponse' to perform the request and return the result
|
|
91
138
|
return await this.browserResponse(path, options);
|
|
92
139
|
}
|
|
93
140
|
else {
|
|
141
|
+
// If Fetch API is not allowed, perform the request using Node.js HTTPS module
|
|
142
|
+
// Return a new promise to handle the asynchronous nature of the HTTPS request
|
|
94
143
|
return new Promise((resolve, reject) => {
|
|
95
|
-
|
|
96
|
-
|
|
144
|
+
// Use the HTTPS module to send a PUT request to the specified path with the given options
|
|
145
|
+
const req = this._https.request(this._getFullPath(path), // Get the full URL path for the request
|
|
146
|
+
options, // Pass the options for the request
|
|
147
|
+
(res) => {
|
|
148
|
+
// Handle the response from the server
|
|
149
|
+
let responseData = ''; // Initialize a variable to accumulate response data
|
|
150
|
+
// Listen for 'data' events to receive chunks of data from the response
|
|
97
151
|
res.on('data', (chunk) => {
|
|
98
|
-
responseData += chunk;
|
|
152
|
+
responseData += chunk; // Append each chunk to the accumulated response data
|
|
99
153
|
});
|
|
154
|
+
// Listen for the 'end' event to know when the response has been fully received
|
|
100
155
|
res.on('end', () => {
|
|
101
|
-
|
|
156
|
+
try {
|
|
157
|
+
// Attempt to parse the accumulated response data as JSON and resolve the promise with it
|
|
158
|
+
resolve(JSON.parse(responseData));
|
|
159
|
+
}
|
|
160
|
+
catch (error) {
|
|
161
|
+
// If parsing fails, resolve the promise with the raw response data
|
|
162
|
+
resolve(responseData);
|
|
163
|
+
}
|
|
102
164
|
});
|
|
103
165
|
});
|
|
166
|
+
// Listen for 'error' events on the request to handle any errors that occur
|
|
104
167
|
req.on('error', (error) => {
|
|
168
|
+
// Reject the promise with the error
|
|
105
169
|
reject(error);
|
|
106
170
|
});
|
|
171
|
+
// Write the provided data to the request body in JSON format
|
|
107
172
|
req.write(JSON.stringify(data));
|
|
173
|
+
// End the request to signal that all data has been sent
|
|
108
174
|
req.end();
|
|
109
175
|
});
|
|
110
176
|
}
|
|
111
177
|
}
|
|
112
178
|
/**
|
|
113
|
-
*
|
|
114
|
-
* @param path
|
|
115
|
-
* @param data
|
|
116
|
-
* @
|
|
179
|
+
* Performs an HTTP DELETE request.
|
|
180
|
+
* @param path - The path to append to the base URL.
|
|
181
|
+
* @param data - Optional data to send in the request body.
|
|
182
|
+
* @return A promise resolving to the response data.
|
|
183
|
+
* @description Define a protected asynchronous method '_fetchDelete' that performs a DELETE request
|
|
117
184
|
*/
|
|
118
185
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
119
186
|
async _fetchDelete(path, data) {
|
|
187
|
+
// Create options for the DELETE request using the 'makeOptions' method
|
|
120
188
|
const options = this.makeOptions('DELETE');
|
|
189
|
+
// Check if the fetch operation should be performed using the Fetch API or an alternative method
|
|
121
190
|
if (!this._NO_FETCH) {
|
|
191
|
+
// If Fetch API is allowed, use 'browserResponse' to perform the request and return the result
|
|
122
192
|
return await this.browserResponse(path, options);
|
|
123
193
|
}
|
|
124
194
|
else {
|
|
195
|
+
// If Fetch API is not allowed, perform the request using Node.js HTTPS module
|
|
196
|
+
// Return a new promise to handle the asynchronous nature of the HTTPS request
|
|
125
197
|
return new Promise((resolve, reject) => {
|
|
126
|
-
|
|
127
|
-
|
|
198
|
+
// Use the HTTPS module to send a DELETE request to the specified path with the given options
|
|
199
|
+
const req = this._https.delete(this._getFullPath(path), // Get the full URL path for the request
|
|
200
|
+
options, // Pass the options for the request
|
|
201
|
+
(res) => {
|
|
202
|
+
// Handle the response from the server
|
|
203
|
+
let responseData = ''; // Initialize a variable to accumulate response data
|
|
204
|
+
// Listen for 'data' events to receive chunks of data from the response
|
|
128
205
|
res.on('data', (chunk) => {
|
|
129
|
-
|
|
206
|
+
responseData += chunk; // Append each chunk to the accumulated response data
|
|
130
207
|
});
|
|
208
|
+
// Listen for the 'end' event to know when the response has been fully received
|
|
131
209
|
res.on('end', () => {
|
|
132
|
-
|
|
210
|
+
try {
|
|
211
|
+
// Attempt to parse the accumulated response data as JSON and resolve the promise with it
|
|
212
|
+
resolve(JSON.parse(responseData));
|
|
213
|
+
}
|
|
214
|
+
catch (error) {
|
|
215
|
+
// If parsing fails, resolve the promise with the raw response data
|
|
216
|
+
resolve(responseData);
|
|
217
|
+
}
|
|
133
218
|
});
|
|
134
219
|
});
|
|
220
|
+
// Listen for 'error' events on the request to handle any errors that occur
|
|
135
221
|
req.on('error', (error) => {
|
|
222
|
+
// Reject the promise with the error
|
|
136
223
|
reject(error);
|
|
137
224
|
});
|
|
138
225
|
});
|
|
139
226
|
}
|
|
140
227
|
}
|
|
141
228
|
/**
|
|
142
|
-
*
|
|
143
|
-
*
|
|
144
|
-
* @
|
|
229
|
+
* Refreshes the authentication token.
|
|
230
|
+
* @return A promise resolving to a boolean indicating success or failure.
|
|
231
|
+
* @description Define an asynchronous method 'refreshToken' that attempts to refresh the authentication token
|
|
145
232
|
*/
|
|
146
233
|
async refreshToken() {
|
|
234
|
+
// Perform a fetch request to the specified URL to refresh the token
|
|
147
235
|
const response = await fetch(this.state.url +
|
|
148
236
|
`/api/content/users-auth-providers/marker/email/users/refresh`, {
|
|
237
|
+
// Use the POST method for the request
|
|
149
238
|
method: 'POST',
|
|
239
|
+
// Set the necessary headers for the request
|
|
150
240
|
headers: {
|
|
151
|
-
'Content-Type': 'application/json',
|
|
152
|
-
'x-app-token': this.state.token,
|
|
241
|
+
'Content-Type': 'application/json', // Indicate that the request body is in JSON format
|
|
242
|
+
'x-app-token': this.state.token, // Include the application token from the state
|
|
153
243
|
},
|
|
244
|
+
// Send the current refresh token in the request body as a JSON string
|
|
154
245
|
body: JSON.stringify({ refreshToken: this.state.refreshToken }),
|
|
155
246
|
});
|
|
247
|
+
// Check if the response status is OK (status code 200-299)
|
|
156
248
|
if (response.ok) {
|
|
249
|
+
// Parse the response body as JSON and store the result
|
|
157
250
|
const result = await response.json();
|
|
251
|
+
// Update the state's refreshToken and accessToken with the new tokens from the response
|
|
158
252
|
this.state.refreshToken = result.refreshToken;
|
|
159
253
|
this.state.accessToken = result.accessToken;
|
|
254
|
+
// If a save function is defined in the state, call it with the new refresh token
|
|
160
255
|
if (this.state.saveFunction) {
|
|
161
256
|
this.state.saveFunction(result.refreshToken);
|
|
162
257
|
}
|
|
258
|
+
// Return true to indicate that the token refresh was successful
|
|
163
259
|
return true;
|
|
164
260
|
}
|
|
165
261
|
else {
|
|
262
|
+
// Return false to indicate that the token refresh failed
|
|
166
263
|
return false;
|
|
167
264
|
}
|
|
168
265
|
}
|
|
169
266
|
/**
|
|
170
|
-
*
|
|
171
|
-
* @param method
|
|
172
|
-
* @param data
|
|
173
|
-
* @
|
|
267
|
+
* Creates options for HTTP requests.
|
|
268
|
+
* @param method - The HTTP method (GET, POST, PUT, DELETE, etc.).
|
|
269
|
+
* @param data - Optional data to include in the request body.
|
|
270
|
+
* @return An object representing the request options.
|
|
174
271
|
*/
|
|
175
272
|
makeOptions(method, data) {
|
|
176
273
|
const options = {
|
|
@@ -180,51 +277,82 @@ class AsyncModules extends syncModules_1.default {
|
|
|
180
277
|
'x-app-token': this.state.token,
|
|
181
278
|
},
|
|
182
279
|
};
|
|
280
|
+
// if method = 'POST' add x-device-metadata
|
|
281
|
+
if (method === 'POST') {
|
|
282
|
+
options.headers = {
|
|
283
|
+
...options.headers,
|
|
284
|
+
'x-device-metadata': this._getDeviceMetadata(),
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
// Check if 'data' is an instance of FormData or Blob
|
|
183
288
|
if (data instanceof FormData || data instanceof Blob) {
|
|
289
|
+
// Remove the 'Content-Type' header from options.headers
|
|
184
290
|
delete options.headers['Content-Type'];
|
|
291
|
+
// Set the 'accept' header to 'application/json', while preserving existing headers
|
|
185
292
|
options.headers = {
|
|
186
293
|
...options.headers,
|
|
187
294
|
accept: 'application/json',
|
|
188
295
|
};
|
|
296
|
+
// Assign 'data' to options.body for sending as request payload
|
|
189
297
|
options.body = data;
|
|
190
298
|
}
|
|
191
299
|
else if (data) {
|
|
300
|
+
// If 'data' exists and is not FormData or Blob, convert it to a JSON string
|
|
301
|
+
// and assign it to options.body
|
|
192
302
|
options.body = JSON.stringify(data);
|
|
193
303
|
}
|
|
304
|
+
// Check if there is an access token available in the state
|
|
194
305
|
if (this.state.accessToken) {
|
|
306
|
+
// Add an 'Authorization' header with the Bearer token to options.headers
|
|
195
307
|
options.headers['Authorization'] = 'Bearer ' + this.state.accessToken;
|
|
196
308
|
}
|
|
197
309
|
return options;
|
|
198
310
|
}
|
|
199
311
|
/**
|
|
200
|
-
*
|
|
201
|
-
* @param path
|
|
202
|
-
* @param options
|
|
203
|
-
* @
|
|
312
|
+
* Handles responses from the browser's fetch API.
|
|
313
|
+
* @param path - The path to append to the base URL.
|
|
314
|
+
* @param options - The options for the fetch request.
|
|
315
|
+
* @return A promise resolving to the response data.
|
|
316
|
+
* @description Define an asynchronous method 'browserResponse' that takes a path and options as parameters
|
|
204
317
|
*/
|
|
205
318
|
async browserResponse(path, options) {
|
|
206
319
|
try {
|
|
320
|
+
// Perform a fetch request using the full URL obtained from '_getFullPath' and the provided options
|
|
207
321
|
const response = await fetch(this._getFullPath(path), options);
|
|
322
|
+
// Check if the response status is OK (status code 200-299)
|
|
208
323
|
if (response.ok) {
|
|
209
324
|
try {
|
|
210
|
-
|
|
325
|
+
// Attempt to read the response body as text
|
|
326
|
+
const text = await response.text();
|
|
327
|
+
// If the response body is not empty, parse it as JSON; otherwise, return an empty object
|
|
328
|
+
return text ? JSON.parse(text) : {};
|
|
329
|
+
// Alternatively, you could directly return the parsed JSON with 'await response.json()'
|
|
211
330
|
}
|
|
212
331
|
catch (e) {
|
|
332
|
+
// If parsing the response fails, return the error
|
|
213
333
|
return e;
|
|
214
334
|
}
|
|
215
335
|
}
|
|
216
336
|
else {
|
|
337
|
+
// Handle non-OK responses
|
|
338
|
+
// Check if the status is 401 (Unauthorized) and custom authentication is not used
|
|
217
339
|
if (response.status == 401 && !this.state.customAuth) {
|
|
340
|
+
// Attempt to refresh the access token
|
|
218
341
|
const refresh = await this.refreshToken();
|
|
219
342
|
if (refresh) {
|
|
343
|
+
// Update the Authorization header with the new access token
|
|
220
344
|
options.headers['Authorization'] =
|
|
221
345
|
'Bearer ' + this.state.accessToken;
|
|
346
|
+
// Retry the fetch request with updated headers
|
|
222
347
|
const secondResponse = await fetch(this._getFullPath(path), options);
|
|
348
|
+
// Return the JSON-parsed response of the retry request
|
|
223
349
|
return await secondResponse.json();
|
|
224
350
|
}
|
|
225
351
|
}
|
|
352
|
+
// Store the response status and parse the response body as JSON
|
|
226
353
|
const status = response.status;
|
|
227
354
|
const res = await response.json();
|
|
355
|
+
// Handle different HTTP status codes with corresponding error functions
|
|
228
356
|
if (status == 400) {
|
|
229
357
|
if (this.state.errorsFunctions && this.state.errorsFunctions['400']) {
|
|
230
358
|
this.state.errorsFunctions['400'](res);
|
|
@@ -250,6 +378,7 @@ class AsyncModules extends syncModules_1.default {
|
|
|
250
378
|
this.state.errorsFunctions['500'](res);
|
|
251
379
|
}
|
|
252
380
|
}
|
|
381
|
+
// Determine whether to return or throw the response based on 'isShell' state
|
|
253
382
|
if (this.state.isShell) {
|
|
254
383
|
return res;
|
|
255
384
|
}
|
|
@@ -259,6 +388,8 @@ class AsyncModules extends syncModules_1.default {
|
|
|
259
388
|
}
|
|
260
389
|
}
|
|
261
390
|
catch (e) {
|
|
391
|
+
// Handle any errors that occur during the fetch process
|
|
392
|
+
// Determine whether to return or throw the error based on 'isShell' state
|
|
262
393
|
if (this.state.isShell) {
|
|
263
394
|
return e;
|
|
264
395
|
}
|
package/dist/base/result.js
CHANGED
|
@@ -3,48 +3,63 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
/* eslint-disable no-restricted-syntax */
|
|
4
4
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
5
5
|
class Result {
|
|
6
|
+
// Constructor that initializes the class with a given data, which can be of type Response or string.
|
|
6
7
|
constructor(data) {
|
|
7
8
|
this.body = data;
|
|
8
9
|
}
|
|
10
|
+
// Asynchronously converts the body to a blob and returns the current instance.
|
|
9
11
|
async blob() {
|
|
12
|
+
// Convert the body to a blob using the blob() method.
|
|
10
13
|
this.body = await this.body.blob();
|
|
11
14
|
return this;
|
|
12
15
|
}
|
|
16
|
+
// Asynchronously parses the body as JSON and returns the current instance.
|
|
13
17
|
async json() {
|
|
18
|
+
// Check if the body is a string, then parse it as JSON, otherwise call the json() method.
|
|
14
19
|
this.body =
|
|
15
20
|
(await typeof this.body) === 'string'
|
|
16
21
|
? JSON.parse(this.body)
|
|
17
22
|
: this.body.json();
|
|
18
23
|
return this;
|
|
19
24
|
}
|
|
25
|
+
// Recursively removes language-specific data from the provided data or the body.
|
|
20
26
|
makeDataWithoutLang(langCode, data) {
|
|
21
27
|
if (data) {
|
|
28
|
+
// If data is an array, recursively process each item.
|
|
22
29
|
if (Array.isArray(data)) {
|
|
23
30
|
return data.map((item) => this.makeDataWithoutLang(langCode, item));
|
|
24
31
|
}
|
|
25
32
|
else {
|
|
33
|
+
// Iterate through each key in the data object.
|
|
26
34
|
for (const key in data) {
|
|
35
|
+
// If the value is an object containing the specified language code, replace it with the language-specific value.
|
|
27
36
|
if (typeof data[key] === 'object' &&
|
|
28
37
|
data[key] &&
|
|
29
38
|
langCode in data[key]) {
|
|
30
39
|
data[key] = data[key][langCode];
|
|
31
40
|
}
|
|
32
41
|
else if (typeof data[key] === 'object' && data[key] != null) {
|
|
42
|
+
// If the value is an object, recursively process it
|
|
33
43
|
data[key] = this.makeDataWithoutLang(langCode, data[key]);
|
|
34
44
|
}
|
|
35
45
|
else if (Array.isArray(data[key])) {
|
|
46
|
+
// If the value is an array, recursively process each item.
|
|
36
47
|
data[key] = data.map((item) => this.makeDataWithoutLang(langCode, item));
|
|
37
48
|
}
|
|
38
49
|
}
|
|
50
|
+
// Return the processed data.
|
|
39
51
|
return data;
|
|
40
52
|
}
|
|
41
53
|
}
|
|
42
54
|
else {
|
|
55
|
+
// Process the body if no specific data is provided.
|
|
43
56
|
if (Array.isArray(this.body)) {
|
|
44
57
|
this.body = this.body.map((item) => this.makeDataWithoutLang(langCode, item));
|
|
45
58
|
}
|
|
46
59
|
else {
|
|
60
|
+
// Iterate through each key in the body object.
|
|
47
61
|
for (const key in this.body) {
|
|
62
|
+
// Similar processing as above but applied to the body.
|
|
48
63
|
if (typeof this.body[key] === 'object' &&
|
|
49
64
|
this.body[key] &&
|
|
50
65
|
langCode in this.body[key]) {
|
|
@@ -62,19 +77,24 @@ class Result {
|
|
|
62
77
|
return this;
|
|
63
78
|
}
|
|
64
79
|
}
|
|
80
|
+
// Recursively simplifies arrays in the provided data or the body by removing single-element arrays.
|
|
65
81
|
makeDataWithoutArray(data) {
|
|
66
82
|
if (data) {
|
|
83
|
+
// If data is an array, recursively process each item.
|
|
67
84
|
if (Array.isArray(data)) {
|
|
68
85
|
return data.map((item) => this.makeDataWithoutArray(item));
|
|
69
86
|
}
|
|
70
87
|
else {
|
|
88
|
+
// Iterate through each key in the data object.
|
|
71
89
|
for (const key in data) {
|
|
90
|
+
// If the value is a single-element array, replace it with the element itself.
|
|
72
91
|
if (Array.isArray(data[key]) && data[key].length === 1) {
|
|
73
92
|
data[key] = data[key][0];
|
|
74
93
|
}
|
|
75
94
|
else if (typeof data[key] === 'object' &&
|
|
76
95
|
data[key] &&
|
|
77
96
|
!Array.isArray(data[key])) {
|
|
97
|
+
// If the value is an object, recursively process it.
|
|
78
98
|
data[key] = this.makeDataWithoutArray(data[key]);
|
|
79
99
|
}
|
|
80
100
|
}
|
|
@@ -82,11 +102,14 @@ class Result {
|
|
|
82
102
|
}
|
|
83
103
|
}
|
|
84
104
|
else {
|
|
105
|
+
// Process the body if no specific data is provided.
|
|
85
106
|
if (Array.isArray(this.body)) {
|
|
86
107
|
return this.body.map((item) => this.makeDataWithoutArray(item));
|
|
87
108
|
}
|
|
88
109
|
else {
|
|
110
|
+
// Iterate through each key in the body object.
|
|
89
111
|
for (const key in this.body) {
|
|
112
|
+
// Similar processing as above but applied to the body.
|
|
90
113
|
if (Array.isArray(this.body[key]) && this.body[key].length === 1) {
|
|
91
114
|
this.body[key] = this.body[key][0];
|
|
92
115
|
}
|