@zeniai/client-epic-state 4.18.82 → 4.18.83
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/lib/init.d.ts +2 -1
- package/lib/init.js +3 -2
- package/lib/zeniAPI.d.ts +12 -1
- package/lib/zeniAPI.js +33 -6
- package/package.json +1 -1
package/lib/init.d.ts
CHANGED
|
@@ -11,8 +11,9 @@ declare let pusher: Pusher;
|
|
|
11
11
|
* Call initialize before using any of the functionalities in this module
|
|
12
12
|
* Note: This version SHOULD BE used only in the client side. Not intended to be used
|
|
13
13
|
* when using from server side.
|
|
14
|
+
* @param onSessionInvalidated Optional callback invoked when a 401 "Invalid session" response is detected
|
|
14
15
|
*/
|
|
15
|
-
export default function initialize(restEndPoints: RESTAPIEndpoints, userId?: string, sessionId?: string, tenantId?: string, isSandboxEnv?: boolean): ZeniStore;
|
|
16
|
+
export default function initialize(restEndPoints: RESTAPIEndpoints, userId?: string, sessionId?: string, tenantId?: string, isSandboxEnv?: boolean, onSessionInvalidated?: () => void): ZeniStore;
|
|
16
17
|
export interface PusherAuthPayload {
|
|
17
18
|
auth: ID;
|
|
18
19
|
}
|
package/lib/init.js
CHANGED
|
@@ -29,8 +29,9 @@ let initialized = false;
|
|
|
29
29
|
* Call initialize before using any of the functionalities in this module
|
|
30
30
|
* Note: This version SHOULD BE used only in the client side. Not intended to be used
|
|
31
31
|
* when using from server side.
|
|
32
|
+
* @param onSessionInvalidated Optional callback invoked when a 401 "Invalid session" response is detected
|
|
32
33
|
*/
|
|
33
|
-
function initialize(restEndPoints, userId, sessionId, tenantId, isSandboxEnv) {
|
|
34
|
+
function initialize(restEndPoints, userId, sessionId, tenantId, isSandboxEnv, onSessionInvalidated) {
|
|
34
35
|
if (initialized) {
|
|
35
36
|
console.warn(`Already initialized with url ${JSON.stringify(exports.zeniAPI.apiEndPoints)}`);
|
|
36
37
|
return exports.store;
|
|
@@ -38,7 +39,7 @@ function initialize(restEndPoints, userId, sessionId, tenantId, isSandboxEnv) {
|
|
|
38
39
|
initialized = true;
|
|
39
40
|
console.info(`initializing with ${JSON.stringify(restEndPoints)}`);
|
|
40
41
|
initialize3rdPartyExtensions();
|
|
41
|
-
exports.zeniAPI = new zeniAPI_1.ZeniAPIClient(restEndPoints, userId, sessionId, tenantId);
|
|
42
|
+
exports.zeniAPI = new zeniAPI_1.ZeniAPIClient(restEndPoints, userId, sessionId, tenantId, onSessionInvalidated);
|
|
42
43
|
exports.store = (0, configureStore_1.default)(exports.zeniAPI, isSandboxEnv);
|
|
43
44
|
return exports.store;
|
|
44
45
|
}
|
package/lib/zeniAPI.d.ts
CHANGED
|
@@ -41,8 +41,19 @@ export declare class ZeniAPIClient implements ZeniAPI {
|
|
|
41
41
|
userId?: string | undefined;
|
|
42
42
|
sessionId?: string | undefined;
|
|
43
43
|
tenantId?: string | undefined;
|
|
44
|
-
|
|
44
|
+
private readonly onSessionInvalidated?;
|
|
45
|
+
private sessionInvalidationTriggered;
|
|
46
|
+
constructor(apiEndPoints: RESTAPIEndpoints, userId?: string | undefined, sessionId?: string | undefined, tenantId?: string | undefined, onSessionInvalidated?: (() => void) | undefined);
|
|
45
47
|
update(userId?: string, sessionId?: string, tenantId?: string): void;
|
|
48
|
+
/**
|
|
49
|
+
* Check if the API response indicates an invalid session (401).
|
|
50
|
+
* If so, trigger the session invalidation callback once.
|
|
51
|
+
*/
|
|
52
|
+
private checkSessionValidity;
|
|
53
|
+
/**
|
|
54
|
+
* Parse JSON response and check for session validity.
|
|
55
|
+
*/
|
|
56
|
+
private processJsonResponse;
|
|
46
57
|
getJSON<T>(restEndPoint: string, headers?: Record<string, unknown>, signal?: AbortSignal): Observable<T>;
|
|
47
58
|
postJSON(restEndPoint: string, body?: Record<string, unknown>, headers?: Record<string, unknown>, signal?: AbortSignal): Observable<FetchResponse>;
|
|
48
59
|
postAndGetJSON<T>(restEndPoint: string, body?: Record<string, unknown>, headers?: Record<string, unknown>, signal?: AbortSignal): Observable<T>;
|
package/lib/zeniAPI.js
CHANGED
|
@@ -3,12 +3,15 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.ZeniAPIClient = void 0;
|
|
4
4
|
const cross_fetch_1 = require("cross-fetch");
|
|
5
5
|
const rxjs_1 = require("rxjs");
|
|
6
|
+
const responsePayload_1 = require("./responsePayload");
|
|
6
7
|
class ZeniAPIClient {
|
|
7
|
-
constructor(apiEndPoints, userId, sessionId, tenantId) {
|
|
8
|
+
constructor(apiEndPoints, userId, sessionId, tenantId, onSessionInvalidated) {
|
|
8
9
|
this.apiEndPoints = apiEndPoints;
|
|
9
10
|
this.userId = userId;
|
|
10
11
|
this.sessionId = sessionId;
|
|
11
12
|
this.tenantId = tenantId;
|
|
13
|
+
this.onSessionInvalidated = onSessionInvalidated;
|
|
14
|
+
this.sessionInvalidationTriggered = false;
|
|
12
15
|
this.apiHeaders = (headers) => Object.assign({ 'Content-Type': 'application/json' }, this.userId != null ? { 'zeni-user-id': this.userId } : {}, this.sessionId != null ? { 'zeni-session-id': this.sessionId } : {}, this.tenantId != null ? { 'zeni-tenant-id': this.tenantId } : {}, headers);
|
|
13
16
|
}
|
|
14
17
|
update(userId, sessionId, tenantId) {
|
|
@@ -16,12 +19,36 @@ class ZeniAPIClient {
|
|
|
16
19
|
this.sessionId = sessionId;
|
|
17
20
|
this.tenantId = tenantId;
|
|
18
21
|
}
|
|
22
|
+
/**
|
|
23
|
+
* Check if the API response indicates an invalid session (401).
|
|
24
|
+
* If so, trigger the session invalidation callback once.
|
|
25
|
+
*/
|
|
26
|
+
checkSessionValidity(response) {
|
|
27
|
+
if (this.sessionInvalidationTriggered ||
|
|
28
|
+
this.onSessionInvalidated == null) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
const resp = response;
|
|
32
|
+
if ((0, responsePayload_1.isInvalidSessionError)(resp === null || resp === void 0 ? void 0 : resp.status)) {
|
|
33
|
+
this.sessionInvalidationTriggered = true;
|
|
34
|
+
this.onSessionInvalidated();
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Parse JSON response and check for session validity.
|
|
39
|
+
*/
|
|
40
|
+
processJsonResponse(response) {
|
|
41
|
+
return response.json().then((json) => {
|
|
42
|
+
this.checkSessionValidity(json);
|
|
43
|
+
return json;
|
|
44
|
+
});
|
|
45
|
+
}
|
|
19
46
|
getJSON(restEndPoint, headers, signal) {
|
|
20
47
|
const promise = (0, cross_fetch_1.fetch)(restEndPoint, {
|
|
21
48
|
method: 'GET',
|
|
22
49
|
headers: this.apiHeaders(headers),
|
|
23
50
|
signal,
|
|
24
|
-
}).then((response) =>
|
|
51
|
+
}).then((response) => this.processJsonResponse(response));
|
|
25
52
|
return (0, rxjs_1.from)(promise);
|
|
26
53
|
}
|
|
27
54
|
postJSON(restEndPoint, body, headers, signal) {
|
|
@@ -39,7 +66,7 @@ class ZeniAPIClient {
|
|
|
39
66
|
body: body != null ? JSON.stringify(body) : undefined,
|
|
40
67
|
headers: Object.assign({ 'Content-Type': 'application/json' }, this.apiHeaders(headers)),
|
|
41
68
|
signal,
|
|
42
|
-
}).then((response) =>
|
|
69
|
+
}).then((response) => this.processJsonResponse(response));
|
|
43
70
|
return (0, rxjs_1.from)(promise);
|
|
44
71
|
}
|
|
45
72
|
deleteAndGetJSON(restEndPoint, body, headers, signal) {
|
|
@@ -52,7 +79,7 @@ class ZeniAPIClient {
|
|
|
52
79
|
body: body != null ? JSON.stringify(body) : undefined,
|
|
53
80
|
headers: apiHeaders,
|
|
54
81
|
signal,
|
|
55
|
-
}).then((response) =>
|
|
82
|
+
}).then((response) => this.processJsonResponse(response));
|
|
56
83
|
return (0, rxjs_1.from)(promise);
|
|
57
84
|
}
|
|
58
85
|
putAndGetJSON(restEndPoint, body, headers, signal) {
|
|
@@ -61,7 +88,7 @@ class ZeniAPIClient {
|
|
|
61
88
|
body: body != null ? JSON.stringify(body) : undefined,
|
|
62
89
|
headers: Object.assign({ 'Content-Type': 'application/json' }, this.apiHeaders(headers)),
|
|
63
90
|
signal,
|
|
64
|
-
}).then((response) =>
|
|
91
|
+
}).then((response) => this.processJsonResponse(response));
|
|
65
92
|
return (0, rxjs_1.from)(promise);
|
|
66
93
|
}
|
|
67
94
|
patchAndGetJSON(restEndPoint, body, headers, signal) {
|
|
@@ -70,7 +97,7 @@ class ZeniAPIClient {
|
|
|
70
97
|
body: body != null ? JSON.stringify(body) : undefined,
|
|
71
98
|
headers: Object.assign({ 'Content-Type': 'application/json' }, this.apiHeaders(headers)),
|
|
72
99
|
signal,
|
|
73
|
-
}).then((response) =>
|
|
100
|
+
}).then((response) => this.processJsonResponse(response));
|
|
74
101
|
return (0, rxjs_1.from)(promise);
|
|
75
102
|
}
|
|
76
103
|
getFile(restEndPoint, headers, signal) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zeniai/client-epic-state",
|
|
3
|
-
"version": "4.18.
|
|
3
|
+
"version": "4.18.83",
|
|
4
4
|
"description": "Shared module between Web & Mobile containing required abstractions for state management, async network communication. ",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|