@smess/networking 1.1.5 → 2.0.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 +211 -0
- package/README.md +165 -114
- package/dist/core/body.d.ts +10 -0
- package/dist/core/body.d.ts.map +1 -0
- package/dist/core/body.js +22 -0
- package/dist/core/body.js.map +1 -0
- package/dist/core/client.d.ts +74 -0
- package/dist/core/client.d.ts.map +1 -0
- package/dist/core/client.js +124 -0
- package/dist/core/client.js.map +1 -0
- package/dist/core/decoders.d.ts +8 -0
- package/dist/core/decoders.d.ts.map +1 -0
- package/dist/core/decoders.js +26 -0
- package/dist/core/decoders.js.map +1 -0
- package/dist/core/endpoint.d.ts +69 -0
- package/dist/core/endpoint.d.ts.map +1 -0
- package/dist/core/endpoint.js +6 -0
- package/dist/core/endpoint.js.map +1 -0
- package/dist/core/hosts.d.ts +25 -0
- package/dist/core/hosts.d.ts.map +1 -0
- package/dist/core/hosts.js +22 -0
- package/dist/core/hosts.js.map +1 -0
- package/dist/core/pagination.d.ts +33 -0
- package/dist/core/pagination.d.ts.map +1 -0
- package/dist/core/pagination.js +23 -0
- package/dist/core/pagination.js.map +1 -0
- package/dist/core/pipeline.d.ts +25 -0
- package/dist/core/pipeline.d.ts.map +1 -0
- package/dist/core/pipeline.js +12 -0
- package/dist/core/pipeline.js.map +1 -0
- package/dist/core/retry.d.ts +18 -0
- package/dist/core/retry.d.ts.map +1 -0
- package/dist/core/retry.js +47 -0
- package/dist/core/retry.js.map +1 -0
- package/dist/core/transport.d.ts +30 -0
- package/dist/core/transport.d.ts.map +1 -0
- package/dist/core/transport.js +73 -0
- package/dist/core/transport.js.map +1 -0
- package/dist/core/types.d.ts +24 -0
- package/dist/core/types.d.ts.map +1 -0
- package/dist/core/types.js +3 -0
- package/dist/core/types.js.map +1 -0
- package/dist/core/url.d.ts +15 -0
- package/dist/core/url.d.ts.map +1 -0
- package/dist/core/url.js +46 -0
- package/dist/core/url.js.map +1 -0
- package/dist/errors/parse-error-body.d.ts +20 -0
- package/dist/errors/parse-error-body.d.ts.map +1 -0
- package/dist/errors/parse-error-body.js +73 -0
- package/dist/errors/parse-error-body.js.map +1 -0
- package/dist/errors/smess-api-error.d.ts +70 -0
- package/dist/errors/smess-api-error.d.ts.map +1 -0
- package/dist/errors/smess-api-error.js +84 -0
- package/dist/errors/smess-api-error.js.map +1 -0
- package/dist/index.d.ts +27 -25
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +15 -29
- package/dist/index.js.map +1 -0
- package/dist/modules/authentication/index.d.ts +89 -0
- package/dist/modules/authentication/index.d.ts.map +1 -0
- package/dist/modules/authentication/index.js +128 -0
- package/dist/modules/authentication/index.js.map +1 -0
- package/dist/modules/campaign/index.d.ts +212 -0
- package/dist/modules/campaign/index.d.ts.map +1 -0
- package/dist/modules/campaign/index.js +193 -0
- package/dist/modules/campaign/index.js.map +1 -0
- package/dist/modules/campaign/types.d.ts +562 -0
- package/dist/modules/campaign/types.d.ts.map +1 -0
- package/dist/modules/campaign/types.js +2 -0
- package/dist/modules/campaign/types.js.map +1 -0
- package/dist/modules/danara/index.d.ts +153 -0
- package/dist/modules/danara/index.d.ts.map +1 -0
- package/dist/modules/danara/index.js +144 -0
- package/dist/modules/danara/index.js.map +1 -0
- package/dist/modules/danara/types.d.ts +479 -0
- package/dist/modules/danara/types.d.ts.map +1 -0
- package/dist/modules/danara/types.js +2 -0
- package/dist/modules/danara/types.js.map +1 -0
- package/dist/modules/smeup-file/index.d.ts +27 -0
- package/dist/modules/smeup-file/index.d.ts.map +1 -0
- package/dist/modules/smeup-file/index.js +17 -0
- package/dist/modules/smeup-file/index.js.map +1 -0
- package/dist/modules/sso/index.d.ts +40 -0
- package/dist/modules/sso/index.d.ts.map +1 -0
- package/dist/modules/sso/index.js +41 -0
- package/dist/modules/sso/index.js.map +1 -0
- package/dist/rx/index.d.ts +11 -0
- package/dist/rx/index.d.ts.map +1 -0
- package/dist/rx/index.js +20 -0
- package/dist/rx/index.js.map +1 -0
- package/dist/session/auth-middleware.d.ts +27 -0
- package/dist/session/auth-middleware.d.ts.map +1 -0
- package/dist/session/auth-middleware.js +122 -0
- package/dist/session/auth-middleware.js.map +1 -0
- package/dist/session/jwt.d.ts +20 -0
- package/dist/session/jwt.d.ts.map +1 -0
- package/dist/session/jwt.js +25 -0
- package/dist/session/jwt.js.map +1 -0
- package/dist/session/stores.d.ts +19 -0
- package/dist/session/stores.d.ts.map +1 -0
- package/dist/session/stores.js +103 -0
- package/dist/session/stores.js.map +1 -0
- package/dist/session/types.d.ts +28 -0
- package/dist/session/types.d.ts.map +1 -0
- package/dist/session/types.js +2 -0
- package/dist/session/types.js.map +1 -0
- package/package.json +80 -30
- package/dist/core/axios.d.ts +0 -51
- package/dist/core/axios.js +0 -65
- package/dist/core/axios.observable.d.ts +0 -11
- package/dist/core/axios.observable.js +0 -58
- package/dist/core/base.provider.advanced.test.d.ts +0 -1
- package/dist/core/base.provider.advanced.test.js +0 -29
- package/dist/core/base.provider.d.ts +0 -15
- package/dist/core/base.provider.js +0 -13
- package/dist/core/base.provider.test.d.ts +0 -1
- package/dist/core/base.provider.test.js +0 -36
- package/dist/core/base.service.wrapper.advanced.test.d.ts +0 -1
- package/dist/core/base.service.wrapper.advanced.test.js +0 -39
- package/dist/core/base.service.wrapper.d.ts +0 -7
- package/dist/core/base.service.wrapper.js +0 -11
- package/dist/core/base.service.wrapper.test.d.ts +0 -1
- package/dist/core/base.service.wrapper.test.js +0 -25
- package/dist/core/rx.http.service.d.ts +0 -51
- package/dist/core/rx.http.service.js +0 -65
- package/dist/services/auth.service.d.ts +0 -69
- package/dist/services/auth.service.js +0 -197
- package/dist/services/auth.service.test.d.ts +0 -1
- package/dist/services/auth.service.test.js +0 -340
- package/dist/services/cache.service.d.ts +0 -31
- package/dist/services/cache.service.js +0 -77
- package/dist/services/cache.service.test.d.ts +0 -1
- package/dist/services/cache.service.test.js +0 -88
- package/dist/services/customer.service.d.ts +0 -213
- package/dist/services/customer.service.js +0 -128
- package/dist/services/customer.service.test.d.ts +0 -1
- package/dist/services/customer.service.test.js +0 -430
- package/dist/services/employee.service.d.ts +0 -73
- package/dist/services/employee.service.js +0 -74
- package/dist/services/employee.service.test.d.ts +0 -1
- package/dist/services/employee.service.test.js +0 -127
- package/dist/services/logger.service.d.ts +0 -31
- package/dist/services/logger.service.js +0 -93
- package/dist/services/logger.service.test.d.ts +0 -1
- package/dist/services/logger.service.test.js +0 -31
- package/dist/services/order.service.d.ts +0 -73
- package/dist/services/order.service.js +0 -97
- package/dist/services/order.service.test.d.ts +0 -1
- package/dist/services/order.service.test.js +0 -163
- package/dist/services/product.service.d.ts +0 -21
- package/dist/services/product.service.js +0 -42
- package/dist/services/product.service.test.d.ts +0 -1
- package/dist/services/product.service.test.js +0 -61
- package/dist/services/smess.service.d.ts +0 -36
- package/dist/services/smess.service.js +0 -128
- package/dist/services/smess.service.test.d.ts +0 -1
- package/dist/services/smess.service.test.js +0 -103
- package/dist/services/store.cart.service.d.ts +0 -158
- package/dist/services/store.cart.service.js +0 -177
- package/dist/services/store.cart.service.test.d.ts +0 -1
- package/dist/services/store.cart.service.test.js +0 -394
- package/dist/services/store.product.service.d.ts +0 -124
- package/dist/services/store.product.service.js +0 -102
- package/dist/services/store.product.service.test.d.ts +0 -1
- package/dist/services/store.product.service.test.js +0 -319
- package/dist/services/store.service.d.ts +0 -79
- package/dist/services/store.service.js +0 -235
- package/dist/services/store.service.test.d.ts +0 -1
- package/dist/services/store.service.test.js +0 -274
- package/dist/services/storefront.cart.service.d.ts +0 -73
- package/dist/services/storefront.cart.service.js +0 -167
- package/dist/services/storefront.cart.service.test.d.ts +0 -1
- package/dist/services/storefront.cart.service.test.js +0 -211
- package/dist/services/storefront.checkout.service.d.ts +0 -62
- package/dist/services/storefront.checkout.service.js +0 -75
- package/dist/services/storefront.checkout.service.test.d.ts +0 -1
- package/dist/services/storefront.checkout.service.test.js +0 -158
- package/dist/services/storefront.customer.metadata.service.d.ts +0 -91
- package/dist/services/storefront.customer.metadata.service.js +0 -225
- package/dist/services/storefront.customer.metadata.service.test.d.ts +0 -1
- package/dist/services/storefront.customer.metadata.service.test.js +0 -271
- package/dist/services/storefront.order.service.d.ts +0 -45
- package/dist/services/storefront.order.service.js +0 -99
- package/dist/services/storefront.order.service.test.d.ts +0 -1
- package/dist/services/storefront.order.service.test.js +0 -158
- package/dist/services/storefront.product.service.d.ts +0 -52
- package/dist/services/storefront.product.service.js +0 -75
- package/dist/services/storefront.product.service.test.d.ts +0 -1
- package/dist/services/storefront.product.service.test.js +0 -120
- package/dist/services/storefront.store.service.d.ts +0 -54
- package/dist/services/storefront.store.service.js +0 -142
- package/dist/services/storefront.store.service.test.d.ts +0 -1
- package/dist/services/storefront.store.service.test.js +0 -90
- package/dist/services/user.service.d.ts +0 -16
- package/dist/services/user.service.js +0 -37
- package/dist/services/user.service.test.d.ts +0 -1
- package/dist/services/user.service.test.js +0 -52
- package/dist/services/utils/withAuthReset.d.ts +0 -3
- package/dist/services/utils/withAuthReset.js +0 -8
- package/dist/src/core/axios.d.ts +0 -51
- package/dist/src/core/axios.js +0 -65
- package/dist/src/core/axios.observable.d.ts +0 -11
- package/dist/src/core/axios.observable.js +0 -58
- package/dist/src/core/base.provider.advanced.test.d.ts +0 -1
- package/dist/src/core/base.provider.advanced.test.js +0 -29
- package/dist/src/core/base.provider.d.ts +0 -15
- package/dist/src/core/base.provider.js +0 -13
- package/dist/src/core/base.provider.test.d.ts +0 -1
- package/dist/src/core/base.provider.test.js +0 -36
- package/dist/src/core/base.service.wrapper.advanced.test.d.ts +0 -1
- package/dist/src/core/base.service.wrapper.advanced.test.js +0 -39
- package/dist/src/core/base.service.wrapper.d.ts +0 -7
- package/dist/src/core/base.service.wrapper.js +0 -11
- package/dist/src/core/base.service.wrapper.test.d.ts +0 -1
- package/dist/src/core/base.service.wrapper.test.js +0 -25
- package/dist/src/core/rx.http.service.d.ts +0 -51
- package/dist/src/core/rx.http.service.js +0 -65
- package/dist/src/index.d.ts +0 -25
- package/dist/src/index.js +0 -29
- package/dist/src/services/auth.service.d.ts +0 -69
- package/dist/src/services/auth.service.js +0 -197
- package/dist/src/services/auth.service.test.d.ts +0 -1
- package/dist/src/services/auth.service.test.js +0 -340
- package/dist/src/services/cache.service.d.ts +0 -31
- package/dist/src/services/cache.service.js +0 -77
- package/dist/src/services/cache.service.test.d.ts +0 -1
- package/dist/src/services/cache.service.test.js +0 -88
- package/dist/src/services/customer.service.d.ts +0 -199
- package/dist/src/services/customer.service.js +0 -114
- package/dist/src/services/customer.service.test.d.ts +0 -1
- package/dist/src/services/customer.service.test.js +0 -430
- package/dist/src/services/employee.service.d.ts +0 -73
- package/dist/src/services/employee.service.js +0 -74
- package/dist/src/services/employee.service.test.d.ts +0 -1
- package/dist/src/services/employee.service.test.js +0 -127
- package/dist/src/services/logger.service.d.ts +0 -31
- package/dist/src/services/logger.service.js +0 -93
- package/dist/src/services/logger.service.test.d.ts +0 -1
- package/dist/src/services/logger.service.test.js +0 -31
- package/dist/src/services/order.service.d.ts +0 -73
- package/dist/src/services/order.service.js +0 -97
- package/dist/src/services/order.service.test.d.ts +0 -1
- package/dist/src/services/order.service.test.js +0 -163
- package/dist/src/services/product.service.d.ts +0 -21
- package/dist/src/services/product.service.js +0 -42
- package/dist/src/services/product.service.test.d.ts +0 -1
- package/dist/src/services/product.service.test.js +0 -61
- package/dist/src/services/smess.service.d.ts +0 -36
- package/dist/src/services/smess.service.js +0 -128
- package/dist/src/services/smess.service.test.d.ts +0 -1
- package/dist/src/services/smess.service.test.js +0 -103
- package/dist/src/services/store.cart.service.d.ts +0 -158
- package/dist/src/services/store.cart.service.js +0 -177
- package/dist/src/services/store.cart.service.test.d.ts +0 -1
- package/dist/src/services/store.cart.service.test.js +0 -394
- package/dist/src/services/store.product.service.d.ts +0 -124
- package/dist/src/services/store.product.service.js +0 -102
- package/dist/src/services/store.product.service.test.d.ts +0 -1
- package/dist/src/services/store.product.service.test.js +0 -319
- package/dist/src/services/store.service.d.ts +0 -79
- package/dist/src/services/store.service.js +0 -235
- package/dist/src/services/store.service.test.d.ts +0 -1
- package/dist/src/services/store.service.test.js +0 -274
- package/dist/src/services/storefront.cart.service.d.ts +0 -73
- package/dist/src/services/storefront.cart.service.js +0 -167
- package/dist/src/services/storefront.cart.service.test.d.ts +0 -1
- package/dist/src/services/storefront.cart.service.test.js +0 -211
- package/dist/src/services/storefront.checkout.service.d.ts +0 -62
- package/dist/src/services/storefront.checkout.service.js +0 -75
- package/dist/src/services/storefront.checkout.service.test.d.ts +0 -1
- package/dist/src/services/storefront.checkout.service.test.js +0 -158
- package/dist/src/services/storefront.customer.metadata.service.d.ts +0 -91
- package/dist/src/services/storefront.customer.metadata.service.js +0 -225
- package/dist/src/services/storefront.customer.metadata.service.test.d.ts +0 -1
- package/dist/src/services/storefront.customer.metadata.service.test.js +0 -271
- package/dist/src/services/storefront.order.service.d.ts +0 -45
- package/dist/src/services/storefront.order.service.js +0 -99
- package/dist/src/services/storefront.order.service.test.d.ts +0 -1
- package/dist/src/services/storefront.order.service.test.js +0 -158
- package/dist/src/services/storefront.product.service.d.ts +0 -52
- package/dist/src/services/storefront.product.service.js +0 -75
- package/dist/src/services/storefront.product.service.test.d.ts +0 -1
- package/dist/src/services/storefront.product.service.test.js +0 -120
- package/dist/src/services/storefront.store.service.d.ts +0 -52
- package/dist/src/services/storefront.store.service.js +0 -140
- package/dist/src/services/storefront.store.service.test.d.ts +0 -1
- package/dist/src/services/storefront.store.service.test.js +0 -90
- package/dist/src/services/user.service.d.ts +0 -16
- package/dist/src/services/user.service.js +0 -37
- package/dist/src/services/user.service.test.d.ts +0 -1
- package/dist/src/services/user.service.test.js +0 -52
- package/dist/src/services/utils/withAuthReset.d.ts +0 -3
- package/dist/src/services/utils/withAuthReset.js +0 -8
- package/dist/src/types/types.d.ts +0 -93
- package/dist/src/types/types.js +0 -82
- package/dist/src/types/types.test.d.ts +0 -1
- package/dist/src/types/types.test.js +0 -25
- package/dist/src/utils/cache-import.d.ts +0 -3
- package/dist/src/utils/cache-import.js +0 -2
- package/dist/src/utils/networking-import.d.ts +0 -4
- package/dist/src/utils/networking-import.js +0 -3
- package/dist/src/utils/rxjs-import.d.ts +0 -5
- package/dist/src/utils/rxjs-import.js +0 -5
- package/dist/types/types.d.ts +0 -96
- package/dist/types/types.js +0 -82
- package/dist/types/types.test.d.ts +0 -1
- package/dist/types/types.test.js +0 -25
- package/dist/utils/cache-import.d.ts +0 -3
- package/dist/utils/cache-import.js +0 -2
- package/dist/utils/networking-import.d.ts +0 -4
- package/dist/utils/networking-import.js +0 -3
- package/dist/utils/rxjs-import.d.ts +0 -5
- package/dist/utils/rxjs-import.js +0 -5
|
@@ -1,197 +0,0 @@
|
|
|
1
|
-
import { Accept, SMESSHeader, SmessService } from "./smess.service";
|
|
2
|
-
import { BehaviorSubject } from "rxjs";
|
|
3
|
-
import { tap } from "rxjs/operators";
|
|
4
|
-
// LocalStorage keys for auth persistence
|
|
5
|
-
const AUTH_TOKEN_KEY = 'smess_auth_token';
|
|
6
|
-
const AUTH_RESPONSE_KEY = 'smess_auth_response';
|
|
7
|
-
export class AuthProvider {
|
|
8
|
-
constructor(config, target, $authToken) {
|
|
9
|
-
this.config = config;
|
|
10
|
-
this.target = target;
|
|
11
|
-
this.$authToken = $authToken;
|
|
12
|
-
}
|
|
13
|
-
get baseURL() {
|
|
14
|
-
return this.config.version
|
|
15
|
-
? `${this.config.baseUrl}/${this.config.version}`
|
|
16
|
-
: this.config.baseUrl;
|
|
17
|
-
}
|
|
18
|
-
get path() {
|
|
19
|
-
switch (this.target) {
|
|
20
|
-
case "login":
|
|
21
|
-
return "/authentication/login_check";
|
|
22
|
-
case "password-forgot":
|
|
23
|
-
return "/authentication/forgot-password";
|
|
24
|
-
case "reset-password":
|
|
25
|
-
return "/authentication/reset-password";
|
|
26
|
-
case "verify-reset-password-token":
|
|
27
|
-
return "/authentication/verify-reset-password-token";
|
|
28
|
-
case "logout":
|
|
29
|
-
return "/authentication/logout";
|
|
30
|
-
case "change-password":
|
|
31
|
-
return "/authentication/change-password";
|
|
32
|
-
default:
|
|
33
|
-
return "/authentication";
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
get method() {
|
|
37
|
-
switch (this.target) {
|
|
38
|
-
default:
|
|
39
|
-
return "POST";
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
get headers() {
|
|
43
|
-
let authHeaders = {};
|
|
44
|
-
switch (this.target) {
|
|
45
|
-
case "login":
|
|
46
|
-
case "password-forgot":
|
|
47
|
-
case "reset-password":
|
|
48
|
-
case "verify-reset-password-token":
|
|
49
|
-
authHeaders = SmessService.getHeaders(Accept.V1);
|
|
50
|
-
break;
|
|
51
|
-
default:
|
|
52
|
-
authHeaders = SmessService.getHeaders(Accept.V1, this.$authToken.value);
|
|
53
|
-
}
|
|
54
|
-
if (this.config.clientId) {
|
|
55
|
-
authHeaders[SMESSHeader.ClientId] = this.config.clientId;
|
|
56
|
-
}
|
|
57
|
-
if (this.config.apiKey) {
|
|
58
|
-
authHeaders[SMESSHeader.ApiKey] = this.config.apiKey;
|
|
59
|
-
}
|
|
60
|
-
return SmessService.filterHeaders(authHeaders);
|
|
61
|
-
}
|
|
62
|
-
getPath(...queryParams) {
|
|
63
|
-
let definedPath = this.path;
|
|
64
|
-
const params = definedPath.match(/\$\{(.+?)\}/g);
|
|
65
|
-
params?.forEach((value, index) => {
|
|
66
|
-
definedPath = definedPath.replace(value, String(queryParams[index]));
|
|
67
|
-
});
|
|
68
|
-
return definedPath;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
export class AuthService {
|
|
72
|
-
constructor(config) {
|
|
73
|
-
this.config = config;
|
|
74
|
-
this.$_authToken = new BehaviorSubject(null);
|
|
75
|
-
this.loadAuthFromStorage();
|
|
76
|
-
}
|
|
77
|
-
// Getter that returns BehaviorSubject with token validation
|
|
78
|
-
get $authToken() {
|
|
79
|
-
const currentToken = this.getValidToken();
|
|
80
|
-
if (currentToken !== this.$_authToken.value) {
|
|
81
|
-
this.$_authToken.next(currentToken);
|
|
82
|
-
}
|
|
83
|
-
return this.$_authToken;
|
|
84
|
-
}
|
|
85
|
-
// Get current auth token value
|
|
86
|
-
get currentAuthToken() {
|
|
87
|
-
return this.getValidToken();
|
|
88
|
-
}
|
|
89
|
-
// Get valid token from memory or localStorage
|
|
90
|
-
getValidToken() {
|
|
91
|
-
// If we have a token in memory, return it (assume it's valid)
|
|
92
|
-
if (this.$_authToken.value) {
|
|
93
|
-
return this.$_authToken.value;
|
|
94
|
-
}
|
|
95
|
-
// Otherwise, try to load from localStorage
|
|
96
|
-
return this.loadFromStorage();
|
|
97
|
-
}
|
|
98
|
-
// Load token from localStorage and check expiration
|
|
99
|
-
loadFromStorage() {
|
|
100
|
-
try {
|
|
101
|
-
const storedResponse = localStorage.getItem(AUTH_RESPONSE_KEY);
|
|
102
|
-
const storedAt = localStorage.getItem(`${AUTH_RESPONSE_KEY}_stored_at`);
|
|
103
|
-
if (storedResponse && storedAt) {
|
|
104
|
-
const authResponse = JSON.parse(storedResponse);
|
|
105
|
-
const storedTime = parseInt(storedAt, 10);
|
|
106
|
-
// Check if token is expired
|
|
107
|
-
if (authResponse.expires_in && typeof authResponse.expires_in === 'number') {
|
|
108
|
-
const expirationTime = storedTime + (authResponse.expires_in * 1000);
|
|
109
|
-
if (Date.now() >= expirationTime) {
|
|
110
|
-
// Token expired, clear storage
|
|
111
|
-
this.clearAuthFromStorage();
|
|
112
|
-
return null;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
// Token is valid, store in memory and return
|
|
116
|
-
const token = String(authResponse.token);
|
|
117
|
-
this.$_authToken.next(token);
|
|
118
|
-
return token;
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
catch (error) {
|
|
122
|
-
console.warn('Failed to load token from localStorage:', error);
|
|
123
|
-
this.clearAuthFromStorage();
|
|
124
|
-
}
|
|
125
|
-
return null;
|
|
126
|
-
}
|
|
127
|
-
// Load auth token from localStorage on initialization
|
|
128
|
-
loadAuthFromStorage() {
|
|
129
|
-
this.loadFromStorage();
|
|
130
|
-
}
|
|
131
|
-
// Save auth response to localStorage
|
|
132
|
-
saveAuthToStorage(authResponse) {
|
|
133
|
-
try {
|
|
134
|
-
localStorage.setItem(AUTH_TOKEN_KEY, String(authResponse.token));
|
|
135
|
-
localStorage.setItem(AUTH_RESPONSE_KEY, JSON.stringify(authResponse));
|
|
136
|
-
localStorage.setItem(`${AUTH_RESPONSE_KEY}_stored_at`, Date.now().toString());
|
|
137
|
-
// Update memory
|
|
138
|
-
this.$_authToken.next(String(authResponse.token));
|
|
139
|
-
}
|
|
140
|
-
catch (error) {
|
|
141
|
-
console.warn('Failed to save auth to localStorage:', error);
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
// Clear auth from localStorage
|
|
145
|
-
clearAuthFromStorage() {
|
|
146
|
-
try {
|
|
147
|
-
localStorage.removeItem(AUTH_TOKEN_KEY);
|
|
148
|
-
localStorage.removeItem(AUTH_RESPONSE_KEY);
|
|
149
|
-
localStorage.removeItem(`${AUTH_RESPONSE_KEY}_stored_at`);
|
|
150
|
-
// Clear memory
|
|
151
|
-
this.$_authToken.next(null);
|
|
152
|
-
}
|
|
153
|
-
catch (error) {
|
|
154
|
-
console.warn('Failed to clear auth from localStorage:', error);
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
provider(target) {
|
|
158
|
-
const targetWrapper = new AuthProvider(this.config, target, this.$authToken);
|
|
159
|
-
return new SmessService(targetWrapper);
|
|
160
|
-
}
|
|
161
|
-
login(payload) {
|
|
162
|
-
return this.provider("login")
|
|
163
|
-
.request(payload)
|
|
164
|
-
.pipe(tap((result) => {
|
|
165
|
-
if (result.hasSuccess && result.response?.token) {
|
|
166
|
-
// Set the auth token when login is successful
|
|
167
|
-
this.$_authToken.next(String(result.response.token));
|
|
168
|
-
// Save to localStorage if rememberMe is true
|
|
169
|
-
if (payload.rememberMe && result.response) {
|
|
170
|
-
this.saveAuthToStorage(result.response);
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
}));
|
|
174
|
-
}
|
|
175
|
-
logout() {
|
|
176
|
-
return this.provider("logout")
|
|
177
|
-
.request()
|
|
178
|
-
.pipe(tap((result) => {
|
|
179
|
-
if (result.hasSuccess) {
|
|
180
|
-
// Clear from localStorage (this will also clear memory)
|
|
181
|
-
this.clearAuthFromStorage();
|
|
182
|
-
}
|
|
183
|
-
}));
|
|
184
|
-
}
|
|
185
|
-
forgotPassword(payload) {
|
|
186
|
-
return this.provider("password-forgot").request(payload);
|
|
187
|
-
}
|
|
188
|
-
changePassword(payload) {
|
|
189
|
-
return this.provider("change-password").request(payload);
|
|
190
|
-
}
|
|
191
|
-
resetPassword(payload) {
|
|
192
|
-
return this.provider("reset-password").request(payload);
|
|
193
|
-
}
|
|
194
|
-
verifyResetPasswordToken(payload) {
|
|
195
|
-
return this.provider("verify-reset-password-token").request(payload);
|
|
196
|
-
}
|
|
197
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,340 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @jest-environment jsdom
|
|
3
|
-
*/
|
|
4
|
-
import { AuthService, } from "./auth.service";
|
|
5
|
-
import { NetworkingUIEventWrapped, AlertModel, } from "../types/types";
|
|
6
|
-
import { of } from "rxjs";
|
|
7
|
-
describe("AuthService", () => {
|
|
8
|
-
const config = {
|
|
9
|
-
baseUrl: "https://api.example.com",
|
|
10
|
-
isProd: false,
|
|
11
|
-
clientId: "test-client-id",
|
|
12
|
-
apiKey: "test-api-key",
|
|
13
|
-
};
|
|
14
|
-
let service;
|
|
15
|
-
let mockLocalStorage;
|
|
16
|
-
beforeEach(() => {
|
|
17
|
-
// Mock localStorage for all tests
|
|
18
|
-
mockLocalStorage = {
|
|
19
|
-
setItem: jest.fn(),
|
|
20
|
-
getItem: jest.fn(),
|
|
21
|
-
removeItem: jest.fn(),
|
|
22
|
-
};
|
|
23
|
-
Object.defineProperty(window, 'localStorage', {
|
|
24
|
-
value: mockLocalStorage,
|
|
25
|
-
writable: true,
|
|
26
|
-
});
|
|
27
|
-
service = new AuthService(config);
|
|
28
|
-
});
|
|
29
|
-
it("login success", async () => {
|
|
30
|
-
const payload = { identifier: "user", password: "pass" };
|
|
31
|
-
const response = {
|
|
32
|
-
token_type: "Bearer",
|
|
33
|
-
expires_in: 3600,
|
|
34
|
-
token: "token",
|
|
35
|
-
refresh_token: "refresh",
|
|
36
|
-
};
|
|
37
|
-
jest.spyOn(service, "provider").mockReturnValue({
|
|
38
|
-
request: () => of(NetworkingUIEventWrapped.succeeded(response)),
|
|
39
|
-
});
|
|
40
|
-
const result = await service.login(payload).toPromise();
|
|
41
|
-
expect(result?.hasSuccess).toBe(true);
|
|
42
|
-
expect(result?.response).toEqual(response);
|
|
43
|
-
// Verify that auth token is set after successful login
|
|
44
|
-
expect(service.currentAuthToken).toBe("token");
|
|
45
|
-
expect(service.$authToken.value).toBe("token");
|
|
46
|
-
});
|
|
47
|
-
it("login error", async () => {
|
|
48
|
-
const payload = { identifier: "user", password: "wrong" };
|
|
49
|
-
jest.spyOn(service, "provider").mockReturnValue({
|
|
50
|
-
request: () => of(NetworkingUIEventWrapped.failed(AlertModel.default())),
|
|
51
|
-
});
|
|
52
|
-
const result = await service.login(payload).toPromise();
|
|
53
|
-
expect(result?.hasFailed).toBe(true);
|
|
54
|
-
expect(result?.alertModel).toBeInstanceOf(AlertModel);
|
|
55
|
-
});
|
|
56
|
-
it("login success with rememberMe", async () => {
|
|
57
|
-
const payload = {
|
|
58
|
-
identifier: "user",
|
|
59
|
-
password: "pass",
|
|
60
|
-
rememberMe: true
|
|
61
|
-
};
|
|
62
|
-
const response = {
|
|
63
|
-
token_type: "Bearer",
|
|
64
|
-
expires_in: 3600,
|
|
65
|
-
token: "token",
|
|
66
|
-
refresh_token: "refresh",
|
|
67
|
-
};
|
|
68
|
-
jest.spyOn(service, "provider").mockReturnValue({
|
|
69
|
-
request: () => of(NetworkingUIEventWrapped.succeeded(response)),
|
|
70
|
-
});
|
|
71
|
-
const result = await service.login(payload).toPromise();
|
|
72
|
-
expect(result?.hasSuccess).toBe(true);
|
|
73
|
-
expect(result?.response).toEqual(response);
|
|
74
|
-
// Verify that auth token is set after successful login
|
|
75
|
-
expect(service.currentAuthToken).toBe("token");
|
|
76
|
-
expect(service.$authToken.value).toBe("token");
|
|
77
|
-
// Verify that localStorage was called to save the auth data
|
|
78
|
-
expect(mockLocalStorage.setItem).toHaveBeenCalledWith('smess_auth_token', 'token');
|
|
79
|
-
expect(mockLocalStorage.setItem).toHaveBeenCalledWith('smess_auth_response', JSON.stringify(response));
|
|
80
|
-
});
|
|
81
|
-
it("login success without rememberMe", async () => {
|
|
82
|
-
const payload = {
|
|
83
|
-
identifier: "user",
|
|
84
|
-
password: "pass",
|
|
85
|
-
rememberMe: false
|
|
86
|
-
};
|
|
87
|
-
const response = {
|
|
88
|
-
token_type: "Bearer",
|
|
89
|
-
expires_in: 3600,
|
|
90
|
-
token: "token",
|
|
91
|
-
refresh_token: "refresh",
|
|
92
|
-
};
|
|
93
|
-
jest.spyOn(service, "provider").mockReturnValue({
|
|
94
|
-
request: () => of(NetworkingUIEventWrapped.succeeded(response)),
|
|
95
|
-
});
|
|
96
|
-
const result = await service.login(payload).toPromise();
|
|
97
|
-
expect(result?.hasSuccess).toBe(true);
|
|
98
|
-
expect(result?.response).toEqual(response);
|
|
99
|
-
// Verify that auth token is set after successful login
|
|
100
|
-
expect(service.currentAuthToken).toBe("token");
|
|
101
|
-
expect(service.$authToken.value).toBe("token");
|
|
102
|
-
// Verify that localStorage was NOT called to save the auth data
|
|
103
|
-
expect(mockLocalStorage.setItem).not.toHaveBeenCalled();
|
|
104
|
-
});
|
|
105
|
-
it("logout success", async () => {
|
|
106
|
-
// First set a token to test clearing it
|
|
107
|
-
service.$authToken.next("test-token");
|
|
108
|
-
expect(service.currentAuthToken).toBe("test-token");
|
|
109
|
-
jest.spyOn(service, "provider").mockReturnValue({
|
|
110
|
-
request: () => of(NetworkingUIEventWrapped.succeeded(undefined)),
|
|
111
|
-
});
|
|
112
|
-
const result = await service.logout().toPromise();
|
|
113
|
-
expect(result?.hasSuccess).toBe(true);
|
|
114
|
-
// Verify that auth token is cleared after successful logout
|
|
115
|
-
expect(service.currentAuthToken).toBe(null);
|
|
116
|
-
expect(service.$authToken.value).toBe(null);
|
|
117
|
-
// Verify that localStorage was cleared
|
|
118
|
-
expect(mockLocalStorage.removeItem).toHaveBeenCalledWith('smess_auth_token');
|
|
119
|
-
expect(mockLocalStorage.removeItem).toHaveBeenCalledWith('smess_auth_response');
|
|
120
|
-
});
|
|
121
|
-
it("loads auth token from localStorage on initialization", () => {
|
|
122
|
-
// Mock localStorage to return a stored auth response
|
|
123
|
-
const storedResponse = {
|
|
124
|
-
token_type: "Bearer",
|
|
125
|
-
expires_in: 3600,
|
|
126
|
-
token: "stored-token",
|
|
127
|
-
refresh_token: "refresh",
|
|
128
|
-
};
|
|
129
|
-
const mockLocalStorage = {
|
|
130
|
-
setItem: jest.fn(),
|
|
131
|
-
getItem: jest.fn().mockImplementation((key) => {
|
|
132
|
-
if (key === 'smess_auth_response') {
|
|
133
|
-
return JSON.stringify(storedResponse);
|
|
134
|
-
}
|
|
135
|
-
if (key === 'smess_auth_response_stored_at') {
|
|
136
|
-
return Date.now().toString();
|
|
137
|
-
}
|
|
138
|
-
return null;
|
|
139
|
-
}),
|
|
140
|
-
removeItem: jest.fn(),
|
|
141
|
-
};
|
|
142
|
-
Object.defineProperty(window, 'localStorage', {
|
|
143
|
-
value: mockLocalStorage,
|
|
144
|
-
writable: true,
|
|
145
|
-
});
|
|
146
|
-
// Create a new service instance
|
|
147
|
-
const newService = new AuthService(config);
|
|
148
|
-
// Verify that the stored token was loaded
|
|
149
|
-
expect(newService.currentAuthToken).toBe('stored-token');
|
|
150
|
-
expect(newService.$authToken.value).toBe('stored-token');
|
|
151
|
-
expect(mockLocalStorage.getItem).toHaveBeenCalledWith('smess_auth_response');
|
|
152
|
-
});
|
|
153
|
-
it("handles localStorage errors gracefully", () => {
|
|
154
|
-
// Mock localStorage to throw an error
|
|
155
|
-
const mockLocalStorage = {
|
|
156
|
-
setItem: jest.fn().mockImplementation(() => {
|
|
157
|
-
throw new Error('localStorage error');
|
|
158
|
-
}),
|
|
159
|
-
getItem: jest.fn().mockImplementation(() => {
|
|
160
|
-
throw new Error('localStorage error');
|
|
161
|
-
}),
|
|
162
|
-
removeItem: jest.fn().mockImplementation(() => {
|
|
163
|
-
throw new Error('localStorage error');
|
|
164
|
-
}),
|
|
165
|
-
};
|
|
166
|
-
Object.defineProperty(window, 'localStorage', {
|
|
167
|
-
value: mockLocalStorage,
|
|
168
|
-
writable: true,
|
|
169
|
-
});
|
|
170
|
-
// Should not throw an error during initialization
|
|
171
|
-
expect(() => new AuthService(config)).not.toThrow();
|
|
172
|
-
// Should not throw an error during login with rememberMe
|
|
173
|
-
const service = new AuthService(config);
|
|
174
|
-
const payload = {
|
|
175
|
-
identifier: "user",
|
|
176
|
-
password: "pass",
|
|
177
|
-
rememberMe: true
|
|
178
|
-
};
|
|
179
|
-
const response = {
|
|
180
|
-
token_type: "Bearer",
|
|
181
|
-
expires_in: 3600,
|
|
182
|
-
token: "token",
|
|
183
|
-
refresh_token: "refresh",
|
|
184
|
-
};
|
|
185
|
-
jest.spyOn(service, "provider").mockReturnValue({
|
|
186
|
-
request: () => of(NetworkingUIEventWrapped.succeeded(response)),
|
|
187
|
-
});
|
|
188
|
-
expect(async () => {
|
|
189
|
-
await service.login(payload).toPromise();
|
|
190
|
-
}).not.toThrow();
|
|
191
|
-
});
|
|
192
|
-
it("handles expired token from localStorage", () => {
|
|
193
|
-
// Mock localStorage with expired token
|
|
194
|
-
const expiredResponse = {
|
|
195
|
-
token_type: "Bearer",
|
|
196
|
-
expires_in: 1, // 1 second
|
|
197
|
-
token: "expired-token",
|
|
198
|
-
refresh_token: "refresh",
|
|
199
|
-
};
|
|
200
|
-
mockLocalStorage.getItem.mockImplementation((key) => {
|
|
201
|
-
if (key === 'smess_auth_response') {
|
|
202
|
-
return JSON.stringify(expiredResponse);
|
|
203
|
-
}
|
|
204
|
-
if (key === 'smess_auth_response_stored_at') {
|
|
205
|
-
// Set stored time to 2 seconds ago (expired)
|
|
206
|
-
return (Date.now() - 2000).toString();
|
|
207
|
-
}
|
|
208
|
-
return null;
|
|
209
|
-
});
|
|
210
|
-
// Create a new service instance
|
|
211
|
-
const newService = new AuthService(config);
|
|
212
|
-
// Verify that expired token is not returned
|
|
213
|
-
expect(newService.currentAuthToken).toBe(null);
|
|
214
|
-
expect(newService.$authToken.value).toBe(null);
|
|
215
|
-
// Verify that localStorage was cleared
|
|
216
|
-
expect(mockLocalStorage.removeItem).toHaveBeenCalledWith('smess_auth_token');
|
|
217
|
-
expect(mockLocalStorage.removeItem).toHaveBeenCalledWith('smess_auth_response');
|
|
218
|
-
expect(mockLocalStorage.removeItem).toHaveBeenCalledWith('smess_auth_response_stored_at');
|
|
219
|
-
});
|
|
220
|
-
it("handles valid token from localStorage", () => {
|
|
221
|
-
// Mock localStorage with valid token
|
|
222
|
-
const validResponse = {
|
|
223
|
-
token_type: "Bearer",
|
|
224
|
-
expires_in: 3600, // 1 hour
|
|
225
|
-
token: "valid-token",
|
|
226
|
-
refresh_token: "refresh",
|
|
227
|
-
};
|
|
228
|
-
mockLocalStorage.getItem.mockImplementation((key) => {
|
|
229
|
-
if (key === 'smess_auth_response') {
|
|
230
|
-
return JSON.stringify(validResponse);
|
|
231
|
-
}
|
|
232
|
-
if (key === 'smess_auth_response_stored_at') {
|
|
233
|
-
// Set stored time to 30 minutes ago (still valid)
|
|
234
|
-
return (Date.now() - 1800000).toString();
|
|
235
|
-
}
|
|
236
|
-
return null;
|
|
237
|
-
});
|
|
238
|
-
// Create a new service instance
|
|
239
|
-
const newService = new AuthService(config);
|
|
240
|
-
// Verify that valid token is returned
|
|
241
|
-
expect(newService.currentAuthToken).toBe("valid-token");
|
|
242
|
-
expect(newService.$authToken.value).toBe("valid-token");
|
|
243
|
-
});
|
|
244
|
-
it("forgotPassword success", async () => {
|
|
245
|
-
const payload = { identifier: "user" };
|
|
246
|
-
const response = { data: "ok" };
|
|
247
|
-
jest.spyOn(service, "provider").mockReturnValue({
|
|
248
|
-
request: () => of(NetworkingUIEventWrapped.succeeded(response)),
|
|
249
|
-
});
|
|
250
|
-
const result = await service.forgotPassword(payload).toPromise();
|
|
251
|
-
expect(result?.hasSuccess).toBe(true);
|
|
252
|
-
expect(result?.response).toEqual(response);
|
|
253
|
-
});
|
|
254
|
-
it("changePassword success", async () => {
|
|
255
|
-
const payload = {
|
|
256
|
-
old_password: "old",
|
|
257
|
-
password: "new",
|
|
258
|
-
};
|
|
259
|
-
const response = { data: "changed" };
|
|
260
|
-
jest.spyOn(service, "provider").mockReturnValue({
|
|
261
|
-
request: () => of(NetworkingUIEventWrapped.succeeded(response)),
|
|
262
|
-
});
|
|
263
|
-
const result = await service.changePassword(payload).toPromise();
|
|
264
|
-
expect(result?.hasSuccess).toBe(true);
|
|
265
|
-
expect(result?.response).toEqual(response);
|
|
266
|
-
});
|
|
267
|
-
it("resetPassword success", async () => {
|
|
268
|
-
const payload = {
|
|
269
|
-
token: "token",
|
|
270
|
-
password: "newpass",
|
|
271
|
-
};
|
|
272
|
-
const response = { data: "reset" };
|
|
273
|
-
jest.spyOn(service, "provider").mockReturnValue({
|
|
274
|
-
request: () => of(NetworkingUIEventWrapped.succeeded(response)),
|
|
275
|
-
});
|
|
276
|
-
const result = await service.resetPassword(payload).toPromise();
|
|
277
|
-
expect(result?.hasSuccess).toBe(true);
|
|
278
|
-
expect(result?.response).toEqual(response);
|
|
279
|
-
});
|
|
280
|
-
it("verifyResetPasswordToken success", async () => {
|
|
281
|
-
const token = "token";
|
|
282
|
-
const response = {
|
|
283
|
-
token_type: "Bearer",
|
|
284
|
-
expires_in: 3600,
|
|
285
|
-
token: "token",
|
|
286
|
-
refresh_token: "refresh",
|
|
287
|
-
};
|
|
288
|
-
jest.spyOn(service, "provider").mockReturnValue({
|
|
289
|
-
request: () => of(NetworkingUIEventWrapped.succeeded(response)),
|
|
290
|
-
});
|
|
291
|
-
const result = await service.verifyResetPasswordToken({
|
|
292
|
-
token,
|
|
293
|
-
new_password: 'newpass',
|
|
294
|
-
}).toPromise();
|
|
295
|
-
expect(result?.hasSuccess).toBe(true);
|
|
296
|
-
expect(result?.response).toEqual(response);
|
|
297
|
-
});
|
|
298
|
-
it("forgotPassword error", async () => {
|
|
299
|
-
const payload = { identifier: "user" };
|
|
300
|
-
jest.spyOn(service, "provider").mockReturnValue({
|
|
301
|
-
request: () => of(NetworkingUIEventWrapped.failed(AlertModel.default())),
|
|
302
|
-
});
|
|
303
|
-
const result = await service.forgotPassword(payload).toPromise();
|
|
304
|
-
expect(result?.hasFailed).toBe(true);
|
|
305
|
-
expect(result?.alertModel).toBeInstanceOf(AlertModel);
|
|
306
|
-
});
|
|
307
|
-
it("changePassword error", async () => {
|
|
308
|
-
const payload = {
|
|
309
|
-
old_password: "old",
|
|
310
|
-
password: "bad",
|
|
311
|
-
};
|
|
312
|
-
jest.spyOn(service, "provider").mockReturnValue({
|
|
313
|
-
request: () => of(NetworkingUIEventWrapped.failed(AlertModel.default())),
|
|
314
|
-
});
|
|
315
|
-
const result = await service.changePassword(payload).toPromise();
|
|
316
|
-
expect(result?.hasFailed).toBe(true);
|
|
317
|
-
expect(result?.alertModel).toBeInstanceOf(AlertModel);
|
|
318
|
-
});
|
|
319
|
-
it("resetPassword error", async () => {
|
|
320
|
-
const payload = { token: "bad", password: "bad" };
|
|
321
|
-
jest.spyOn(service, "provider").mockReturnValue({
|
|
322
|
-
request: () => of(NetworkingUIEventWrapped.failed(AlertModel.default())),
|
|
323
|
-
});
|
|
324
|
-
const result = await service.resetPassword(payload).toPromise();
|
|
325
|
-
expect(result?.hasFailed).toBe(true);
|
|
326
|
-
expect(result?.alertModel).toBeInstanceOf(AlertModel);
|
|
327
|
-
});
|
|
328
|
-
it("verifyResetPasswordToken error", async () => {
|
|
329
|
-
const token = "bad";
|
|
330
|
-
jest.spyOn(service, "provider").mockReturnValue({
|
|
331
|
-
request: () => of(NetworkingUIEventWrapped.failed(AlertModel.default())),
|
|
332
|
-
});
|
|
333
|
-
const result = await service.verifyResetPasswordToken({
|
|
334
|
-
token,
|
|
335
|
-
new_password: 'newpass',
|
|
336
|
-
}).toPromise();
|
|
337
|
-
expect(result?.hasFailed).toBe(true);
|
|
338
|
-
expect(result?.alertModel).toBeInstanceOf(AlertModel);
|
|
339
|
-
});
|
|
340
|
-
});
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { Dexie, Table } from "../utils/cache-import";
|
|
2
|
-
export interface CacheMetadata {
|
|
3
|
-
key: string;
|
|
4
|
-
cachedAt: number;
|
|
5
|
-
expiresAt: number;
|
|
6
|
-
version: string;
|
|
7
|
-
}
|
|
8
|
-
export declare class GenericDB<T> extends Dexie {
|
|
9
|
-
data: Table<T>;
|
|
10
|
-
cache: Table<CacheMetadata>;
|
|
11
|
-
constructor(name: string, dataStoreSchema: string);
|
|
12
|
-
}
|
|
13
|
-
export declare class GenericIndexedDBService<T, TCached> {
|
|
14
|
-
private toCached;
|
|
15
|
-
private fromCached;
|
|
16
|
-
private readonly CACHE_DURATION;
|
|
17
|
-
readonly db: GenericDB<TCached>;
|
|
18
|
-
constructor(dbName: string, storeSchema: string, toCached: (raw: T) => TCached, fromCached: (cached: TCached) => T);
|
|
19
|
-
saveAll(items: T[], cacheKey: string): Promise<void>;
|
|
20
|
-
getAll(cacheKey: string): Promise<T[] | null>;
|
|
21
|
-
saveOne(item: T): Promise<void>;
|
|
22
|
-
getOne(id: string): Promise<T | null>;
|
|
23
|
-
delete(id: string): Promise<void>;
|
|
24
|
-
clearAll(): Promise<void>;
|
|
25
|
-
getCacheInfo(key: string): Promise<{
|
|
26
|
-
count: number;
|
|
27
|
-
lastUpdated?: Date;
|
|
28
|
-
}>;
|
|
29
|
-
setStats(key: string, stats: unknown): Promise<void>;
|
|
30
|
-
getStats(key: string): Promise<unknown | null>;
|
|
31
|
-
}
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import { Dexie } from "../utils/cache-import";
|
|
2
|
-
export class GenericDB extends Dexie {
|
|
3
|
-
constructor(name, dataStoreSchema) {
|
|
4
|
-
super(name);
|
|
5
|
-
this.version(1).stores({
|
|
6
|
-
data: dataStoreSchema,
|
|
7
|
-
cache: "key, cachedAt, expiresAt",
|
|
8
|
-
});
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
export class GenericIndexedDBService {
|
|
12
|
-
constructor(dbName, storeSchema, toCached, fromCached) {
|
|
13
|
-
this.toCached = toCached;
|
|
14
|
-
this.fromCached = fromCached;
|
|
15
|
-
this.CACHE_DURATION = 24 * 60 * 60 * 1000; // 24 hours
|
|
16
|
-
this.db = new GenericDB(dbName, storeSchema);
|
|
17
|
-
}
|
|
18
|
-
async saveAll(items, cacheKey) {
|
|
19
|
-
const cachedItems = items.map(this.toCached);
|
|
20
|
-
await this.db.transaction("rw", this.db.data, this.db.cache, async () => {
|
|
21
|
-
await this.db.data.clear();
|
|
22
|
-
await this.db.data.bulkAdd(cachedItems);
|
|
23
|
-
await this.db.cache.put({
|
|
24
|
-
key: cacheKey,
|
|
25
|
-
cachedAt: Date.now(),
|
|
26
|
-
expiresAt: Date.now() + this.CACHE_DURATION,
|
|
27
|
-
version: "1.0",
|
|
28
|
-
});
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
async getAll(cacheKey) {
|
|
32
|
-
const cacheInfo = await this.db.cache.get(cacheKey);
|
|
33
|
-
if (!cacheInfo || Date.now() > cacheInfo.expiresAt)
|
|
34
|
-
return null;
|
|
35
|
-
const cachedItems = await this.db.data.toArray();
|
|
36
|
-
return cachedItems.map(this.fromCached);
|
|
37
|
-
}
|
|
38
|
-
async saveOne(item) {
|
|
39
|
-
const cached = this.toCached(item);
|
|
40
|
-
await this.db.data.put(cached);
|
|
41
|
-
}
|
|
42
|
-
async getOne(id) {
|
|
43
|
-
const cached = await this.db.data.get(id);
|
|
44
|
-
return cached ? this.fromCached(cached) : null;
|
|
45
|
-
}
|
|
46
|
-
async delete(id) {
|
|
47
|
-
await this.db.data.delete(id);
|
|
48
|
-
}
|
|
49
|
-
async clearAll() {
|
|
50
|
-
await this.db.transaction("rw", this.db.data, this.db.cache, async () => {
|
|
51
|
-
await this.db.data.clear();
|
|
52
|
-
await this.db.cache.clear();
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
async getCacheInfo(key) {
|
|
56
|
-
const count = await this.db.data.count();
|
|
57
|
-
const cacheInfo = await this.db.cache.get(key);
|
|
58
|
-
return {
|
|
59
|
-
count,
|
|
60
|
-
lastUpdated: cacheInfo ? new Date(cacheInfo.cachedAt) : undefined,
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
async setStats(key, stats) {
|
|
64
|
-
await this.db.cache.put({
|
|
65
|
-
key,
|
|
66
|
-
cachedAt: Date.now(),
|
|
67
|
-
expiresAt: Date.now() + this.CACHE_DURATION,
|
|
68
|
-
version: JSON.stringify(stats),
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
async getStats(key) {
|
|
72
|
-
const cacheInfo = await this.db.cache.get(key);
|
|
73
|
-
if (!cacheInfo || Date.now() > cacheInfo.expiresAt)
|
|
74
|
-
return null;
|
|
75
|
-
return JSON.parse(cacheInfo.version);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import "fake-indexeddb/auto";
|