Skip to content
Open
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
31 changes: 30 additions & 1 deletion src/models/json-api.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,38 @@ export class JsonApiModel {
this.lastSyncModels = included;
}

public save(params?: any, headers?: HttpHeaders, customUrl?: string): Observable<this> {
public save(): Observable<this>;
public save(params: {[s: string]: any}): Observable<this>;
public save(headers: HttpHeaders): Observable<this>;
public save(customUrl: string): Observable<this>;
public save(params: {[s: string]: any}, headers: HttpHeaders): Observable<this>;
public save(params: {[s: string]: any}, customUrl: string): Observable<this>;
public save(headers: HttpHeaders, customUrl: string): Observable<this>;
public save(params: {[s: string]: any}, headers: HttpHeaders, customUrl: string): Observable<this>;
public save(...args: any[]): Observable<this> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would rather have a single argument instead of this type checking.
something like:

public save(options: SaveRequestOptions): Observable<this>;

@iamruslanbakirov what do you think about that?

Copy link
Author

Choose a reason for hiding this comment

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

Maybe you right, but typescript has overloading and it's great feature for this cause. And I saw same implementation in AngularJS.

so, when you have only one parameter you should be again and again create options object. it's bored 😑

this.checkChanges();
const attributesMetadata: any = this[AttributeMetadataIndex];
let [params, headers, customUrl] = args;

const length = args.length;

if (length) {
if (params instanceof HttpHeaders) {
headers = params;
params = {};
}

if (typeof params === 'string') {
customUrl = params;
params = {};
}

if (typeof headers === 'string') {
customUrl = headers;
headers = undefined;
}
}

return this.internalDatastore.saveRecord(attributesMetadata, this, params, headers, customUrl);
}

Expand Down
19 changes: 18 additions & 1 deletion src/services/json-api-datastore.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
BASE_URL_FROM_CONFIG,
DatastoreWithConfig
} from '../../test/datastore-with-config.service';
import { HttpHeaders } from '@angular/common/http';
import { HttpHeaders, HttpRequest } from '@angular/common/http';

let datastore: Datastore;
let datastoreWithConfig: DatastoreWithConfig;
Expand Down Expand Up @@ -321,6 +321,23 @@ describe('JsonApiDatastore', () => {
}, { status: 201, statusText: 'Created' });
});

it('should create new author with difference arguments', () => {
const expectedUrl = `${BASE_URL}/${API_VERSION}/author/internal/`;
const author = datastore.createRecord(Author, {
name: AUTHOR_NAME,
date_of_birth: AUTHOR_BIRTH
});

author.save(`${BASE_URL}/${API_VERSION}/author/internal/`).subscribe();
httpMock.expectOne({ method: 'POST', url: expectedUrl });

author.save({ check: 'params' }, `${BASE_URL}/${API_VERSION}/author/internal/`).subscribe();
httpMock.expectOne({ method: 'POST', url: `${expectedUrl}?check=params` });

author.save(new HttpHeaders().set('Auth', '123')).subscribe();
httpMock.expectOne((req: HttpRequest<any>) => req.headers.get('Auth') === '123');
});

it('should throw error on new author with 201 response but no body', () => {
const expectedUrl = `${BASE_URL}/${API_VERSION}/authors`;
const author = datastore.createRecord(Author, {
Expand Down