|
| 1 | +import type { ThrowableError } from '@orpc/shared' |
1 | 2 | import type { ORPCError } from './error' |
2 | 3 | import type { ClientContext, ClientOptions, ClientPromiseResult, FriendlyClientOptions } from './types' |
3 | 4 | import { isDefinedError } from './error' |
4 | 5 |
|
5 | | -export type SafeResult<TOutput, TError extends Error> = |
6 | | - | [error: null, data: TOutput, isDefined: false] |
7 | | - & { error: null, data: TOutput, isDefined: false } |
8 | | - | [error: Exclude<TError, ORPCError<any, any>>, data: undefined, isDefined: false] |
9 | | - & { error: Exclude<TError, ORPCError<any, any>>, data: undefined, isDefined: false } |
10 | | - | [error: Extract<TError, ORPCError<any, any>>, data: undefined, isDefined: true] |
11 | | - & { error: Extract<TError, ORPCError<any, any>>, data: undefined, isDefined: true } |
| 6 | +export type SafeResult<TOutput, TError> = |
| 7 | + | [error: null, data: TOutput, isDefined: false, success: true] |
| 8 | + & { error: null, data: TOutput, isDefined: false, success: true } |
| 9 | + | [error: Exclude<TError, ORPCError<any, any>>, data: undefined, isDefined: false, success: false] |
| 10 | + & { error: Exclude<TError, ORPCError<any, any>>, data: undefined, isDefined: false, success: false } |
| 11 | + | [error: Extract<TError, ORPCError<any, any>>, data: undefined, isDefined: true, success: false] |
| 12 | + & { error: Extract<TError, ORPCError<any, any>>, data: undefined, isDefined: true, success: false } |
12 | 13 |
|
13 | | -export async function safe<TOutput, TError extends Error>(promise: ClientPromiseResult<TOutput, TError>): Promise<SafeResult<TOutput, TError>> { |
| 14 | +export async function safe<TOutput, TError = ThrowableError>(promise: ClientPromiseResult<TOutput, TError>): Promise<SafeResult<TOutput, TError>> { |
14 | 15 | try { |
15 | 16 | const output = await promise |
16 | 17 | return Object.assign( |
17 | | - [null, output, false] satisfies [null, TOutput, false], |
18 | | - { error: null, data: output, isDefined: false as const }, |
| 18 | + [null, output, false, true] satisfies [null, TOutput, false, true], |
| 19 | + { error: null, data: output, isDefined: false as const, success: true as const }, |
19 | 20 | ) |
20 | 21 | } |
21 | 22 | catch (e) { |
22 | 23 | const error = e as TError |
23 | 24 |
|
24 | 25 | if (isDefinedError(error)) { |
25 | 26 | return Object.assign( |
26 | | - [error, undefined, true] satisfies [typeof error, undefined, true], |
27 | | - { error, data: undefined, isDefined: true as const }, |
| 27 | + [error, undefined, true, false] satisfies [typeof error, undefined, true, false], |
| 28 | + { error, data: undefined, isDefined: true as const, success: false as const }, |
28 | 29 | ) |
29 | 30 | } |
30 | 31 |
|
31 | 32 | return Object.assign( |
32 | | - [error as Exclude<TError, ORPCError<any, any>>, undefined, false] satisfies [Exclude<TError, ORPCError<any, any>>, undefined, false], |
33 | | - { error: error as Exclude<TError, ORPCError<any, any>>, data: undefined, isDefined: false as const }, |
| 33 | + [error as Exclude<TError, ORPCError<any, any>>, undefined, false, false] satisfies [Exclude<TError, ORPCError<any, any>>, undefined, false, false], |
| 34 | + { error: error as Exclude<TError, ORPCError<any, any>>, data: undefined, isDefined: false as const, success: false as const }, |
34 | 35 | ) |
35 | 36 | } |
36 | 37 | } |
|
0 commit comments