aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/wt/odlux/framework/src/services/restService.ts
diff options
context:
space:
mode:
Diffstat (limited to 'sdnr/wt/odlux/framework/src/services/restService.ts')
-rw-r--r--sdnr/wt/odlux/framework/src/services/restService.ts57
1 files changed, 36 insertions, 21 deletions
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