Skip to content

Commit aa59d19

Browse files
authored
feat!: update onFinish state (#332)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Refactor** - Enhanced clarity in the management of operation outcomes, providing more explicit error and success indications. - **Tests** - Modified test cases to align with the updated outcome handling, ensuring accurate validation of results. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 3191601 commit aa59d19

3 files changed

Lines changed: 26 additions & 9 deletions

File tree

packages/shared/src/interceptor.test-d.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,15 @@ it('onError', () => {
5757
})
5858

5959
it('onFinish', () => {
60-
const interceptor: Interceptor<{ foo: string }, 'success', 'error'> = onFinish((state, options) => {
61-
expectTypeOf(state).toEqualTypeOf<['success', null, 'success'] | [undefined, 'error', 'error']>()
60+
const interceptor: Interceptor<{ foo: string }, 'success', 'error'> = onFinish(([error, data, isSuccess], options) => {
61+
if (error || !isSuccess) {
62+
expectTypeOf(error).toEqualTypeOf<'error'>()
63+
expectTypeOf(data).toEqualTypeOf<undefined>()
64+
}
65+
else {
66+
expectTypeOf(error).toEqualTypeOf<null>()
67+
expectTypeOf(data).toEqualTypeOf<'success'>()
68+
}
6269

6370
expectTypeOf(options.foo).toEqualTypeOf<string>()
6471
expectTypeOf(options.next).toBeCallableWith<[options?: { foo: string }]>()
@@ -67,8 +74,16 @@ it('onFinish', () => {
6774

6875
os.$context<{ something: string }>().use(onFinish(() => { }))
6976

70-
os.$context<{ something: string }>().use(onFinish((state, { context, next }) => {
71-
expectTypeOf(state).toEqualTypeOf<[Awaited<MiddlewareResult<Context, unknown>>, null, 'success'] | [undefined, Error, 'error']>()
77+
os.$context<{ something: string }>().use(onFinish(([error, data, isSuccess], { context, next }) => {
78+
if (error || !isSuccess) {
79+
expectTypeOf(error).toEqualTypeOf<Error>()
80+
expectTypeOf(data).toEqualTypeOf<undefined>()
81+
}
82+
else {
83+
expectTypeOf(error).toEqualTypeOf<null>()
84+
expectTypeOf(data).toEqualTypeOf<Awaited<MiddlewareResult<Context, unknown>>>()
85+
}
86+
7287
expectTypeOf(context).toEqualTypeOf<{ something: string }>()
7388
expectTypeOf(next).toEqualTypeOf<MiddlewareNextFn<unknown>>()
7489
})).handler(({ context }) => {

packages/shared/src/interceptor.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ describe('onStart / onSuccess / onError / onFinish', () => {
156156

157157
expect(onFinishFn).toHaveBeenCalledTimes(1)
158158
expect(onFinishFn).toHaveBeenCalledWith(
159-
['__main__', null, 'success'],
159+
[null, '__main__', true],
160160
{
161161
foo: 'bar',
162162
next: expect.any(Function),
@@ -197,7 +197,7 @@ describe('onStart / onSuccess / onError / onFinish', () => {
197197

198198
expect(onFinishFn).toHaveBeenCalledTimes(1)
199199
expect(onFinishFn).toHaveBeenCalledWith(
200-
[undefined, new Error('__error__'), 'error'],
200+
[new Error('__error__'), undefined, false],
201201
{
202202
foo: 'bar',
203203
next: expect.any(Function),

packages/shared/src/interceptor.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ export function onError<T, TOptions extends { next(): any }, TRest extends any[]
6363
}
6464
}
6565

66-
export type OnFinishState<TResult, TError> = [TResult, null, 'success'] | [undefined, TError, 'error']
66+
export type OnFinishState<TResult, TError> =
67+
| [error: TError, data: undefined, isSuccess: false]
68+
| [error: null, data: TResult, isSuccess: true]
6769

6870
/**
6971
* Can used for interceptors or middlewares
@@ -83,11 +85,11 @@ export function onFinish<T, TOptions extends { next(): any }, TRest extends any[
8385
return async (options, ...rest) => {
8486
try {
8587
const result = await options.next()
86-
state = [result, null, 'success']
88+
state = [null, result, true]
8789
return result
8890
}
8991
catch (error) {
90-
state = [undefined, error, 'error']
92+
state = [error, undefined, false]
9193
throw error
9294
}
9395
finally {

0 commit comments

Comments
 (0)