complexqa_frontend_core 2.2.3 → 2.3.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "complexqa_frontend_core",
3
- "version": "2.2.3",
3
+ "version": "2.3.1",
4
4
  "description": "core of web ",
5
5
  "type": "module",
6
6
  "exports": {
@@ -1,340 +1,340 @@
1
- import axios from 'axios';
2
- import qs from 'query-string';
3
- import { ApiException } from "../exceptions/ApiException.js";
4
- import { clone_object, echo, is_array, is_object } from "../utils/utils.js";
5
- import { UserService } from "../services/UserService.js";
6
-
7
- /**
8
- *
9
- * @version v.1.0 (03/06/2026)
10
- */
11
- export class ApiAbstractClass
12
- {
13
- init_options;
14
- element_class_instance;
15
- dev_stage_config;
16
- parent_app;
17
- host_api;
18
- stage = 'production';
19
- /** @type {string|false} */
20
- module_prefix = false;
21
- api_prefix = '';
22
- method;
23
- headers;
24
- search_cache_enabled = false;
25
-
26
-
27
- /**
28
- *
29
- * @version v.1.0 (03/06/2026)
30
- * @param options
31
- */
32
- constructor(options)
33
- {
34
- if (options?.init_options)
35
- {
36
- this.init_options = options?.init_options;
37
- }
38
- if (options?.stage)
39
- {
40
- this.stage = options?.stage;
41
- }
42
- if (options?.dev_stage_config)
43
- {
44
- this.dev_stage_config = options?.dev_stage_config;
45
- }
46
- if (options?.parent_app)
47
- {
48
- this.parent_app = options?.parent_app;
49
- }
50
- if (options?.host_api)
51
- {
52
- this.host_api = options?.host_api;
53
- }
54
- }
55
-
56
-
57
- set_element_class_instance(element_class_instance)
58
- {
59
- this.element_class_instance = element_class_instance;
60
- }
61
-
62
-
63
-
64
- /**
65
- *
66
- * @version v.0.1 (26/05/2024)
67
- * @param {object|import('../types/family_service/typeFOR.js').typeFOR} payload
68
- */
69
- async search(payload)
70
- {
71
- if (!this.module_prefix)
72
- {
73
- throw new Error('Error #10-1152 in ApiAbstractClass');
74
- }
75
-
76
- if (this.stage === 'development')
77
- {
78
- let response = await this.mock_search(payload);
79
- return {
80
- response: response,
81
- payload : payload
82
- };
83
- }
84
-
85
- let url = `/${ this.api_prefix }/${ this.module_prefix }/search`;
86
-
87
- payload = this.handle_request_payload(payload);
88
- let response = this.api_request(url, payload);
89
- response = this.handle_response(response);
90
-
91
- return {
92
- response: response,
93
- payload : payload
94
- };
95
- }
96
-
97
-
98
- /**
99
- *
100
- * @version v.0.1 (27/06/2024)
101
- * @param payload
102
- * @returns {Promise<array>}
103
- */
104
- async mock_search(payload)
105
- {
106
- // @todo - дописать
107
- if (this.dev_stage_config.api_response_search_element_count > 0 && this.dev_stage_config.api_response_search_element_count < 999)
108
- {
109
-
110
- }
111
- else
112
- {
113
- throw new Error('Error #100-154');
114
- }
115
-
116
- let model = new this.element_class_instance();
117
- let response = model.get_random_demo_data(this.dev_stage_config.api_response_search_element_count)
118
-
119
- return response;
120
-
121
- }
122
-
123
-
124
-
125
-
126
- /******************************************************************************************/
127
- /******************************************************************************************/
128
-
129
- /******************************************************************************************/
130
-
131
- /**
132
- *
133
- * @param payload
134
- * @returns {*}
135
- */
136
- handle_request_payload(payload)
137
- {
138
- return this.object_replace_null_recursively(payload);
139
- }
140
-
141
-
142
- /**
143
- *
144
- * @param object
145
- * @returns {*}
146
- */
147
- object_replace_null_recursively(object)
148
- {
149
- let result = clone_object(object);
150
- for (let key in result)
151
- {
152
- if (result[ key ] === null)
153
- {
154
- result[ key ] = 'NULL';
155
- }
156
- else if (( result[ key ]?.constructor === Object ) || ( result[ key ]?.constructor === Array ))
157
- {
158
- result[ key ] = this.object_replace_null_recursively(result[ key ]);
159
- }
160
- }
161
-
162
- return result;
163
- }
164
-
165
-
166
- /**
167
- *
168
- * @param url
169
- * @param payload
170
- * @param method
171
- * @returns {Promise<Promise<axios.AxiosResponse<any>> | *>}
172
- * @version v.0.2 (26/01/2025)
173
- * @todo - валидация входящих параметров
174
- */
175
- async api_request(url, payload, method = 'POST')
176
- {
177
- let request_params;
178
- if (!payload)
179
- {
180
- request_params = {};
181
- }
182
- else
183
- {
184
- request_params = clone_object(payload)
185
- }
186
-
187
- request_params = this.mixin_service_params(request_params);
188
-
189
- if (this.host_api)
190
- {
191
- const page_hostname = (typeof window !== 'undefined' && window.location)
192
- ? window.location.hostname
193
- : '';
194
- const api_hostname = String(this.host_api).split(':')[0];
195
- if (!page_hostname || page_hostname !== api_hostname)
196
- {
197
- url = 'https://' + this.host_api + url;
198
- }
199
- }
200
-
201
- const headers = { ...(this.headers || {}) };
202
- const csrf_token = UserService.get_csrf_token();
203
- if (csrf_token)
204
- {
205
- headers['X-CSRF-TOKEN'] = csrf_token;
206
- }
207
-
208
- let request_options = {
209
- method : method,
210
- headers: headers,
211
- url : url
212
- };
213
-
214
- if (method === 'get' || method === 'GET')
215
- {
216
- request_options.paramsSerializer = this.object_to_string;
217
- request_options.params = request_params;
218
- }
219
- else
220
- {
221
- request_options.data = (request_params);
222
- }
223
-
224
- let config = {};
225
-
226
- return axios(request_options, config);
227
- }
228
-
229
-
230
- /**
231
- *
232
- * @param object
233
- * @returns {string}
234
- * @todo - валидация входящих параметров
235
- */
236
- object_to_string(object)
237
- {
238
- let result = qs.stringify(object, { array_format: 'indices' });
239
-
240
- return result;
241
- }
242
-
243
-
244
- mixin_service_params(params)
245
- {
246
- params.client = 'webapp';
247
- return params;
248
- }
249
-
250
-
251
- /**
252
- *
253
- * @param {object} response
254
- * @param {object} init_class_instance
255
- * @returns {Promise<any>}
256
- * @version v.0.1 (04/03/2026)
257
- */
258
- handle_response(response, init_class_instance = true)
259
- {
260
- response = response.then((api_response) =>
261
- {
262
- let error_payload = {
263
- api_response: api_response?.data?.api_response,
264
- api_code : api_response?.data?.api_code,
265
- api_result : api_response?.data?.api_result,
266
- api_error : api_response?.data?.api_error,
267
- hint : api_response?.data?.hint
268
- };
269
-
270
- if (api_response?.data?.api_error?.adapted_message)
271
- {
272
- error_payload.message = api_response.data.api_error.adapted_message
273
- throw new ApiException(error_payload);
274
- }
275
- else if (api_response?.data?.api_code)
276
- {
277
- let api_code = api_response.data.api_code;
278
-
279
- if (( api_code >= 200 ) && ( api_code <= 299 ))
280
- {
281
- if (is_array(api_response?.data?.api_result) && this.element_class_instance)
282
- {
283
- let result = api_response?.data.api_result.map((row) =>
284
- {
285
- if (init_class_instance)
286
- {
287
- return new this.element_class_instance(row);
288
- }
289
- else
290
- {
291
- return row;
292
- }
293
- });
294
-
295
- return result;
296
- }
297
- else if (is_object(api_response?.data?.api_result))
298
- {
299
- return api_response?.data?.api_result;
300
- }
301
- else if (api_response?.data?.api_result === false)
302
- {
303
- return api_response?.data?.api_result;
304
- }
305
- else
306
- {
307
- return api_response?.data;
308
- }
309
- }
310
- else if (api_response?.data?.api_result === false)
311
- {
312
- throw new ApiException(error_payload);
313
- }
314
- else
315
- {
316
- throw new ApiException(error_payload);
317
- }
318
- }
319
- else
320
- {
321
- return api_response?.data;
322
- }
323
- })
324
- .catch((error) =>
325
- {
326
-
327
- echo({ app: this });
328
- console.error(error);
329
- if (error instanceof ApiException)
330
- {
331
- throw error;
332
- }
333
-
334
-
335
- throw new ApiException({ error: error });
336
- });
337
-
338
- return response;
339
- }
1
+ import axios from 'axios';
2
+ import qs from 'query-string';
3
+ import { ApiException } from "../exceptions/ApiException.js";
4
+ import { clone_object, echo, is_array, is_object } from "../utils/utils.js";
5
+ import { UserService } from "../services/UserService.js";
6
+
7
+ /**
8
+ *
9
+ * @version v.1.0 (03/06/2026)
10
+ */
11
+ export class ApiAbstractClass
12
+ {
13
+ init_options;
14
+ element_class_instance;
15
+ dev_stage_config;
16
+ parent_app;
17
+ host_api;
18
+ stage = 'production';
19
+ /** @type {string|false} */
20
+ module_prefix = false;
21
+ api_prefix = '';
22
+ method;
23
+ headers;
24
+ search_cache_enabled = false;
25
+
26
+
27
+ /**
28
+ *
29
+ * @version v.1.0 (03/06/2026)
30
+ * @param options
31
+ */
32
+ constructor(options)
33
+ {
34
+ if (options?.init_options)
35
+ {
36
+ this.init_options = options?.init_options;
37
+ }
38
+ if (options?.stage)
39
+ {
40
+ this.stage = options?.stage;
41
+ }
42
+ if (options?.dev_stage_config)
43
+ {
44
+ this.dev_stage_config = options?.dev_stage_config;
45
+ }
46
+ if (options?.parent_app)
47
+ {
48
+ this.parent_app = options?.parent_app;
49
+ }
50
+ if (options?.host_api)
51
+ {
52
+ this.host_api = options?.host_api;
53
+ }
54
+ }
55
+
56
+
57
+ set_element_class_instance(element_class_instance)
58
+ {
59
+ this.element_class_instance = element_class_instance;
60
+ }
61
+
62
+
63
+
64
+ /**
65
+ *
66
+ * @version v.0.1 (26/05/2024)
67
+ * @param {object|import('../types/family_service/typeFOR.js').typeFOR} payload
68
+ */
69
+ async search(payload)
70
+ {
71
+ if (!this.module_prefix)
72
+ {
73
+ throw new Error('Error #10-1152 in ApiAbstractClass');
74
+ }
75
+
76
+ if (this.stage === 'development')
77
+ {
78
+ let response = await this.mock_search(payload);
79
+ return {
80
+ response: response,
81
+ payload : payload
82
+ };
83
+ }
84
+
85
+ let url = `/${ this.api_prefix }/${ this.module_prefix }/search`;
86
+
87
+ payload = this.handle_request_payload(payload);
88
+ let response = this.api_request(url, payload);
89
+ response = this.handle_response(response);
90
+
91
+ return {
92
+ response: response,
93
+ payload : payload
94
+ };
95
+ }
96
+
97
+
98
+ /**
99
+ *
100
+ * @version v.0.1 (27/06/2024)
101
+ * @param payload
102
+ * @returns {Promise<array>}
103
+ */
104
+ async mock_search(payload)
105
+ {
106
+ // @todo - дописать
107
+ if (this.dev_stage_config.api_response_search_element_count > 0 && this.dev_stage_config.api_response_search_element_count < 999)
108
+ {
109
+
110
+ }
111
+ else
112
+ {
113
+ throw new Error('Error #100-154');
114
+ }
115
+
116
+ let model = new this.element_class_instance();
117
+ let response = model.get_random_demo_data(this.dev_stage_config.api_response_search_element_count)
118
+
119
+ return response;
120
+
121
+ }
122
+
123
+
124
+
125
+
126
+ /******************************************************************************************/
127
+ /******************************************************************************************/
128
+
129
+ /******************************************************************************************/
130
+
131
+ /**
132
+ *
133
+ * @param payload
134
+ * @returns {*}
135
+ */
136
+ handle_request_payload(payload)
137
+ {
138
+ return this.object_replace_null_recursively(payload);
139
+ }
140
+
141
+
142
+ /**
143
+ *
144
+ * @param object
145
+ * @returns {*}
146
+ */
147
+ object_replace_null_recursively(object)
148
+ {
149
+ let result = clone_object(object);
150
+ for (let key in result)
151
+ {
152
+ if (result[ key ] === null)
153
+ {
154
+ result[ key ] = 'NULL';
155
+ }
156
+ else if (( result[ key ]?.constructor === Object ) || ( result[ key ]?.constructor === Array ))
157
+ {
158
+ result[ key ] = this.object_replace_null_recursively(result[ key ]);
159
+ }
160
+ }
161
+
162
+ return result;
163
+ }
164
+
165
+
166
+ /**
167
+ *
168
+ * @param url
169
+ * @param payload
170
+ * @param method
171
+ * @returns {Promise<Promise<axios.AxiosResponse<any>> | *>}
172
+ * @version v.0.2 (26/01/2025)
173
+ * @todo - валидация входящих параметров
174
+ */
175
+ async api_request(url, payload, method = 'POST')
176
+ {
177
+ let request_params;
178
+ if (!payload)
179
+ {
180
+ request_params = {};
181
+ }
182
+ else
183
+ {
184
+ request_params = clone_object(payload)
185
+ }
186
+
187
+ request_params = this.mixin_service_params(request_params);
188
+
189
+ if (this.host_api)
190
+ {
191
+ const page_hostname = (typeof window !== 'undefined' && window.location)
192
+ ? window.location.hostname
193
+ : '';
194
+ const api_hostname = String(this.host_api).split(':')[0];
195
+ if (!page_hostname || page_hostname !== api_hostname)
196
+ {
197
+ url = 'https://' + this.host_api + url;
198
+ }
199
+ }
200
+
201
+ const headers = { ...(this.headers || {}) };
202
+ const csrf_token = UserService.get_csrf_token();
203
+ if (csrf_token)
204
+ {
205
+ headers['X-CSRF-TOKEN'] = csrf_token;
206
+ }
207
+
208
+ let request_options = {
209
+ method : method,
210
+ headers: headers,
211
+ url : url
212
+ };
213
+
214
+ if (method === 'get' || method === 'GET')
215
+ {
216
+ request_options.paramsSerializer = this.object_to_string;
217
+ request_options.params = request_params;
218
+ }
219
+ else
220
+ {
221
+ request_options.data = (request_params);
222
+ }
223
+
224
+ let config = {};
225
+
226
+ return axios(request_options, config);
227
+ }
228
+
229
+
230
+ /**
231
+ *
232
+ * @param object
233
+ * @returns {string}
234
+ * @todo - валидация входящих параметров
235
+ */
236
+ object_to_string(object)
237
+ {
238
+ let result = qs.stringify(object, { array_format: 'indices' });
239
+
240
+ return result;
241
+ }
242
+
243
+
244
+ mixin_service_params(params)
245
+ {
246
+ params.client = 'webapp';
247
+ return params;
248
+ }
249
+
250
+
251
+ /**
252
+ *
253
+ * @param {object} response
254
+ * @param {object} init_class_instance
255
+ * @returns {Promise<any>}
256
+ * @version v.0.1 (04/03/2026)
257
+ */
258
+ handle_response(response, init_class_instance = true)
259
+ {
260
+ response = response.then((api_response) =>
261
+ {
262
+ let error_payload = {
263
+ api_response: api_response?.data?.api_response,
264
+ api_code : api_response?.data?.api_code,
265
+ api_result : api_response?.data?.api_result,
266
+ api_error : api_response?.data?.api_error,
267
+ hint : api_response?.data?.hint
268
+ };
269
+
270
+ if (api_response?.data?.api_error?.adapted_message)
271
+ {
272
+ error_payload.message = api_response.data.api_error.adapted_message
273
+ throw new ApiException(error_payload);
274
+ }
275
+ else if (api_response?.data?.api_code)
276
+ {
277
+ let api_code = api_response.data.api_code;
278
+
279
+ if (( api_code >= 200 ) && ( api_code <= 299 ))
280
+ {
281
+ if (is_array(api_response?.data?.api_result) && this.element_class_instance)
282
+ {
283
+ let result = api_response?.data.api_result.map((row) =>
284
+ {
285
+ if (init_class_instance)
286
+ {
287
+ return new this.element_class_instance(row);
288
+ }
289
+ else
290
+ {
291
+ return row;
292
+ }
293
+ });
294
+
295
+ return result;
296
+ }
297
+ else if (is_object(api_response?.data?.api_result))
298
+ {
299
+ return api_response?.data?.api_result;
300
+ }
301
+ else if (api_response?.data?.api_result === false)
302
+ {
303
+ return api_response?.data?.api_result;
304
+ }
305
+ else
306
+ {
307
+ return api_response?.data;
308
+ }
309
+ }
310
+ else if (api_response?.data?.api_result === false)
311
+ {
312
+ throw new ApiException(error_payload);
313
+ }
314
+ else
315
+ {
316
+ throw new ApiException(error_payload);
317
+ }
318
+ }
319
+ else
320
+ {
321
+ return api_response?.data;
322
+ }
323
+ })
324
+ .catch((error) =>
325
+ {
326
+
327
+ echo({ app: this });
328
+ console.error(error);
329
+ if (error instanceof ApiException)
330
+ {
331
+ throw error;
332
+ }
333
+
334
+
335
+ throw new ApiException({ error: error });
336
+ });
337
+
338
+ return response;
339
+ }
340
340
  }