From eaf3023a6bb135f1c80755d29636c83ccbf8569f Mon Sep 17 00:00:00 2001 From: Zeev Katz Date: Sat, 2 Jun 2018 14:51:56 +0300 Subject: [PATCH 1/7] migration rxjs 6.0.2 --- package.json | 5 +- src/services/json-api-datastore.service.ts | 66 ++++++++++++---------- 2 files changed, 39 insertions(+), 32 deletions(-) diff --git a/package.json b/package.json index 3a345501..2963be74 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ }, "peerDependencies": { "reflect-metadata": ">=0.1.3", - "rxjs": ">=5.2.0" + "rxjs": ">=6.2.0" }, "devDependencies": { "@angular/animations": "^4.4.3", @@ -70,7 +70,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/services/json-api-datastore.service.ts b/src/services/json-api-datastore.service.ts index 20ceae06..9d6104e9 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'; @@ -24,17 +21,16 @@ export class JsonApiDatastore { // tslint:disable-next-line:variable-name private _store: {[type: string]: {[id: string]: JsonApiModel}} = {}; private toQueryString: Function = this.datastoreConfig.overrides - && this.datastoreConfig.overrides.toQueryString ? - this.datastoreConfig.overrides.toQueryString : this._toQueryString; + && this.datastoreConfig.overrides.toQueryString ? + this.datastoreConfig.overrides.toQueryString : this._toQueryString; // tslint:enable:max-line-length 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,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 { From bfb3ae2ccc7c6ae444a2b7d7bd0c3d5049841087 Mon Sep 17 00:00:00 2001 From: Zeev Katz Date: Sat, 2 Jun 2018 15:18:59 +0300 Subject: [PATCH 2/7] added new unit tests for json api model --- src/models/json-api.model.spec.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/models/json-api.model.spec.ts b/src/models/json-api.model.spec.ts index 96ea0b83..9b85f317 100644 --- a/src/models/json-api.model.spec.ts +++ b/src/models/json-api.model.spec.ts @@ -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).toBeTruthy('Daniele'); + expect(author.hasDirtyAttributes).toBeFalsy(); + }); + }); }); From db6d626df03e9d9393cff4e17df3c7df0974ab4d Mon Sep 17 00:00:00 2001 From: Zeev Katz Date: Sun, 3 Jun 2018 08:46:53 +0300 Subject: [PATCH 3/7] reflect-metadata is a dev dependency and not a peer dependency --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index 2963be74..80198e7e 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,6 @@ "qs": "^6.5.1" }, "peerDependencies": { - "reflect-metadata": ">=0.1.3", "rxjs": ">=6.2.0" }, "devDependencies": { From 09b6bd182136e8cbfffd2d1c6affb7f4dcfcf2ec Mon Sep 17 00:00:00 2001 From: Zeev Katz Date: Sat, 9 Jun 2018 18:48:11 +0300 Subject: [PATCH 4/7] removed rxjs-compat --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index 80198e7e..d827d952 100644 --- a/package.json +++ b/package.json @@ -70,7 +70,6 @@ "reflect-metadata": "^0.1.3", "rimraf": "^2.6.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", From 66805327a065f03ae832933dade41c0c3e2cafad Mon Sep 17 00:00:00 2001 From: Zeev Katz Date: Sat, 9 Jun 2018 18:55:38 +0300 Subject: [PATCH 5/7] added indentation --- src/services/json-api-datastore.service.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/services/json-api-datastore.service.ts b/src/services/json-api-datastore.service.ts index 9d6104e9..b4dcbd71 100644 --- a/src/services/json-api-datastore.service.ts +++ b/src/services/json-api-datastore.service.ts @@ -21,8 +21,8 @@ export class JsonApiDatastore { // tslint:disable-next-line:variable-name private _store: {[type: string]: {[id: string]: JsonApiModel}} = {}; private toQueryString: Function = this.datastoreConfig.overrides - && this.datastoreConfig.overrides.toQueryString ? - this.datastoreConfig.overrides.toQueryString : this._toQueryString; + && this.datastoreConfig.overrides.toQueryString ? + this.datastoreConfig.overrides.toQueryString : this._toQueryString; // tslint:enable:max-line-length private get getDirtyAttributes() { @@ -150,8 +150,6 @@ export class JsonApiDatastore { ); } - - deleteRecord( modelType: ModelType, id: string, From 2b24fba68a1b197f1e9f09e0794cb0cc74a91061 Mon Sep 17 00:00:00 2001 From: Zeev Katz Date: Sat, 9 Jun 2018 19:03:50 +0300 Subject: [PATCH 6/7] testing if author name equal to the initial author name --- src/models/json-api.model.spec.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/models/json-api.model.spec.ts b/src/models/json-api.model.spec.ts index 9b85f317..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; @@ -158,14 +158,14 @@ describe('JsonApiModel', () => { } }); - it ('should return that has dirty attributes', () => { + it('should return that has dirty attributes', () => { author.name = 'New Name'; expect(author.hasDirtyAttributes).toBeTruthy(); }); - it ('should to rollback to the initial author name', () => { + it('should to rollback to the initial author name', () => { author.rollbackAttributes(); - expect(author.name).toBeTruthy('Daniele'); + expect(author.name).toEqual('Daniele'); expect(author.hasDirtyAttributes).toBeFalsy(); }); }); From e7c20ccff1df0b14305b39639bd06c3cf2c1f39d Mon Sep 17 00:00:00 2001 From: Zeev Katz Date: Sat, 9 Jun 2018 19:17:05 +0300 Subject: [PATCH 7/7] added rxjs-compat --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index d827d952..80198e7e 100644 --- a/package.json +++ b/package.json @@ -70,6 +70,7 @@ "reflect-metadata": "^0.1.3", "rimraf": "^2.6.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",