From 62f7073f0c0f773c0099e5a6f4f16bb8062f41c8 Mon Sep 17 00:00:00 2001 From: gibarila <47109545+gibarila@users.noreply.github.com> Date: Tue, 5 Mar 2019 17:32:30 +0200 Subject: [PATCH] Expending the usage of custom headers to: remove, removeItems, saveItems, queryItem and getItemById (#19) * test pass * small fix * repair in getById * pr fixes * more fixes --- lib/api/repository/readonly-repository.ts | 14 ++++++++++++-- lib/api/repository/repository.ts | 18 +++++++++--------- test/api/repository.spec.ts | 13 +++++++++++++ test/mock/todo-list.entity.ts | 1 + test/mock/todo.entity.ts | 3 ++- test/paris.spec.ts | 10 +++++----- 6 files changed, 42 insertions(+), 17 deletions(-) diff --git a/lib/api/repository/readonly-repository.ts b/lib/api/repository/readonly-repository.ts index a39dcb4..4bcae62 100644 --- a/lib/api/repository/readonly-repository.ts +++ b/lib/api/repository/readonly-repository.ts @@ -187,6 +187,7 @@ export class ReadonlyRepository imple */ queryItem(query: DataQuery, dataOptions: DataOptions = defaultDataOptions): Observable { let httpOptions:HttpOptions = this.getQueryHttpOptions(query); + let currentOptions = this.addCustomHeaders(query); let endpoint:string; @@ -197,7 +198,7 @@ export class ReadonlyRepository imple const getItem$:Observable = this.paris.dataStore.get( endpoint, - httpOptions, + Object.assign({}, httpOptions, currentOptions), this.getBaseUrl(query), this.entityBackendConfig.timeout ? { timeout: this.entityBackendConfig.timeout } : null).pipe( catchError((err: AjaxError) => { @@ -288,10 +289,11 @@ export class ReadonlyRepository imple } else { const endpoint:string = this.entityBackendConfig.parseItemQuery ? this.entityBackendConfig.parseItemQuery(itemId, this.entity, this.paris.config, params) : `${this.getEndpointName({ where: params })}/${itemId}`; + let currentOptions = this.addCustomHeaders(itemId); const getItem$:Observable = this.paris.dataStore.get( endpoint, - params && {params: params}, + Object.assign({}, params && {params: params}, currentOptions), this.getBaseUrl({where: params}), this.entityBackendConfig.timeout ? { timeout: this.entityBackendConfig.timeout } : null ).pipe( @@ -309,6 +311,14 @@ export class ReadonlyRepository imple } } + addCustomHeaders(data: any): Record{ + let currentOptions = {}; + if (this.entityBackendConfig.customHeaders){ + (currentOptions).customHeaders = this.entityBackendConfig.customHeaders instanceof Function ? this.entityBackendConfig.customHeaders(data, this.paris.config) : this.entityBackendConfig.customHeaders; + } + return currentOptions; + } + /** * Creates a JSON object that can be saved to server, with the reverse logic of getItemModelData * @param {TEntity} item diff --git a/lib/api/repository/repository.ts b/lib/api/repository/repository.ts index 81711fe..5210b84 100644 --- a/lib/api/repository/repository.ts +++ b/lib/api/repository/repository.ts @@ -60,10 +60,8 @@ export class Repository extends Reado const saveData: TRawData = this.serializeItem(item, serializationData); const endpointName:string = this.getEndpointName(options && options.params ? { where: options.params } : null); const endpoint:string = this.entityBackendConfig.parseSaveQuery ? this.entityBackendConfig.parseSaveQuery(item, this.entity, this.paris.config, options) : `${endpointName}/${item.id || ''}`; - let httpOptions = {data: saveData}; - if (this.entityBackendConfig.customHeaders){ - (httpOptions).customHeaders = this.entityBackendConfig.customHeaders instanceof Function ? this.entityBackendConfig.customHeaders(item, this.paris.config) : this.entityBackendConfig.customHeaders; - } + let httpOptions = this.addCustomHeaders(item); + (httpOptions).data = saveData; return this.paris.dataStore.save(endpoint, this.getSaveMethod(item), Object.assign({}, options, httpOptions), this.getBaseUrl(options && {where: options.params})) .pipe( catchError((err: AjaxError) => { @@ -147,8 +145,9 @@ export class Repository extends Reado : Object.assign({}, options, {data: {items: itemsData}}); const endpointName:string = this.getEndpointName(options && options.params ? { where: options.params } : null); + let currentOptions = this.addCustomHeaders(itemsData); - return this.paris.dataStore.save(`${endpointName}/`, method, saveHttpOptions, this.getBaseUrl(options && {where: options.params})) + return this.paris.dataStore.save(`${endpointName}/`, method, Object.assign({}, saveHttpOptions, currentOptions), this.getBaseUrl(options && {where: options.params})) .pipe( catchError((err: AjaxError) => { this.emitEntityHttpErrorEvent(err); @@ -179,14 +178,14 @@ export class Repository extends Reado let httpOptions:HttpOptions = options || { data: {}}; if (!httpOptions.data) httpOptions.data = {}; - + let currentOptions = this.addCustomHeaders(item); if (this.entityBackendConfig.getRemoveData) Object.assign(httpOptions.data, this.entityBackendConfig.getRemoveData([item])); const endpointName:string = this.getEndpointName(options && options.params ? { where: options.params } : null); const endpoint:string = this.entityBackendConfig.parseRemoveQuery ? this.entityBackendConfig.parseRemoveQuery([item], this.entity, this.paris.config) : `${endpointName}/${item.id || ''}`; - return this.paris.dataStore.delete(endpoint, httpOptions, this.getBaseUrl(options && {where: options.params})) + return this.paris.dataStore.delete(endpoint, Object.assign({}, httpOptions, currentOptions), this.getBaseUrl(options && {where: options.params})) .pipe( catchError((err: AjaxError) => { this.emitEntityHttpErrorEvent(err); @@ -234,7 +233,7 @@ export class Repository extends Reado let httpOptions:HttpOptions = options || { data: {}}; if (!httpOptions.data) httpOptions.data = {}; - + let currentOptions = this.addCustomHeaders(items); Object.assign(httpOptions.data, this.entityBackendConfig.getRemoveData ? this.entityBackendConfig.getRemoveData(items) : { ids: items.map(item => item.id) } @@ -243,7 +242,8 @@ export class Repository extends Reado const endpointName:string = this.getEndpointName(options && options.params ? { where: options.params } : null); const endpoint:string = this.entityBackendConfig.parseRemoveQuery ? this.entityBackendConfig.parseRemoveQuery(items, this.entity, this.paris.config) : endpointName; - return this.paris.dataStore.delete(endpoint, httpOptions, this.getBaseUrl(options && {where: options.params})) + + return this.paris.dataStore.delete(endpoint, Object.assign({}, httpOptions, currentOptions), this.getBaseUrl(options && {where: options.params})) .pipe( catchError((err: AjaxError) => { this.emitEntityHttpErrorEvent(err); diff --git a/test/api/repository.spec.ts b/test/api/repository.spec.ts index 0378d07..7d6cd69 100644 --- a/test/api/repository.spec.ts +++ b/test/api/repository.spec.ts @@ -69,7 +69,20 @@ describe('Repository', () => { expect(errorCallback).not.toBeCalled(); }); + + it ("filling custom headers to todoList", done => { + let dictionary = todoListRepo.addCustomHeaders(newTodoList); + done(); + expect(dictionary).toEqual({"customHeaders": {"headerKey": "headerValue"}}); + }); + + it ("filling custom headers to todo", done => { + let dictionary = todoRepo.addCustomHeaders(newTodoItem); + done(); + expect(dictionary).toEqual({"customHeaders": {"keyForNewTodoItem": "valueForNewTodoItem"}}); + }); }); + }); interface MockConfigData { diff --git a/test/mock/todo-list.entity.ts b/test/mock/todo-list.entity.ts index 15a4f3b..3cee2a6 100644 --- a/test/mock/todo-list.entity.ts +++ b/test/mock/todo-list.entity.ts @@ -8,6 +8,7 @@ import {TodoListState} from "./todo-list-state.value-object"; singularName: "Todo list", pluralName: "Todo lists", endpoint: (config:ParisConfig) => `v${config.data.version}/list`, + customHeaders: ({"headerKey": "headerValue"}), parseDataSet: (rawDataSet:TodoListRawDataSet) => ({ items: rawDataSet.lists, next: rawDataSet.$nextPage, diff --git a/test/mock/todo.entity.ts b/test/mock/todo.entity.ts index 4692ba3..6c4d38e 100644 --- a/test/mock/todo.entity.ts +++ b/test/mock/todo.entity.ts @@ -8,7 +8,8 @@ import {TodoStatus} from "./todo-status.entity"; singularName: "Todo item", pluralName: "Todo items", endpoint: "todo", - timeout: 20000 + timeout: 20000, + customHeaders: (data, config) => data ? (data.text === "New todo item" ? {"keyForNewTodoItem": "valueForNewTodoItem"} : {"keyForRegularTodoItem": "valueForRegularTodoItem"}) : {} }) export class Todo extends EntityModelBase{ @EntityField() diff --git a/test/paris.spec.ts b/test/paris.spec.ts index 712e859..8e29441 100644 --- a/test/paris.spec.ts +++ b/test/paris.spec.ts @@ -63,22 +63,22 @@ describe('Paris main', () => { expect(paris.dataStore.httpService.request).toHaveBeenCalledWith( 'GET', '/todo/1', - undefined, + {"customHeaders": {"keyForRegularTodoItem": "valueForRegularTodoItem"}}, { timeout: 20000 } ); }); it('should call Repository.getItemById with correct params', () => { - paris.getItemById(Todo, 1, null, { test: 1 , customHeaders: {'TestHeader': 'TestValue'}}); - expect(repo.getItemById).toHaveBeenCalledWith(1, defaultDataOptions, { test: 1, customHeaders: {'TestHeader': 'TestValue'} }); + paris.getItemById(Todo, 1, null, { test: 1 }); + expect(repo.getItemById).toHaveBeenCalledWith(1, defaultDataOptions, { test: 1 }); }); it('should call Http.request with correct params', () => { - paris.getItemById(Todo, 1, null, { test: 1, customHeaders: {'TestHeader': 'TestValue'}}); + paris.getItemById(Todo, 1, null, { test: 1}); expect(paris.dataStore.httpService.request).toHaveBeenCalledWith( 'GET', '/todo/1', - { params: { test: 1, customHeaders: {'TestHeader': 'TestValue'} }}, + { customHeaders: {'keyForRegularTodoItem': 'valueForRegularTodoItem'}, params: { test: 1 }}, { timeout: 20000 } ); });