Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions packages/shared/src/interceptor.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { Context } from '../../server/src/context'
import type { MiddlewareNextFn, MiddlewareResult } from '../../server/src/middleware'
import type { Interceptor } from './interceptor'
import type { PromiseWithError } from './types'
import { os } from '../../server/src/builder'
import { onError, onFinish, onStart, onSuccess } from './interceptor'

it('onStart', () => {
Expand All @@ -8,6 +11,13 @@ it('onStart', () => {
expectTypeOf(options.next).toBeCallableWith<[options?: { foo: string }]>()
expectTypeOf(options.next()).toEqualTypeOf<PromiseWithError<'success', 'error'>>()
})

os.$context<{ something: string }>().use(onStart(({ context, next }) => {
expectTypeOf(context).toEqualTypeOf<{ something: string }>()
expectTypeOf(next).toEqualTypeOf<MiddlewareNextFn<unknown>>()
})).handler(({ context }) => {
expectTypeOf(context).toMatchTypeOf<{ something: string }>()
})
})

it('onSuccess', () => {
Expand All @@ -18,6 +28,14 @@ it('onSuccess', () => {
expectTypeOf(options.next).toBeCallableWith<[options?: { foo: string }]>()
expectTypeOf(options.next()).toEqualTypeOf<PromiseWithError<'success', 'error'>>()
})

os.$context<{ something: string }>().use(onSuccess((data, { context, next }) => {
expectTypeOf(data).toEqualTypeOf <Awaited<MiddlewareResult<Context, unknown>>>()
expectTypeOf(context).toEqualTypeOf<{ something: string }>()
expectTypeOf(next).toEqualTypeOf<MiddlewareNextFn<unknown>>()
})).handler(({ context }) => {
expectTypeOf(context).toMatchTypeOf<{ something: string }>()
})
})

it('onError', () => {
Expand All @@ -28,6 +46,14 @@ it('onError', () => {
expectTypeOf(options.next).toBeCallableWith<[options?: { foo: string }]>()
expectTypeOf(options.next()).toEqualTypeOf<PromiseWithError<'success', 'error'>>()
})

os.$context<{ something: string }>().use(onError((error, { context, next }) => {
expectTypeOf(error).toEqualTypeOf<Error>()
expectTypeOf(context).toEqualTypeOf<{ something: string }>()
expectTypeOf(next).toEqualTypeOf<MiddlewareNextFn<unknown>>()
})).handler(({ context }) => {
expectTypeOf(context).toMatchTypeOf<{ something: string }>()
})
})

it('onFinish', () => {
Expand All @@ -38,4 +64,14 @@ it('onFinish', () => {
expectTypeOf(options.next).toBeCallableWith<[options?: { foo: string }]>()
expectTypeOf(options.next()).toEqualTypeOf<PromiseWithError<'success', 'error'>>()
})

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

os.$context<{ something: string }>().use(onFinish((state, { context, next }) => {
expectTypeOf(state).toEqualTypeOf<[Awaited<MiddlewareResult<Context, unknown>>, null, 'success'] | [undefined, Error, 'error']>()
expectTypeOf(context).toEqualTypeOf<{ something: string }>()
expectTypeOf(next).toEqualTypeOf<MiddlewareNextFn<unknown>>()
})).handler(({ context }) => {
expectTypeOf(context).toMatchTypeOf<{ something: string }>()
})
})
18 changes: 9 additions & 9 deletions packages/shared/src/interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ export type Interceptor<
TOptions extends InterceptableOptions,
TResult,
TError,
> = (options: InterceptorOptions<TOptions, TResult, TError>) => PromiseWithError<TResult, TError>
> = (options: InterceptorOptions<TOptions, TResult, TError>) => Promisable<TResult>

/**
* Can used for interceptors or middlewares
*/
export function onStart<TOptions extends { next(): any }, TRest extends any[]>(
export function onStart<T, TOptions extends { next(): any }, TRest extends any[]>(
callback: NoInfer<(options: TOptions, ...rest: TRest) => Promisable<void>>,
): (options: TOptions, ...rest: TRest) => Promise<Awaited<ReturnType<TOptions['next']>>> {
): (options: TOptions, ...rest: TRest) => T | Promise<Awaited<ReturnType<TOptions['next']>>> {
return async (options, ...rest) => {
await callback(options, ...rest)
return await options.next()
Expand All @@ -32,9 +32,9 @@ export function onStart<TOptions extends { next(): any }, TRest extends any[]>(
/**
* Can used for interceptors or middlewares
*/
export function onSuccess<TOptions extends { next(): any }, TRest extends any[]>(
export function onSuccess<T, TOptions extends { next(): any }, TRest extends any[]>(
callback: NoInfer<(result: Awaited<ReturnType<TOptions['next']>>, options: TOptions, ...rest: TRest) => Promisable<void>>,
): (options: TOptions, ...rest: TRest) => Promise<Awaited<ReturnType<TOptions['next']>>> {
): (options: TOptions, ...rest: TRest) => T | Promise<Awaited<ReturnType<TOptions['next']>>> {
return async (options, ...rest) => {
const result = await options.next()
await callback(result, options, ...rest)
Expand All @@ -45,13 +45,13 @@ export function onSuccess<TOptions extends { next(): any }, TRest extends any[]>
/**
* Can used for interceptors or middlewares
*/
export function onError<TOptions extends { next(): any }, TRest extends any[]>(
export function onError<T, TOptions extends { next(): any }, TRest extends any[]>(
callback: NoInfer<(
error: ReturnType<TOptions['next']> extends PromiseWithError<any, infer E> ? E : ThrowableError,
options: TOptions,
...rest: TRest
) => Promisable<void>>,
): (options: TOptions, ...rest: TRest) => Promise<Awaited<ReturnType<TOptions['next']>>> {
): (options: TOptions, ...rest: TRest) => T | Promise<Awaited<ReturnType<TOptions['next']>>> {
return async (options, ...rest) => {
try {
return await options.next()
Expand All @@ -68,7 +68,7 @@ export type OnFinishState<TResult, TError> = [TResult, null, 'success'] | [undef
/**
* Can used for interceptors or middlewares
*/
export function onFinish<TOptions extends { next(): any }, TRest extends any[]>(
export function onFinish<T, TOptions extends { next(): any }, TRest extends any[]>(
callback: NoInfer<(
state: OnFinishState<
Awaited<ReturnType<TOptions['next']>>,
Expand All @@ -77,7 +77,7 @@ export function onFinish<TOptions extends { next(): any }, TRest extends any[]>(
options: TOptions,
...rest: TRest
) => Promisable<void>>,
): (options: TOptions, ...rest: TRest) => Promise<Awaited<ReturnType<TOptions['next']>>> {
): (options: TOptions, ...rest: TRest) => T | Promise<Awaited<ReturnType<TOptions['next']>>> {
let state: any

return async (options, ...rest) => {
Expand Down