зеркало из https://github.com/microsoft/paris.git
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
This commit is contained in:
Родитель
35a180c93a
Коммит
62f7073f0c
|
@ -187,6 +187,7 @@ export class ReadonlyRepository<TEntity extends ModelBase, TRawData = any> imple
|
|||
*/
|
||||
queryItem(query: DataQuery, dataOptions: DataOptions = defaultDataOptions): Observable<TEntity> {
|
||||
let httpOptions:HttpOptions = this.getQueryHttpOptions(query);
|
||||
let currentOptions = this.addCustomHeaders(query);
|
||||
|
||||
let endpoint:string;
|
||||
|
||||
|
@ -197,7 +198,7 @@ export class ReadonlyRepository<TEntity extends ModelBase, TRawData = any> imple
|
|||
|
||||
const getItem$:Observable<TEntity> = 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<TEntity extends ModelBase, TRawData = any> 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<TEntity> = 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<TEntity extends ModelBase, TRawData = any> imple
|
|||
}
|
||||
}
|
||||
|
||||
addCustomHeaders(data: any): Record<string,string>{
|
||||
let currentOptions = {};
|
||||
if (this.entityBackendConfig.customHeaders){
|
||||
(<any>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
|
||||
|
|
|
@ -60,10 +60,8 @@ export class Repository<TEntity extends ModelBase, TRawData = any> 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){
|
||||
(<any>httpOptions).customHeaders = this.entityBackendConfig.customHeaders instanceof Function ? this.entityBackendConfig.customHeaders(item, this.paris.config) : this.entityBackendConfig.customHeaders;
|
||||
}
|
||||
let httpOptions = this.addCustomHeaders(item);
|
||||
(<any>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<TEntity extends ModelBase, TRawData = any> 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<TEntity extends ModelBase, TRawData = any> 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<TEntity extends ModelBase, TRawData = any> 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<TEntity extends ModelBase, TRawData = any> 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);
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -8,6 +8,7 @@ import {TodoListState} from "./todo-list-state.value-object";
|
|||
singularName: "Todo list",
|
||||
pluralName: "Todo lists",
|
||||
endpoint: (config:ParisConfig<MockConfigData>) => `v${config.data.version}/list`,
|
||||
customHeaders: ({"headerKey": "headerValue"}),
|
||||
parseDataSet: (rawDataSet:TodoListRawDataSet) => ({
|
||||
items: rawDataSet.lists,
|
||||
next: rawDataSet.$nextPage,
|
||||
|
|
|
@ -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<number>{
|
||||
@EntityField()
|
||||
|
|
|
@ -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 }
|
||||
);
|
||||
});
|
||||
|
|
Загрузка…
Ссылка в новой задаче