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
69 changes: 69 additions & 0 deletions packages/effect/src/middleware.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,75 @@ describe('middlewareGen', () => {
void procedure
})

it('infers everything without type arguments when created via os.middleware', () => {
interface ServerContext extends WithEffectContext<Service1> {
auth: boolean
}

const requireAuth = os
.$context<ServerContext>()
.middleware(middlewareGen(function* ({ context, next }) {
expectTypeOf(context.auth).toEqualTypeOf<boolean>()
yield* Service1

return yield* next({ context: { user: 'user' as const } })
}))

const procedure = os
.$context<ServerContext>()
.input(z.object({ id: z.string() }))
.output(z.string())
.use(requireAuth)
.handler(({ context }) => {
expectTypeOf(context.user).toEqualTypeOf<'user'>()

return 'output'
})

void procedure
})

it('supports standalone middleware on procedures with concrete input/output', () => {
interface ServerContext extends WithEffectContext<Service1> {
auth: boolean
}

const requireAuth = middlewareGen<ServerContext, { user: 'user' }>(
function* ({ context, next }) {
expectTypeOf(context.auth).toEqualTypeOf<boolean>()
yield* Service1

return yield* next({ context: { user: 'user' as const } })
},
)

const procedure = os
.$context<ServerContext>()
.input(z.object({ id: z.string() }))
.output(z.string())
.use(requireAuth)
.handler(({ context }) => {
expectTypeOf(context.user).toEqualTypeOf<'user'>()

return 'output'
})

void procedure

const decorated = os
.$context<ServerContext>()
.middleware(requireAuth)

const decoratedProcedure = os
.$context<ServerContext>()
.input(z.object({ id: z.string() }))
.output(z.string())
.use(decorated)
.handler(() => 'output')

void decoratedProcedure
})

it('can infer typed errors through os.middleware', () => {
// Unlike `.use`, `os.middleware` types `errors` from the builder's error map
// because its middleware parameter references the error map non-generically.
Expand Down
8 changes: 4 additions & 4 deletions packages/effect/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ export type AnyMiddlewareGen = MiddlewareGen<any, any, any, any, any, any>
*/
export function middlewareGen<
TInContext extends Context,
TInput,
TOutput,
TErrorMap extends ErrorMap,
TYield extends Effect.Effect<any, any, InferEffectServices<TInContext>>,
TOutContext extends Context = object,
TInput = unknown,
TOutput = any, // TOutput = any by default is important to make middleware can be used in any output by default
TErrorMap extends ErrorMap = Record<never, never>,
TYield extends Effect.Effect<any, any, InferEffectServices<TInContext>> = Effect.Effect<any, any, InferEffectServices<TInContext>>,
>(
middleware: MiddlewareGen<TInContext, TOutContext, TInput, TOutput, TYield, ORPCErrorConstructorMap<TErrorMap>>,
): Middleware<TInContext, TOutContext, TInput, TOutput, TErrorMap> {
Expand Down
Loading