export const capture = any>(fn: T) => { type P = Parameters; type R = ReturnType; type IRes = R extends Promise ? Promise<{ err: string | null; data: U | null }> : { err: string | null; data: R | null }; return (...args: P): IRes => { try { const res = fn(...args); if (res instanceof Promise) { return res .then((data) => ({ err: null, data, })) .catch((err) => ({ err: err.message, data: null, })) as IRes; } return { err: null, data: res, } as IRes; } catch (error) { return { err: error.message, data: null, } as IRes; } }; }; export const jsonParse = (text: string) => capture<(text: string) => T>(JSON.parse)(text);