mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-15 05:53:03 +00:00
request.effects.ts pipeable operators
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import { Inject, Injectable, Injector } from '@angular/core';
|
import { Inject, Injectable, Injector } from '@angular/core';
|
||||||
import { Request } from '@angular/http';
|
import { Request } from '@angular/http';
|
||||||
import { RequestArgs } from '@angular/http/src/interfaces';
|
import { RequestArgs } from '@angular/http/src/interfaces';
|
||||||
import { Actions, Effect } from '@ngrx/effects';
|
import { Actions, Effect, ofType } from '@ngrx/effects';
|
||||||
// tslint:disable-next-line:import-blacklist
|
// tslint:disable-next-line:import-blacklist
|
||||||
import { Observable } from 'rxjs';
|
import { Observable } from 'rxjs';
|
||||||
|
|
||||||
@@ -18,32 +18,34 @@ import { RequestEntry } from './request.reducer';
|
|||||||
import { RequestService } from './request.service';
|
import { RequestService } from './request.service';
|
||||||
import { DSpaceRESTv2Serializer } from '../dspace-rest-v2/dspace-rest-v2.serializer';
|
import { DSpaceRESTv2Serializer } from '../dspace-rest-v2/dspace-rest-v2.serializer';
|
||||||
import { NormalizedObjectFactory } from '../cache/models/normalized-object-factory';
|
import { NormalizedObjectFactory } from '../cache/models/normalized-object-factory';
|
||||||
|
import { catchError, flatMap, map, tap } from 'rxjs/operators';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class RequestEffects {
|
export class RequestEffects {
|
||||||
|
|
||||||
@Effect() execute = this.actions$
|
@Effect() execute = this.actions$.ofType(RequestActionTypes.EXECUTE).pipe(
|
||||||
.ofType(RequestActionTypes.EXECUTE)
|
flatMap((action: RequestExecuteAction) => {
|
||||||
.flatMap((action: RequestExecuteAction) => {
|
|
||||||
return this.requestService.getByUUID(action.payload)
|
return this.requestService.getByUUID(action.payload)
|
||||||
.take(1);
|
.take(1);
|
||||||
})
|
}),
|
||||||
.map((entry: RequestEntry) => entry.request)
|
map((entry: RequestEntry) => entry.request),
|
||||||
.flatMap((request: RestRequest) => {
|
flatMap((request: RestRequest) => {
|
||||||
let body;
|
let body;
|
||||||
if (isNotEmpty(request.body)) {
|
if (isNotEmpty(request.body)) {
|
||||||
const serializer = new DSpaceRESTv2Serializer(NormalizedObjectFactory.getConstructor(request.body.type));
|
const serializer = new DSpaceRESTv2Serializer(NormalizedObjectFactory.getConstructor(request.body.type));
|
||||||
body = JSON.stringify(serializer.serialize(request.body));
|
body = JSON.stringify(serializer.serialize(request.body));
|
||||||
}
|
}
|
||||||
return this.restApi.request(request.method, request.href, body)
|
return this.restApi.request(request.method, request.href, body).pipe(
|
||||||
.map((data: DSpaceRESTV2Response) =>
|
map((data: DSpaceRESTV2Response) => this.injector.get(request.getResponseParser()).parse(request, data)),
|
||||||
this.injector.get(request.getResponseParser()).parse(request, data))
|
tap((response: RestResponse) => this.responseCache.add(request.href, response, this.EnvConfig.cache.msToLive)),
|
||||||
.do((response: RestResponse) => this.responseCache.add(request.href, response, this.EnvConfig.cache.msToLive))
|
map((response: RestResponse) => new RequestCompleteAction(request.uuid)),
|
||||||
.map((response: RestResponse) => new RequestCompleteAction(request.uuid))
|
catchError((error: RequestError) => Observable.of(new ErrorResponse(error)).pipe(
|
||||||
.catch((error: RequestError) => Observable.of(new ErrorResponse(error))
|
tap((response: RestResponse) => this.responseCache.add(request.href, response, this.EnvConfig.cache.msToLive)),
|
||||||
.do((response: RestResponse) => this.responseCache.add(request.href, response, this.EnvConfig.cache.msToLive))
|
map((response: RestResponse) => new RequestCompleteAction(request.uuid))
|
||||||
.map((response: RestResponse) => new RequestCompleteAction(request.uuid)));
|
))
|
||||||
});
|
);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
@Inject(GLOBAL_CONFIG) private EnvConfig: GlobalConfig,
|
@Inject(GLOBAL_CONFIG) private EnvConfig: GlobalConfig,
|
||||||
|
Reference in New Issue
Block a user