(request: () => Promise<T>, config?: OperateConfig)
| 36 | * @returns |
| 37 | */ |
| 38 | export const operator = async <T>(request: () => Promise<T>, config?: OperateConfig): Promise<[boolean, any?]> => { |
| 39 | const { setOperating, formatMessage, formatReason } = config || {}; |
| 40 | |
| 41 | try { |
| 42 | setOperating?.(true); |
| 43 | const res = await request(); |
| 44 | const content = formatMessage?.() ?? 'Operation successfully completed'; |
| 45 | if (!config?.hideToast) { |
| 46 | message.success(content); |
| 47 | } |
| 48 | return [true, res]; |
| 49 | } catch (err) { |
| 50 | console.error('Operation failed.', err); |
| 51 | const reason = formatReason?.(err) ?? (err as any).response?.data?.message ?? 'Operation failed.'; |
| 52 | message.error(reason); |
| 53 | return [false, err]; |
| 54 | } finally { |
| 55 | setOperating?.(false); |
| 56 | } |
| 57 | }; |
no test coverage detected