Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for #2891 #2889 #2897

Merged
merged 7 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/app/core/data/request.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
map,
mergeMap,
take,
withLatestFrom,
} from 'rxjs/operators';

import {
Expand All @@ -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,
Expand All @@ -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()
Expand All @@ -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) {
Expand Down Expand Up @@ -89,6 +96,7 @@ export class RequestEffects {
private restApi: DspaceRestService,
private injector: Injector,
protected requestService: RequestService,
protected xsrfService: XSRFService,
) { }

}
8 changes: 0 additions & 8 deletions src/app/core/data/request.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
getTestScheduler,
} from 'jasmine-marbles';
import {
BehaviorSubject,
EMPTY,
Observable,
of as observableOf,
Expand All @@ -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,
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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;
});
Expand Down
22 changes: 3 additions & 19 deletions src/app/core/data/request.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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));
});
}

Expand Down
24 changes: 14 additions & 10 deletions src/app/core/lazy-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,19 @@
injector: Injector,
): Observable<T> {
return defer(() => {
return loader()
.then((serviceOrDefault) => {
if ('default' in serviceOrDefault) {
return injector!.get(serviceOrDefault.default);
}
return injector!.get(serviceOrDefault);
})
.catch((error) => {
throw error;
});
if (typeof loader === 'function') {
return loader()
Fixed Show fixed Hide fixed
.then((serviceOrDefault) => {
if ('default' in serviceOrDefault) {
return injector!.get(serviceOrDefault.default);

Check warning on line 33 in src/app/core/lazy-service.ts

View check run for this annotation

Codecov / codecov/patch

src/app/core/lazy-service.ts#L33

Added line #L33 was not covered by tests
}
return injector!.get(serviceOrDefault);
})
.catch((error) => {
throw error;

Check warning on line 38 in src/app/core/lazy-service.ts

View check run for this annotation

Codecov / codecov/patch

src/app/core/lazy-service.ts#L38

Added line #L38 was not covered by tests
});
} else {
return null;
}
});
}
Loading