diff --git a/package.json b/package.json index 3a345501..80198e7e 100644 --- a/package.json +++ b/package.json @@ -38,8 +38,7 @@ "qs": "^6.5.1" }, "peerDependencies": { - "reflect-metadata": ">=0.1.3", - "rxjs": ">=5.2.0" + "rxjs": ">=6.2.0" }, "devDependencies": { "@angular/animations": "^4.4.3", @@ -70,7 +69,8 @@ "karma-webpack": "^2.0.3", "reflect-metadata": "^0.1.3", "rimraf": "^2.6.2", - "rxjs": "5.4.2", + "rxjs": "^6.2.0", + "rxjs-compat": "^6.2.0", "sourcemap-istanbul-instrumenter-loader": "^0.2.0", "ts-loader": "^2.3.7", "tslint": "^5.7.0", diff --git a/src/models/json-api.model.spec.ts b/src/models/json-api.model.spec.ts index 96ea0b83..313de9f7 100644 --- a/src/models/json-api.model.spec.ts +++ b/src/models/json-api.model.spec.ts @@ -127,7 +127,7 @@ describe('JsonApiModel', () => { }); describe('update relationships', () => { - it ('should return updated relationship', () => { + it('should return updated relationship', () => { const REL = 'books'; const BOOK_NUMBER = 1; const CHAPTERS_NUMBER = 4; @@ -149,4 +149,24 @@ describe('JsonApiModel', () => { }); }); }); + + describe('hasDirtyAttributes & rollbackAttributes', () => { + const author = new Author(datastore, { + id: '1', + attributes: { + name: 'Daniele' + } + }); + + it('should return that has dirty attributes', () => { + author.name = 'New Name'; + expect(author.hasDirtyAttributes).toBeTruthy(); + }); + + it('should to rollback to the initial author name', () => { + author.rollbackAttributes(); + expect(author.name).toEqual('Daniele'); + expect(author.hasDirtyAttributes).toBeFalsy(); + }); + }); }); diff --git a/src/services/json-api-datastore.service.ts b/src/services/json-api-datastore.service.ts index 20ceae06..b4dcbd71 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'; @@ -30,11 +27,10 @@ export class JsonApiDatastore { private get getDirtyAttributes() { if (this.datastoreConfig.overrides - && this.datastoreConfig.overrides.getDirtyAttributes) { + && 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,20 +137,19 @@ 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)) + ); } - - deleteRecord( modelType: ModelType, id: string, @@ -158,7 +159,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 +320,7 @@ export class JsonApiDatastore { return deserializedModel; } - protected handleError(error: any): ErrorObservable { + protected handleError(error: any): ErrorObservable { if ( error instanceof HttpErrorResponse && @@ -326,11 +330,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 {