feat: add transformPayloadFilter method to IDataTransformer and integrate it in getMany for filter transformation

This commit is contained in:
Firman Ramdhani
2026-07-20 14:29:20 +07:00
parent f2cff7699a
commit efa1c086a0
2 changed files with 24 additions and 1 deletions
@@ -75,6 +75,12 @@ export interface IDataTransformer<TEntity extends BaseEntity = BaseEntity, TDTO
* If not provided, falls back to `transformToDTO`. * If not provided, falls back to `transformToDTO`.
*/ */
transformEditPayload?(entity: Partial<TEntity>): Partial<TDTO>; transformEditPayload?(entity: Partial<TEntity>): Partial<TDTO>;
/**
* Transform the filter payload before a `getMany()` call.
* If not provided, falls back to identity.
*/
transformPayloadFilter?(filter: Record<string, any>): Record<string, any>;
} }
// ─── Abstract Base Transformer ────────────────────────────────── // ─── Abstract Base Transformer ──────────────────────────────────
@@ -213,4 +219,17 @@ export abstract class BaseDataTransformer<TEntity extends BaseEntity = BaseEntit
transformEditPayload(entity: Partial<TEntity>): Partial<TDTO> { transformEditPayload(entity: Partial<TEntity>): Partial<TDTO> {
return this.transformToDTO(entity as TEntity) as Partial<TDTO>; return this.transformToDTO(entity as TEntity) as Partial<TDTO>;
} }
/**
* Transform the filter payload before a `getMany()` call.
*
* Override this when getMany filters need special formatting
* (e.g., date formats, converting arrays to comma-separated strings).
*
* @param filter - The filter params from the frontend
* @returns The mapped filter params for the API query
*/
transformPayloadFilter(filter: Record<string, any>): Record<string, any> {
return filter;
}
} }
@@ -168,7 +168,11 @@ export abstract class BaseRemoteDataServices<E extends BaseEntity = BaseEntity,
* through `transformGetManyResponse()` before being returned. * through `transformGetManyResponse()` before being returned.
*/ */
async getMany<T = E[]>(config?: AxiosRequestConfig): Promise<ApiResponse<T>> { async getMany<T = E[]>(config?: AxiosRequestConfig): Promise<ApiResponse<T>> {
const result = await this.execute<T>(DESCRIPTORS.getMany, { config }); const finalConfig = config?.params && this.transformer?.transformPayloadFilter
? { ...config, params: this.transformer.transformPayloadFilter(config.params) }
: config;
const result = await this.execute<T>(DESCRIPTORS.getMany, { config: finalConfig });
if (this.transformer && Array.isArray(result.data)) { if (this.transformer && Array.isArray(result.data)) {
return { return {