From b88785ba3924ed3f56f93481c8b55eda3b2290da Mon Sep 17 00:00:00 2001 From: Giuseppe Digilio Date: Fri, 5 Apr 2024 09:43:47 +0200 Subject: [PATCH] [DURACOM-247] Move check for initialized token to request effects --- src/app/core/data/request.effects.ts | 13 ++++++++++--- src/app/core/data/request.service.spec.ts | 9 +-------- src/app/core/data/request.service.ts | 19 +++---------------- 3 files changed, 14 insertions(+), 27 deletions(-) diff --git a/src/app/core/data/request.effects.ts b/src/app/core/data/request.effects.ts index 889d909bfa3..d79dd9a2835 100644 --- a/src/app/core/data/request.effects.ts +++ b/src/app/core/data/request.effects.ts @@ -1,7 +1,7 @@ import { Injectable, Injector } from '@angular/core'; import { Actions, createEffect, ofType } from '@ngrx/effects'; -import { catchError, filter, map, mergeMap, take } from 'rxjs/operators'; +import { catchError, filter, map, mergeMap, take, withLatestFrom } from 'rxjs/operators'; import { hasValue, isNotEmpty } from '../../shared/empty.util'; import { StoreActionTypes } from '../../store.actions'; @@ -9,6 +9,7 @@ import { getClassForType } from '../cache/builders/build-decorators'; import { RawRestResponse } from '../dspace-rest/raw-rest-response.model'; import { DspaceRestService } from '../dspace-rest/dspace-rest.service'; import { DSpaceSerializer } from '../dspace-rest/dspace.serializer'; +import { XSRFService } from '../xsrf/xsrf.service'; import { RequestActionTypes, RequestErrorAction, @@ -19,6 +20,7 @@ import { import { RequestService } from './request.service'; import { ParsedResponse } from '../cache/response.models'; import { RequestError } from './request-error.model'; +import { RestRequestMethod } from './rest-request-method'; import { RestRequestWithResponseParser } from './rest-request-with-response-parser.model'; import { RequestEntry } from './request-entry.model'; @@ -33,7 +35,11 @@ export class RequestEffects { ); }), filter((entry: RequestEntry) => hasValue(entry)), - map((entry: RequestEntry) => entry.request), + withLatestFrom(this.xsrfService.tokenInitialized$), + // If it's a GET request, or we have an XSRF token, dispatch it immediately + // Otherwise wait for the XSRF token first + filter(([entry, tokenInitialized]: [RequestEntry, boolean]) => entry.request.method === RestRequestMethod.GET || tokenInitialized === true), + map(([entry, tokenInitialized]: [RequestEntry, boolean]) => entry.request), mergeMap((request: RestRequestWithResponseParser) => { let body = request.body; if (isNotEmpty(request.body) && !request.isMultipart) { @@ -73,7 +79,8 @@ export class RequestEffects { private actions$: Actions, private restApi: DspaceRestService, private injector: Injector, - protected requestService: RequestService + protected requestService: RequestService, + protected xsrfService: XSRFService, ) { } } diff --git a/src/app/core/data/request.service.spec.ts b/src/app/core/data/request.service.spec.ts index c557066e165..ae07a1c3d5d 100644 --- a/src/app/core/data/request.service.spec.ts +++ b/src/app/core/data/request.service.spec.ts @@ -1,6 +1,6 @@ import { Store, StoreModule } from '@ngrx/store'; import { cold, getTestScheduler } from 'jasmine-marbles'; -import { BehaviorSubject, EMPTY, Observable, of as observableOf } from 'rxjs'; +import { EMPTY, Observable, of as observableOf } from 'rxjs'; import { TestScheduler } from 'rxjs/testing'; import { getMockObjectCacheService } from '../../shared/mocks/object-cache.service.mock'; @@ -8,7 +8,6 @@ import { defaultUUID, getMockUUIDService } from '../../shared/mocks/uuid.service import { ObjectCacheService } from '../cache/object-cache.service'; import { coreReducers} from '../core.reducers'; import { UUIDService } from '../shared/uuid.service'; -import { XSRFService } from '../xsrf/xsrf.service'; import { RequestConfigureAction, RequestExecuteAction, RequestStaleAction } from './request.actions'; import { DeleteRequest, @@ -36,7 +35,6 @@ describe('RequestService', () => { let uuidService: UUIDService; let store: Store; let mockStore: MockStore; - let xsrfService: XSRFService; const testUUID = '5f2a0d2a-effa-4d54-bd54-5663b960f9eb'; const testHref = 'https://rest.api/endpoint/selfLink'; @@ -82,16 +80,11 @@ describe('RequestService', () => { store = TestBed.inject(Store); mockStore = store as MockStore; mockStore.setState(initialState); - xsrfService = { - tokenInitialized$: new BehaviorSubject(false), - } as XSRFService; service = new RequestService( objectCache, uuidService, store, - xsrfService, - undefined ); serviceAsAny = service as any; }); diff --git a/src/app/core/data/request.service.ts b/src/app/core/data/request.service.ts index 0af6e4098fd..063b19f8019 100644 --- a/src/app/core/data/request.service.ts +++ b/src/app/core/data/request.service.ts @@ -8,10 +8,9 @@ import cloneDeep from 'lodash/cloneDeep'; import { hasValue, isEmpty, isNotEmpty, hasNoValue } from '../../shared/empty.util'; import { ObjectCacheEntry } from '../cache/object-cache.reducer'; import { ObjectCacheService } from '../cache/object-cache.service'; -import { IndexState, MetaIndexState } from '../index/index.reducer'; +import { IndexState } from '../index/index.reducer'; import { requestIndexSelector, getUrlWithoutEmbedParams } from '../index/index.selectors'; import { UUIDService } from '../shared/uuid.service'; -import { XSRFService } from '../xsrf/xsrf.service'; import { RequestConfigureAction, RequestExecuteAction, @@ -137,9 +136,7 @@ export class RequestService { constructor(private objectCache: ObjectCacheService, private uuidService: UUIDService, - private store: Store, - protected xsrfService: XSRFService, - private indexStore: Store) { + private store: Store) { } generateRequestId(): string { @@ -421,17 +418,7 @@ export class RequestService { */ private dispatchRequest(request: RestRequest) { this.store.dispatch(new RequestConfigureAction(request)); - // If it's a GET request, or we have an XSRF token, dispatch it immediately - if (request.method === RestRequestMethod.GET || this.xsrfService.tokenInitialized$.getValue() === true) { - this.store.dispatch(new RequestExecuteAction(request.uuid)); - } else { - // Otherwise wait for the XSRF token first - this.xsrfService.tokenInitialized$.pipe( - find((hasInitialized: boolean) => hasInitialized === true), - ).subscribe(() => { - this.store.dispatch(new RequestExecuteAction(request.uuid)); - }); - } + this.store.dispatch(new RequestExecuteAction(request.uuid)); } /**