method to extend data source response data

This commit is contained in:
Niels Lyngsø
2023-07-04 20:21:53 +02:00
parent 441dd93f5c
commit eeb3929b1a
2 changed files with 22 additions and 3 deletions

View File

@@ -0,0 +1,18 @@
import { DataSourceResponse } from '../index.js';
type FilterKeys<T, U> = {
[K in keyof T]: K extends keyof U ? never : K;
};
type Diff<U, T> = Pick<T, FilterKeys<T, U>[keyof T]>;
export function extendDataSourceResponseData<
ExtendedDataType extends IncomingDataType,
IncomingDataType extends object = object,
MissingPropsType extends object = Omit<Diff<IncomingDataType, ExtendedDataType>, '$type'>,
// Maybe this Omit<..., "$ype"> can be removed, but for now it kept showing up as a difference, though its not a difference on the two types.
ToType = IncomingDataType & ExtendedDataType
>(response: DataSourceResponse<IncomingDataType>, appendData: MissingPropsType): DataSourceResponse<ToType> {
if (response.data === undefined) return response as unknown as DataSourceResponse<ToType>;
return { ...response, data: { ...response.data, ...appendData } as unknown as ToType };
}

View File

@@ -1,8 +1,9 @@
export * from './collection-data-source.interface.js';
export * from './copy-data-source.interface.js';
export * from './data-source-response.interface.js';
export * from './data-source.interface.js';
export * from './extend-data-source-response-data.function.js';
export * from './folder-data-source.interface.js';
export * from './tree-data-source.interface.js';
export * from './item-data-source.interface.js';
export * from './move-data-source.interface.js';
export * from './copy-data-source.interface.js';
export * from './collection-data-source.interface.js';
export * from './tree-data-source.interface.js';