import { FractalResponseData, FractalResponseList } from '@/api/http'; type Transformer = (callback: FractalResponseData) => T; const isList = (data: FractalResponseList | FractalResponseData): data is FractalResponseList => data.object === 'list'; function transform(data: null | undefined, transformer: Transformer, missing?: M): M; function transform(data: FractalResponseData | null | undefined, transformer: Transformer, missing?: M): T | M; function transform(data: FractalResponseList | null | undefined, transformer: Transformer, missing?: M): T[] | M; function transform (data: FractalResponseData | FractalResponseList | null | undefined, transformer: Transformer, missing = undefined) { if (data === undefined || data === null) { return missing; } if (isList(data)) { return data.data.map(transformer); } if (!data || !data.attributes || data.object === 'null_resource') { return missing; } return transformer(data); } export { transform };