From d2ec558ac24332085d97a1a0ec1b962ac80354e5 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 | 10 +++++++++- src/app/core/data/request.service.spec.ts | 8 -------- src/app/core/data/request.service.ts | 22 +++------------------- 3 files changed, 12 insertions(+), 28 deletions(-) diff --git a/src/app/core/data/request.effects.ts b/src/app/core/data/request.effects.ts index 6501f43133..bc403bc271 100644 --- a/src/app/core/data/request.effects.ts +++ b/src/app/core/data/request.effects.ts @@ -13,6 +13,7 @@ import { map, mergeMap, take, + withLatestFrom, } from 'rxjs/operators'; import { @@ -25,6 +26,7 @@ import { ParsedResponse } from '../cache/response.models'; import { DSpaceSerializer } from '../dspace-rest/dspace.serializer'; import { DspaceRestService } from '../dspace-rest/dspace-rest.service'; import { RawRestResponse } from '../dspace-rest/raw-rest-response.model'; +import { XSRFService } from '../xsrf/xsrf.service'; import { RequestActionTypes, RequestErrorAction, @@ -35,6 +37,7 @@ import { import { RequestService } from './request.service'; import { RequestEntry } from './request-entry.model'; import { RequestError } from './request-error.model'; +import { RestRequestMethod } from './rest-request-method'; import { RestRequestWithResponseParser } from './rest-request-with-response-parser.model'; @Injectable() @@ -48,7 +51,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) { @@ -89,6 +96,7 @@ export class RequestEffects { private restApi: DspaceRestService, private injector: Injector, 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 e194901679..d8fa04973e 100644 --- a/src/app/core/data/request.service.spec.ts +++ b/src/app/core/data/request.service.spec.ts @@ -17,7 +17,6 @@ import { getTestScheduler, } from 'jasmine-marbles'; import { - BehaviorSubject, EMPTY, Observable, of as observableOf, @@ -34,7 +33,6 @@ import { ObjectCacheService } from '../cache/object-cache.service'; import { coreReducers } from '../core.reducers'; import { CoreState } from '../core-state.model'; import { UUIDService } from '../shared/uuid.service'; -import { XSRFService } from '../xsrf/xsrf.service'; import { RequestConfigureAction, RequestExecuteAction, @@ -62,7 +60,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'; @@ -108,16 +105,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 3e2bbfdc9f..52ec9b56e2 100644 --- a/src/app/core/data/request.service.ts +++ b/src/app/core/data/request.service.ts @@ -34,16 +34,12 @@ import { ObjectCacheService } from '../cache/object-cache.service'; import { CommitSSBAction } from '../cache/server-sync-buffer.actions'; import { coreSelector } from '../core.selectors'; import { CoreState } from '../core-state.model'; -import { - IndexState, - MetaIndexState, -} from '../index/index.reducer'; +import { IndexState } from '../index/index.reducer'; import { getUrlWithoutEmbedParams, requestIndexSelector, } from '../index/index.selectors'; import { UUIDService } from '../shared/uuid.service'; -import { XSRFService } from '../xsrf/xsrf.service'; import { RequestConfigureAction, RequestExecuteAction, @@ -169,9 +165,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 { @@ -455,17 +449,7 @@ export class RequestService { private dispatchRequest(request: RestRequest) { asapScheduler.schedule(() => { 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)); }); }