datastake-daf 0.6.494 → 0.6.496
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/dist/services/index.js +162 -36
- package/package.json +1 -1
- package/src/@daf/services/AdminService.js +73 -0
- package/src/@daf/services/BaseService.js +26 -23
- package/src/@daf/services/helpers/BaseHTTPService.js +216 -200
- package/src/@daf/services/helpers/LazyService.js +31 -0
- package/src/services.js +3 -1
package/dist/services/index.js
CHANGED
|
@@ -6135,9 +6135,6 @@ class BaseHTTPService {
|
|
|
6135
6135
|
timeout = 300000,
|
|
6136
6136
|
customAxiosConfig = {}
|
|
6137
6137
|
} = config;
|
|
6138
|
-
console.log({
|
|
6139
|
-
baseHTTPService: config
|
|
6140
|
-
});
|
|
6141
6138
|
this.getToken = getToken || (() => null);
|
|
6142
6139
|
this.getHeaders = getHeaders || (() => ({}));
|
|
6143
6140
|
this.getBaseURL = getBaseURL || (() => null);
|
|
@@ -6149,7 +6146,14 @@ class BaseHTTPService {
|
|
|
6149
6146
|
// Init
|
|
6150
6147
|
this.cancelToken = {};
|
|
6151
6148
|
this.token = this.getToken();
|
|
6152
|
-
this.
|
|
6149
|
+
this.api = null;
|
|
6150
|
+
this._axiosInitialized = false;
|
|
6151
|
+
}
|
|
6152
|
+
_ensureAxiosInitialized() {
|
|
6153
|
+
if (!this._axiosInitialized) {
|
|
6154
|
+
this.setupAxios();
|
|
6155
|
+
this._axiosInitialized = true;
|
|
6156
|
+
}
|
|
6153
6157
|
}
|
|
6154
6158
|
setupAxios() {
|
|
6155
6159
|
const headers = {
|
|
@@ -6167,8 +6171,6 @@ class BaseHTTPService {
|
|
|
6167
6171
|
}
|
|
6168
6172
|
setupInterceptors() {
|
|
6169
6173
|
this.api.interceptors.response.use(response => response, error => Promise.reject(error));
|
|
6170
|
-
|
|
6171
|
-
// Request interceptor - clean JSON data
|
|
6172
6174
|
this.api.interceptors.request.use(config => {
|
|
6173
6175
|
if (['post', 'put', 'patch'].includes(config.method)) {
|
|
6174
6176
|
config.data = this.cleanJSON(config.data);
|
|
@@ -6193,6 +6195,7 @@ class BaseHTTPService {
|
|
|
6193
6195
|
}
|
|
6194
6196
|
this.api = axios$1.create(options);
|
|
6195
6197
|
this.setupInterceptors();
|
|
6198
|
+
this._axiosInitialized = true;
|
|
6196
6199
|
}
|
|
6197
6200
|
async formatResponse(call) {
|
|
6198
6201
|
try {
|
|
@@ -6213,6 +6216,8 @@ class BaseHTTPService {
|
|
|
6213
6216
|
}
|
|
6214
6217
|
}
|
|
6215
6218
|
apiPost(options) {
|
|
6219
|
+
this._ensureAxiosInitialized();
|
|
6220
|
+
|
|
6216
6221
|
// Check if there are any previous pending requests
|
|
6217
6222
|
if (typeof this.cancelToken[options.url] != typeof undefined) {
|
|
6218
6223
|
try {
|
|
@@ -6238,6 +6243,7 @@ class BaseHTTPService {
|
|
|
6238
6243
|
}).catch(err => this.onError(err)));
|
|
6239
6244
|
}
|
|
6240
6245
|
apiGet(options) {
|
|
6246
|
+
this._ensureAxiosInitialized();
|
|
6241
6247
|
const token = this.getToken();
|
|
6242
6248
|
if (token !== this.token || !token) {
|
|
6243
6249
|
this.reloadAxios();
|
|
@@ -6257,6 +6263,7 @@ class BaseHTTPService {
|
|
|
6257
6263
|
}).catch(err => this.onError(err)));
|
|
6258
6264
|
}
|
|
6259
6265
|
apiDelete(options) {
|
|
6266
|
+
this._ensureAxiosInitialized();
|
|
6260
6267
|
const token = this.getToken();
|
|
6261
6268
|
if (token !== this.token || !token) {
|
|
6262
6269
|
this.reloadAxios();
|
|
@@ -6276,6 +6283,7 @@ class BaseHTTPService {
|
|
|
6276
6283
|
}).catch(err => this.onError(err)));
|
|
6277
6284
|
}
|
|
6278
6285
|
apiPut(options) {
|
|
6286
|
+
this._ensureAxiosInitialized();
|
|
6279
6287
|
if (typeof this.cancelToken[options.url] != typeof undefined) {
|
|
6280
6288
|
try {
|
|
6281
6289
|
this.cancelToken[options.url].cancel("Operation canceled due to new request.");
|
|
@@ -6300,6 +6308,7 @@ class BaseHTTPService {
|
|
|
6300
6308
|
}).catch(err => this.onError(err, options.onUnauthorized)));
|
|
6301
6309
|
}
|
|
6302
6310
|
apiPatch(options) {
|
|
6311
|
+
this._ensureAxiosInitialized();
|
|
6303
6312
|
const token = this.getToken();
|
|
6304
6313
|
if (token !== this.token || !token) {
|
|
6305
6314
|
this.reloadAxios();
|
|
@@ -6392,6 +6401,36 @@ const getConfig = () => {
|
|
|
6392
6401
|
return globalConfig;
|
|
6393
6402
|
};
|
|
6394
6403
|
|
|
6404
|
+
/**
|
|
6405
|
+
* Creates a lazy-loading proxy for a service class
|
|
6406
|
+
* The service instance is only created when a method is first accessed
|
|
6407
|
+
* This ensures that global configuration is applied before service initialization
|
|
6408
|
+
*
|
|
6409
|
+
* @param {Class} ServiceClass - The service class to instantiate lazily
|
|
6410
|
+
* @returns {Proxy} A proxy that creates the instance on first access
|
|
6411
|
+
*
|
|
6412
|
+
* @example
|
|
6413
|
+
* class MyService extends BaseService {
|
|
6414
|
+
* getData() { return this.apiGet({ url: '/data' }); }
|
|
6415
|
+
* }
|
|
6416
|
+
* export default createLazyService(MyService);
|
|
6417
|
+
*/
|
|
6418
|
+
function createLazyService(ServiceClass) {
|
|
6419
|
+
let instance = null;
|
|
6420
|
+
return new Proxy({}, {
|
|
6421
|
+
get(target, prop) {
|
|
6422
|
+
// Create instance on first property access
|
|
6423
|
+
if (!instance) {
|
|
6424
|
+
instance = new ServiceClass();
|
|
6425
|
+
}
|
|
6426
|
+
const value = instance[prop];
|
|
6427
|
+
|
|
6428
|
+
// Bind methods to maintain correct 'this' context
|
|
6429
|
+
return typeof value === 'function' ? value.bind(instance) : value;
|
|
6430
|
+
}
|
|
6431
|
+
});
|
|
6432
|
+
}
|
|
6433
|
+
|
|
6395
6434
|
/**
|
|
6396
6435
|
* Generic error handler factory for axios requests
|
|
6397
6436
|
* Highly configurable to adapt to different project needs
|
|
@@ -6486,49 +6525,53 @@ class ErrorService {
|
|
|
6486
6525
|
}
|
|
6487
6526
|
}
|
|
6488
6527
|
|
|
6528
|
+
// In datastake-daf - src/services/BaseService.js
|
|
6489
6529
|
class BaseService extends BaseHTTPService {
|
|
6490
6530
|
constructor() {
|
|
6491
|
-
|
|
6492
|
-
console.log({
|
|
6493
|
-
baseService: config
|
|
6494
|
-
});
|
|
6495
|
-
const onError = createErrorHandler({
|
|
6496
|
-
getStorageManager: () => StorageManager,
|
|
6497
|
-
handleError: ({
|
|
6498
|
-
status,
|
|
6499
|
-
statusText,
|
|
6500
|
-
data
|
|
6501
|
-
}) => {
|
|
6502
|
-
ErrorService.handle({
|
|
6503
|
-
status,
|
|
6504
|
-
statusText,
|
|
6505
|
-
data
|
|
6506
|
-
});
|
|
6507
|
-
},
|
|
6508
|
-
tokenStorageKey: 'token',
|
|
6509
|
-
unauthorizedRedirect: '/',
|
|
6510
|
-
checkTokenValidity: true,
|
|
6511
|
-
handleNetworkError: true
|
|
6512
|
-
});
|
|
6531
|
+
// Call super with lazy getters that fetch config at runtime
|
|
6513
6532
|
super({
|
|
6514
6533
|
getToken: () => getToken(),
|
|
6515
|
-
getHeaders: () =>
|
|
6516
|
-
|
|
6517
|
-
|
|
6518
|
-
|
|
6519
|
-
|
|
6534
|
+
getHeaders: () => {
|
|
6535
|
+
const config = getConfig();
|
|
6536
|
+
return {
|
|
6537
|
+
Language: config.language || StorageManager.get('datastakeLng') || 'en',
|
|
6538
|
+
'ngrok-skip-browser-warning': true,
|
|
6539
|
+
Application: config.application
|
|
6540
|
+
};
|
|
6541
|
+
},
|
|
6520
6542
|
getBaseURL: options => {
|
|
6543
|
+
const config = getConfig();
|
|
6521
6544
|
if (options?.isStore) return config.storeUrl;
|
|
6522
6545
|
return config.mainApiUrl;
|
|
6523
6546
|
},
|
|
6524
|
-
onError:
|
|
6547
|
+
onError: error => {
|
|
6548
|
+
// Create error handler lazily
|
|
6549
|
+
const errorHandler = createErrorHandler({
|
|
6550
|
+
getStorageManager: () => StorageManager,
|
|
6551
|
+
handleError: ({
|
|
6552
|
+
status,
|
|
6553
|
+
statusText,
|
|
6554
|
+
data
|
|
6555
|
+
}) => {
|
|
6556
|
+
ErrorService.handle({
|
|
6557
|
+
status,
|
|
6558
|
+
statusText,
|
|
6559
|
+
data
|
|
6560
|
+
});
|
|
6561
|
+
},
|
|
6562
|
+
tokenStorageKey: 'token',
|
|
6563
|
+
unauthorizedRedirect: '/',
|
|
6564
|
+
checkTokenValidity: true,
|
|
6565
|
+
handleNetworkError: true
|
|
6566
|
+
});
|
|
6567
|
+
return errorHandler(error);
|
|
6568
|
+
},
|
|
6525
6569
|
timeout: 300000
|
|
6526
6570
|
});
|
|
6527
|
-
this.config = config;
|
|
6528
6571
|
}
|
|
6529
6572
|
apiGet(options) {
|
|
6530
6573
|
const token = getToken();
|
|
6531
|
-
const config =
|
|
6574
|
+
const config = getConfig();
|
|
6532
6575
|
if (token !== this.token || !token) {
|
|
6533
6576
|
this.reloadAxios();
|
|
6534
6577
|
}
|
|
@@ -6553,9 +6596,92 @@ class BaseService extends BaseHTTPService {
|
|
|
6553
6596
|
}
|
|
6554
6597
|
}
|
|
6555
6598
|
|
|
6599
|
+
// src/services/AdminService.js
|
|
6600
|
+
class AdminService extends BaseService {
|
|
6601
|
+
updateCompany(id, data) {
|
|
6602
|
+
return this.apiPut({
|
|
6603
|
+
url: `/companies/${id}`,
|
|
6604
|
+
data
|
|
6605
|
+
});
|
|
6606
|
+
}
|
|
6607
|
+
getAccount({
|
|
6608
|
+
id
|
|
6609
|
+
}) {
|
|
6610
|
+
return this.apiGet({
|
|
6611
|
+
url: `/accounts/${id}`
|
|
6612
|
+
});
|
|
6613
|
+
}
|
|
6614
|
+
inviteAccount(data) {
|
|
6615
|
+
return this.apiPost({
|
|
6616
|
+
url: `/accounts/inviteAccount`,
|
|
6617
|
+
data
|
|
6618
|
+
});
|
|
6619
|
+
}
|
|
6620
|
+
getAccounts({
|
|
6621
|
+
params
|
|
6622
|
+
}) {
|
|
6623
|
+
return this.apiGet({
|
|
6624
|
+
params,
|
|
6625
|
+
url: `/accounts`
|
|
6626
|
+
});
|
|
6627
|
+
}
|
|
6628
|
+
updateAccount({
|
|
6629
|
+
data,
|
|
6630
|
+
id
|
|
6631
|
+
}) {
|
|
6632
|
+
return this.apiPut({
|
|
6633
|
+
data,
|
|
6634
|
+
url: `/accounts/${id}`
|
|
6635
|
+
});
|
|
6636
|
+
}
|
|
6637
|
+
toggleAccountStatus({
|
|
6638
|
+
id
|
|
6639
|
+
}) {
|
|
6640
|
+
return this.apiPut({
|
|
6641
|
+
url: `/accounts/suspendActivate/${id}`
|
|
6642
|
+
});
|
|
6643
|
+
}
|
|
6644
|
+
transferAdmin({
|
|
6645
|
+
userId,
|
|
6646
|
+
id
|
|
6647
|
+
}) {
|
|
6648
|
+
return this.apiPut({
|
|
6649
|
+
url: `/accounts/transfer-rights/${id}`,
|
|
6650
|
+
data: {
|
|
6651
|
+
userId
|
|
6652
|
+
}
|
|
6653
|
+
});
|
|
6654
|
+
}
|
|
6655
|
+
getUsers({
|
|
6656
|
+
params
|
|
6657
|
+
}) {
|
|
6658
|
+
return this.apiGet({
|
|
6659
|
+
params,
|
|
6660
|
+
url: `/accounts/users`
|
|
6661
|
+
});
|
|
6662
|
+
}
|
|
6663
|
+
impersonate({
|
|
6664
|
+
id
|
|
6665
|
+
}) {
|
|
6666
|
+
return this.apiGet({
|
|
6667
|
+
url: `/accounts/impersonate/${id}`
|
|
6668
|
+
});
|
|
6669
|
+
}
|
|
6670
|
+
cancelInvitation({
|
|
6671
|
+
token
|
|
6672
|
+
}) {
|
|
6673
|
+
return this.apiPut({
|
|
6674
|
+
url: `/accounts/cancelInvitation/${token}`
|
|
6675
|
+
});
|
|
6676
|
+
}
|
|
6677
|
+
}
|
|
6678
|
+
var AdminService$1 = createLazyService(AdminService);
|
|
6679
|
+
|
|
6680
|
+
exports.AdminService = AdminService$1;
|
|
6556
6681
|
exports.BaseHTTPService = BaseHTTPService;
|
|
6557
6682
|
exports.BaseService = BaseService;
|
|
6558
6683
|
exports.ErrorHandler = ErrorHandler;
|
|
6559
6684
|
exports.ErrorService = ErrorService;
|
|
6560
6685
|
exports.configureServices = configureServices;
|
|
6686
|
+
exports.createLazyService = createLazyService;
|
|
6561
6687
|
exports.getConfig = getConfig;
|
package/package.json
CHANGED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// src/services/AdminService.js
|
|
2
|
+
import { BaseService } from './BaseService.js';
|
|
3
|
+
import { createLazyService } from './helpers/LazyService.js';
|
|
4
|
+
|
|
5
|
+
class AdminService extends BaseService {
|
|
6
|
+
updateCompany(id, data) {
|
|
7
|
+
return this.apiPut({
|
|
8
|
+
url: `/companies/${id}`,
|
|
9
|
+
data,
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
getAccount({ id }) {
|
|
14
|
+
return this.apiGet({
|
|
15
|
+
url: `/accounts/${id}`,
|
|
16
|
+
})
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
inviteAccount(data) {
|
|
20
|
+
return this.apiPost({
|
|
21
|
+
url: `/accounts/inviteAccount`,
|
|
22
|
+
data,
|
|
23
|
+
})
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
getAccounts({ params }) {
|
|
27
|
+
return this.apiGet({
|
|
28
|
+
params,
|
|
29
|
+
url: `/accounts`,
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
updateAccount({ data, id }) {
|
|
34
|
+
return this.apiPut({
|
|
35
|
+
data,
|
|
36
|
+
url: `/accounts/${id}`,
|
|
37
|
+
})
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
toggleAccountStatus({ id }) {
|
|
41
|
+
return this.apiPut({
|
|
42
|
+
url: `/accounts/suspendActivate/${id}`,
|
|
43
|
+
})
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
transferAdmin({ userId, id }) {
|
|
47
|
+
return this.apiPut({
|
|
48
|
+
url: `/accounts/transfer-rights/${id}`,
|
|
49
|
+
data: { userId },
|
|
50
|
+
})
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
getUsers({ params }) {
|
|
54
|
+
return this.apiGet({
|
|
55
|
+
params,
|
|
56
|
+
url: `/accounts/users`,
|
|
57
|
+
})
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
impersonate({ id }) {
|
|
61
|
+
return this.apiGet({
|
|
62
|
+
url: `/accounts/impersonate/${id}`,
|
|
63
|
+
})
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
cancelInvitation({ token }) {
|
|
67
|
+
return this.apiPut({
|
|
68
|
+
url: `/accounts/cancelInvitation/${token}`,
|
|
69
|
+
})
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export default createLazyService(AdminService);
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
// In datastake-daf - src/services/BaseService.js
|
|
1
2
|
import { BaseHTTPService } from './helpers/BaseHTTPService.js';
|
|
2
3
|
import { getToken } from '../../helpers/Token.js';
|
|
3
4
|
import { createErrorHandler } from '../../helpers/errorHandling.js';
|
|
@@ -7,41 +8,43 @@ import { ErrorService } from './ErrorService.js';
|
|
|
7
8
|
|
|
8
9
|
export class BaseService extends BaseHTTPService {
|
|
9
10
|
constructor() {
|
|
10
|
-
|
|
11
|
-
console.log({baseService: config})
|
|
12
|
-
|
|
13
|
-
const onError = createErrorHandler({
|
|
14
|
-
getStorageManager: () => StorageManager,
|
|
15
|
-
handleError: ({ status, statusText, data }) => {
|
|
16
|
-
ErrorService.handle({ status, statusText, data });
|
|
17
|
-
},
|
|
18
|
-
tokenStorageKey: 'token',
|
|
19
|
-
unauthorizedRedirect: '/',
|
|
20
|
-
checkTokenValidity: true,
|
|
21
|
-
handleNetworkError: true,
|
|
22
|
-
});
|
|
23
|
-
|
|
11
|
+
// Call super with lazy getters that fetch config at runtime
|
|
24
12
|
super({
|
|
25
13
|
getToken: () => getToken(),
|
|
26
|
-
getHeaders: () =>
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
14
|
+
getHeaders: () => {
|
|
15
|
+
const config = getConfig();
|
|
16
|
+
return {
|
|
17
|
+
Language: config.language || StorageManager.get('datastakeLng') || 'en',
|
|
18
|
+
'ngrok-skip-browser-warning': true,
|
|
19
|
+
Application: config.application,
|
|
20
|
+
};
|
|
21
|
+
},
|
|
31
22
|
getBaseURL: (options) => {
|
|
23
|
+
const config = getConfig();
|
|
32
24
|
if (options?.isStore) return config.storeUrl;
|
|
33
25
|
return config.mainApiUrl;
|
|
34
26
|
},
|
|
35
|
-
onError:
|
|
27
|
+
onError: (error) => {
|
|
28
|
+
// Create error handler lazily
|
|
29
|
+
const errorHandler = createErrorHandler({
|
|
30
|
+
getStorageManager: () => StorageManager,
|
|
31
|
+
handleError: ({ status, statusText, data }) => {
|
|
32
|
+
ErrorService.handle({ status, statusText, data });
|
|
33
|
+
},
|
|
34
|
+
tokenStorageKey: 'token',
|
|
35
|
+
unauthorizedRedirect: '/',
|
|
36
|
+
checkTokenValidity: true,
|
|
37
|
+
handleNetworkError: true,
|
|
38
|
+
});
|
|
39
|
+
return errorHandler(error);
|
|
40
|
+
},
|
|
36
41
|
timeout: 300000,
|
|
37
42
|
});
|
|
38
|
-
|
|
39
|
-
this.config = config;
|
|
40
43
|
}
|
|
41
44
|
|
|
42
45
|
apiGet(options) {
|
|
43
46
|
const token = getToken();
|
|
44
|
-
const config =
|
|
47
|
+
const config = getConfig();
|
|
45
48
|
|
|
46
49
|
if (token !== this.token || !token) {
|
|
47
50
|
this.reloadAxios();
|
|
@@ -34,225 +34,241 @@ const paramsSerializer = (params) => {
|
|
|
34
34
|
* Provides axios wrapper with token management, cancel tokens, and interceptors
|
|
35
35
|
*/
|
|
36
36
|
export class BaseHTTPService {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
console.log({baseHTTPService: config})
|
|
37
|
+
constructor(config = {}) {
|
|
38
|
+
const {getHeaders, getBaseURL, onError, timeout = 300000, customAxiosConfig = {}} = config;
|
|
40
39
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
40
|
+
this.getToken = getToken || (() => null);
|
|
41
|
+
this.getHeaders = getHeaders || (() => ({}));
|
|
42
|
+
this.getBaseURL = getBaseURL || (() => null);
|
|
43
|
+
this.onError = onError || (() => null);
|
|
44
|
+
this.cleanJSON = cleanJSON;
|
|
45
|
+
this.timeout = timeout;
|
|
46
|
+
this.customAxiosConfig = customAxiosConfig;
|
|
48
47
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
48
|
+
// Init
|
|
49
|
+
this.cancelToken = {}
|
|
50
|
+
this.token = this.getToken();
|
|
51
|
+
|
|
52
|
+
this.api = null;
|
|
53
|
+
this._axiosInitialized = false;
|
|
54
|
+
}
|
|
52
55
|
|
|
53
|
-
|
|
54
|
-
|
|
56
|
+
_ensureAxiosInitialized() {
|
|
57
|
+
if (!this._axiosInitialized) {
|
|
58
|
+
this.setupAxios();
|
|
59
|
+
this._axiosInitialized = true;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
55
62
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
63
|
+
setupAxios() {
|
|
64
|
+
const headers = {
|
|
65
|
+
Authorization: `Bearer ${this.token}`,
|
|
66
|
+
...this.getHeaders(),
|
|
67
|
+
};
|
|
61
68
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
+
this.api = axios.create({
|
|
70
|
+
baseURL: this.getBaseURL(),
|
|
71
|
+
headers,
|
|
72
|
+
timeout: this.timeout,
|
|
73
|
+
paramsSerializer: paramsSerializer,
|
|
74
|
+
...this.customAxiosConfig,
|
|
75
|
+
});
|
|
69
76
|
|
|
70
|
-
|
|
71
|
-
|
|
77
|
+
this.setupInterceptors();
|
|
78
|
+
}
|
|
72
79
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
80
|
+
setupInterceptors() {
|
|
81
|
+
this.api.interceptors.response.use(
|
|
82
|
+
(response) => response,
|
|
83
|
+
(error) => Promise.reject(error)
|
|
84
|
+
);
|
|
78
85
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
const token = this.getToken();
|
|
94
|
-
this.token = token;
|
|
86
|
+
this.api.interceptors.request.use(
|
|
87
|
+
(config) => {
|
|
88
|
+
if (['post', 'put', 'patch'].includes(config.method)) {
|
|
89
|
+
config.data = this.cleanJSON(config.data);
|
|
90
|
+
}
|
|
91
|
+
return config;
|
|
92
|
+
},
|
|
93
|
+
(error) => Promise.reject(error)
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
reloadAxios() {
|
|
98
|
+
const token = this.getToken();
|
|
99
|
+
this.token = token;
|
|
95
100
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
101
|
+
const options = {
|
|
102
|
+
baseURL: this.getBaseURL(),
|
|
103
|
+
timeout: this.timeout,
|
|
104
|
+
paramsSerializer: paramsSerializer,
|
|
105
|
+
...this.customAxiosConfig,
|
|
106
|
+
};
|
|
102
107
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
108
|
+
if (token) {
|
|
109
|
+
options.headers = {
|
|
110
|
+
Authorization: `Bearer ${token}`,
|
|
111
|
+
...this.getHeaders(),
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
this.api = axios.create(options);
|
|
116
|
+
this.setupInterceptors();
|
|
117
|
+
this._axiosInitialized = true;
|
|
118
|
+
}
|
|
109
119
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
120
|
+
async formatResponse(call) {
|
|
121
|
+
try {
|
|
122
|
+
const res = await call;
|
|
123
|
+
const { data, status, statusText } = res || {};
|
|
124
|
+
this.cancelToken = {};
|
|
125
|
+
return { data, status, statusText };
|
|
126
|
+
} catch (error) {
|
|
127
|
+
return Promise.reject(error);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
113
130
|
|
|
114
|
-
|
|
131
|
+
apiPost(options) {
|
|
132
|
+
this._ensureAxiosInitialized();
|
|
133
|
+
|
|
134
|
+
// Check if there are any previous pending requests
|
|
135
|
+
if (typeof this.cancelToken[options.url] != typeof undefined) {
|
|
115
136
|
try {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
return {
|
|
120
|
-
} catch (error) {
|
|
121
|
-
return Promise.reject(error);
|
|
137
|
+
this.cancelToken[options.url].cancel("Operation canceled due to new request.");
|
|
138
|
+
} catch (e) {
|
|
139
|
+
console.log(e);
|
|
140
|
+
return {};
|
|
122
141
|
}
|
|
123
|
-
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
this.cancelToken[options.url] = axios.CancelToken.source();
|
|
145
|
+
const token = this.getToken();
|
|
146
|
+
|
|
147
|
+
if (token !== this.token || !token) {
|
|
148
|
+
this.reloadAxios();
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return this.formatResponse(
|
|
152
|
+
this.api.post(options.url, options.data, {
|
|
153
|
+
baseURL: options.baseURL || this.getBaseURL(options),
|
|
154
|
+
cancelToken: this.cancelToken[options.url].token,
|
|
155
|
+
headers: {
|
|
156
|
+
Authorization: `Bearer ${this.getToken()}`,
|
|
157
|
+
...this.getHeaders(),
|
|
158
|
+
...(options?.headers || {}),
|
|
159
|
+
}
|
|
160
|
+
})
|
|
161
|
+
.catch((err) => this.onError(err))
|
|
162
|
+
);
|
|
163
|
+
}
|
|
124
164
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
165
|
+
apiGet(options) {
|
|
166
|
+
this._ensureAxiosInitialized();
|
|
167
|
+
|
|
168
|
+
const token = this.getToken();
|
|
169
|
+
|
|
170
|
+
if (token !== this.token || !token) {
|
|
171
|
+
this.reloadAxios();
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const { url, ...config } = options;
|
|
175
|
+
|
|
176
|
+
return this.formatResponse(
|
|
177
|
+
this.api.get(url, {
|
|
178
|
+
...config,
|
|
179
|
+
baseURL: options.baseURL || this.getBaseURL(options),
|
|
180
|
+
headers: {
|
|
181
|
+
Authorization: `Bearer ${this.getToken()}`,
|
|
182
|
+
...this.getHeaders(),
|
|
183
|
+
...(config?.headers || {}),
|
|
133
184
|
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
if (token !== this.token || !token) {
|
|
140
|
-
this.reloadAxios();
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
return this.formatResponse(
|
|
144
|
-
this.api.post(options.url, options.data, {
|
|
145
|
-
baseURL: options.baseURL || this.getBaseURL(options),
|
|
146
|
-
cancelToken: this.cancelToken[options.url].token,
|
|
147
|
-
headers: {
|
|
148
|
-
Authorization: `Bearer ${this.getToken()}`,
|
|
149
|
-
...this.getHeaders(),
|
|
150
|
-
...(options?.headers || {}),
|
|
151
|
-
}
|
|
152
|
-
})
|
|
153
|
-
.catch((err) => this.onError(err))
|
|
154
|
-
);
|
|
155
|
-
}
|
|
185
|
+
})
|
|
186
|
+
.catch((err) => this.onError(err))
|
|
187
|
+
);
|
|
188
|
+
}
|
|
156
189
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
)
|
|
178
|
-
|
|
190
|
+
apiDelete(options) {
|
|
191
|
+
this._ensureAxiosInitialized();
|
|
192
|
+
|
|
193
|
+
const token = this.getToken();
|
|
194
|
+
|
|
195
|
+
if (token !== this.token || !token) {
|
|
196
|
+
this.reloadAxios();
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const { url, ...config } = options;
|
|
200
|
+
|
|
201
|
+
return this.formatResponse(
|
|
202
|
+
this.api.delete(url, {
|
|
203
|
+
...config,
|
|
204
|
+
baseURL: options.baseURL || this.getBaseURL(options),
|
|
205
|
+
headers: {
|
|
206
|
+
Authorization: `Bearer ${this.getToken()}`,
|
|
207
|
+
...this.getHeaders(),
|
|
208
|
+
...(config?.headers || {}),
|
|
209
|
+
}
|
|
210
|
+
})
|
|
211
|
+
.catch((err) => this.onError(err))
|
|
212
|
+
);
|
|
213
|
+
}
|
|
179
214
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
215
|
+
apiPut(options) {
|
|
216
|
+
this._ensureAxiosInitialized();
|
|
217
|
+
|
|
218
|
+
if (typeof this.cancelToken[options.url] != typeof undefined) {
|
|
219
|
+
try {
|
|
220
|
+
this.cancelToken[options.url].cancel("Operation canceled due to new request.");
|
|
221
|
+
} catch (e) {
|
|
222
|
+
console.log(e);
|
|
223
|
+
return {};
|
|
185
224
|
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
apiPut(options) {
|
|
204
|
-
if (typeof this.cancelToken[options.url] != typeof undefined) {
|
|
205
|
-
try {
|
|
206
|
-
this.cancelToken[options.url].cancel("Operation canceled due to new request.");
|
|
207
|
-
} catch (e) {
|
|
208
|
-
console.log(e);
|
|
209
|
-
return {};
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
this.cancelToken[options.url] = axios.CancelToken.source();
|
|
228
|
+
const token = this.getToken();
|
|
229
|
+
|
|
230
|
+
if (token !== this.token || !token) {
|
|
231
|
+
this.reloadAxios();
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
return this.formatResponse(
|
|
235
|
+
this.api.put(options.url, options.data, {
|
|
236
|
+
cancelToken: this.cancelToken[options.url].token,
|
|
237
|
+
baseURL: options.baseURL || this.getBaseURL(options),
|
|
238
|
+
headers: {
|
|
239
|
+
Authorization: `Bearer ${this.getToken()}`,
|
|
240
|
+
...this.getHeaders(),
|
|
241
|
+
...(options?.headers || {}),
|
|
210
242
|
}
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
if (token !== this.token || !token) {
|
|
217
|
-
this.reloadAxios();
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
return this.formatResponse(
|
|
221
|
-
this.api.put(options.url, options.data, {
|
|
222
|
-
cancelToken: this.cancelToken[options.url].token,
|
|
223
|
-
baseURL: options.baseURL || this.getBaseURL(options),
|
|
224
|
-
headers: {
|
|
225
|
-
Authorization: `Bearer ${this.getToken()}`,
|
|
226
|
-
...this.getHeaders(),
|
|
227
|
-
...(options?.headers || {}),
|
|
228
|
-
}
|
|
229
|
-
})
|
|
230
|
-
.catch((err) => this.onError(err, options.onUnauthorized))
|
|
231
|
-
);
|
|
232
|
-
}
|
|
243
|
+
})
|
|
244
|
+
.catch((err) => this.onError(err, options.onUnauthorized))
|
|
245
|
+
);
|
|
246
|
+
}
|
|
233
247
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
)
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
248
|
+
apiPatch(options) {
|
|
249
|
+
this._ensureAxiosInitialized();
|
|
250
|
+
|
|
251
|
+
const token = this.getToken();
|
|
252
|
+
|
|
253
|
+
if (token !== this.token || !token) {
|
|
254
|
+
this.reloadAxios();
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
return this.formatResponse(
|
|
258
|
+
this.api.patch(options.url, options.data, {
|
|
259
|
+
baseURL: options.baseURL || this.getBaseURL(options),
|
|
260
|
+
headers: {
|
|
261
|
+
Authorization: `Bearer ${this.getToken()}`,
|
|
262
|
+
...this.getHeaders(),
|
|
263
|
+
...(options?.headers || {}),
|
|
264
|
+
}
|
|
265
|
+
})
|
|
266
|
+
.catch((err) => this.onError(err))
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// Utility method
|
|
271
|
+
assignParamsToUrl(path, params) {
|
|
272
|
+
return assignParamsToUrl(path, params);
|
|
273
|
+
}
|
|
258
274
|
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates a lazy-loading proxy for a service class
|
|
3
|
+
* The service instance is only created when a method is first accessed
|
|
4
|
+
* This ensures that global configuration is applied before service initialization
|
|
5
|
+
*
|
|
6
|
+
* @param {Class} ServiceClass - The service class to instantiate lazily
|
|
7
|
+
* @returns {Proxy} A proxy that creates the instance on first access
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* class MyService extends BaseService {
|
|
11
|
+
* getData() { return this.apiGet({ url: '/data' }); }
|
|
12
|
+
* }
|
|
13
|
+
* export default createLazyService(MyService);
|
|
14
|
+
*/
|
|
15
|
+
export function createLazyService(ServiceClass) {
|
|
16
|
+
let instance = null;
|
|
17
|
+
|
|
18
|
+
return new Proxy({}, {
|
|
19
|
+
get(target, prop) {
|
|
20
|
+
// Create instance on first property access
|
|
21
|
+
if (!instance) {
|
|
22
|
+
instance = new ServiceClass();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const value = instance[prop];
|
|
26
|
+
|
|
27
|
+
// Bind methods to maintain correct 'this' context
|
|
28
|
+
return typeof value === 'function' ? value.bind(instance) : value;
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
}
|
package/src/services.js
CHANGED
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
export { BaseHTTPService } from './@daf/services/helpers/BaseHTTPService.js';
|
|
3
3
|
export { ErrorHandler } from './@daf/services/helpers/ErrorHandler.js';
|
|
4
4
|
export { configureServices, getConfig } from './@daf/services/helpers/config.js';
|
|
5
|
+
export { createLazyService } from './@daf/services/helpers/LazyService.js';
|
|
5
6
|
|
|
6
7
|
// SERVICES
|
|
7
8
|
export { BaseService } from './@daf/services/BaseService.js';
|
|
8
|
-
export { ErrorService } from './@daf/services/ErrorService.js';
|
|
9
|
+
export { ErrorService } from './@daf/services/ErrorService.js';
|
|
10
|
+
export { default as AdminService } from './@daf/services/AdminService.js';
|