diff --git a/package.json b/package.json index c464aec7..fb04ae74 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ }, "peerDependencies": { "reflect-metadata": ">=0.1.3", - "rxjs": ">=5.2.0" + "rxjs": ">=6" }, "devDependencies": { "@angular/animations": "^4.4.3", @@ -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", diff --git a/src/models/json-api.model.ts b/src/models/json-api.model.ts index 7c7e193a..df66671e 100644 --- a/src/models/json-api.model.ts +++ b/src/models/json-api.model.ts @@ -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'; diff --git a/src/services/json-api-datastore.service.ts b/src/services/json-api-datastore.service.ts index 20ceae06..35f56906 100644 --- a/src/services/json-api-datastore.service.ts +++ b/src/services/json-api-datastore.service.ts @@ -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'; @@ -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; @@ -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( @@ -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( @@ -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(modelType: ModelType, data?: any): T { @@ -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)) + ); } @@ -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(modelType: ModelType, id: string): T | null { @@ -316,7 +322,7 @@ export class JsonApiDatastore { return deserializedModel; } - protected handleError(error: any): ErrorObservable { + protected handleError(error: any): ErrorObservable { if ( error instanceof HttpErrorResponse && @@ -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): any {