mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 18:14:17 +00:00
[DURACOM-247] Move check for initialized token to request effects
This commit is contained in:
@@ -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,
|
||||
) { }
|
||||
|
||||
}
|
||||
|
@@ -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<CoreState>;
|
||||
let mockStore: MockStore<CoreState>;
|
||||
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<CoreState>;
|
||||
mockStore.setState(initialState);
|
||||
xsrfService = {
|
||||
tokenInitialized$: new BehaviorSubject(false),
|
||||
} as XSRFService;
|
||||
|
||||
service = new RequestService(
|
||||
objectCache,
|
||||
uuidService,
|
||||
store,
|
||||
xsrfService,
|
||||
undefined,
|
||||
);
|
||||
serviceAsAny = service as any;
|
||||
});
|
||||
|
@@ -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<CoreState>,
|
||||
protected xsrfService: XSRFService,
|
||||
private indexStore: Store<MetaIndexState>) {
|
||||
private store: Store<CoreState>) {
|
||||
}
|
||||
|
||||
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));
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user