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
432 changes: 267 additions & 165 deletions packages/server/src/builder-variants.test-d.ts

Large diffs are not rendered by default.

107 changes: 56 additions & 51 deletions packages/server/src/builder-variants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AnySchema, ContractRouter, ErrorMap, HTTPPath, InferSchemaInput, InferSchemaOutput, MergedErrorMap, Meta, Route, Schema } from '@orpc/contract'
import type { BuilderDef } from './builder'
import type { ConflictContextGuard, Context, MergedContext } from './context'
import type { Context, ContextExtendsGuard, MergedCurrentContext, MergedInitialContext } from './context'
import type { ORPCErrorConstructorMap } from './error'
import type { Lazy } from './lazy'
import type { MapInputMiddleware, Middleware } from './middleware'
Expand Down Expand Up @@ -30,19 +30,19 @@ export interface BuilderWithMiddlewares<
TMeta
>

'use'<UOutContext extends Context>(
'use'<UOutContext extends Context, UInContext extends Context = TCurrentContext>(
middleware: Middleware<
TCurrentContext,
UInContext,
UOutContext,
unknown,
unknown,
ORPCErrorConstructorMap<TErrorMap>,
TMeta
>,
): ConflictContextGuard<MergedContext<TCurrentContext, UOutContext>> &
BuilderWithMiddlewares<
TInitialContext,
MergedContext<TCurrentContext, UOutContext>,
): ContextExtendsGuard<TCurrentContext, UInContext>
& BuilderWithMiddlewares<
MergedInitialContext<TInitialContext, UInContext, TCurrentContext>,
MergedCurrentContext<TCurrentContext, UOutContext>,
TInputSchema,
TOutputSchema,
TErrorMap,
Expand Down Expand Up @@ -75,11 +75,11 @@ export interface BuilderWithMiddlewares<

'router'<U extends Router<ContractRouter<TMeta>, TCurrentContext>>(
router: U
): EnhancedRouter<U, TInitialContext, TErrorMap>
): EnhancedRouter<U, TInitialContext, TCurrentContext, TErrorMap>

'lazy'<U extends Router<ContractRouter<TMeta>, TCurrentContext>>(
loader: () => Promise<{ default: U }>,
): EnhancedRouter<Lazy<U>, TInitialContext, TErrorMap>
): EnhancedRouter<Lazy<U>, TInitialContext, TCurrentContext, TErrorMap>
}

export interface ProcedureBuilder<
Expand All @@ -103,19 +103,19 @@ export interface ProcedureBuilder<
TMeta
>

'use'<UOutContext extends Context>(
'use'<UOutContext extends Context, UInContext extends Context = TCurrentContext>(
middleware: Middleware<
TCurrentContext,
UInContext,
UOutContext,
unknown,
unknown,
ORPCErrorConstructorMap<TErrorMap>,
TMeta
>,
): ConflictContextGuard<MergedContext<TCurrentContext, UOutContext>> &
ProcedureBuilder<
TInitialContext,
MergedContext<TCurrentContext, UOutContext>,
): ContextExtendsGuard<TCurrentContext, UInContext>
& ProcedureBuilder<
MergedInitialContext<TInitialContext, UInContext, TCurrentContext>,
MergedCurrentContext<TCurrentContext, UOutContext>,
TInputSchema,
TOutputSchema,
TErrorMap,
Expand Down Expand Up @@ -157,39 +157,39 @@ export interface ProcedureBuilderWithInput<
errors: U,
): ProcedureBuilderWithInput<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, MergedErrorMap<TErrorMap, U>, TMeta>

'use'<UOutContext extends Context>(
'use'<UOutContext extends Context, UInContext extends Context = TCurrentContext>(
middleware: Middleware<
TCurrentContext,
UInContext,
UOutContext,
InferSchemaOutput<TInputSchema>,
unknown,
ORPCErrorConstructorMap<TErrorMap>,
TMeta
>,
): ConflictContextGuard<MergedContext<TCurrentContext, UOutContext>> &
ProcedureBuilderWithInput<
TInitialContext,
MergedContext<TCurrentContext, UOutContext>,
): ContextExtendsGuard<TCurrentContext, UInContext>
& ProcedureBuilderWithInput<
MergedInitialContext<TInitialContext, UInContext, TCurrentContext>,
MergedCurrentContext<TCurrentContext, UOutContext>,
TInputSchema,
TOutputSchema,
TErrorMap,
TMeta
>

'use'<UOutContext extends Context, UInput>(
'use'<UOutContext extends Context, UInput, UInContext extends Context = TCurrentContext>(
middleware: Middleware<
TCurrentContext,
UInContext,
UOutContext,
UInput,
unknown,
ORPCErrorConstructorMap<TErrorMap>,
TMeta
>,
mapInput: MapInputMiddleware<InferSchemaOutput<TInputSchema>, UInput>,
): ConflictContextGuard<MergedContext<TCurrentContext, UOutContext>> &
ProcedureBuilderWithInput<
TInitialContext,
MergedContext<TCurrentContext, UOutContext>,
): ContextExtendsGuard<TCurrentContext, UInContext>
& ProcedureBuilderWithInput<
MergedInitialContext<TInitialContext, UInContext, TCurrentContext>,
MergedCurrentContext<TCurrentContext, UOutContext>,
TInputSchema,
TOutputSchema,
TErrorMap,
Expand Down Expand Up @@ -234,19 +234,19 @@ export interface ProcedureBuilderWithOutput<
TMeta
>

'use'<UOutContext extends Context>(
'use'<UOutContext extends Context, UInContext extends Context = TCurrentContext>(
middleware: Middleware<
TCurrentContext,
UInContext,
UOutContext,
unknown,
InferSchemaInput<TOutputSchema>,
ORPCErrorConstructorMap<TErrorMap>,
TMeta
>,
): ConflictContextGuard<MergedContext<TCurrentContext, UOutContext>> &
ProcedureBuilderWithOutput<
TInitialContext,
MergedContext<TCurrentContext, UOutContext>,
): ContextExtendsGuard<TCurrentContext, UInContext>
& ProcedureBuilderWithOutput<
MergedInitialContext<TInitialContext, UInContext, TCurrentContext>,
MergedCurrentContext<TCurrentContext, UOutContext>,
TInputSchema,
TOutputSchema,
TErrorMap,
Expand Down Expand Up @@ -284,39 +284,39 @@ export interface ProcedureBuilderWithInputOutput<
errors: U,
): ProcedureBuilderWithInputOutput<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, MergedErrorMap<TErrorMap, U>, TMeta>

'use'<UOutContext extends Context>(
'use'<UOutContext extends Context, UInContext extends Context = TCurrentContext>(
middleware: Middleware<
TCurrentContext,
UInContext,
UOutContext,
InferSchemaOutput<TInputSchema>,
InferSchemaInput<TOutputSchema>,
ORPCErrorConstructorMap<TErrorMap>,
TMeta
>,
): ConflictContextGuard<MergedContext<TCurrentContext, UOutContext>> &
ProcedureBuilderWithInputOutput<
TInitialContext,
MergedContext<TCurrentContext, UOutContext>,
): ContextExtendsGuard<TCurrentContext, UInContext>
& ProcedureBuilderWithInputOutput<
MergedInitialContext<TInitialContext, UInContext, TCurrentContext>,
MergedCurrentContext<TCurrentContext, UOutContext>,
TInputSchema,
TOutputSchema,
TErrorMap,
TMeta
>

'use'<UOutContext extends Context, UInput>(
'use'<UOutContext extends Context, UInput, UInContext extends Context = TCurrentContext>(
middleware: Middleware<
TCurrentContext,
UInContext,
UOutContext,
UInput,
InferSchemaInput<TOutputSchema>,
ORPCErrorConstructorMap<TErrorMap>,
TMeta
>,
mapInput: MapInputMiddleware<InferSchemaOutput<TInputSchema>, UInput>,
): ConflictContextGuard<MergedContext<TCurrentContext, UOutContext>> &
ProcedureBuilderWithInputOutput<
TInitialContext,
MergedContext<TCurrentContext, UOutContext>,
): ContextExtendsGuard<TCurrentContext, UInContext>
& ProcedureBuilderWithInputOutput<
MergedInitialContext<TInitialContext, UInContext, TCurrentContext>,
MergedCurrentContext<TCurrentContext, UOutContext>,
TInputSchema,
TOutputSchema,
TErrorMap,
Expand Down Expand Up @@ -348,27 +348,32 @@ export interface RouterBuilder<
errors: U,
): RouterBuilder<TInitialContext, TCurrentContext, MergedErrorMap<TErrorMap, U>, TMeta>

'use'<UOutContext extends Context>(
'use'<UOutContext extends Context, UInContext extends Context = TCurrentContext>(
middleware: Middleware<
TCurrentContext,
UInContext,
UOutContext,
unknown,
unknown,
ORPCErrorConstructorMap<TErrorMap>,
TMeta
>,
): ConflictContextGuard<MergedContext<TCurrentContext, UOutContext>> &
RouterBuilder<TInitialContext, MergedContext<TCurrentContext, UOutContext>, TErrorMap, TMeta>
): ContextExtendsGuard<TCurrentContext, UInContext>
& RouterBuilder<
MergedInitialContext<TInitialContext, UInContext, TCurrentContext>,
MergedCurrentContext<TCurrentContext, UOutContext>,
TErrorMap,
TMeta
>

'prefix'(prefix: HTTPPath): RouterBuilder<TInitialContext, TCurrentContext, TErrorMap, TMeta>

'tag'(...tags: string[]): RouterBuilder<TInitialContext, TCurrentContext, TErrorMap, TMeta>

'router'<U extends Router<ContractRouter<TMeta>, TCurrentContext>>(
router: U
): EnhancedRouter<U, TInitialContext, TErrorMap>
): EnhancedRouter<U, TInitialContext, TCurrentContext, TErrorMap>

'lazy'<U extends Router<ContractRouter<TMeta>, TCurrentContext>>(
loader: () => Promise<{ default: U }>,
): EnhancedRouter<Lazy<U>, TInitialContext, TErrorMap>
): EnhancedRouter<Lazy<U>, TInitialContext, TCurrentContext, TErrorMap>
}
52 changes: 37 additions & 15 deletions packages/server/src/builder.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { BuilderWithMiddlewares, ProcedureBuilder, ProcedureBuilderWithInpu
import type { Context } from './context'
import type { ORPCErrorConstructorMap } from './error'
import type { Lazy } from './lazy'
import type { MiddlewareOutputFn } from './middleware'
import type { Middleware, MiddlewareOutputFn } from './middleware'
import type { DecoratedMiddleware } from './middleware-decorated'
import type { Procedure } from './procedure'
import type { DecoratedProcedure } from './procedure-decorated'
Expand Down Expand Up @@ -232,19 +232,17 @@ describe('Builder', () => {

expectTypeOf(applied).toEqualTypeOf<
BuilderWithMiddlewares<
InitialContext,
CurrentContext & { extra: boolean },
InitialContext & Record<never, never>,
Omit<CurrentContext, 'extra'> & { extra: boolean },
typeof inputSchema,
typeof outputSchema,
typeof baseErrorMap,
BaseMeta
>
>()

// @ts-expect-error --- conflict context
builder.use(({ next }) => next({ context: { db: 123 } }))
// conflict but not detected
expectTypeOf(builder.use(({ next }) => next({ context: { db: undefined } }))).toMatchTypeOf<never>()
// invalid TInContext
expectTypeOf(builder.use({} as Middleware<{ auth: 'invalid' }, any, any, any, any, any>)).toEqualTypeOf<never>()
// @ts-expect-error --- input is not match
builder.use(({ next }, input: 'invalid') => next({}))
// @ts-expect-error --- output is not match
Expand Down Expand Up @@ -276,8 +274,8 @@ describe('Builder', () => {

expectTypeOf(applied).toEqualTypeOf<
BuilderWithMiddlewares<
InitialContext,
CurrentContext & { extra: boolean },
InitialContext & Record<never, never>,
Omit<CurrentContext, 'extra'> & { extra: boolean },
typeof inputSchema,
typeof outputSchema,
typeof baseErrorMap,
Expand All @@ -291,15 +289,39 @@ describe('Builder', () => {
input => ({ invalid: true }),
)

// @ts-expect-error --- conflict context
builder.use(({ next }) => next({ context: { db: 123 } }), input => ({ mapped: true }))
// conflict but not detected
expectTypeOf(builder.use(({ next }) => next({ context: { db: undefined } }), input => ({ mapped: true }))).toMatchTypeOf<never>()
// invalid TInContext
expectTypeOf(builder.use({} as Middleware<{ auth: 'invalid' }, any, any, any, any, any>, () => { })).toEqualTypeOf<never>()
// @ts-expect-error --- input is not match
builder.use(({ next }, input: 'invalid') => next({}), input => ({ mapped: true }))
// @ts-expect-error --- output is not match
builder.use(({ next }, input, output: MiddlewareOutputFn<'invalid'>) => next({}), input => ({ mapped: true }))
})

it('with TInContext', () => {
const mid = {} as Middleware<{ cacheable?: boolean }, Record<never, never>, unknown, unknown, ORPCErrorConstructorMap<any>, BaseMeta>

expectTypeOf(builder.use(mid)).toEqualTypeOf<
BuilderWithMiddlewares<
InitialContext & { cacheable?: boolean },
Omit<CurrentContext, never> & Record<never, never>,
typeof inputSchema,
typeof outputSchema,
typeof baseErrorMap,
BaseMeta
>
>()

expectTypeOf(builder.use(mid, () => { })).toEqualTypeOf<
BuilderWithMiddlewares<
InitialContext & { cacheable?: boolean },
Omit<CurrentContext, never> & Record<never, never>,
typeof inputSchema,
typeof outputSchema,
typeof baseErrorMap,
BaseMeta
>
>()
})
})

it('.meta', () => {
Expand Down Expand Up @@ -410,7 +432,7 @@ describe('Builder', () => {

it('.router', () => {
expectTypeOf(builder.router(router)).toEqualTypeOf<
EnhancedRouter<typeof router, InitialContext, typeof baseErrorMap>
EnhancedRouter<typeof router, InitialContext, CurrentContext, typeof baseErrorMap>
>()

builder.router({
Expand All @@ -434,7 +456,7 @@ describe('Builder', () => {

it('.lazy', () => {
expectTypeOf(builder.lazy(() => Promise.resolve({ default: router }))).toEqualTypeOf<
EnhancedRouter<Lazy<typeof router>, InitialContext, typeof baseErrorMap>
EnhancedRouter<Lazy<typeof router>, InitialContext, CurrentContext, typeof baseErrorMap>
>()

// @ts-expect-error - initial context is not match
Expand Down
Loading