diff --git a/src/models/json-api.model.ts b/src/models/json-api.model.ts index bc0857ce..a0a967fd 100644 --- a/src/models/json-api.model.ts +++ b/src/models/json-api.model.ts @@ -56,9 +56,38 @@ export class JsonApiModel { this.lastSyncModels = included; } - public save(params?: any, headers?: HttpHeaders, customUrl?: string): Observable { + public save(): Observable; + public save(params: {[s: string]: any}): Observable; + public save(headers: HttpHeaders): Observable; + public save(customUrl: string): Observable; + public save(params: {[s: string]: any}, headers: HttpHeaders): Observable; + public save(params: {[s: string]: any}, customUrl: string): Observable; + public save(headers: HttpHeaders, customUrl: string): Observable; + public save(params: {[s: string]: any}, headers: HttpHeaders, customUrl: string): Observable; + public save(...args: any[]): Observable { 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); } diff --git a/src/services/json-api-datastore.service.spec.ts b/src/services/json-api-datastore.service.spec.ts index 7d6da9d0..c7ebc051 100644 --- a/src/services/json-api-datastore.service.spec.ts +++ b/src/services/json-api-datastore.service.spec.ts @@ -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; @@ -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) => 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, {