import { AnyFunction } from '@mds-core/mds-types' ; import { ServiceResponse , ServiceErrorDescriptor , ServiceErrorDescriptorType } from '../@types' ; export const isServiceError = < E extends string = ServiceErrorDescriptorType > ( error , ... types ) : error is ServiceErrorDescriptor < E > => typeof error === 'object' && error !== null && ( error as ServiceErrorDescriptor < E > ) . isServiceError === true && ( types . length === 0 || types . includes ( ( error as ServiceErrorDescriptor < E > ) . type ) ) ; export type ServiceResponseMethod < R = any > = AnyFunction < Promise < ServiceResponse < R > > > ; export const UnwrapServiceResult = < M extends ServiceResponseMethod > ( method ) => async ( ... args ) : Promise < ReturnType < M > extends Promise < ServiceResponse < infer R > > ? R : never > => { const response = await method ( ... args ) ; if ( response . error ) { throw response . error ; } return response . result ; } ;