Skip to content
Merged
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
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need rxjs-compat? My understanding ist, that this is only needed for backwards compatibility and the aim of this pull request is to update all rxjs related code to the current version.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But there are packages that depend on rxjs<6.0.0 and need rxjs-compat.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edit: Actually I needed to update my devDependencies as well :/ (angular to ^6)

I just checked it, but it seems the error message only looks like that there is a dependency problem.

The actual problem is, that there is still old rxjs code used:

I fixed both points locally and the servetest script runs through. Does it work for you to?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, additionally I had to update the following devDependencies:

    "@angular/animations": "^6.0.4",
    "@angular/common": "^6.0.4",
    "@angular/compiler": "^6.0.4",
    "@angular/compiler-cli": "^6.0.4",
    "@angular/core": "^6.0.4",
    "@angular/platform-browser": "^6.0.4",
    "@angular/platform-browser-dynamic": "^6.0.4",
    "@angular/platform-server": "^6.0.4",
...
    "codelyzer": "~4.2.1",
....
    "typescript": "2.7.2",
....
    "zone.js": "^0.8.26"

@safo6m: Would it make sense to do this in this pull request (as rxjs 6.x is only usable in angular 6+)?

"sourcemap-istanbul-instrumenter-loader": "^0.2.0",
"ts-loader": "^2.3.7",
"tslint": "^5.7.0",
Expand Down
22 changes: 21 additions & 1 deletion src/models/json-api.model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
});
});
});
64 changes: 34 additions & 30 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 @@ -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;
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,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<T extends JsonApiModel>(
modelType: ModelType<T>,
id: string,
Expand All @@ -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<T extends JsonApiModel>(modelType: ModelType<T>, id: string): T | null {
Expand Down Expand Up @@ -316,7 +320,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 +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<JsonApiModel>): any {
Expand Down