mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 10:04:11 +00:00
[DURACOM-247] Move check for initialized token to request effects
This commit is contained in:
@@ -13,6 +13,7 @@ import {
|
|||||||
map,
|
map,
|
||||||
mergeMap,
|
mergeMap,
|
||||||
take,
|
take,
|
||||||
|
withLatestFrom,
|
||||||
} from 'rxjs/operators';
|
} from 'rxjs/operators';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
@@ -25,6 +26,7 @@ import { ParsedResponse } from '../cache/response.models';
|
|||||||
import { DSpaceSerializer } from '../dspace-rest/dspace.serializer';
|
import { DSpaceSerializer } from '../dspace-rest/dspace.serializer';
|
||||||
import { DspaceRestService } from '../dspace-rest/dspace-rest.service';
|
import { DspaceRestService } from '../dspace-rest/dspace-rest.service';
|
||||||
import { RawRestResponse } from '../dspace-rest/raw-rest-response.model';
|
import { RawRestResponse } from '../dspace-rest/raw-rest-response.model';
|
||||||
|
import { XSRFService } from '../xsrf/xsrf.service';
|
||||||
import {
|
import {
|
||||||
RequestActionTypes,
|
RequestActionTypes,
|
||||||
RequestErrorAction,
|
RequestErrorAction,
|
||||||
@@ -35,6 +37,7 @@ import {
|
|||||||
import { RequestService } from './request.service';
|
import { RequestService } from './request.service';
|
||||||
import { RequestEntry } from './request-entry.model';
|
import { RequestEntry } from './request-entry.model';
|
||||||
import { RequestError } from './request-error.model';
|
import { RequestError } from './request-error.model';
|
||||||
|
import { RestRequestMethod } from './rest-request-method';
|
||||||
import { RestRequestWithResponseParser } from './rest-request-with-response-parser.model';
|
import { RestRequestWithResponseParser } from './rest-request-with-response-parser.model';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
@@ -48,7 +51,11 @@ export class RequestEffects {
|
|||||||
);
|
);
|
||||||
}),
|
}),
|
||||||
filter((entry: RequestEntry) => hasValue(entry)),
|
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) => {
|
mergeMap((request: RestRequestWithResponseParser) => {
|
||||||
let body = request.body;
|
let body = request.body;
|
||||||
if (isNotEmpty(request.body) && !request.isMultipart) {
|
if (isNotEmpty(request.body) && !request.isMultipart) {
|
||||||
@@ -89,6 +96,7 @@ export class RequestEffects {
|
|||||||
private restApi: DspaceRestService,
|
private restApi: DspaceRestService,
|
||||||
private injector: Injector,
|
private injector: Injector,
|
||||||
protected requestService: RequestService,
|
protected requestService: RequestService,
|
||||||
|
protected xsrfService: XSRFService,
|
||||||
) { }
|
) { }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -17,7 +17,6 @@ import {
|
|||||||
getTestScheduler,
|
getTestScheduler,
|
||||||
} from 'jasmine-marbles';
|
} from 'jasmine-marbles';
|
||||||
import {
|
import {
|
||||||
BehaviorSubject,
|
|
||||||
EMPTY,
|
EMPTY,
|
||||||
Observable,
|
Observable,
|
||||||
of as observableOf,
|
of as observableOf,
|
||||||
@@ -34,7 +33,6 @@ import { ObjectCacheService } from '../cache/object-cache.service';
|
|||||||
import { coreReducers } from '../core.reducers';
|
import { coreReducers } from '../core.reducers';
|
||||||
import { CoreState } from '../core-state.model';
|
import { CoreState } from '../core-state.model';
|
||||||
import { UUIDService } from '../shared/uuid.service';
|
import { UUIDService } from '../shared/uuid.service';
|
||||||
import { XSRFService } from '../xsrf/xsrf.service';
|
|
||||||
import {
|
import {
|
||||||
RequestConfigureAction,
|
RequestConfigureAction,
|
||||||
RequestExecuteAction,
|
RequestExecuteAction,
|
||||||
@@ -62,7 +60,6 @@ describe('RequestService', () => {
|
|||||||
let uuidService: UUIDService;
|
let uuidService: UUIDService;
|
||||||
let store: Store<CoreState>;
|
let store: Store<CoreState>;
|
||||||
let mockStore: MockStore<CoreState>;
|
let mockStore: MockStore<CoreState>;
|
||||||
let xsrfService: XSRFService;
|
|
||||||
|
|
||||||
const testUUID = '5f2a0d2a-effa-4d54-bd54-5663b960f9eb';
|
const testUUID = '5f2a0d2a-effa-4d54-bd54-5663b960f9eb';
|
||||||
const testHref = 'https://rest.api/endpoint/selfLink';
|
const testHref = 'https://rest.api/endpoint/selfLink';
|
||||||
@@ -108,16 +105,11 @@ describe('RequestService', () => {
|
|||||||
store = TestBed.inject(Store);
|
store = TestBed.inject(Store);
|
||||||
mockStore = store as MockStore<CoreState>;
|
mockStore = store as MockStore<CoreState>;
|
||||||
mockStore.setState(initialState);
|
mockStore.setState(initialState);
|
||||||
xsrfService = {
|
|
||||||
tokenInitialized$: new BehaviorSubject(false),
|
|
||||||
} as XSRFService;
|
|
||||||
|
|
||||||
service = new RequestService(
|
service = new RequestService(
|
||||||
objectCache,
|
objectCache,
|
||||||
uuidService,
|
uuidService,
|
||||||
store,
|
store,
|
||||||
xsrfService,
|
|
||||||
undefined,
|
|
||||||
);
|
);
|
||||||
serviceAsAny = service as any;
|
serviceAsAny = service as any;
|
||||||
});
|
});
|
||||||
|
@@ -34,16 +34,12 @@ import { ObjectCacheService } from '../cache/object-cache.service';
|
|||||||
import { CommitSSBAction } from '../cache/server-sync-buffer.actions';
|
import { CommitSSBAction } from '../cache/server-sync-buffer.actions';
|
||||||
import { coreSelector } from '../core.selectors';
|
import { coreSelector } from '../core.selectors';
|
||||||
import { CoreState } from '../core-state.model';
|
import { CoreState } from '../core-state.model';
|
||||||
import {
|
import { IndexState } from '../index/index.reducer';
|
||||||
IndexState,
|
|
||||||
MetaIndexState,
|
|
||||||
} from '../index/index.reducer';
|
|
||||||
import {
|
import {
|
||||||
getUrlWithoutEmbedParams,
|
getUrlWithoutEmbedParams,
|
||||||
requestIndexSelector,
|
requestIndexSelector,
|
||||||
} from '../index/index.selectors';
|
} from '../index/index.selectors';
|
||||||
import { UUIDService } from '../shared/uuid.service';
|
import { UUIDService } from '../shared/uuid.service';
|
||||||
import { XSRFService } from '../xsrf/xsrf.service';
|
|
||||||
import {
|
import {
|
||||||
RequestConfigureAction,
|
RequestConfigureAction,
|
||||||
RequestExecuteAction,
|
RequestExecuteAction,
|
||||||
@@ -169,9 +165,7 @@ export class RequestService {
|
|||||||
|
|
||||||
constructor(private objectCache: ObjectCacheService,
|
constructor(private objectCache: ObjectCacheService,
|
||||||
private uuidService: UUIDService,
|
private uuidService: UUIDService,
|
||||||
private store: Store<CoreState>,
|
private store: Store<CoreState>) {
|
||||||
protected xsrfService: XSRFService,
|
|
||||||
private indexStore: Store<MetaIndexState>) {
|
|
||||||
}
|
}
|
||||||
|
|
||||||
generateRequestId(): string {
|
generateRequestId(): string {
|
||||||
@@ -455,17 +449,7 @@ export class RequestService {
|
|||||||
private dispatchRequest(request: RestRequest) {
|
private dispatchRequest(request: RestRequest) {
|
||||||
asapScheduler.schedule(() => {
|
asapScheduler.schedule(() => {
|
||||||
this.store.dispatch(new RequestConfigureAction(request));
|
this.store.dispatch(new RequestConfigureAction(request));
|
||||||
// If it's a GET request, or we have an XSRF token, dispatch it immediately
|
this.store.dispatch(new RequestExecuteAction(request.uuid));
|
||||||
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