Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
},
"peerDependencies": {
"reflect-metadata": ">=0.1.3",
"rxjs": ">=5.2.0"
"rxjs": ">=6"
},
"devDependencies": {
"@angular/animations": "^4.4.3",
Expand Down Expand Up @@ -71,7 +71,8 @@
"karma-webpack": "^2.0.3",
"reflect-metadata": "^0.1.3",
"rimraf": "^2.6.2",
"rxjs": "5.4.2",
"rxjs": "6",
"rxjs-compat": "6",
"sourcemap-istanbul-instrumenter-loader": "^0.2.0",
"ts-loader": "^2.3.7",
"tslint": "^5.7.0",
Expand Down
2 changes: 1 addition & 1 deletion src/models/json-api.model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import find from 'lodash-es/find';
import includes from 'lodash-es/includes';
import { Observable } from 'rxjs/Observable';
import { Observable } from 'rxjs';
import { JsonApiDatastore, ModelType } from '../services/json-api-datastore.service';
import { ModelConfig } from '../interfaces/model-config.interface';
import * as _ from 'lodash';
Expand Down
60 changes: 33 additions & 27 deletions src/services/json-api-datastore.service.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpResponse, HttpErrorResponse } from '@angular/common/http';
import find from 'lodash-es/find';
import { Observable } from 'rxjs/Observable';
import { map, catchError } from 'rxjs/operators';
import { throwError, of, Observable } from 'rxjs';
import { ErrorObservable } from 'rxjs/observable/ErrorObservable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';
import 'rxjs/add/observable/of';
import { JsonApiModel } from '../models/json-api.model';
import { ErrorResponse } from '../models/error-response.model';
import { JsonApiQueryData } from '../models/json-api-query-data';
Expand All @@ -32,9 +29,8 @@ export class JsonApiDatastore {
if (this.datastoreConfig.overrides
&& this.datastoreConfig.overrides.getDirtyAttributes) {
return this.datastoreConfig.overrides.getDirtyAttributes;
} else {
return JsonApiDatastore.getDirtyAttributes;
}
return JsonApiDatastore.getDirtyAttributes;
}

protected config: DatastoreConfig;
Expand All @@ -51,8 +47,10 @@ export class JsonApiDatastore {
const requestHeaders: HttpHeaders = this.buildHeaders(headers);
const url: string = this.buildUrl(modelType, params, undefined, customUrl);
return this.http.get(url, { headers: requestHeaders })
.map((res: any) => this.extractQueryData(res, modelType))
.catch((res: any) => this.handleError(res));
.pipe(
map((res: any) => this.extractQueryData(res, modelType)),
catchError((res: any) => this.handleError(res))
);
}

findAll<T extends JsonApiModel>(
Expand All @@ -65,8 +63,10 @@ export class JsonApiDatastore {
const url: string = this.buildUrl(modelType, params, undefined, customUrl);

return this.http.get(url, { headers: requestHeaders })
.map((res: any) => this.extractQueryData(res, modelType, true))
.catch((res: any) => this.handleError(res));
.pipe(
map((res: any) => this.extractQueryData(res, modelType, true)),
catchError((res: any) => this.handleError(res))
);
}

findRecord<T extends JsonApiModel>(
Expand All @@ -80,8 +80,10 @@ export class JsonApiDatastore {
const url: string = this.buildUrl(modelType, params, id, customUrl);

return this.http.get(url, { headers: requestHeaders, observe: 'response' })
.map((res) => this.extractRecordData(res, modelType))
.catch((res: any) => this.handleError(res));
.pipe(
map((res) => this.extractRecordData(res, modelType)),
catchError((res: any) => this.handleError(res))
);
}

createRecord<T extends JsonApiModel>(modelType: ModelType<T>, data?: any): T {
Expand Down Expand Up @@ -135,16 +137,17 @@ export class JsonApiDatastore {
}

return httpCall
.map((res) => [200, 201].indexOf(res.status) !== -1 ? this.extractRecordData(res, modelType, model) : model)
.catch((res) => {
if (res == null) {
return Observable.of(model);
}

return this.handleError(res);
})
.map((res) => this.resetMetadataAttributes(res, attributesMetadata, modelType))
.map((res) => this.updateRelationships(res, relationships));
.pipe(
map((res) => [200, 201].indexOf(res.status) !== -1 ? this.extractRecordData(res, modelType, model) : model),
catchError((res) => {
if (res == null) {
return of(model);
}
return this.handleError(res);
}),
map((res) => this.resetMetadataAttributes(res, attributesMetadata, modelType)),
map((res) => this.updateRelationships(res, relationships))
);
}


Expand All @@ -158,7 +161,10 @@ export class JsonApiDatastore {
const requestHeaders: HttpHeaders = this.buildHeaders(headers);
const url: string = this.buildUrl(modelType, null, id, customUrl);

return this.http.delete(url, { headers: requestHeaders }).catch((res: HttpErrorResponse) => this.handleError(res));
return this.http.delete(url, { headers: requestHeaders })
.pipe(
catchError((res: HttpErrorResponse) => this.handleError(res))
);
}

peekRecord<T extends JsonApiModel>(modelType: ModelType<T>, id: string): T | null {
Expand Down Expand Up @@ -316,7 +322,7 @@ export class JsonApiDatastore {
return deserializedModel;
}

protected handleError(error: any): ErrorObservable {
protected handleError(error: any): ErrorObservable<any> {

if (
error instanceof HttpErrorResponse &&
Expand All @@ -326,11 +332,11 @@ export class JsonApiDatastore {
) {
const errors: ErrorResponse = new ErrorResponse(error.error.errors);
console.error(error, errors);
return Observable.throw(errors);
return throwError(errors);
}

console.error(error);
return Observable.throw(error);
return throwError(error);
}

protected parseMeta(body: any, modelType: ModelType<JsonApiModel>): any {
Expand Down