aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/wt/odlux/framework/src/services
diff options
context:
space:
mode:
authorsai-neetha <sai-neetha.phulmali@highstreet-technologies.com>2023-07-07 18:11:09 +0200
committersai-neetha <sai-neetha.phulmali@highstreet-technologies.com>2023-07-07 18:12:17 +0200
commitfad3167f42d585e3144547db4c6dd7d00ea7b18a (patch)
treed6578fd008c717748e6110c2072bbe65fcb91e2e /sdnr/wt/odlux/framework/src/services
parent8efd8356d7ea705e282a72aeb74d4199cdf21851 (diff)
Update ODLUX
node version yarn version update Issue-ID: CCSDK-3923 Signed-off-by: sai-neetha <sai-neetha.phulmali@highstreet-technologies.com> Change-Id: Ibd3d6a6f45a14be4f1d175cf6fc5c8738aa11dea
Diffstat (limited to 'sdnr/wt/odlux/framework/src/services')
-rw-r--r--sdnr/wt/odlux/framework/src/services/applicationApi.ts20
-rw-r--r--sdnr/wt/odlux/framework/src/services/broadcastService.ts45
-rw-r--r--sdnr/wt/odlux/framework/src/services/index.ts2
-rw-r--r--sdnr/wt/odlux/framework/src/services/restService.ts57
-rw-r--r--sdnr/wt/odlux/framework/src/services/userdataService.ts21
5 files changed, 73 insertions, 72 deletions
diff --git a/sdnr/wt/odlux/framework/src/services/applicationApi.ts b/sdnr/wt/odlux/framework/src/services/applicationApi.ts
index 8246ee8fa..faa998450 100644
--- a/sdnr/wt/odlux/framework/src/services/applicationApi.ts
+++ b/sdnr/wt/odlux/framework/src/services/applicationApi.ts
@@ -18,7 +18,7 @@
import { Event } from '../common/event';
import { ApplicationStore } from '../store/applicationStore';
-import { AuthMessage, getBroadcastChannel, sendMessage } from './broadcastService';
+import { AuthMessage, sendMessage } from './broadcastService';
let resolveApplicationStoreInitialized: (store: ApplicationStore) => void;
let applicationStore: ApplicationStore | null = null;
@@ -26,32 +26,32 @@ const applicationStoreInitialized: Promise<ApplicationStore> = new Promise((reso
const loginEvent = new Event();
const logoutEvent = new Event();
-let channel : BroadcastChannel | undefined;
-const authChannelName = "odlux_auth";
+
+const authChannelName = 'odlux_auth';
export const onLogin = () => {
- const message : AuthMessage = {key: 'login', data: {}}
+ const message : AuthMessage = { key: 'login', data: {} };
sendMessage(message, authChannelName);
loginEvent.invoke();
-}
+};
export const onLogout = () => {
- document.cookie = "JSESSIONID=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
+ document.cookie = 'JSESSIONID=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
- const message : AuthMessage = {key: 'logout', data: {}}
+ const message : AuthMessage = { key: 'logout', data: {} };
sendMessage(message, authChannelName);
logoutEvent.invoke();
-}
+};
export const setApplicationStore = (store: ApplicationStore) => {
if (!applicationStore && store) {
applicationStore = store;
resolveApplicationStoreInitialized(store);
}
-}
+};
export const applicationApi = {
get applicationStore(): ApplicationStore | null {
@@ -68,7 +68,7 @@ export const applicationApi = {
get logoutEvent() {
return logoutEvent;
- }
+ },
};
export default applicationApi; \ No newline at end of file
diff --git a/sdnr/wt/odlux/framework/src/services/broadcastService.ts b/sdnr/wt/odlux/framework/src/services/broadcastService.ts
index 40968e54f..202bf5563 100644
--- a/sdnr/wt/odlux/framework/src/services/broadcastService.ts
+++ b/sdnr/wt/odlux/framework/src/services/broadcastService.ts
@@ -16,11 +16,11 @@
* ============LICENSE_END==========================================================================
*/
-import { setGeneralSettingsAction } from "../actions/settingsAction";
-import { loginUserAction, logoutUser } from "../actions/authentication";
-import { ReplaceAction } from "../actions/navigationActions";
-import { User } from "../models/authentication";
-import { ApplicationStore } from "../store/applicationStore";
+import { setGeneralSettingsAction } from '../actions/settingsAction';
+import { loginUserAction, logoutUser } from '../actions/authentication';
+import { ReplaceAction } from '../actions/navigationActions';
+import { User } from '../models/authentication';
+import { ApplicationStore } from '../store/applicationStore';
type Broadcaster = {
channel: BroadcastChannel;
@@ -47,17 +47,9 @@ export const saveChannel = (channel: BroadcastChannel, channelName: string) => {
channels.push({ channel: channel, key: channelName });
};
-export const startBroadcastChannel = (applicationStore: ApplicationStore) => {
- store = applicationStore;
-
- //might decide to use one general broadcast channel with more keys in the future
- createAuthBroadcastChannel();
- createSettingsBroadcastChannel();
-};
-
const createSettingsBroadcastChannel = () => {
- const name = "odlux_settings";
+ const name = 'odlux_settings';
const bc: BroadcastChannel = new BroadcastChannel(name);
channels.push({ channel: bc, key: name });
@@ -73,34 +65,41 @@ const createSettingsBroadcastChannel = () => {
}
}
}
- }
+ };
};
const createAuthBroadcastChannel = () => {
- const name = "odlux_auth";
+ const name = 'odlux_auth';
const bc: BroadcastChannel = new BroadcastChannel(name);
channels.push({ channel: bc, key: name });
bc.onmessage = (eventMessage: MessageEvent<AuthMessage>) => {
- console.log(eventMessage)
+ console.log(eventMessage);
if (eventMessage.data.key === 'login') {
if (!store?.state.framework.authenticationState.user) {
- const initialToken = localStorage.getItem("userToken");
+ const initialToken = localStorage.getItem('userToken');
if (initialToken) {
store?.dispatch(loginUserAction(User.fromString(initialToken)));
- store?.dispatch(new ReplaceAction("/"));
+ store?.dispatch(new ReplaceAction('/'));
}
}
- }
- else if (eventMessage.data.key === 'logout') {
+ } else if (eventMessage.data.key === 'logout') {
if (store?.state.framework.authenticationState.user) {
store?.dispatch(logoutUser());
- store?.dispatch(new ReplaceAction("/login"));
+ store?.dispatch(new ReplaceAction('/login'));
}
}
- }
+ };
+};
+
+export const startBroadcastChannel = (applicationStore: ApplicationStore) => {
+ store = applicationStore;
+
+ // might decide to use one general broadcast channel with more keys in the future
+ createAuthBroadcastChannel();
+ createSettingsBroadcastChannel();
};
export const getBroadcastChannel = (channelName: string) => {
diff --git a/sdnr/wt/odlux/framework/src/services/index.ts b/sdnr/wt/odlux/framework/src/services/index.ts
index 85f0708a6..2f64ba0ac 100644
--- a/sdnr/wt/odlux/framework/src/services/index.ts
+++ b/sdnr/wt/odlux/framework/src/services/index.ts
@@ -18,5 +18,5 @@
export { applicationManager } from './applicationManager';
export { subscribe, unsubscribe } from './notificationService';
export { requestRest } from './restService';
-export { saveUserdata, getUserdata } from './userdataService';
+export { saveUserData as saveUserdata, getUserData as getUserdata } from './userdataService';
diff --git a/sdnr/wt/odlux/framework/src/services/restService.ts b/sdnr/wt/odlux/framework/src/services/restService.ts
index a296c52a4..0be3dca07 100644
--- a/sdnr/wt/odlux/framework/src/services/restService.ts
+++ b/sdnr/wt/odlux/framework/src/services/restService.ts
@@ -68,41 +68,30 @@ export const getAccessPolicyByUrl = (url: string) => {
};
-/** Sends a rest request to the given path.
- * @returns The data, or null it there was any error
- */
-export async function requestRest<TData>(path: string = '', init: RequestInit = {}, authenticate: boolean = true, isResource: boolean = false): Promise<TData | null | undefined> {
- const res = await requestRestExt<TData>(path, init, authenticate, isResource);
- if (res && res.status >= 200 && res.status < 300) {
- return res.data;
- }
- return null;
-}
-
/** Sends a rest request to the given path and reports the server state.
* @returns An object with the server state, a message and the data or undefined in case of a json parse error.
*/
-export async function requestRestExt<TData>(path: string = '', init: RequestInit = {}, authenticate: boolean = true, isResource: boolean = false): Promise<{ status: number; message?: string; data: TData | null | undefined }> {
+export async function requestRestExt<TData>(path: string = '', initParam: RequestInit = {}, authenticate: boolean = true, isResource: boolean = false): Promise<{ status: number; message?: string; data: TData | null | undefined }> {
const result: { status: number; message?: string; data: TData | null } = {
status: -1,
data: null,
};
const isAbsUrl = absUrlPattern.test(path);
const uri = isAbsUrl ? path : isResource ? path.replace(/\/{2,}/i, '/') : (baseUri) + ('/' + path).replace(/\/{2,}/i, '/');
- init = {
+ const init = {
'method': 'GET',
- ...init,
+ ...initParam,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
- ...init.headers,
- },
+ ...initParam.headers,
+ } as HeadersInit,
};
if (!isAbsUrl && authenticate && storeService.applicationStore) {
const { state: { framework: { authenticationState: { user } } } } = storeService.applicationStore;
// do not request if the user is not valid
- if (!user || !user.isValid) {
+ if (!user || !user.isValid || !user.token || !user.tokenType) {
return {
...result,
message: 'User is not valid or not logged in.',
@@ -116,16 +105,31 @@ export async function requestRestExt<TData>(path: string = '', init: RequestInit
}
const fetchResult = await fetch(uri, init);
-
- if (fetchResult.status === 403) {
- storeService.applicationStore && storeService.applicationStore.dispatch(new AddErrorInfoAction({ title: 'Forbidden', message:'Status: [403], access denied.' }));
+ if (fetchResult.status === 309) {
+ const redirectUrl = fetchResult.headers.get('Location');
+ if (! redirectUrl) {
+ throw new Error('Status code 309 requires header "Location"');
+ }
+ localStorage.removeItem('userToken');
+ window.location.href = redirectUrl;
+ return {
+ ...result,
+ status: fetchResult.status,
+ message: 'Redirecting to new URL.',
+ };
+ } else if (fetchResult.status === 403) {
+ if (storeService.applicationStore) {
+ storeService.applicationStore.dispatch(new AddErrorInfoAction({ title: 'Forbidden', message:'Status: [403], access denied.' }));
+ }
return {
...result,
status: 403,
message: 'Forbidden.',
};
} else if (fetchResult.status === 401) {
- storeService.applicationStore && storeService.applicationStore.dispatch(new ReplaceAction(`/login?returnTo=${storeService.applicationStore.state.framework.navigationState.pathname}`));
+ if (storeService.applicationStore) {
+ storeService.applicationStore.dispatch(new ReplaceAction(`/login?returnTo=${storeService.applicationStore.state.framework.navigationState.pathname}`));
+ }
return {
...result,
status: 401,
@@ -150,4 +154,15 @@ export async function requestRestExt<TData>(path: string = '', init: RequestInit
data: undefined,
};
}
+}
+
+/** Sends a rest request to the given path.
+ * @returns The data, or null it there was any error
+ */
+export async function requestRest<TData>(path: string = '', init: RequestInit = {}, authenticate: boolean = true, isResource: boolean = false): Promise<TData | null | undefined> {
+ const res = await requestRestExt<TData>(path, init, authenticate, isResource);
+ if (res && res.status >= 200 && res.status < 300) {
+ return res.data;
+ }
+ return null;
} \ No newline at end of file
diff --git a/sdnr/wt/odlux/framework/src/services/userdataService.ts b/sdnr/wt/odlux/framework/src/services/userdataService.ts
index 5c9b576c3..53de8e1c3 100644
--- a/sdnr/wt/odlux/framework/src/services/userdataService.ts
+++ b/sdnr/wt/odlux/framework/src/services/userdataService.ts
@@ -16,26 +16,13 @@
* ============LICENSE_END==========================================================================
*/
-import { requestRest } from "./restService";
+import { requestRest } from './restService';
+const settingsPath = '/userdata';
- const settingsPath ="/userdata";
+export const getUserData = <TData>(partialPath?: string) => requestRest<TData>(partialPath ? settingsPath + partialPath : settingsPath, { method: 'GET' });
+export const saveUserData = <TData>(partialPath: string, data: string) => requestRest<TData>(settingsPath + partialPath, { method: 'PUT', body: data });
- export function getUserdata<TData>(partialPath?: string){
- let path = settingsPath;
- if(partialPath){
- path+=partialPath
- }
-
- const result = requestRest<TData>(path, {method: "GET"})
- return result;
- }
-
- export function saveUserdata<TData>(partialPath: string, data: string){
-
- const result = requestRest<TData>(settingsPath+partialPath, {method: "PUT", body: data})
- return result;
- }