Skip to content

Commit

Permalink
fix: improve internal middleware validation typings (#2762)
Browse files Browse the repository at this point in the history
* fix existing type error

Signed-off-by: Marc MacLeod <[email protected]>

* constrain middleware validator to AnyValidator

Signed-off-by: Marc MacLeod <[email protected]>

---------

Signed-off-by: Marc MacLeod <[email protected]>
  • Loading branch information
marbemac authored Nov 15, 2024
1 parent 83cb148 commit 4d7c669
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
33 changes: 20 additions & 13 deletions packages/start/src/client/createMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,19 @@ export type ClientResultWithContext<TServerContext, TClientContext> = {
headers: HeadersInit
}

export type AnyMiddleware = MiddlewareTypes<any, any, any, any, any, any>
export type AnyMiddleware = MiddlewareTypes<
any,
any,
AnyValidator,
any,
any,
any
>

export interface MiddlewareTypes<
TId,
TMiddlewares,
TValidator,
TValidator extends AnyValidator,
TServerContext,
TClientContext,
TClientAfterContext,
Expand All @@ -266,13 +273,13 @@ export interface MiddlewareTypes<
export interface MiddlewareValidator<
TId,
TMiddlewares,
TValidator,
TValidator extends AnyValidator,
TServerContext,
TClientContext,
TClientAfterContext,
> {
validator: <TNewValidator>(
input: Constrain<TNewValidator, AnyValidator>,
validator: <TNewValidator extends AnyValidator>(
input: TNewValidator,
) => MiddlewareAfterMiddleware<
TId,
TMiddlewares,
Expand All @@ -286,7 +293,7 @@ export interface MiddlewareValidator<
export interface MiddlewareClientAfter<
TId,
TMiddlewares,
TValidator,
TValidator extends AnyValidator,
TServerContext,
TClientContext,
TClientAfterContext,
Expand All @@ -312,7 +319,7 @@ export interface MiddlewareClientAfter<
export interface MiddlewareAfterServer<
TId,
TMiddlewares,
TValidator,
TValidator extends AnyValidator,
TServerContext,
TClientContext,
TClientAfterContext,
Expand All @@ -336,7 +343,7 @@ export interface MiddlewareAfterServer<
export interface MiddlewareServer<
TId,
TMiddlewares,
TValidator,
TValidator extends AnyValidator,
TServerContext,
TClientContext,
TClientAfterContext,
Expand All @@ -362,7 +369,7 @@ export interface MiddlewareServer<
export interface MiddlewareAfterClient<
TId,
TMiddlewares,
TValidator,
TValidator extends AnyValidator,
TServerContext,
TClientContext,
TClientAfterContext,
Expand All @@ -386,7 +393,7 @@ export interface MiddlewareAfterClient<
export interface MiddlewareClient<
TId,
TMiddlewares,
TValidator,
TValidator extends AnyValidator,
TServerContext,
TClientContext,
TClientAfterContext,
Expand All @@ -411,7 +418,7 @@ export interface MiddlewareClient<
export interface MiddlewareAfterMiddleware<
TId,
TMiddlewares,
TValidator,
TValidator extends AnyValidator,
TServerContext,
TClientContext,
TClientAfterContext,
Expand Down Expand Up @@ -451,7 +458,7 @@ export interface MiddlewareAfterMiddleware<
export interface Middleware<
TId,
TMiddlewares,
TValidator,
TValidator extends AnyValidator,
TServerContext,
TClientContext,
TClientAfterContext,
Expand All @@ -478,7 +485,7 @@ export interface Middleware<
export function createMiddleware<
const TId,
const TMiddlewares,
TValidator = undefined,
TValidator extends AnyValidator = undefined,
TServerContext = undefined,
TClientContext = undefined,
TClientAfterContext = undefined,
Expand Down
7 changes: 5 additions & 2 deletions packages/start/src/client/createServerFn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,14 @@ const applyMiddleware = (
function execValidator(validator: AnyValidator, input: unknown): unknown {
if (validator == null) return {}

if ('~validate' in validator) {
const result = validator['~validate']({ value: input })
if ('~standard' in validator) {
const result = validator['~standard'].validate(input)

if ('value' in result) return result.value

if (result instanceof Promise)
throw new Error('Async validation not supported')

throw new Error(JSON.stringify(result.issues, undefined, 2))
}

Expand Down

0 comments on commit 4d7c669

Please sign in to comment.