Skip to content

Commit ee46dab

Browse files
committed
feat(client)!: rename success property to isSuccess in safe utility
For better readability and better compatible with `isDefined`
1 parent c40d0c9 commit ee46dab

4 files changed

Lines changed: 20 additions & 20 deletions

File tree

apps/content/docs/best-practices/no-throw-literal.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ Avoid using `any` or `unknown` for `throwableError` because doing so prevents th
3434
:::
3535

3636
:::tip
37-
If you configure `throwableError` as `null | undefined | {}`, adjust your code to check the `success` property:
37+
If you configure `throwableError` as `null | undefined | {}`, adjust your code to check the `isSuccess` property instead of `error`:
3838

3939
```ts
40-
const { error, data, success } = await safe(client('input'))
40+
const { error, data, isSuccess } = await safe(client('input'))
4141

42-
if (!success) {
42+
if (!isSuccess) {
4343
if (isDefinedError(error)) {
4444
// handle type-safe error
4545
}

packages/client/src/utils.test-d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ describe('safe', async () => {
77
const client = {} as Client<ClientContext, string, number, Error | ORPCError<'BAD_GATEWAY', { val: string }>>
88

99
it('tuple style', async () => {
10-
const [error, data, isDefined, success] = await safe(client('123'))
10+
const [error, data, isDefined, isSuccess] = await safe(client('123'))
1111

12-
if (error || !success) {
12+
if (error || !isSuccess) {
1313
expectTypeOf(error).toEqualTypeOf<Error | ORPCError<'BAD_GATEWAY', { val: string }>>()
1414
expectTypeOf(data).toEqualTypeOf<undefined>()
1515
expectTypeOf(isDefined).toEqualTypeOf<boolean>()
@@ -33,9 +33,9 @@ describe('safe', async () => {
3333
})
3434

3535
it('object style', async () => {
36-
const { error, data, isDefined, success } = await safe(client('123'))
36+
const { error, data, isDefined, isSuccess } = await safe(client('123'))
3737

38-
if (error || !success) {
38+
if (error || !isSuccess) {
3939
expectTypeOf(error).toEqualTypeOf<Error | ORPCError<'BAD_GATEWAY', { val: string }>>()
4040
expectTypeOf(data).toEqualTypeOf<undefined>()
4141
expectTypeOf(isDefined).toEqualTypeOf<boolean>()

packages/client/src/utils.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@ import { resolveFriendlyClientOptions, safe } from './utils'
44
it('safe', async () => {
55
const r1 = await safe(Promise.resolve(1))
66
expect([...r1]).toEqual([null, 1, false, true])
7-
expect({ ...r1 }).toEqual(expect.objectContaining({ error: null, data: 1, isDefined: false, success: true }))
7+
expect({ ...r1 }).toEqual(expect.objectContaining({ error: null, data: 1, isDefined: false, isSuccess: true }))
88

99
const e2 = new Error('error')
1010
const r2 = await safe(Promise.reject(e2))
1111
expect([...r2]).toEqual([e2, undefined, false, false])
12-
expect({ ...r2 }).toEqual(expect.objectContaining({ error: e2, data: undefined, isDefined: false, success: false }))
12+
expect({ ...r2 }).toEqual(expect.objectContaining({ error: e2, data: undefined, isDefined: false, isSuccess: false }))
1313

1414
const e3 = new ORPCError('BAD_GATEWAY', { defined: true })
1515
const r3 = await safe(Promise.reject(e3))
1616
expect([...r3]).toEqual([e3, undefined, true, false])
17-
expect({ ...r3 }).toEqual(expect.objectContaining({ error: e3, data: undefined, isDefined: true, success: false }))
17+
expect({ ...r3 }).toEqual(expect.objectContaining({ error: e3, data: undefined, isDefined: true, isSuccess: false }))
1818

1919
const e4 = new ORPCError('BAD_GATEWAY')
2020
const r4 = await safe(Promise.reject(e4))
2121
expect([...r4]).toEqual([e4, undefined, false, false])
22-
expect({ ...r4 }).toEqual(expect.objectContaining({ error: e4, data: undefined, isDefined: false, success: false }))
22+
expect({ ...r4 }).toEqual(expect.objectContaining({ error: e4, data: undefined, isDefined: false, isSuccess: false }))
2323
})
2424

2525
it('resolveFriendlyClientOptions', () => {

packages/client/src/utils.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ import type { ClientContext, ClientOptions, ClientPromiseResult, FriendlyClientO
44
import { isDefinedError } from './error'
55

66
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 }
7+
| [error: null, data: TOutput, isDefined: false, isSuccess: true]
8+
& { error: null, data: TOutput, isDefined: false, isSuccess: true }
9+
| [error: Exclude<TError, ORPCError<any, any>>, data: undefined, isDefined: false, isSuccess: false]
10+
& { error: Exclude<TError, ORPCError<any, any>>, data: undefined, isDefined: false, isSuccess: false }
11+
| [error: Extract<TError, ORPCError<any, any>>, data: undefined, isDefined: true, isSuccess: false]
12+
& { error: Extract<TError, ORPCError<any, any>>, data: undefined, isDefined: true, isSuccess: false }
1313

1414
export async function safe<TOutput, TError = ThrowableError>(promise: ClientPromiseResult<TOutput, TError>): Promise<SafeResult<TOutput, TError>> {
1515
try {
1616
const output = await promise
1717
return Object.assign(
1818
[null, output, false, true] satisfies [null, TOutput, false, true],
19-
{ error: null, data: output, isDefined: false as const, success: true as const },
19+
{ error: null, data: output, isDefined: false as const, isSuccess: true as const },
2020
)
2121
}
2222
catch (e) {
@@ -25,13 +25,13 @@ export async function safe<TOutput, TError = ThrowableError>(promise: ClientProm
2525
if (isDefinedError(error)) {
2626
return Object.assign(
2727
[error, undefined, true, false] satisfies [typeof error, undefined, true, false],
28-
{ error, data: undefined, isDefined: true as const, success: false as const },
28+
{ error, data: undefined, isDefined: true as const, isSuccess: false as const },
2929
)
3030
}
3131

3232
return Object.assign(
3333
[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+
{ error: error as Exclude<TError, ORPCError<any, any>>, data: undefined, isDefined: false as const, isSuccess: false as const },
3535
)
3636
}
3737
}

0 commit comments

Comments
 (0)