From 38694503efabb629bbff43e9163685f3f6724911 Mon Sep 17 00:00:00 2001 From: unnoq Date: Sat, 5 Apr 2025 16:10:54 +0700 Subject: [PATCH 1/3] feat!: update onFinish state --- packages/shared/src/interceptor.test-d.ts | 19 +++++++++++++++---- packages/shared/src/interceptor.ts | 8 +++++--- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/packages/shared/src/interceptor.test-d.ts b/packages/shared/src/interceptor.test-d.ts index e635048a5..00eccc621 100644 --- a/packages/shared/src/interceptor.test-d.ts +++ b/packages/shared/src/interceptor.test-d.ts @@ -57,8 +57,13 @@ it('onError', () => { }) it('onFinish', () => { - const interceptor: Interceptor<{ foo: string }, 'success', 'error'> = onFinish((state, options) => { - expectTypeOf(state).toEqualTypeOf<['success', null, 'success'] | [undefined, 'error', 'error']>() + const interceptor: Interceptor<{ foo: string }, 'success', 'error'> = onFinish(([error, data, isSuccess], options) => { + if (error || !isSuccess) { + expectTypeOf(error).toEqualTypeOf<'error'>() + } + else { + expectTypeOf(data).toEqualTypeOf<'success'>() + } expectTypeOf(options.foo).toEqualTypeOf() expectTypeOf(options.next).toBeCallableWith<[options?: { foo: string }]>() @@ -67,8 +72,14 @@ it('onFinish', () => { os.$context<{ something: string }>().use(onFinish(() => { })) - os.$context<{ something: string }>().use(onFinish((state, { context, next }) => { - expectTypeOf(state).toEqualTypeOf<[Awaited>, null, 'success'] | [undefined, Error, 'error']>() + os.$context<{ something: string }>().use(onFinish(([error, data, isSuccess], { context, next }) => { + if (error || !isSuccess) { + expectTypeOf(error).toEqualTypeOf() + } + else { + expectTypeOf(data).toEqualTypeOf>>() + } + expectTypeOf(context).toEqualTypeOf<{ something: string }>() expectTypeOf(next).toEqualTypeOf>() })).handler(({ context }) => { diff --git a/packages/shared/src/interceptor.ts b/packages/shared/src/interceptor.ts index cd1a845d1..ac6ec405c 100644 --- a/packages/shared/src/interceptor.ts +++ b/packages/shared/src/interceptor.ts @@ -63,7 +63,9 @@ export function onError = [TResult, null, 'success'] | [undefined, TError, 'error'] +export type OnFinishState = + | [error: TError, data: unknown, isSuccess: false] + | [error: null, data: TResult, isSuccess: true] /** * Can used for interceptors or middlewares @@ -83,11 +85,11 @@ export function onFinish { try { const result = await options.next() - state = [result, null, 'success'] + state = [null, result, true] return result } catch (error) { - state = [undefined, error, 'error'] + state = [error, undefined, false] throw error } finally { From 41d89a0da439e79741d56c9b92ffdc6024f592d7 Mon Sep 17 00:00:00 2001 From: unnoq Date: Sat, 5 Apr 2025 16:23:13 +0700 Subject: [PATCH 2/3] tests --- packages/shared/src/interceptor.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/shared/src/interceptor.test.ts b/packages/shared/src/interceptor.test.ts index 20f37da9d..58ecbe216 100644 --- a/packages/shared/src/interceptor.test.ts +++ b/packages/shared/src/interceptor.test.ts @@ -156,7 +156,7 @@ describe('onStart / onSuccess / onError / onFinish', () => { expect(onFinishFn).toHaveBeenCalledTimes(1) expect(onFinishFn).toHaveBeenCalledWith( - ['__main__', null, 'success'], + [null, '__main__', true], { foo: 'bar', next: expect.any(Function), @@ -197,7 +197,7 @@ describe('onStart / onSuccess / onError / onFinish', () => { expect(onFinishFn).toHaveBeenCalledTimes(1) expect(onFinishFn).toHaveBeenCalledWith( - [undefined, new Error('__error__'), 'error'], + [new Error('__error__'), undefined, false], { foo: 'bar', next: expect.any(Function), From 52787119f4128f80e11d4fd361c010b068939685 Mon Sep 17 00:00:00 2001 From: unnoq Date: Sat, 5 Apr 2025 16:38:15 +0700 Subject: [PATCH 3/3] fixed --- packages/shared/src/interceptor.test-d.ts | 4 ++++ packages/shared/src/interceptor.ts | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/shared/src/interceptor.test-d.ts b/packages/shared/src/interceptor.test-d.ts index 00eccc621..694c741e8 100644 --- a/packages/shared/src/interceptor.test-d.ts +++ b/packages/shared/src/interceptor.test-d.ts @@ -60,8 +60,10 @@ it('onFinish', () => { const interceptor: Interceptor<{ foo: string }, 'success', 'error'> = onFinish(([error, data, isSuccess], options) => { if (error || !isSuccess) { expectTypeOf(error).toEqualTypeOf<'error'>() + expectTypeOf(data).toEqualTypeOf() } else { + expectTypeOf(error).toEqualTypeOf() expectTypeOf(data).toEqualTypeOf<'success'>() } @@ -75,8 +77,10 @@ it('onFinish', () => { os.$context<{ something: string }>().use(onFinish(([error, data, isSuccess], { context, next }) => { if (error || !isSuccess) { expectTypeOf(error).toEqualTypeOf() + expectTypeOf(data).toEqualTypeOf() } else { + expectTypeOf(error).toEqualTypeOf() expectTypeOf(data).toEqualTypeOf>>() } diff --git a/packages/shared/src/interceptor.ts b/packages/shared/src/interceptor.ts index ac6ec405c..30c280d6f 100644 --- a/packages/shared/src/interceptor.ts +++ b/packages/shared/src/interceptor.ts @@ -64,7 +64,7 @@ export function onError = - | [error: TError, data: unknown, isSuccess: false] + | [error: TError, data: undefined, isSuccess: false] | [error: null, data: TResult, isSuccess: true] /**