Skip to content

Commit 3191601

Browse files
authored
fix(server): onSuccess, onError, ... cannot use as a middleware (#331)
Closes: https://discord.com/channels/1308966753044398161/1357918036568313906 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Refactor** - Improved internal processing to support more flexible outcomes, enhancing overall system stability. - **Tests** - Expanded test coverage for key processing stages to ensure consistent, reliable behavior. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent ce2f84d commit 3191601

2 files changed

Lines changed: 45 additions & 9 deletions

File tree

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import type { Context } from '../../server/src/context'
2+
import type { MiddlewareNextFn, MiddlewareResult } from '../../server/src/middleware'
13
import type { Interceptor } from './interceptor'
24
import type { PromiseWithError } from './types'
5+
import { os } from '../../server/src/builder'
36
import { onError, onFinish, onStart, onSuccess } from './interceptor'
47

58
it('onStart', () => {
@@ -8,6 +11,13 @@ it('onStart', () => {
811
expectTypeOf(options.next).toBeCallableWith<[options?: { foo: string }]>()
912
expectTypeOf(options.next()).toEqualTypeOf<PromiseWithError<'success', 'error'>>()
1013
})
14+
15+
os.$context<{ something: string }>().use(onStart(({ context, next }) => {
16+
expectTypeOf(context).toEqualTypeOf<{ something: string }>()
17+
expectTypeOf(next).toEqualTypeOf<MiddlewareNextFn<unknown>>()
18+
})).handler(({ context }) => {
19+
expectTypeOf(context).toMatchTypeOf<{ something: string }>()
20+
})
1121
})
1222

1323
it('onSuccess', () => {
@@ -18,6 +28,14 @@ it('onSuccess', () => {
1828
expectTypeOf(options.next).toBeCallableWith<[options?: { foo: string }]>()
1929
expectTypeOf(options.next()).toEqualTypeOf<PromiseWithError<'success', 'error'>>()
2030
})
31+
32+
os.$context<{ something: string }>().use(onSuccess((data, { context, next }) => {
33+
expectTypeOf(data).toEqualTypeOf <Awaited<MiddlewareResult<Context, unknown>>>()
34+
expectTypeOf(context).toEqualTypeOf<{ something: string }>()
35+
expectTypeOf(next).toEqualTypeOf<MiddlewareNextFn<unknown>>()
36+
})).handler(({ context }) => {
37+
expectTypeOf(context).toMatchTypeOf<{ something: string }>()
38+
})
2139
})
2240

2341
it('onError', () => {
@@ -28,6 +46,14 @@ it('onError', () => {
2846
expectTypeOf(options.next).toBeCallableWith<[options?: { foo: string }]>()
2947
expectTypeOf(options.next()).toEqualTypeOf<PromiseWithError<'success', 'error'>>()
3048
})
49+
50+
os.$context<{ something: string }>().use(onError((error, { context, next }) => {
51+
expectTypeOf(error).toEqualTypeOf<Error>()
52+
expectTypeOf(context).toEqualTypeOf<{ something: string }>()
53+
expectTypeOf(next).toEqualTypeOf<MiddlewareNextFn<unknown>>()
54+
})).handler(({ context }) => {
55+
expectTypeOf(context).toMatchTypeOf<{ something: string }>()
56+
})
3157
})
3258

3359
it('onFinish', () => {
@@ -38,4 +64,14 @@ it('onFinish', () => {
3864
expectTypeOf(options.next).toBeCallableWith<[options?: { foo: string }]>()
3965
expectTypeOf(options.next()).toEqualTypeOf<PromiseWithError<'success', 'error'>>()
4066
})
67+
68+
os.$context<{ something: string }>().use(onFinish(() => { }))
69+
70+
os.$context<{ something: string }>().use(onFinish((state, { context, next }) => {
71+
expectTypeOf(state).toEqualTypeOf<[Awaited<MiddlewareResult<Context, unknown>>, null, 'success'] | [undefined, Error, 'error']>()
72+
expectTypeOf(context).toEqualTypeOf<{ something: string }>()
73+
expectTypeOf(next).toEqualTypeOf<MiddlewareNextFn<unknown>>()
74+
})).handler(({ context }) => {
75+
expectTypeOf(context).toMatchTypeOf<{ something: string }>()
76+
})
4177
})

packages/shared/src/interceptor.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ export type Interceptor<
1515
TOptions extends InterceptableOptions,
1616
TResult,
1717
TError,
18-
> = (options: InterceptorOptions<TOptions, TResult, TError>) => PromiseWithError<TResult, TError>
18+
> = (options: InterceptorOptions<TOptions, TResult, TError>) => Promisable<TResult>
1919

2020
/**
2121
* Can used for interceptors or middlewares
2222
*/
23-
export function onStart<TOptions extends { next(): any }, TRest extends any[]>(
23+
export function onStart<T, TOptions extends { next(): any }, TRest extends any[]>(
2424
callback: NoInfer<(options: TOptions, ...rest: TRest) => Promisable<void>>,
25-
): (options: TOptions, ...rest: TRest) => Promise<Awaited<ReturnType<TOptions['next']>>> {
25+
): (options: TOptions, ...rest: TRest) => T | Promise<Awaited<ReturnType<TOptions['next']>>> {
2626
return async (options, ...rest) => {
2727
await callback(options, ...rest)
2828
return await options.next()
@@ -32,9 +32,9 @@ export function onStart<TOptions extends { next(): any }, TRest extends any[]>(
3232
/**
3333
* Can used for interceptors or middlewares
3434
*/
35-
export function onSuccess<TOptions extends { next(): any }, TRest extends any[]>(
35+
export function onSuccess<T, TOptions extends { next(): any }, TRest extends any[]>(
3636
callback: NoInfer<(result: Awaited<ReturnType<TOptions['next']>>, options: TOptions, ...rest: TRest) => Promisable<void>>,
37-
): (options: TOptions, ...rest: TRest) => Promise<Awaited<ReturnType<TOptions['next']>>> {
37+
): (options: TOptions, ...rest: TRest) => T | Promise<Awaited<ReturnType<TOptions['next']>>> {
3838
return async (options, ...rest) => {
3939
const result = await options.next()
4040
await callback(result, options, ...rest)
@@ -45,13 +45,13 @@ export function onSuccess<TOptions extends { next(): any }, TRest extends any[]>
4545
/**
4646
* Can used for interceptors or middlewares
4747
*/
48-
export function onError<TOptions extends { next(): any }, TRest extends any[]>(
48+
export function onError<T, TOptions extends { next(): any }, TRest extends any[]>(
4949
callback: NoInfer<(
5050
error: ReturnType<TOptions['next']> extends PromiseWithError<any, infer E> ? E : ThrowableError,
5151
options: TOptions,
5252
...rest: TRest
5353
) => Promisable<void>>,
54-
): (options: TOptions, ...rest: TRest) => Promise<Awaited<ReturnType<TOptions['next']>>> {
54+
): (options: TOptions, ...rest: TRest) => T | Promise<Awaited<ReturnType<TOptions['next']>>> {
5555
return async (options, ...rest) => {
5656
try {
5757
return await options.next()
@@ -68,7 +68,7 @@ export type OnFinishState<TResult, TError> = [TResult, null, 'success'] | [undef
6868
/**
6969
* Can used for interceptors or middlewares
7070
*/
71-
export function onFinish<TOptions extends { next(): any }, TRest extends any[]>(
71+
export function onFinish<T, TOptions extends { next(): any }, TRest extends any[]>(
7272
callback: NoInfer<(
7373
state: OnFinishState<
7474
Awaited<ReturnType<TOptions['next']>>,
@@ -77,7 +77,7 @@ export function onFinish<TOptions extends { next(): any }, TRest extends any[]>(
7777
options: TOptions,
7878
...rest: TRest
7979
) => Promisable<void>>,
80-
): (options: TOptions, ...rest: TRest) => Promise<Awaited<ReturnType<TOptions['next']>>> {
80+
): (options: TOptions, ...rest: TRest) => T | Promise<Awaited<ReturnType<TOptions['next']>>> {
8181
let state: any
8282

8383
return async (options, ...rest) => {

0 commit comments

Comments
 (0)