@tanstack/start-client-core
Advanced tools
@@ -36,2 +36,4 @@ import { StartInstanceOptions } from './createStart.js'; | ||
| allClientSendContext: AssignAllClientSendContext<TMiddlewares, TClientSendContext>; | ||
| validator: TInputValidator; | ||
| /** @deprecated Use `validator` instead. */ | ||
| inputValidator: TInputValidator; | ||
@@ -76,2 +78,4 @@ } | ||
| middleware?: TMiddlewares; | ||
| validator?: ConstrainValidator<TRegister, 'GET', TInputValidator>; | ||
| /** @deprecated Use `validator` instead. */ | ||
| inputValidator?: ConstrainValidator<TRegister, 'GET', TInputValidator>; | ||
@@ -141,3 +145,5 @@ client?: FunctionMiddlewareClientFn<TRegister, TMiddlewares, TInputValidator, TServerContext, TClientContext>; | ||
| export interface FunctionMiddlewareValidator<TRegister, TMiddlewares> { | ||
| inputValidator: <TNewValidator>(inputValidator: ConstrainValidator<TRegister, 'GET', TNewValidator>) => FunctionMiddlewareAfterValidator<TRegister, TMiddlewares, TNewValidator>; | ||
| validator: <TNewValidator>(validator: ConstrainValidator<TRegister, 'GET', TNewValidator>) => FunctionMiddlewareAfterValidator<TRegister, TMiddlewares, TNewValidator>; | ||
| /** @deprecated Use `validator` instead. */ | ||
| inputValidator: <TNewValidator>(validator: ConstrainValidator<TRegister, 'GET', TNewValidator>) => FunctionMiddlewareAfterValidator<TRegister, TMiddlewares, TNewValidator>; | ||
| } | ||
@@ -144,0 +150,0 @@ export interface FunctionMiddlewareAfterValidator<TRegister, TMiddlewares, TInputValidator> extends FunctionMiddlewareWithTypes<TRegister, TMiddlewares, TInputValidator, undefined, undefined, undefined, undefined>, FunctionMiddlewareServer<TRegister, TMiddlewares, TInputValidator, undefined, undefined>, FunctionMiddlewareClient<TRegister, TMiddlewares, TInputValidator> { |
@@ -7,2 +7,8 @@ //#region src/createMiddleware.ts | ||
| }; | ||
| const setValidator = (validator) => { | ||
| return createMiddleware({}, Object.assign(resolvedOptions, { | ||
| validator, | ||
| inputValidator: validator | ||
| })); | ||
| }; | ||
| return { | ||
@@ -13,5 +19,4 @@ options: resolvedOptions, | ||
| }, | ||
| inputValidator: (inputValidator) => { | ||
| return createMiddleware({}, Object.assign(resolvedOptions, { inputValidator })); | ||
| }, | ||
| validator: setValidator, | ||
| inputValidator: setValidator, | ||
| client: (client) => { | ||
@@ -18,0 +23,0 @@ return createMiddleware({}, Object.assign(resolvedOptions, { client })); |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"createMiddleware.js","names":[],"sources":["../../src/createMiddleware.ts"],"sourcesContent":["import type { StartInstanceOptions } from './createStart'\nimport type {\n AnyServerFn,\n ConstrainValidator,\n CustomFetch,\n Method,\n} from './createServerFn'\nimport type { ClientFnMeta, ServerFnMeta } from './constants'\nimport type {\n AnyContext,\n Assign,\n Constrain,\n Expand,\n IntersectAssign,\n Register,\n ResolveValidatorInput,\n ResolveValidatorOutput,\n ValidateSerializableInput,\n} from '@tanstack/router-core'\n\nexport type CreateMiddlewareFn<TRegister> = <TType extends MiddlewareType>(\n options?: {\n type?: TType\n },\n __opts?: FunctionMiddlewareOptions<\n TRegister,\n unknown,\n undefined,\n undefined,\n undefined\n >,\n) => CreateMiddlewareResult<TRegister, TType>\n\nexport const createMiddleware: CreateMiddlewareFn<{}> = (options, __opts) => {\n const resolvedOptions = {\n type: 'request',\n ...(__opts || options),\n }\n\n return {\n options: resolvedOptions,\n middleware: (middleware: any) => {\n return createMiddleware(\n {},\n Object.assign(resolvedOptions, { middleware }),\n )\n },\n inputValidator: (inputValidator: any) => {\n return createMiddleware(\n {},\n Object.assign(resolvedOptions, { inputValidator }),\n )\n },\n client: (client: any) => {\n return createMiddleware({}, Object.assign(resolvedOptions, { client }))\n },\n server: (server: any) => {\n return createMiddleware({}, Object.assign(resolvedOptions, { server }))\n },\n } as any\n}\n\nexport type MiddlewareType = 'request' | 'function'\n\nexport type CreateMiddlewareResult<\n TRegister,\n TType extends MiddlewareType,\n> = 'request' extends TType\n ? RequestMiddleware<TRegister>\n : FunctionMiddleware<TRegister>\n\nexport interface FunctionMiddleware<\n TRegister,\n> extends FunctionMiddlewareAfterMiddleware<TRegister, unknown> {\n middleware: <const TNewMiddlewares = undefined>(\n middlewares: Constrain<\n TNewMiddlewares,\n ReadonlyArray<AnyRequestMiddleware | AnyFunctionMiddleware>\n >,\n ) => FunctionMiddlewareAfterMiddleware<TRegister, TNewMiddlewares>\n}\n\nexport interface FunctionMiddlewareAfterMiddleware<TRegister, TMiddlewares>\n extends\n FunctionMiddlewareWithTypes<\n TRegister,\n TMiddlewares,\n undefined,\n undefined,\n undefined,\n undefined,\n undefined\n >,\n FunctionMiddlewareServer<\n TRegister,\n TMiddlewares,\n undefined,\n undefined,\n undefined\n >,\n FunctionMiddlewareClient<TRegister, TMiddlewares, undefined>,\n FunctionMiddlewareValidator<TRegister, TMiddlewares> {}\n\nexport interface FunctionMiddlewareWithTypes<\n TRegister,\n TMiddlewares,\n TInputValidator,\n TServerContext,\n TServerSendContext,\n TClientContext,\n TClientSendContext,\n> {\n '~types': FunctionMiddlewareTypes<\n TRegister,\n TMiddlewares,\n TInputValidator,\n TServerContext,\n TServerSendContext,\n TClientContext,\n TClientSendContext\n >\n options: FunctionMiddlewareOptions<\n TRegister,\n TMiddlewares,\n TInputValidator,\n TServerContext,\n TClientContext\n >\n}\n\nexport interface FunctionMiddlewareTypes<\n in out TRegister,\n in out TMiddlewares,\n in out TInputValidator,\n in out TServerContext,\n in out TServerSendContext,\n in out TClientContext,\n in out TClientSendContext,\n> {\n type: 'function'\n middlewares: TMiddlewares\n input: ResolveValidatorInput<TInputValidator>\n allInput: IntersectAllValidatorInputs<TMiddlewares, TInputValidator>\n output: ResolveValidatorOutput<TInputValidator>\n allOutput: IntersectAllValidatorOutputs<TMiddlewares, TInputValidator>\n clientContext: TClientContext\n allClientContextBeforeNext: AssignAllClientContextBeforeNext<\n TMiddlewares,\n TClientContext\n >\n allClientContextAfterNext: AssignAllClientContextAfterNext<\n TMiddlewares,\n TClientContext,\n TClientSendContext\n >\n serverContext: TServerContext\n serverSendContext: TServerSendContext\n allServerSendContext: AssignAllServerSendContext<\n TMiddlewares,\n TServerSendContext\n >\n allServerContext: AssignAllServerFnContext<\n TRegister,\n TMiddlewares,\n TServerSendContext,\n TServerContext\n >\n clientSendContext: TClientSendContext\n allClientSendContext: AssignAllClientSendContext<\n TMiddlewares,\n TClientSendContext\n >\n inputValidator: TInputValidator\n}\n\n/**\n * Recursively resolve the input type produced by a sequence of middleware\n */\nexport type IntersectAllValidatorInputs<TMiddlewares, TInputValidator> =\n unknown extends TInputValidator\n ? TInputValidator\n : TInputValidator extends undefined\n ? IntersectAllMiddleware<TMiddlewares, 'allInput'>\n : IntersectAssign<\n IntersectAllMiddleware<TMiddlewares, 'allInput'>,\n ResolveValidatorInput<TInputValidator>\n >\n\nexport type IntersectAllMiddleware<\n TMiddlewares,\n TType extends\n | keyof AnyFunctionMiddleware['~types']\n | keyof AnyRequestMiddleware['~types']\n | keyof AnyServerFn['~types'],\n TAcc = undefined,\n> = TMiddlewares extends readonly [infer TMiddleware, ...infer TRest]\n ? TMiddleware extends\n | AnyFunctionMiddleware\n | AnyRequestMiddleware\n | AnyServerFn\n ? IntersectAllMiddleware<\n TRest,\n TType,\n IntersectAssign<\n TAcc,\n TMiddleware['~types'][TType & keyof TMiddleware['~types']]\n >\n >\n : TAcc\n : TAcc\n\nexport type AnyFunctionMiddleware = FunctionMiddlewareWithTypes<\n any,\n any,\n any,\n any,\n any,\n any,\n any\n>\n\n/**\n * Recursively merge the output type produced by a sequence of middleware\n */\nexport type IntersectAllValidatorOutputs<TMiddlewares, TInputValidator> =\n unknown extends TInputValidator\n ? TInputValidator\n : TInputValidator extends undefined\n ? IntersectAllMiddleware<TMiddlewares, 'allOutput'>\n : IntersectAssign<\n IntersectAllMiddleware<TMiddlewares, 'allOutput'>,\n Awaited<ResolveValidatorOutput<TInputValidator>>\n >\n\n/**\n * Recursively resolve the client context type produced by a sequence of middleware\n */\nexport type AssignAllClientContextBeforeNext<\n TMiddlewares,\n TClientContext = undefined,\n> = unknown extends TClientContext\n ? TClientContext\n : Assign<\n AssignAllMiddleware<TMiddlewares, 'allClientContextBeforeNext'>,\n TClientContext\n >\n\nexport type AssignAllMiddleware<\n TMiddlewares,\n TType extends\n | keyof AnyFunctionMiddleware['~types']\n | keyof AnyRequestMiddleware['~types']\n | keyof AnyServerFn['~types'],\n TAcc = undefined,\n> = TMiddlewares extends readonly [infer TMiddleware, ...infer TRest]\n ? TMiddleware extends\n | AnyFunctionMiddleware\n | AnyRequestMiddleware\n | AnyServerFn\n ? AssignAllMiddleware<\n TRest,\n TType,\n Assign<TAcc, TMiddleware['~types'][TType & keyof TMiddleware['~types']]>\n >\n : TAcc\n : TAcc\n\nexport type AssignAllClientContextAfterNext<\n TMiddlewares,\n TClientContext = undefined,\n TSendContext = undefined,\n> = unknown extends TClientContext\n ? Assign<TClientContext, TSendContext>\n : Assign<\n AssignAllMiddleware<TMiddlewares, 'allClientContextAfterNext'>,\n Assign<TClientContext, TSendContext>\n >\n\nexport type AssignAllServerSendContext<\n TMiddlewares,\n TSendContext = undefined,\n> = unknown extends TSendContext\n ? TSendContext\n : Assign<\n AssignAllMiddleware<TMiddlewares, 'allServerSendContext'>,\n TSendContext\n >\n\nexport type AssignAllServerRequestContext<\n TRegister,\n TMiddlewares,\n TSendContext = undefined,\n TServerContext = undefined,\n> = Assign<\n // Fetch Request Context\n GlobalFetchRequestContext,\n Assign<\n GlobalServerRequestContext<TRegister>,\n __AssignAllServerRequestContext<TMiddlewares, TSendContext, TServerContext>\n >\n>\n\n// export type GlobalFetchRequestContext<TRegister> = AnyContext\nexport type GlobalFetchRequestContext = Register extends {\n server: { requestContext: infer TRequestContext }\n}\n ? TRequestContext\n : AnyContext\n\nexport type GlobalServerRequestContext<TRegister> = TRegister extends {\n config: StartInstanceOptions<any, any, infer TRequestMiddlewares, any>\n}\n ? AssignAllMiddleware<TRequestMiddlewares, 'allServerContext'>\n : AnyContext\n\ntype __AssignAllServerRequestContext<\n TMiddlewares,\n TSendContext = undefined,\n TServerContext = undefined,\n> = unknown extends TSendContext\n ? Assign<TSendContext, TServerContext>\n : Assign<\n AssignAllMiddleware<TMiddlewares, 'allServerContext'>,\n Assign<TSendContext, TServerContext>\n >\n\nexport type AssignAllServerFnContext<\n TRegister,\n TMiddlewares,\n TSendContext = undefined,\n TServerContext = undefined,\n> = Assign<\n GlobalFetchRequestContext,\n Assign<\n GlobalServerRequestContext<TRegister>, // TODO: This enabled global middleware\n // type inference, but creates a circular types issue. No idea how to fix this.\n // AnyContext,\n Assign<\n GlobalServerFnContext<TRegister>, // TODO: This enabled global middleware\n // type inference, but creates a circular types issue. No idea how to fix this.\n // AnyContext,/\n __AssignAllServerFnContext<TMiddlewares, TSendContext, TServerContext>\n >\n >\n>\n\ntype GlobalServerFnContext<TRegister> = TRegister extends {\n config: StartInstanceOptions<any, any, any, infer TFunctionMiddlewares>\n}\n ? AssignAllMiddleware<TFunctionMiddlewares, 'allServerContext'>\n : AnyContext\n\ntype __AssignAllServerFnContext<\n TMiddlewares,\n TSendContext = undefined,\n TServerContext = undefined,\n> = unknown extends TSendContext\n ? Assign<TSendContext, TServerContext>\n : Assign<\n AssignAllMiddleware<TMiddlewares, 'allServerContext'>,\n Assign<TSendContext, TServerContext>\n >\n\nexport type AssignAllClientSendContext<\n TMiddlewares,\n TSendContext = undefined,\n> = unknown extends TSendContext\n ? TSendContext\n : Assign<\n AssignAllMiddleware<TMiddlewares, 'allClientSendContext'>,\n TSendContext\n >\n\nexport interface FunctionMiddlewareOptions<\n in out TRegister,\n in out TMiddlewares,\n in out TInputValidator,\n in out TServerContext,\n in out TClientContext,\n> {\n middleware?: TMiddlewares\n inputValidator?: ConstrainValidator<TRegister, 'GET', TInputValidator>\n client?: FunctionMiddlewareClientFn<\n TRegister,\n TMiddlewares,\n TInputValidator,\n TServerContext,\n TClientContext\n >\n server?: FunctionMiddlewareServerFn<\n TRegister,\n TMiddlewares,\n TInputValidator,\n TServerContext,\n unknown,\n unknown\n >\n}\n\nexport type FunctionMiddlewareClientNextFn<TRegister, TMiddlewares> = <\n TSendContext = undefined,\n TNewClientContext = undefined,\n>(ctx?: {\n context?: TNewClientContext\n sendContext?: ValidateSerializableInput<TRegister, TSendContext>\n headers?: HeadersInit\n fetch?: CustomFetch\n}) => Promise<\n FunctionClientResultWithContext<TMiddlewares, TSendContext, TNewClientContext>\n>\n\nexport interface FunctionMiddlewareServer<\n TRegister,\n TMiddlewares,\n TInputValidator,\n TServerSendContext,\n TClientContext,\n> {\n server: <TNewServerContext = undefined, TSendContext = undefined>(\n server: FunctionMiddlewareServerFn<\n TRegister,\n TMiddlewares,\n TInputValidator,\n TServerSendContext,\n TNewServerContext,\n TSendContext\n >,\n ) => FunctionMiddlewareAfterServer<\n TRegister,\n TMiddlewares,\n TInputValidator,\n TNewServerContext,\n TServerSendContext,\n TClientContext,\n TSendContext\n >\n}\n\nexport type FunctionMiddlewareServerFn<\n TRegister,\n TMiddlewares,\n TInputValidator,\n TServerSendContext,\n TNewServerContext,\n TSendContext,\n> = (\n options: FunctionMiddlewareServerFnOptions<\n TRegister,\n TMiddlewares,\n TInputValidator,\n TServerSendContext\n >,\n) => FunctionMiddlewareServerFnResult<\n TRegister,\n TMiddlewares,\n TServerSendContext,\n TNewServerContext,\n TSendContext\n>\n\nexport type FunctionMiddlewareServerNextFn<\n TRegister,\n TMiddlewares,\n TServerSendContext,\n> = <TNewServerContext = undefined, TSendContext = undefined>(ctx?: {\n context?: TNewServerContext\n sendContext?: ValidateSerializableInput<TRegister, TSendContext>\n}) => Promise<\n FunctionServerResultWithContext<\n TRegister,\n TMiddlewares,\n TServerSendContext,\n TNewServerContext,\n TSendContext\n >\n>\n\nexport type FunctionServerResultWithContext<\n in out TRegister,\n in out TMiddlewares,\n in out TServerSendContext,\n in out TServerContext,\n in out TSendContext,\n> = {\n 'use functions must return the result of next()': true\n '~types': {\n context: TServerContext\n sendContext: TSendContext\n }\n context: Expand<\n AssignAllServerFnContext<\n TRegister,\n TMiddlewares,\n TServerSendContext,\n TServerContext\n >\n >\n sendContext: Expand<AssignAllClientSendContext<TMiddlewares, TSendContext>>\n}\n\nexport interface FunctionMiddlewareServerFnOptions<\n in out TRegister,\n in out TMiddlewares,\n in out TInputValidator,\n in out TServerSendContext,\n> {\n data: Expand<IntersectAllValidatorOutputs<TMiddlewares, TInputValidator>>\n context: Expand<\n AssignAllServerFnContext<TRegister, TMiddlewares, TServerSendContext>\n >\n next: FunctionMiddlewareServerNextFn<\n TRegister,\n TMiddlewares,\n TServerSendContext\n >\n method: Method\n serverFnMeta: ServerFnMeta\n signal: AbortSignal\n}\n\nexport type FunctionMiddlewareServerFnResult<\n TRegister,\n TMiddlewares,\n TServerSendContext,\n TServerContext,\n TSendContext,\n> =\n | Promise<\n FunctionServerResultWithContext<\n TRegister,\n TMiddlewares,\n TServerSendContext,\n TServerContext,\n TSendContext\n >\n >\n | FunctionServerResultWithContext<\n TRegister,\n TMiddlewares,\n TServerSendContext,\n TServerContext,\n TSendContext\n >\n\nexport interface FunctionMiddlewareAfterServer<\n TRegister,\n TMiddlewares,\n TInputValidator,\n TServerContext,\n TServerSendContext,\n TClientContext,\n TClientSendContext,\n> extends FunctionMiddlewareWithTypes<\n TRegister,\n TMiddlewares,\n TInputValidator,\n TServerContext,\n TServerSendContext,\n TClientContext,\n TClientSendContext\n> {}\n\nexport interface FunctionMiddlewareClient<\n TRegister,\n TMiddlewares,\n TInputValidator,\n> {\n client: <TSendServerContext = undefined, TNewClientContext = undefined>(\n client: FunctionMiddlewareClientFn<\n TRegister,\n TMiddlewares,\n TInputValidator,\n TSendServerContext,\n TNewClientContext\n >,\n ) => FunctionMiddlewareAfterClient<\n TRegister,\n TMiddlewares,\n TInputValidator,\n TSendServerContext,\n TNewClientContext\n >\n}\n\nexport type FunctionMiddlewareClientFn<\n TRegister,\n TMiddlewares,\n TInputValidator,\n TSendContext,\n TClientContext,\n> = (\n options: FunctionMiddlewareClientFnOptions<\n TRegister,\n TMiddlewares,\n TInputValidator\n >,\n) => FunctionMiddlewareClientFnResult<\n TMiddlewares,\n TSendContext,\n TClientContext\n>\n\nexport interface FunctionMiddlewareClientFnOptions<\n in out TRegister,\n in out TMiddlewares,\n in out TInputValidator,\n> {\n data: Expand<IntersectAllValidatorInputs<TMiddlewares, TInputValidator>>\n context: Expand<AssignAllClientContextBeforeNext<TMiddlewares>>\n sendContext: Expand<AssignAllServerSendContext<TMiddlewares>>\n method: Method\n signal: AbortSignal\n serverFnMeta: ClientFnMeta\n next: FunctionMiddlewareClientNextFn<TRegister, TMiddlewares>\n filename: string\n fetch?: CustomFetch\n}\n\nexport type FunctionMiddlewareClientFnResult<\n TMiddlewares,\n TSendContext,\n TClientContext,\n> =\n | Promise<\n FunctionClientResultWithContext<\n TMiddlewares,\n TSendContext,\n TClientContext\n >\n >\n | FunctionClientResultWithContext<TMiddlewares, TSendContext, TClientContext>\n\nexport type FunctionClientResultWithContext<\n in out TMiddlewares,\n in out TSendContext,\n in out TClientContext,\n> = {\n 'use functions must return the result of next()': true\n context: Expand<AssignAllClientContextAfterNext<TMiddlewares, TClientContext>>\n sendContext: Expand<AssignAllServerSendContext<TMiddlewares, TSendContext>>\n headers: HeadersInit\n fetch?: CustomFetch\n}\n\nexport interface FunctionMiddlewareAfterClient<\n TRegister,\n TMiddlewares,\n TInputValidator,\n TServerSendContext,\n TClientContext,\n>\n extends\n FunctionMiddlewareWithTypes<\n TRegister,\n TMiddlewares,\n TInputValidator,\n undefined,\n TServerSendContext,\n TClientContext,\n undefined\n >,\n FunctionMiddlewareServer<\n TRegister,\n TMiddlewares,\n TInputValidator,\n TServerSendContext,\n TClientContext\n > {}\n\nexport interface FunctionMiddlewareValidator<TRegister, TMiddlewares> {\n inputValidator: <TNewValidator>(\n inputValidator: ConstrainValidator<TRegister, 'GET', TNewValidator>,\n ) => FunctionMiddlewareAfterValidator<TRegister, TMiddlewares, TNewValidator>\n}\n\nexport interface FunctionMiddlewareAfterValidator<\n TRegister,\n TMiddlewares,\n TInputValidator,\n>\n extends\n FunctionMiddlewareWithTypes<\n TRegister,\n TMiddlewares,\n TInputValidator,\n undefined,\n undefined,\n undefined,\n undefined\n >,\n FunctionMiddlewareServer<\n TRegister,\n TMiddlewares,\n TInputValidator,\n undefined,\n undefined\n >,\n FunctionMiddlewareClient<TRegister, TMiddlewares, TInputValidator> {}\n\nexport interface RequestMiddleware<\n TRegister,\n> extends RequestMiddlewareAfterMiddleware<TRegister, undefined> {\n middleware: <const TMiddlewares = undefined>(\n middlewares: Constrain<TMiddlewares, ReadonlyArray<AnyRequestMiddleware>>,\n ) => RequestMiddlewareAfterMiddleware<TRegister, TMiddlewares>\n}\n\nexport type AnyRequestMiddleware = RequestMiddlewareWithTypes<any, any, any>\n\nexport interface RequestMiddlewareWithTypes<\n TRegister,\n TMiddlewares,\n TServerContext,\n> {\n '~types': RequestMiddlewareTypes<TRegister, TMiddlewares, TServerContext>\n options: RequestMiddlewareOptions<TRegister, TMiddlewares, TServerContext>\n}\n\nexport interface RequestMiddlewareOptions<\n in out TRegister,\n in out TMiddlewares,\n in out TServerContext,\n> {\n middleware?: TMiddlewares\n server?: RequestServerFn<TRegister, TMiddlewares, TServerContext>\n}\nexport interface RequestMiddlewareTypes<\n TRegister,\n TMiddlewares,\n TServerContext,\n> {\n type: 'request'\n // this only exists so we can use request middlewares in server functions\n allInput: undefined\n // this only exists so we can use request middlewares in server functions\n allOutput: undefined\n middlewares: TMiddlewares\n serverContext: TServerContext\n allServerContext: AssignAllServerRequestContext<\n TRegister,\n TMiddlewares,\n undefined,\n TServerContext\n >\n}\n\nexport interface RequestMiddlewareAfterMiddleware<TRegister, TMiddlewares>\n extends\n RequestMiddlewareWithTypes<TRegister, TMiddlewares, undefined>,\n RequestMiddlewareServer<TRegister, TMiddlewares> {}\n\nexport interface RequestMiddlewareServer<TRegister, TMiddlewares> {\n server: <TServerContext = undefined>(\n fn: RequestServerFn<TRegister, TMiddlewares, TServerContext>,\n ) => RequestMiddlewareAfterServer<TRegister, TMiddlewares, TServerContext>\n}\n\nexport type RequestServerFn<TRegister, TMiddlewares, TServerContext> = (\n options: RequestServerOptions<TRegister, TMiddlewares>,\n) => RequestMiddlewareServerFnResult<TRegister, TMiddlewares, TServerContext>\n\nexport interface RequestServerOptions<TRegister, TMiddlewares> {\n request: Request\n pathname: string\n context: Expand<AssignAllServerRequestContext<TRegister, TMiddlewares>>\n next: RequestServerNextFn<TRegister, TMiddlewares>\n /**\n * Type of Start handler currently processing this request.\n */\n handlerType: 'serverFn' | 'router'\n /**\n * Metadata about the server function being invoked.\n * This is only present when the request is handling a server function call.\n * For regular page requests, this will be undefined.\n */\n serverFnMeta?: ServerFnMeta\n}\n\nexport type RequestServerNextFn<TRegister, TMiddlewares> = <\n TServerContext = undefined,\n>(\n options?: RequestServerNextFnOptions<TServerContext>,\n) => RequestServerNextFnResult<TRegister, TMiddlewares, TServerContext>\n\nexport interface RequestServerNextFnOptions<TServerContext> {\n context?: TServerContext\n}\n\nexport type RequestServerNextFnResult<TRegister, TMiddlewares, TServerContext> =\n | Promise<RequestServerResult<TRegister, TMiddlewares, TServerContext>>\n | RequestServerResult<TRegister, TMiddlewares, TServerContext>\n\nexport type RequestMiddlewareServerFnResult<\n TRegister,\n TMiddlewares,\n TServerContext,\n> =\n | Promise<\n RequestServerResult<TRegister, TMiddlewares, TServerContext> | Response\n >\n | RequestServerResult<TRegister, TMiddlewares, TServerContext>\n | Response\n\nexport interface RequestServerResult<TRegister, TMiddlewares, TServerContext> {\n request: Request\n pathname: string\n context: Expand<\n AssignAllServerRequestContext<\n TRegister,\n TMiddlewares,\n undefined,\n TServerContext\n >\n >\n response: Response\n}\n\nexport interface RequestMiddlewareAfterServer<\n TRegister,\n TMiddlewares,\n TServerContext,\n> extends RequestMiddlewareWithTypes<TRegister, TMiddlewares, TServerContext> {}\n"],"mappings":";AAiCA,IAAa,oBAA4C,SAAS,WAAW;CAC3E,MAAM,kBAAkB;EACtB,MAAM;EACN,GAAI,UAAU;CAChB;CAEA,OAAO;EACL,SAAS;EACT,aAAa,eAAoB;GAC/B,OAAO,iBACL,CAAC,GACD,OAAO,OAAO,iBAAiB,EAAE,WAAW,CAAC,CAC/C;EACF;EACA,iBAAiB,mBAAwB;GACvC,OAAO,iBACL,CAAC,GACD,OAAO,OAAO,iBAAiB,EAAE,eAAe,CAAC,CACnD;EACF;EACA,SAAS,WAAgB;GACvB,OAAO,iBAAiB,CAAC,GAAG,OAAO,OAAO,iBAAiB,EAAE,OAAO,CAAC,CAAC;EACxE;EACA,SAAS,WAAgB;GACvB,OAAO,iBAAiB,CAAC,GAAG,OAAO,OAAO,iBAAiB,EAAE,OAAO,CAAC,CAAC;EACxE;CACF;AACF"} | ||
| {"version":3,"file":"createMiddleware.js","names":[],"sources":["../../src/createMiddleware.ts"],"sourcesContent":["import type { StartInstanceOptions } from './createStart'\nimport type {\n AnyServerFn,\n ConstrainValidator,\n CustomFetch,\n Method,\n} from './createServerFn'\nimport type { ClientFnMeta, ServerFnMeta } from './constants'\nimport type {\n AnyContext,\n Assign,\n Constrain,\n Expand,\n IntersectAssign,\n Register,\n ResolveValidatorInput,\n ResolveValidatorOutput,\n ValidateSerializableInput,\n} from '@tanstack/router-core'\n\nexport type CreateMiddlewareFn<TRegister> = <TType extends MiddlewareType>(\n options?: {\n type?: TType\n },\n __opts?: FunctionMiddlewareOptions<\n TRegister,\n unknown,\n undefined,\n undefined,\n undefined\n >,\n) => CreateMiddlewareResult<TRegister, TType>\n\nexport const createMiddleware: CreateMiddlewareFn<{}> = (options, __opts) => {\n const resolvedOptions = {\n type: 'request',\n ...(__opts || options),\n }\n const setValidator = (validator: any) => {\n return createMiddleware(\n {},\n Object.assign(resolvedOptions, {\n validator,\n // TODO remove upon stable\n inputValidator: validator,\n }),\n )\n }\n\n return {\n options: resolvedOptions,\n middleware: (middleware: any) => {\n return createMiddleware(\n {},\n Object.assign(resolvedOptions, { middleware }),\n )\n },\n validator: setValidator,\n // TODO remove upon stable\n inputValidator: setValidator,\n client: (client: any) => {\n return createMiddleware({}, Object.assign(resolvedOptions, { client }))\n },\n server: (server: any) => {\n return createMiddleware({}, Object.assign(resolvedOptions, { server }))\n },\n } as any\n}\n\nexport type MiddlewareType = 'request' | 'function'\n\nexport type CreateMiddlewareResult<\n TRegister,\n TType extends MiddlewareType,\n> = 'request' extends TType\n ? RequestMiddleware<TRegister>\n : FunctionMiddleware<TRegister>\n\nexport interface FunctionMiddleware<\n TRegister,\n> extends FunctionMiddlewareAfterMiddleware<TRegister, unknown> {\n middleware: <const TNewMiddlewares = undefined>(\n middlewares: Constrain<\n TNewMiddlewares,\n ReadonlyArray<AnyRequestMiddleware | AnyFunctionMiddleware>\n >,\n ) => FunctionMiddlewareAfterMiddleware<TRegister, TNewMiddlewares>\n}\n\nexport interface FunctionMiddlewareAfterMiddleware<TRegister, TMiddlewares>\n extends\n FunctionMiddlewareWithTypes<\n TRegister,\n TMiddlewares,\n undefined,\n undefined,\n undefined,\n undefined,\n undefined\n >,\n FunctionMiddlewareServer<\n TRegister,\n TMiddlewares,\n undefined,\n undefined,\n undefined\n >,\n FunctionMiddlewareClient<TRegister, TMiddlewares, undefined>,\n FunctionMiddlewareValidator<TRegister, TMiddlewares> {}\n\nexport interface FunctionMiddlewareWithTypes<\n TRegister,\n TMiddlewares,\n TInputValidator,\n TServerContext,\n TServerSendContext,\n TClientContext,\n TClientSendContext,\n> {\n '~types': FunctionMiddlewareTypes<\n TRegister,\n TMiddlewares,\n TInputValidator,\n TServerContext,\n TServerSendContext,\n TClientContext,\n TClientSendContext\n >\n options: FunctionMiddlewareOptions<\n TRegister,\n TMiddlewares,\n TInputValidator,\n TServerContext,\n TClientContext\n >\n}\n\nexport interface FunctionMiddlewareTypes<\n in out TRegister,\n in out TMiddlewares,\n in out TInputValidator,\n in out TServerContext,\n in out TServerSendContext,\n in out TClientContext,\n in out TClientSendContext,\n> {\n type: 'function'\n middlewares: TMiddlewares\n input: ResolveValidatorInput<TInputValidator>\n allInput: IntersectAllValidatorInputs<TMiddlewares, TInputValidator>\n output: ResolveValidatorOutput<TInputValidator>\n allOutput: IntersectAllValidatorOutputs<TMiddlewares, TInputValidator>\n clientContext: TClientContext\n allClientContextBeforeNext: AssignAllClientContextBeforeNext<\n TMiddlewares,\n TClientContext\n >\n allClientContextAfterNext: AssignAllClientContextAfterNext<\n TMiddlewares,\n TClientContext,\n TClientSendContext\n >\n serverContext: TServerContext\n serverSendContext: TServerSendContext\n allServerSendContext: AssignAllServerSendContext<\n TMiddlewares,\n TServerSendContext\n >\n allServerContext: AssignAllServerFnContext<\n TRegister,\n TMiddlewares,\n TServerSendContext,\n TServerContext\n >\n clientSendContext: TClientSendContext\n allClientSendContext: AssignAllClientSendContext<\n TMiddlewares,\n TClientSendContext\n >\n validator: TInputValidator\n // TODO remove upon stable\n /** @deprecated Use `validator` instead. */\n inputValidator: TInputValidator\n}\n\n/**\n * Recursively resolve the input type produced by a sequence of middleware\n */\nexport type IntersectAllValidatorInputs<TMiddlewares, TInputValidator> =\n unknown extends TInputValidator\n ? TInputValidator\n : TInputValidator extends undefined\n ? IntersectAllMiddleware<TMiddlewares, 'allInput'>\n : IntersectAssign<\n IntersectAllMiddleware<TMiddlewares, 'allInput'>,\n ResolveValidatorInput<TInputValidator>\n >\n\nexport type IntersectAllMiddleware<\n TMiddlewares,\n TType extends\n | keyof AnyFunctionMiddleware['~types']\n | keyof AnyRequestMiddleware['~types']\n | keyof AnyServerFn['~types'],\n TAcc = undefined,\n> = TMiddlewares extends readonly [infer TMiddleware, ...infer TRest]\n ? TMiddleware extends\n | AnyFunctionMiddleware\n | AnyRequestMiddleware\n | AnyServerFn\n ? IntersectAllMiddleware<\n TRest,\n TType,\n IntersectAssign<\n TAcc,\n TMiddleware['~types'][TType & keyof TMiddleware['~types']]\n >\n >\n : TAcc\n : TAcc\n\nexport type AnyFunctionMiddleware = FunctionMiddlewareWithTypes<\n any,\n any,\n any,\n any,\n any,\n any,\n any\n>\n\n/**\n * Recursively merge the output type produced by a sequence of middleware\n */\nexport type IntersectAllValidatorOutputs<TMiddlewares, TInputValidator> =\n unknown extends TInputValidator\n ? TInputValidator\n : TInputValidator extends undefined\n ? IntersectAllMiddleware<TMiddlewares, 'allOutput'>\n : IntersectAssign<\n IntersectAllMiddleware<TMiddlewares, 'allOutput'>,\n Awaited<ResolveValidatorOutput<TInputValidator>>\n >\n\n/**\n * Recursively resolve the client context type produced by a sequence of middleware\n */\nexport type AssignAllClientContextBeforeNext<\n TMiddlewares,\n TClientContext = undefined,\n> = unknown extends TClientContext\n ? TClientContext\n : Assign<\n AssignAllMiddleware<TMiddlewares, 'allClientContextBeforeNext'>,\n TClientContext\n >\n\nexport type AssignAllMiddleware<\n TMiddlewares,\n TType extends\n | keyof AnyFunctionMiddleware['~types']\n | keyof AnyRequestMiddleware['~types']\n | keyof AnyServerFn['~types'],\n TAcc = undefined,\n> = TMiddlewares extends readonly [infer TMiddleware, ...infer TRest]\n ? TMiddleware extends\n | AnyFunctionMiddleware\n | AnyRequestMiddleware\n | AnyServerFn\n ? AssignAllMiddleware<\n TRest,\n TType,\n Assign<TAcc, TMiddleware['~types'][TType & keyof TMiddleware['~types']]>\n >\n : TAcc\n : TAcc\n\nexport type AssignAllClientContextAfterNext<\n TMiddlewares,\n TClientContext = undefined,\n TSendContext = undefined,\n> = unknown extends TClientContext\n ? Assign<TClientContext, TSendContext>\n : Assign<\n AssignAllMiddleware<TMiddlewares, 'allClientContextAfterNext'>,\n Assign<TClientContext, TSendContext>\n >\n\nexport type AssignAllServerSendContext<\n TMiddlewares,\n TSendContext = undefined,\n> = unknown extends TSendContext\n ? TSendContext\n : Assign<\n AssignAllMiddleware<TMiddlewares, 'allServerSendContext'>,\n TSendContext\n >\n\nexport type AssignAllServerRequestContext<\n TRegister,\n TMiddlewares,\n TSendContext = undefined,\n TServerContext = undefined,\n> = Assign<\n // Fetch Request Context\n GlobalFetchRequestContext,\n Assign<\n GlobalServerRequestContext<TRegister>,\n __AssignAllServerRequestContext<TMiddlewares, TSendContext, TServerContext>\n >\n>\n\n// export type GlobalFetchRequestContext<TRegister> = AnyContext\nexport type GlobalFetchRequestContext = Register extends {\n server: { requestContext: infer TRequestContext }\n}\n ? TRequestContext\n : AnyContext\n\nexport type GlobalServerRequestContext<TRegister> = TRegister extends {\n config: StartInstanceOptions<any, any, infer TRequestMiddlewares, any>\n}\n ? AssignAllMiddleware<TRequestMiddlewares, 'allServerContext'>\n : AnyContext\n\ntype __AssignAllServerRequestContext<\n TMiddlewares,\n TSendContext = undefined,\n TServerContext = undefined,\n> = unknown extends TSendContext\n ? Assign<TSendContext, TServerContext>\n : Assign<\n AssignAllMiddleware<TMiddlewares, 'allServerContext'>,\n Assign<TSendContext, TServerContext>\n >\n\nexport type AssignAllServerFnContext<\n TRegister,\n TMiddlewares,\n TSendContext = undefined,\n TServerContext = undefined,\n> = Assign<\n GlobalFetchRequestContext,\n Assign<\n GlobalServerRequestContext<TRegister>, // TODO: This enabled global middleware\n // type inference, but creates a circular types issue. No idea how to fix this.\n // AnyContext,\n Assign<\n GlobalServerFnContext<TRegister>, // TODO: This enabled global middleware\n // type inference, but creates a circular types issue. No idea how to fix this.\n // AnyContext,/\n __AssignAllServerFnContext<TMiddlewares, TSendContext, TServerContext>\n >\n >\n>\n\ntype GlobalServerFnContext<TRegister> = TRegister extends {\n config: StartInstanceOptions<any, any, any, infer TFunctionMiddlewares>\n}\n ? AssignAllMiddleware<TFunctionMiddlewares, 'allServerContext'>\n : AnyContext\n\ntype __AssignAllServerFnContext<\n TMiddlewares,\n TSendContext = undefined,\n TServerContext = undefined,\n> = unknown extends TSendContext\n ? Assign<TSendContext, TServerContext>\n : Assign<\n AssignAllMiddleware<TMiddlewares, 'allServerContext'>,\n Assign<TSendContext, TServerContext>\n >\n\nexport type AssignAllClientSendContext<\n TMiddlewares,\n TSendContext = undefined,\n> = unknown extends TSendContext\n ? TSendContext\n : Assign<\n AssignAllMiddleware<TMiddlewares, 'allClientSendContext'>,\n TSendContext\n >\n\nexport interface FunctionMiddlewareOptions<\n in out TRegister,\n in out TMiddlewares,\n in out TInputValidator,\n in out TServerContext,\n in out TClientContext,\n> {\n middleware?: TMiddlewares\n validator?: ConstrainValidator<TRegister, 'GET', TInputValidator>\n // TODO remove upon stable\n /** @deprecated Use `validator` instead. */\n inputValidator?: ConstrainValidator<TRegister, 'GET', TInputValidator>\n client?: FunctionMiddlewareClientFn<\n TRegister,\n TMiddlewares,\n TInputValidator,\n TServerContext,\n TClientContext\n >\n server?: FunctionMiddlewareServerFn<\n TRegister,\n TMiddlewares,\n TInputValidator,\n TServerContext,\n unknown,\n unknown\n >\n}\n\nexport type FunctionMiddlewareClientNextFn<TRegister, TMiddlewares> = <\n TSendContext = undefined,\n TNewClientContext = undefined,\n>(ctx?: {\n context?: TNewClientContext\n sendContext?: ValidateSerializableInput<TRegister, TSendContext>\n headers?: HeadersInit\n fetch?: CustomFetch\n}) => Promise<\n FunctionClientResultWithContext<TMiddlewares, TSendContext, TNewClientContext>\n>\n\nexport interface FunctionMiddlewareServer<\n TRegister,\n TMiddlewares,\n TInputValidator,\n TServerSendContext,\n TClientContext,\n> {\n server: <TNewServerContext = undefined, TSendContext = undefined>(\n server: FunctionMiddlewareServerFn<\n TRegister,\n TMiddlewares,\n TInputValidator,\n TServerSendContext,\n TNewServerContext,\n TSendContext\n >,\n ) => FunctionMiddlewareAfterServer<\n TRegister,\n TMiddlewares,\n TInputValidator,\n TNewServerContext,\n TServerSendContext,\n TClientContext,\n TSendContext\n >\n}\n\nexport type FunctionMiddlewareServerFn<\n TRegister,\n TMiddlewares,\n TInputValidator,\n TServerSendContext,\n TNewServerContext,\n TSendContext,\n> = (\n options: FunctionMiddlewareServerFnOptions<\n TRegister,\n TMiddlewares,\n TInputValidator,\n TServerSendContext\n >,\n) => FunctionMiddlewareServerFnResult<\n TRegister,\n TMiddlewares,\n TServerSendContext,\n TNewServerContext,\n TSendContext\n>\n\nexport type FunctionMiddlewareServerNextFn<\n TRegister,\n TMiddlewares,\n TServerSendContext,\n> = <TNewServerContext = undefined, TSendContext = undefined>(ctx?: {\n context?: TNewServerContext\n sendContext?: ValidateSerializableInput<TRegister, TSendContext>\n}) => Promise<\n FunctionServerResultWithContext<\n TRegister,\n TMiddlewares,\n TServerSendContext,\n TNewServerContext,\n TSendContext\n >\n>\n\nexport type FunctionServerResultWithContext<\n in out TRegister,\n in out TMiddlewares,\n in out TServerSendContext,\n in out TServerContext,\n in out TSendContext,\n> = {\n 'use functions must return the result of next()': true\n '~types': {\n context: TServerContext\n sendContext: TSendContext\n }\n context: Expand<\n AssignAllServerFnContext<\n TRegister,\n TMiddlewares,\n TServerSendContext,\n TServerContext\n >\n >\n sendContext: Expand<AssignAllClientSendContext<TMiddlewares, TSendContext>>\n}\n\nexport interface FunctionMiddlewareServerFnOptions<\n in out TRegister,\n in out TMiddlewares,\n in out TInputValidator,\n in out TServerSendContext,\n> {\n data: Expand<IntersectAllValidatorOutputs<TMiddlewares, TInputValidator>>\n context: Expand<\n AssignAllServerFnContext<TRegister, TMiddlewares, TServerSendContext>\n >\n next: FunctionMiddlewareServerNextFn<\n TRegister,\n TMiddlewares,\n TServerSendContext\n >\n method: Method\n serverFnMeta: ServerFnMeta\n signal: AbortSignal\n}\n\nexport type FunctionMiddlewareServerFnResult<\n TRegister,\n TMiddlewares,\n TServerSendContext,\n TServerContext,\n TSendContext,\n> =\n | Promise<\n FunctionServerResultWithContext<\n TRegister,\n TMiddlewares,\n TServerSendContext,\n TServerContext,\n TSendContext\n >\n >\n | FunctionServerResultWithContext<\n TRegister,\n TMiddlewares,\n TServerSendContext,\n TServerContext,\n TSendContext\n >\n\nexport interface FunctionMiddlewareAfterServer<\n TRegister,\n TMiddlewares,\n TInputValidator,\n TServerContext,\n TServerSendContext,\n TClientContext,\n TClientSendContext,\n> extends FunctionMiddlewareWithTypes<\n TRegister,\n TMiddlewares,\n TInputValidator,\n TServerContext,\n TServerSendContext,\n TClientContext,\n TClientSendContext\n> {}\n\nexport interface FunctionMiddlewareClient<\n TRegister,\n TMiddlewares,\n TInputValidator,\n> {\n client: <TSendServerContext = undefined, TNewClientContext = undefined>(\n client: FunctionMiddlewareClientFn<\n TRegister,\n TMiddlewares,\n TInputValidator,\n TSendServerContext,\n TNewClientContext\n >,\n ) => FunctionMiddlewareAfterClient<\n TRegister,\n TMiddlewares,\n TInputValidator,\n TSendServerContext,\n TNewClientContext\n >\n}\n\nexport type FunctionMiddlewareClientFn<\n TRegister,\n TMiddlewares,\n TInputValidator,\n TSendContext,\n TClientContext,\n> = (\n options: FunctionMiddlewareClientFnOptions<\n TRegister,\n TMiddlewares,\n TInputValidator\n >,\n) => FunctionMiddlewareClientFnResult<\n TMiddlewares,\n TSendContext,\n TClientContext\n>\n\nexport interface FunctionMiddlewareClientFnOptions<\n in out TRegister,\n in out TMiddlewares,\n in out TInputValidator,\n> {\n data: Expand<IntersectAllValidatorInputs<TMiddlewares, TInputValidator>>\n context: Expand<AssignAllClientContextBeforeNext<TMiddlewares>>\n sendContext: Expand<AssignAllServerSendContext<TMiddlewares>>\n method: Method\n signal: AbortSignal\n serverFnMeta: ClientFnMeta\n next: FunctionMiddlewareClientNextFn<TRegister, TMiddlewares>\n filename: string\n fetch?: CustomFetch\n}\n\nexport type FunctionMiddlewareClientFnResult<\n TMiddlewares,\n TSendContext,\n TClientContext,\n> =\n | Promise<\n FunctionClientResultWithContext<\n TMiddlewares,\n TSendContext,\n TClientContext\n >\n >\n | FunctionClientResultWithContext<TMiddlewares, TSendContext, TClientContext>\n\nexport type FunctionClientResultWithContext<\n in out TMiddlewares,\n in out TSendContext,\n in out TClientContext,\n> = {\n 'use functions must return the result of next()': true\n context: Expand<AssignAllClientContextAfterNext<TMiddlewares, TClientContext>>\n sendContext: Expand<AssignAllServerSendContext<TMiddlewares, TSendContext>>\n headers: HeadersInit\n fetch?: CustomFetch\n}\n\nexport interface FunctionMiddlewareAfterClient<\n TRegister,\n TMiddlewares,\n TInputValidator,\n TServerSendContext,\n TClientContext,\n>\n extends\n FunctionMiddlewareWithTypes<\n TRegister,\n TMiddlewares,\n TInputValidator,\n undefined,\n TServerSendContext,\n TClientContext,\n undefined\n >,\n FunctionMiddlewareServer<\n TRegister,\n TMiddlewares,\n TInputValidator,\n TServerSendContext,\n TClientContext\n > {}\n\nexport interface FunctionMiddlewareValidator<TRegister, TMiddlewares> {\n validator: <TNewValidator>(\n validator: ConstrainValidator<TRegister, 'GET', TNewValidator>,\n ) => FunctionMiddlewareAfterValidator<TRegister, TMiddlewares, TNewValidator>\n // TODO remove upon stable\n /** @deprecated Use `validator` instead. */\n inputValidator: <TNewValidator>(\n validator: ConstrainValidator<TRegister, 'GET', TNewValidator>,\n ) => FunctionMiddlewareAfterValidator<TRegister, TMiddlewares, TNewValidator>\n}\n\nexport interface FunctionMiddlewareAfterValidator<\n TRegister,\n TMiddlewares,\n TInputValidator,\n>\n extends\n FunctionMiddlewareWithTypes<\n TRegister,\n TMiddlewares,\n TInputValidator,\n undefined,\n undefined,\n undefined,\n undefined\n >,\n FunctionMiddlewareServer<\n TRegister,\n TMiddlewares,\n TInputValidator,\n undefined,\n undefined\n >,\n FunctionMiddlewareClient<TRegister, TMiddlewares, TInputValidator> {}\n\nexport interface RequestMiddleware<\n TRegister,\n> extends RequestMiddlewareAfterMiddleware<TRegister, undefined> {\n middleware: <const TMiddlewares = undefined>(\n middlewares: Constrain<TMiddlewares, ReadonlyArray<AnyRequestMiddleware>>,\n ) => RequestMiddlewareAfterMiddleware<TRegister, TMiddlewares>\n}\n\nexport type AnyRequestMiddleware = RequestMiddlewareWithTypes<any, any, any>\n\nexport interface RequestMiddlewareWithTypes<\n TRegister,\n TMiddlewares,\n TServerContext,\n> {\n '~types': RequestMiddlewareTypes<TRegister, TMiddlewares, TServerContext>\n options: RequestMiddlewareOptions<TRegister, TMiddlewares, TServerContext>\n}\n\nexport interface RequestMiddlewareOptions<\n in out TRegister,\n in out TMiddlewares,\n in out TServerContext,\n> {\n middleware?: TMiddlewares\n server?: RequestServerFn<TRegister, TMiddlewares, TServerContext>\n}\nexport interface RequestMiddlewareTypes<\n TRegister,\n TMiddlewares,\n TServerContext,\n> {\n type: 'request'\n // this only exists so we can use request middlewares in server functions\n allInput: undefined\n // this only exists so we can use request middlewares in server functions\n allOutput: undefined\n middlewares: TMiddlewares\n serverContext: TServerContext\n allServerContext: AssignAllServerRequestContext<\n TRegister,\n TMiddlewares,\n undefined,\n TServerContext\n >\n}\n\nexport interface RequestMiddlewareAfterMiddleware<TRegister, TMiddlewares>\n extends\n RequestMiddlewareWithTypes<TRegister, TMiddlewares, undefined>,\n RequestMiddlewareServer<TRegister, TMiddlewares> {}\n\nexport interface RequestMiddlewareServer<TRegister, TMiddlewares> {\n server: <TServerContext = undefined>(\n fn: RequestServerFn<TRegister, TMiddlewares, TServerContext>,\n ) => RequestMiddlewareAfterServer<TRegister, TMiddlewares, TServerContext>\n}\n\nexport type RequestServerFn<TRegister, TMiddlewares, TServerContext> = (\n options: RequestServerOptions<TRegister, TMiddlewares>,\n) => RequestMiddlewareServerFnResult<TRegister, TMiddlewares, TServerContext>\n\nexport interface RequestServerOptions<TRegister, TMiddlewares> {\n request: Request\n pathname: string\n context: Expand<AssignAllServerRequestContext<TRegister, TMiddlewares>>\n next: RequestServerNextFn<TRegister, TMiddlewares>\n /**\n * Type of Start handler currently processing this request.\n */\n handlerType: 'serverFn' | 'router'\n /**\n * Metadata about the server function being invoked.\n * This is only present when the request is handling a server function call.\n * For regular page requests, this will be undefined.\n */\n serverFnMeta?: ServerFnMeta\n}\n\nexport type RequestServerNextFn<TRegister, TMiddlewares> = <\n TServerContext = undefined,\n>(\n options?: RequestServerNextFnOptions<TServerContext>,\n) => RequestServerNextFnResult<TRegister, TMiddlewares, TServerContext>\n\nexport interface RequestServerNextFnOptions<TServerContext> {\n context?: TServerContext\n}\n\nexport type RequestServerNextFnResult<TRegister, TMiddlewares, TServerContext> =\n | Promise<RequestServerResult<TRegister, TMiddlewares, TServerContext>>\n | RequestServerResult<TRegister, TMiddlewares, TServerContext>\n\nexport type RequestMiddlewareServerFnResult<\n TRegister,\n TMiddlewares,\n TServerContext,\n> =\n | Promise<\n RequestServerResult<TRegister, TMiddlewares, TServerContext> | Response\n >\n | RequestServerResult<TRegister, TMiddlewares, TServerContext>\n | Response\n\nexport interface RequestServerResult<TRegister, TMiddlewares, TServerContext> {\n request: Request\n pathname: string\n context: Expand<\n AssignAllServerRequestContext<\n TRegister,\n TMiddlewares,\n undefined,\n TServerContext\n >\n >\n response: Response\n}\n\nexport interface RequestMiddlewareAfterServer<\n TRegister,\n TMiddlewares,\n TServerContext,\n> extends RequestMiddlewareWithTypes<TRegister, TMiddlewares, TServerContext> {}\n"],"mappings":";AAiCA,IAAa,oBAA4C,SAAS,WAAW;CAC3E,MAAM,kBAAkB;EACtB,MAAM;EACN,GAAI,UAAU;CAChB;CACA,MAAM,gBAAgB,cAAmB;EACvC,OAAO,iBACL,CAAC,GACD,OAAO,OAAO,iBAAiB;GAC7B;GAEA,gBAAgB;EAClB,CAAC,CACH;CACF;CAEA,OAAO;EACL,SAAS;EACT,aAAa,eAAoB;GAC/B,OAAO,iBACL,CAAC,GACD,OAAO,OAAO,iBAAiB,EAAE,WAAW,CAAC,CAC/C;EACF;EACA,WAAW;EAEX,gBAAgB;EAChB,SAAS,WAAgB;GACvB,OAAO,iBAAiB,CAAC,GAAG,OAAO,OAAO,iBAAiB,EAAE,OAAO,CAAC,CAAC;EACxE;EACA,SAAS,WAAgB;GACvB,OAAO,iBAAiB,CAAC,GAAG,OAAO,OAAO,iBAAiB,EAAE,OAAO,CAAC,CAAC;EACxE;CACF;AACF"} |
@@ -80,2 +80,4 @@ import { TSS_SERVER_FUNCTION_FACTORY, ClientFnMeta, ServerFnMeta, TSS_SERVER_FUNCTION } from './constants.js'; | ||
| middleware?: Constrain<TMiddlewares, ReadonlyArray<AnyFunctionMiddleware | AnyRequestMiddleware>>; | ||
| validator?: ConstrainValidator<TRegister, TMethod, TInputValidator, TStrict>; | ||
| /** @deprecated Use `validator` instead. */ | ||
| inputValidator?: ConstrainValidator<TRegister, TMethod, TInputValidator, TStrict>; | ||
@@ -95,4 +97,6 @@ extractedFn?: CompiledFetcherFn<TRegister, TResponse>; | ||
| } | ||
| export type ValidatorFn<TRegister, TMethod extends Method, TMiddlewares, TStrict extends ServerFnStrict> = <TInputValidator>(inputValidator: ConstrainValidator<TRegister, TMethod, TInputValidator, TStrict>) => ServerFnAfterValidator<TRegister, TMethod, TMiddlewares, TInputValidator, TStrict>; | ||
| export type ValidatorFn<TRegister, TMethod extends Method, TMiddlewares, TStrict extends ServerFnStrict> = <TInputValidator>(validator: ConstrainValidator<TRegister, TMethod, TInputValidator, TStrict>) => ServerFnAfterValidator<TRegister, TMethod, TMiddlewares, TInputValidator, TStrict>; | ||
| export interface ServerFnValidator<TRegister, TMethod extends Method, TMiddlewares, TStrict extends ServerFnStrict> { | ||
| validator: ValidatorFn<TRegister, TMethod, TMiddlewares, TStrict>; | ||
| /** @deprecated Use `validator` instead. */ | ||
| inputValidator: ValidatorFn<TRegister, TMethod, TMiddlewares, TStrict>; | ||
@@ -121,2 +125,4 @@ } | ||
| middlewares: TMiddlewares; | ||
| validator: TInputValidator; | ||
| /** @deprecated Use `validator` instead. */ | ||
| inputValidator: TInputValidator; | ||
@@ -123,0 +129,0 @@ response: TResponse; |
@@ -11,2 +11,9 @@ import { TSS_SERVER_FUNCTION_FACTORY } from "./constants.js"; | ||
| if (typeof resolvedOptions.method === "undefined") resolvedOptions.method = "GET"; | ||
| const setValidator = (validator) => { | ||
| return createServerFn(void 0, { | ||
| ...resolvedOptions, | ||
| validator, | ||
| inputValidator: validator | ||
| }); | ||
| }; | ||
| const res = { | ||
@@ -28,8 +35,4 @@ options: resolvedOptions, | ||
| }, | ||
| inputValidator: (inputValidator) => { | ||
| return createServerFn(void 0, { | ||
| ...resolvedOptions, | ||
| inputValidator | ||
| }); | ||
| }, | ||
| validator: setValidator, | ||
| inputValidator: setValidator, | ||
| handler: (...args) => { | ||
@@ -97,3 +100,5 @@ const [extractedFn, serverFn] = args; | ||
| try { | ||
| if ("inputValidator" in nextMiddleware.options && nextMiddleware.options.inputValidator && env === "server") ctx.data = await execValidator(nextMiddleware.options.inputValidator, ctx.data); | ||
| let validator = "validator" in nextMiddleware.options ? nextMiddleware.options.validator : void 0; | ||
| if (!validator && "inputValidator" in nextMiddleware.options) validator = nextMiddleware.options.inputValidator; | ||
| if (validator && env === "server") ctx.data = await execValidator(validator, ctx.data); | ||
| let middlewareFn = void 0; | ||
@@ -181,3 +186,3 @@ if (env === "client") { | ||
| options: { | ||
| inputValidator: options.inputValidator, | ||
| inputValidator: options.validator ?? options.inputValidator, | ||
| client: async ({ next, sendContext, fetch, ...ctx }) => { | ||
@@ -184,0 +189,0 @@ const payload = { |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"createServerFn.js","names":[],"sources":["../../src/createServerFn.ts"],"sourcesContent":["import { mergeHeaders } from '@tanstack/router-core/ssr/client'\n\nimport { isRedirect, parseRedirect } from '@tanstack/router-core'\nimport { TSS_SERVER_FUNCTION_FACTORY } from './constants'\nimport { getStartOptions } from './getStartOptions'\nimport { getStartContextServerOnly } from './getStartContextServerOnly'\nimport { createNullProtoObject, safeObjectMerge } from './safeObjectMerge'\nimport type {\n ClientFnMeta,\n ServerFnMeta,\n TSS_SERVER_FUNCTION,\n} from './constants'\nimport type {\n AnyValidator,\n Constrain,\n Expand,\n Register,\n RegisteredSerializableInput,\n ResolveValidatorInput,\n ValidateSerializable,\n ValidateSerializableInput,\n Validator,\n} from '@tanstack/router-core'\nimport type {\n AnyFunctionMiddleware,\n AnyRequestMiddleware,\n AssignAllServerFnContext,\n IntersectAllValidatorInputs,\n IntersectAllValidatorOutputs,\n} from './createMiddleware'\n\nexport type ServerFnStrict = boolean | { input?: boolean; output?: boolean }\n\nexport interface ServerFnOptions<\n TMethod extends Method = Method,\n TStrict extends ServerFnStrict = true,\n> {\n method?: TMethod\n strict?: TStrict\n}\n\nexport type ServerFnStrictInput<TStrict extends ServerFnStrict> =\n TStrict extends false\n ? false\n : TStrict extends { input: infer TInput extends boolean }\n ? TInput\n : true\n\nexport type ServerFnStrictOutput<TStrict extends ServerFnStrict> =\n TStrict extends false\n ? false\n : TStrict extends { output: infer TOutput extends boolean }\n ? TOutput\n : true\n\nexport type CreateServerFn<TRegister> = <\n TMethod extends Method,\n TStrict extends ServerFnStrict = true,\n TResponse = unknown,\n TMiddlewares = undefined,\n TInputValidator = undefined,\n>(\n options?: ServerFnOptions<TMethod, TStrict>,\n __opts?: ServerFnBaseOptions<\n TRegister,\n TMethod,\n TResponse,\n TMiddlewares,\n TInputValidator,\n TStrict\n >,\n) => ServerFnBuilder<TRegister, TMethod, TStrict>\n\nexport const createServerFn: CreateServerFn<Register> = (options, __opts) => {\n const resolvedOptions = (__opts || options || {}) as ServerFnBaseOptions<\n any,\n any,\n any,\n any,\n any,\n any\n >\n\n if (typeof resolvedOptions.method === 'undefined') {\n resolvedOptions.method = 'GET' as Method\n }\n\n const res: ServerFnBuilder<Register, Method, ServerFnStrict> = {\n options: resolvedOptions,\n middleware: (middleware) => {\n // multiple calls to `middleware()` merge the middlewares with the previously supplied ones\n // this is primarily useful for letting users create their own abstractions on top of `createServerFn`\n\n const newMiddleware = [...(resolvedOptions.middleware || [])]\n middleware.map((m) => {\n if (TSS_SERVER_FUNCTION_FACTORY in m) {\n if (m.options.middleware) {\n newMiddleware.push(...m.options.middleware)\n }\n } else {\n newMiddleware.push(m)\n }\n })\n\n const newOptions = {\n ...resolvedOptions,\n middleware: newMiddleware,\n }\n const res = createServerFn(undefined, newOptions)\n res[TSS_SERVER_FUNCTION_FACTORY] = true\n return res\n },\n inputValidator: (inputValidator) => {\n const newOptions = { ...resolvedOptions, inputValidator }\n return createServerFn(undefined, newOptions) as any\n },\n handler: (...args) => {\n // This function signature changes due to AST transformations\n // in the babel plugin. We need to cast it to the correct\n // function signature post-transformation\n const [extractedFn, serverFn] = args as unknown as [\n CompiledFetcherFn<Register, any>,\n ServerFn<Register, Method, any, any, any>,\n ]\n\n // Keep the original function around so we can use it\n // in the server environment\n const newOptions = { ...resolvedOptions, extractedFn, serverFn }\n\n const resolvedMiddleware = [\n ...(newOptions.middleware || []),\n serverFnBaseToMiddleware(newOptions),\n ]\n\n // We want to make sure the new function has the same\n // properties as the original function\n\n // Propagate the declared HTTP method onto the extracted handler\n // so the manifest-exported symbol (resolved by getServerFnById)\n // carries `method`, enabling the server handler to reject\n // mismatched HTTP methods before parsing request payloads.\n ;(extractedFn as any).method = resolvedOptions.method\n\n return Object.assign(\n async (opts?: CompiledFetcherFnOptions) => {\n // Start by executing the client-side middleware chain\n const result = await executeMiddleware(resolvedMiddleware, 'client', {\n ...extractedFn,\n ...newOptions,\n data: opts?.data,\n headers: opts?.headers,\n signal: opts?.signal,\n fetch: opts?.fetch,\n context: createNullProtoObject(),\n })\n\n const redirect = parseRedirect(result.error)\n if (redirect) {\n throw redirect\n }\n\n if (result.error) throw result.error\n return result.result\n },\n {\n // This copies over the URL, function ID\n ...extractedFn,\n // Expose the declared HTTP method so the server handler\n // can reject mismatched methods before parsing payloads\n method: resolvedOptions.method,\n // The extracted function on the server-side calls\n // this function\n __executeServer: async (opts: any) => {\n const startContext = getStartContextServerOnly()\n const serverContextAfterGlobalMiddlewares =\n startContext.contextAfterGlobalMiddlewares\n const ctx = {\n ...extractedFn,\n ...opts,\n // Ensure we use the full serverFnMeta from the provider file's extractedFn\n // (which has id, name, filename) rather than the partial one from SSR/client\n // callers (which only has id)\n serverFnMeta: extractedFn.serverFnMeta,\n // Merge client context first so trusted server middleware context wins.\n context: safeObjectMerge(\n opts.context,\n serverContextAfterGlobalMiddlewares,\n ),\n request: startContext.request,\n }\n\n const result = await executeMiddleware(\n resolvedMiddleware,\n 'server',\n ctx,\n ).then((d) => ({\n // Only send the result and sendContext back to the client\n result: d.result,\n error: d.error,\n context: d.sendContext,\n }))\n\n return result\n },\n },\n ) as any\n },\n } as ServerFnBuilder<Register, Method, ServerFnStrict>\n const fun = (options?: ServerFnOptions<Method, ServerFnStrict>) => {\n const newOptions = {\n ...resolvedOptions,\n ...options,\n }\n return createServerFn(undefined, newOptions)\n }\n return Object.assign(fun, res) as any\n}\n\nexport async function executeMiddleware(\n middlewares: Array<AnyFunctionMiddleware | AnyRequestMiddleware>,\n env: 'client' | 'server',\n opts: ServerFnMiddlewareOptions,\n): Promise<ServerFnMiddlewareResult> {\n const globalMiddlewares = getStartOptions()?.functionMiddleware || []\n let flattenedMiddlewares = flattenMiddlewares([\n ...globalMiddlewares,\n ...middlewares,\n ])\n\n // On server, filter out middlewares that already executed in the request phase\n // to prevent duplicate execution (issue #5239)\n if (env === 'server') {\n const startContext = getStartContextServerOnly({ throwIfNotFound: false })\n if (startContext?.executedRequestMiddlewares) {\n flattenedMiddlewares = flattenedMiddlewares.filter(\n (m) => !startContext.executedRequestMiddlewares.has(m),\n )\n }\n }\n\n const callNextMiddleware: NextFn = async (ctx) => {\n // Get the next middleware\n const nextMiddleware = flattenedMiddlewares.shift()\n\n // If there are no more middlewares, return the context\n if (!nextMiddleware) {\n return ctx\n }\n\n // Execute the middleware\n try {\n if (\n 'inputValidator' in nextMiddleware.options &&\n nextMiddleware.options.inputValidator &&\n env === 'server'\n ) {\n // Execute the middleware's input function\n ctx.data = await execValidator(\n nextMiddleware.options.inputValidator,\n ctx.data,\n )\n }\n\n let middlewareFn: MiddlewareFn | undefined = undefined\n if (env === 'client') {\n if ('client' in nextMiddleware.options) {\n middlewareFn = nextMiddleware.options.client as\n | MiddlewareFn\n | undefined\n }\n }\n // env === 'server'\n else if ('server' in nextMiddleware.options) {\n middlewareFn = nextMiddleware.options.server as MiddlewareFn | undefined\n }\n\n if (middlewareFn) {\n const userNext = async (\n userCtx: ServerFnMiddlewareResult | undefined = {} as any,\n ) => {\n // Return the next middleware\n // Use safeObjectMerge for context objects to prevent prototype pollution\n const nextCtx = {\n ...ctx,\n ...userCtx,\n context: safeObjectMerge(ctx.context, userCtx.context),\n sendContext: safeObjectMerge(ctx.sendContext, userCtx.sendContext),\n headers: mergeHeaders(ctx.headers, userCtx.headers),\n _callSiteFetch: ctx._callSiteFetch,\n fetch: ctx._callSiteFetch ?? userCtx.fetch ?? ctx.fetch,\n result:\n userCtx.result !== undefined\n ? userCtx.result\n : userCtx instanceof Response\n ? userCtx\n : (ctx as any).result,\n error: userCtx.error ?? (ctx as any).error,\n }\n\n const result = await callNextMiddleware(nextCtx)\n\n if (result.error) {\n throw result.error\n }\n\n return result\n }\n\n // Execute the middleware\n const result = await middlewareFn({\n ...ctx,\n next: userNext,\n })\n\n // If result is NOT a ctx object, we need to return it as\n // the { result }\n if (isRedirect(result)) {\n return {\n ...ctx,\n error: result,\n }\n }\n\n if (result instanceof Response) {\n return {\n ...ctx,\n result,\n }\n }\n\n if (!(result as any)) {\n throw new Error(\n 'User middleware returned undefined. You must call next() or return a result in your middlewares.',\n )\n }\n\n return result\n }\n\n return callNextMiddleware(ctx)\n } catch (error: any) {\n return {\n ...ctx,\n error,\n }\n }\n }\n\n // Start the middleware chain\n return callNextMiddleware({\n ...opts,\n headers: opts.headers || {},\n sendContext: opts.sendContext || {},\n context: opts.context || createNullProtoObject(),\n _callSiteFetch: opts.fetch,\n })\n}\n\nexport type CompiledFetcherFnOptions = {\n method: Method\n data: unknown\n headers?: HeadersInit\n signal?: AbortSignal\n fetch?: CustomFetch\n context?: any\n}\n\nexport type Fetcher<TMiddlewares, TInputValidator, TResponse> =\n undefined extends IntersectAllValidatorInputs<TMiddlewares, TInputValidator>\n ? OptionalFetcher<TMiddlewares, TInputValidator, TResponse>\n : RequiredFetcher<TMiddlewares, TInputValidator, TResponse>\n\nexport interface FetcherBase {\n [TSS_SERVER_FUNCTION]: true\n url: string\n method: Method\n __executeServer: (opts: {\n method: Method\n data: unknown\n headers?: HeadersInit\n context?: any\n }) => Promise<unknown>\n}\n\nexport interface OptionalFetcher<\n TMiddlewares,\n TInputValidator,\n TResponse,\n> extends FetcherBase {\n (\n options?: OptionalFetcherDataOptions<TMiddlewares, TInputValidator>,\n ): Promise<Awaited<TResponse>>\n}\n\nexport interface RequiredFetcher<\n TMiddlewares,\n TInputValidator,\n TResponse,\n> extends FetcherBase {\n (\n opts: RequiredFetcherDataOptions<TMiddlewares, TInputValidator>,\n ): Promise<Awaited<TResponse>>\n}\n\n// Ideally, this type should just be `export type CustomFetch = typeof globalThis.fetch`, but that conflicts with the type overrides the `bun-types` package - a dependency of unplugin.\n// Relevant bun issues:\n// - https://github.com/oven-sh/bun/issues/23500\n// - https://github.com/oven-sh/bun/issues/23741\nexport type CustomFetch = typeof fetch extends (...args: infer A) => infer R\n ? (...args: A) => R\n : never\n\nexport type FetcherBaseOptions = {\n headers?: HeadersInit\n signal?: AbortSignal\n fetch?: CustomFetch\n}\n\nexport interface OptionalFetcherDataOptions<\n TMiddlewares,\n TInputValidator,\n> extends FetcherBaseOptions {\n data?: Expand<IntersectAllValidatorInputs<TMiddlewares, TInputValidator>>\n}\n\nexport interface RequiredFetcherDataOptions<\n TMiddlewares,\n TInputValidator,\n> extends FetcherBaseOptions {\n data: Expand<IntersectAllValidatorInputs<TMiddlewares, TInputValidator>>\n}\n\nexport type RscStream<T> = {\n __cacheState: T\n}\n\nexport type Method = 'GET' | 'POST'\n\nexport type ServerFnReturnType<\n TRegister,\n TResponse,\n TStrict extends ServerFnStrict = true,\n> =\n ServerFnStrictOutput<TStrict> extends false\n ? TResponse\n : TResponse extends PromiseLike<infer U>\n ? Promise<ServerFnReturnType<TRegister, U, TStrict>>\n : TResponse extends Response\n ? TResponse\n : ValidateSerializableInput<TRegister, TResponse>\n\nexport type ServerFn<\n TRegister,\n TMethod,\n TMiddlewares,\n TInputValidator,\n TResponse,\n TStrict extends ServerFnStrict = true,\n> = (\n ctx: ServerFnCtx<TRegister, TMethod, TMiddlewares, TInputValidator>,\n) => ServerFnReturnType<TRegister, TResponse, TStrict>\n\nexport interface ServerFnCtx<\n TRegister,\n TMethod,\n TMiddlewares,\n TInputValidator,\n> {\n data: Expand<IntersectAllValidatorOutputs<TMiddlewares, TInputValidator>>\n serverFnMeta: ServerFnMeta\n context: Expand<AssignAllServerFnContext<TRegister, TMiddlewares, {}>>\n method: TMethod\n}\n\nexport type CompiledFetcherFn<TRegister, TResponse> = {\n (\n opts: CompiledFetcherFnOptions & ServerFnBaseOptions<TRegister, Method>,\n ): Promise<TResponse>\n url: string\n serverFnMeta: ServerFnMeta\n}\n\nexport type ServerFnBaseOptions<\n TRegister,\n TMethod extends Method = 'GET',\n TResponse = unknown,\n TMiddlewares = unknown,\n TInputValidator = unknown,\n TStrict extends ServerFnStrict = true,\n> = {\n method: TMethod\n strict?: TStrict\n middleware?: Constrain<\n TMiddlewares,\n ReadonlyArray<AnyFunctionMiddleware | AnyRequestMiddleware>\n >\n inputValidator?: ConstrainValidator<\n TRegister,\n TMethod,\n TInputValidator,\n TStrict\n >\n extractedFn?: CompiledFetcherFn<TRegister, TResponse>\n serverFn?: ServerFn<\n TRegister,\n TMethod,\n TMiddlewares,\n TInputValidator,\n TResponse,\n TStrict\n >\n}\n\nexport type ValidateValidatorInput<\n TRegister,\n TMethod extends Method,\n TInputValidator,\n TStrict extends ServerFnStrict = true,\n> =\n ServerFnStrictInput<TStrict> extends false\n ? ResolveValidatorInput<TInputValidator>\n : TMethod extends 'POST'\n ? ResolveValidatorInput<TInputValidator> extends FormData\n ? ResolveValidatorInput<TInputValidator>\n : ValidateSerializable<\n ResolveValidatorInput<TInputValidator>,\n RegisteredSerializableInput<TRegister>\n >\n : ValidateSerializable<\n ResolveValidatorInput<TInputValidator>,\n RegisteredSerializableInput<TRegister>\n >\n\nexport type ValidateValidator<\n TRegister,\n TMethod extends Method,\n TInputValidator,\n TStrict extends ServerFnStrict = true,\n> =\n ValidateValidatorInput<\n TRegister,\n TMethod,\n TInputValidator,\n TStrict\n > extends infer TInput\n ? Validator<TInput, any>\n : never\n\nexport type ConstrainValidator<\n TRegister,\n TMethod extends Method,\n TInputValidator,\n TStrict extends ServerFnStrict = true,\n> =\n | (unknown extends TInputValidator\n ? TInputValidator\n : ResolveValidatorInput<TInputValidator> extends ValidateValidator<\n TRegister,\n TMethod,\n TInputValidator,\n TStrict\n >\n ? TInputValidator\n : never)\n | ValidateValidator<TRegister, TMethod, TInputValidator, TStrict>\n\nexport type AppendMiddlewares<TMiddlewares, TNewMiddlewares> =\n TMiddlewares extends ReadonlyArray<any>\n ? TNewMiddlewares extends ReadonlyArray<any>\n ? readonly [...TMiddlewares, ...TNewMiddlewares]\n : TMiddlewares\n : TNewMiddlewares\n\nexport interface ServerFnMiddleware<\n TRegister,\n TMethod extends Method,\n TMiddlewares,\n TInputValidator,\n TStrict extends ServerFnStrict,\n> {\n middleware: <const TNewMiddlewares>(\n middlewares: Constrain<\n TNewMiddlewares,\n ReadonlyArray<AnyFunctionMiddleware | AnyRequestMiddleware | AnyServerFn>\n >,\n ) => ServerFnAfterMiddleware<\n TRegister,\n TMethod,\n AppendMiddlewares<TMiddlewares, TNewMiddlewares>,\n TInputValidator,\n TStrict\n >\n}\n\nexport interface ServerFnAfterMiddleware<\n TRegister,\n TMethod extends Method,\n TMiddlewares,\n TInputValidator,\n TStrict extends ServerFnStrict,\n>\n extends\n ServerFnWithTypes<\n TRegister,\n TMethod,\n TMiddlewares,\n TInputValidator,\n undefined,\n TStrict\n >,\n ServerFnMiddleware<TRegister, TMethod, TMiddlewares, undefined, TStrict>,\n ServerFnValidator<TRegister, TMethod, TMiddlewares, TStrict>,\n ServerFnHandler<\n TRegister,\n TMethod,\n TMiddlewares,\n TInputValidator,\n TStrict\n > {\n <\n TNewMethod extends Method = TMethod,\n TNewStrict extends ServerFnStrict = TStrict,\n >(\n options?: ServerFnOptions<TNewMethod, TNewStrict>,\n ): ServerFnAfterMiddleware<\n TRegister,\n TNewMethod,\n TMiddlewares,\n TInputValidator,\n TNewStrict\n >\n}\n\nexport type ValidatorFn<\n TRegister,\n TMethod extends Method,\n TMiddlewares,\n TStrict extends ServerFnStrict,\n> = <TInputValidator>(\n inputValidator: ConstrainValidator<\n TRegister,\n TMethod,\n TInputValidator,\n TStrict\n >,\n) => ServerFnAfterValidator<\n TRegister,\n TMethod,\n TMiddlewares,\n TInputValidator,\n TStrict\n>\n\nexport interface ServerFnValidator<\n TRegister,\n TMethod extends Method,\n TMiddlewares,\n TStrict extends ServerFnStrict,\n> {\n inputValidator: ValidatorFn<TRegister, TMethod, TMiddlewares, TStrict>\n}\n\nexport interface ServerFnAfterValidator<\n TRegister,\n TMethod extends Method,\n TMiddlewares,\n TInputValidator,\n TStrict extends ServerFnStrict,\n>\n extends\n ServerFnWithTypes<\n TRegister,\n TMethod,\n TMiddlewares,\n TInputValidator,\n undefined,\n TStrict\n >,\n ServerFnMiddleware<\n TRegister,\n TMethod,\n TMiddlewares,\n TInputValidator,\n TStrict\n >,\n ServerFnHandler<\n TRegister,\n TMethod,\n TMiddlewares,\n TInputValidator,\n TStrict\n > {}\n\nexport interface ServerFnAfterTyper<\n TRegister,\n TMethod extends Method,\n TMiddlewares,\n TInputValidator,\n TStrict extends ServerFnStrict,\n>\n extends\n ServerFnWithTypes<\n TRegister,\n TMethod,\n TMiddlewares,\n TInputValidator,\n undefined,\n TStrict\n >,\n ServerFnHandler<\n TRegister,\n TMethod,\n TMiddlewares,\n TInputValidator,\n TStrict\n > {}\n\n// Handler\nexport interface ServerFnHandler<\n TRegister,\n TMethod extends Method,\n TMiddlewares,\n TInputValidator,\n TStrict extends ServerFnStrict,\n> {\n handler: <TNewResponse>(\n fn?: ServerFn<\n TRegister,\n TMethod,\n TMiddlewares,\n TInputValidator,\n TNewResponse,\n TStrict\n >,\n ) => Fetcher<TMiddlewares, TInputValidator, TNewResponse>\n}\n\nexport interface ServerFnBuilder<\n TRegister,\n TMethod extends Method = 'GET',\n TStrict extends ServerFnStrict = true,\n>\n extends\n ServerFnWithTypes<\n TRegister,\n TMethod,\n undefined,\n undefined,\n undefined,\n TStrict\n >,\n ServerFnMiddleware<TRegister, TMethod, undefined, undefined, TStrict>,\n ServerFnValidator<TRegister, TMethod, undefined, TStrict>,\n ServerFnHandler<TRegister, TMethod, undefined, undefined, TStrict> {\n <\n TNewMethod extends Method = TMethod,\n TNewStrict extends ServerFnStrict = TStrict,\n >(\n options?: ServerFnOptions<TNewMethod, TNewStrict>,\n ): ServerFnBuilder<TRegister, TNewMethod, TNewStrict>\n options: ServerFnBaseOptions<\n TRegister,\n TMethod,\n unknown,\n undefined,\n undefined,\n TStrict\n >\n}\n\nexport interface ServerFnWithTypes<\n in out TRegister,\n in out TMethod extends Method,\n in out TMiddlewares,\n in out TInputValidator,\n in out TResponse,\n in out TStrict extends ServerFnStrict,\n> {\n '~types': ServerFnTypes<\n TRegister,\n TMethod,\n TMiddlewares,\n TInputValidator,\n TResponse,\n TStrict\n >\n options: ServerFnBaseOptions<\n TRegister,\n TMethod,\n unknown,\n undefined,\n undefined,\n TStrict\n >\n [TSS_SERVER_FUNCTION_FACTORY]: true\n}\n\nexport type AnyServerFn = ServerFnWithTypes<any, any, any, any, any, any>\n\nexport interface ServerFnTypes<\n in out TRegister,\n in out TMethod extends Method,\n in out TMiddlewares,\n in out TInputValidator,\n in out TResponse,\n in out TStrict extends ServerFnStrict,\n> {\n method: TMethod\n strict: TStrict\n middlewares: TMiddlewares\n inputValidator: TInputValidator\n response: TResponse\n allServerContext: AssignAllServerFnContext<TRegister, TMiddlewares>\n allInput: IntersectAllValidatorInputs<TMiddlewares, TInputValidator>\n allOutput: IntersectAllValidatorOutputs<TMiddlewares, TInputValidator>\n}\n\nexport function flattenMiddlewares<\n T extends AnyFunctionMiddleware | AnyRequestMiddleware,\n>(middlewares: Array<T>, maxDepth: number = 100): Array<T> {\n const seen = new Set<T>()\n const flattened: Array<T> = []\n\n const recurse = (middleware: Array<T>, depth: number) => {\n if (depth > maxDepth) {\n throw new Error(\n `Middleware nesting depth exceeded maximum of ${maxDepth}. Check for circular references.`,\n )\n }\n middleware.forEach((m) => {\n if (m.options.middleware) {\n recurse(m.options.middleware as Array<T>, depth + 1)\n }\n\n if (!seen.has(m)) {\n seen.add(m)\n flattened.push(m)\n }\n })\n }\n\n recurse(middlewares, 0)\n\n return flattened\n}\n\nexport type ServerFnMiddlewareOptions = {\n method: Method\n data: any\n headers?: HeadersInit\n signal?: AbortSignal\n sendContext?: any\n context?: any\n serverFnMeta: ClientFnMeta\n fetch?: CustomFetch\n /** @internal - Preserves the call-site fetch to ensure it has highest priority over middleware */\n _callSiteFetch?: CustomFetch\n}\n\nexport type ServerFnMiddlewareResult = ServerFnMiddlewareOptions & {\n result?: unknown\n error?: unknown\n}\n\nexport type NextFn = (\n ctx: ServerFnMiddlewareResult,\n) => Promise<ServerFnMiddlewareResult>\n\nexport type MiddlewareFn = (\n ctx: ServerFnMiddlewareOptions & {\n next: NextFn\n },\n) => Promise<ServerFnMiddlewareResult>\n\nexport async function execValidator(\n validator: AnyValidator,\n input: unknown,\n): Promise<unknown> {\n if (validator == null) return {}\n\n if ('~standard' in validator) {\n const result = await validator['~standard'].validate(input)\n\n if (result.issues)\n throw new Error(JSON.stringify(result.issues, undefined, 2))\n\n return result.value\n }\n\n if ('parse' in validator) {\n return validator.parse(input)\n }\n\n if (typeof validator === 'function') {\n return validator(input)\n }\n\n throw new Error('Invalid validator type!')\n}\n\nfunction serverFnBaseToMiddleware(\n options: ServerFnBaseOptions<any, any, any, any, any>,\n): AnyFunctionMiddleware {\n return {\n '~types': undefined!,\n options: {\n inputValidator: options.inputValidator,\n client: async ({ next, sendContext, fetch, ...ctx }) => {\n const payload = {\n ...ctx,\n // switch the sendContext over to context\n context: sendContext,\n fetch,\n }\n\n // Execute the extracted function\n // but not before serializing the context\n const res = await options.extractedFn?.(payload)\n\n return next(res)\n },\n server: async ({ next, ...ctx }) => {\n // Execute the server function\n const result = await options.serverFn?.(ctx)\n\n return next({\n ...ctx,\n result,\n } as any)\n },\n },\n }\n}\n"],"mappings":"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"} | ||
| {"version":3,"file":"createServerFn.js","names":[],"sources":["../../src/createServerFn.ts"],"sourcesContent":["import { mergeHeaders } from '@tanstack/router-core/ssr/client'\n\nimport { isRedirect, parseRedirect } from '@tanstack/router-core'\nimport { TSS_SERVER_FUNCTION_FACTORY } from './constants'\nimport { getStartOptions } from './getStartOptions'\nimport { getStartContextServerOnly } from './getStartContextServerOnly'\nimport { createNullProtoObject, safeObjectMerge } from './safeObjectMerge'\nimport type {\n ClientFnMeta,\n ServerFnMeta,\n TSS_SERVER_FUNCTION,\n} from './constants'\nimport type {\n AnyValidator,\n Constrain,\n Expand,\n Register,\n RegisteredSerializableInput,\n ResolveValidatorInput,\n ValidateSerializable,\n ValidateSerializableInput,\n Validator,\n} from '@tanstack/router-core'\nimport type {\n AnyFunctionMiddleware,\n AnyRequestMiddleware,\n AssignAllServerFnContext,\n IntersectAllValidatorInputs,\n IntersectAllValidatorOutputs,\n} from './createMiddleware'\n\nexport type ServerFnStrict = boolean | { input?: boolean; output?: boolean }\n\nexport interface ServerFnOptions<\n TMethod extends Method = Method,\n TStrict extends ServerFnStrict = true,\n> {\n method?: TMethod\n strict?: TStrict\n}\n\nexport type ServerFnStrictInput<TStrict extends ServerFnStrict> =\n TStrict extends false\n ? false\n : TStrict extends { input: infer TInput extends boolean }\n ? TInput\n : true\n\nexport type ServerFnStrictOutput<TStrict extends ServerFnStrict> =\n TStrict extends false\n ? false\n : TStrict extends { output: infer TOutput extends boolean }\n ? TOutput\n : true\n\nexport type CreateServerFn<TRegister> = <\n TMethod extends Method,\n TStrict extends ServerFnStrict = true,\n TResponse = unknown,\n TMiddlewares = undefined,\n TInputValidator = undefined,\n>(\n options?: ServerFnOptions<TMethod, TStrict>,\n __opts?: ServerFnBaseOptions<\n TRegister,\n TMethod,\n TResponse,\n TMiddlewares,\n TInputValidator,\n TStrict\n >,\n) => ServerFnBuilder<TRegister, TMethod, TStrict>\n\nexport const createServerFn: CreateServerFn<Register> = (options, __opts) => {\n const resolvedOptions = (__opts || options || {}) as ServerFnBaseOptions<\n any,\n any,\n any,\n any,\n any,\n any\n >\n\n if (typeof resolvedOptions.method === 'undefined') {\n resolvedOptions.method = 'GET' as Method\n }\n\n const setValidator = (validator: any) => {\n // TODO remove upon stable\n const newOptions = {\n ...resolvedOptions,\n validator,\n inputValidator: validator,\n }\n return createServerFn(undefined, newOptions) as any\n }\n\n const res: ServerFnBuilder<Register, Method, ServerFnStrict> = {\n options: resolvedOptions,\n middleware: (middleware) => {\n // multiple calls to `middleware()` merge the middlewares with the previously supplied ones\n // this is primarily useful for letting users create their own abstractions on top of `createServerFn`\n\n const newMiddleware = [...(resolvedOptions.middleware || [])]\n middleware.map((m) => {\n if (TSS_SERVER_FUNCTION_FACTORY in m) {\n if (m.options.middleware) {\n newMiddleware.push(...m.options.middleware)\n }\n } else {\n newMiddleware.push(m)\n }\n })\n\n const newOptions = {\n ...resolvedOptions,\n middleware: newMiddleware,\n }\n const res = createServerFn(undefined, newOptions)\n res[TSS_SERVER_FUNCTION_FACTORY] = true\n return res\n },\n validator: setValidator,\n // TODO remove upon stable\n inputValidator: setValidator,\n handler: (...args) => {\n // This function signature changes due to AST transformations\n // in the babel plugin. We need to cast it to the correct\n // function signature post-transformation\n const [extractedFn, serverFn] = args as unknown as [\n CompiledFetcherFn<Register, any>,\n ServerFn<Register, Method, any, any, any>,\n ]\n\n // Keep the original function around so we can use it\n // in the server environment\n const newOptions = { ...resolvedOptions, extractedFn, serverFn }\n\n const resolvedMiddleware = [\n ...(newOptions.middleware || []),\n serverFnBaseToMiddleware(newOptions),\n ]\n\n // We want to make sure the new function has the same\n // properties as the original function\n\n // Propagate the declared HTTP method onto the extracted handler\n // so the manifest-exported symbol (resolved by getServerFnById)\n // carries `method`, enabling the server handler to reject\n // mismatched HTTP methods before parsing request payloads.\n ;(extractedFn as any).method = resolvedOptions.method\n\n return Object.assign(\n async (opts?: CompiledFetcherFnOptions) => {\n // Start by executing the client-side middleware chain\n const result = await executeMiddleware(resolvedMiddleware, 'client', {\n ...extractedFn,\n ...newOptions,\n data: opts?.data,\n headers: opts?.headers,\n signal: opts?.signal,\n fetch: opts?.fetch,\n context: createNullProtoObject(),\n })\n\n const redirect = parseRedirect(result.error)\n if (redirect) {\n throw redirect\n }\n\n if (result.error) throw result.error\n return result.result\n },\n {\n // This copies over the URL, function ID\n ...extractedFn,\n // Expose the declared HTTP method so the server handler\n // can reject mismatched methods before parsing payloads\n method: resolvedOptions.method,\n // The extracted function on the server-side calls\n // this function\n __executeServer: async (opts: any) => {\n const startContext = getStartContextServerOnly()\n const serverContextAfterGlobalMiddlewares =\n startContext.contextAfterGlobalMiddlewares\n const ctx = {\n ...extractedFn,\n ...opts,\n // Ensure we use the full serverFnMeta from the provider file's extractedFn\n // (which has id, name, filename) rather than the partial one from SSR/client\n // callers (which only has id)\n serverFnMeta: extractedFn.serverFnMeta,\n // Merge client context first so trusted server middleware context wins.\n context: safeObjectMerge(\n opts.context,\n serverContextAfterGlobalMiddlewares,\n ),\n request: startContext.request,\n }\n\n const result = await executeMiddleware(\n resolvedMiddleware,\n 'server',\n ctx,\n ).then((d) => ({\n // Only send the result and sendContext back to the client\n result: d.result,\n error: d.error,\n context: d.sendContext,\n }))\n\n return result\n },\n },\n ) as any\n },\n } as ServerFnBuilder<Register, Method, ServerFnStrict>\n const fun = (options?: ServerFnOptions<Method, ServerFnStrict>) => {\n const newOptions = {\n ...resolvedOptions,\n ...options,\n }\n return createServerFn(undefined, newOptions)\n }\n return Object.assign(fun, res) as any\n}\n\nexport async function executeMiddleware(\n middlewares: Array<AnyFunctionMiddleware | AnyRequestMiddleware>,\n env: 'client' | 'server',\n opts: ServerFnMiddlewareOptions,\n): Promise<ServerFnMiddlewareResult> {\n const globalMiddlewares = getStartOptions()?.functionMiddleware || []\n let flattenedMiddlewares = flattenMiddlewares([\n ...globalMiddlewares,\n ...middlewares,\n ])\n\n // On server, filter out middlewares that already executed in the request phase\n // to prevent duplicate execution (issue #5239)\n if (env === 'server') {\n const startContext = getStartContextServerOnly({ throwIfNotFound: false })\n if (startContext?.executedRequestMiddlewares) {\n flattenedMiddlewares = flattenedMiddlewares.filter(\n (m) => !startContext.executedRequestMiddlewares.has(m),\n )\n }\n }\n\n const callNextMiddleware: NextFn = async (ctx) => {\n // Get the next middleware\n const nextMiddleware = flattenedMiddlewares.shift()\n\n // If there are no more middlewares, return the context\n if (!nextMiddleware) {\n return ctx\n }\n\n // Execute the middleware\n try {\n let validator =\n 'validator' in nextMiddleware.options\n ? nextMiddleware.options.validator\n : undefined\n\n // TODO remove upon stable\n if (!validator && 'inputValidator' in nextMiddleware.options) {\n validator = nextMiddleware.options.inputValidator\n }\n\n if (validator && env === 'server') {\n // Execute the middleware's input function\n ctx.data = await execValidator(validator as AnyValidator, ctx.data)\n }\n\n let middlewareFn: MiddlewareFn | undefined = undefined\n if (env === 'client') {\n if ('client' in nextMiddleware.options) {\n middlewareFn = nextMiddleware.options.client as\n | MiddlewareFn\n | undefined\n }\n }\n // env === 'server'\n else if ('server' in nextMiddleware.options) {\n middlewareFn = nextMiddleware.options.server as MiddlewareFn | undefined\n }\n\n if (middlewareFn) {\n const userNext = async (\n userCtx: ServerFnMiddlewareResult | undefined = {} as any,\n ) => {\n // Return the next middleware\n // Use safeObjectMerge for context objects to prevent prototype pollution\n const nextCtx = {\n ...ctx,\n ...userCtx,\n context: safeObjectMerge(ctx.context, userCtx.context),\n sendContext: safeObjectMerge(ctx.sendContext, userCtx.sendContext),\n headers: mergeHeaders(ctx.headers, userCtx.headers),\n _callSiteFetch: ctx._callSiteFetch,\n fetch: ctx._callSiteFetch ?? userCtx.fetch ?? ctx.fetch,\n result:\n userCtx.result !== undefined\n ? userCtx.result\n : userCtx instanceof Response\n ? userCtx\n : (ctx as any).result,\n error: userCtx.error ?? (ctx as any).error,\n }\n\n const result = await callNextMiddleware(nextCtx)\n\n if (result.error) {\n throw result.error\n }\n\n return result\n }\n\n // Execute the middleware\n const result = await middlewareFn({\n ...ctx,\n next: userNext,\n })\n\n // If result is NOT a ctx object, we need to return it as\n // the { result }\n if (isRedirect(result)) {\n return {\n ...ctx,\n error: result,\n }\n }\n\n if (result instanceof Response) {\n return {\n ...ctx,\n result,\n }\n }\n\n if (!(result as any)) {\n throw new Error(\n 'User middleware returned undefined. You must call next() or return a result in your middlewares.',\n )\n }\n\n return result\n }\n\n return callNextMiddleware(ctx)\n } catch (error: any) {\n return {\n ...ctx,\n error,\n }\n }\n }\n\n // Start the middleware chain\n return callNextMiddleware({\n ...opts,\n headers: opts.headers || {},\n sendContext: opts.sendContext || {},\n context: opts.context || createNullProtoObject(),\n _callSiteFetch: opts.fetch,\n })\n}\n\nexport type CompiledFetcherFnOptions = {\n method: Method\n data: unknown\n headers?: HeadersInit\n signal?: AbortSignal\n fetch?: CustomFetch\n context?: any\n}\n\nexport type Fetcher<TMiddlewares, TInputValidator, TResponse> =\n undefined extends IntersectAllValidatorInputs<TMiddlewares, TInputValidator>\n ? OptionalFetcher<TMiddlewares, TInputValidator, TResponse>\n : RequiredFetcher<TMiddlewares, TInputValidator, TResponse>\n\nexport interface FetcherBase {\n [TSS_SERVER_FUNCTION]: true\n url: string\n method: Method\n __executeServer: (opts: {\n method: Method\n data: unknown\n headers?: HeadersInit\n context?: any\n }) => Promise<unknown>\n}\n\nexport interface OptionalFetcher<\n TMiddlewares,\n TInputValidator,\n TResponse,\n> extends FetcherBase {\n (\n options?: OptionalFetcherDataOptions<TMiddlewares, TInputValidator>,\n ): Promise<Awaited<TResponse>>\n}\n\nexport interface RequiredFetcher<\n TMiddlewares,\n TInputValidator,\n TResponse,\n> extends FetcherBase {\n (\n opts: RequiredFetcherDataOptions<TMiddlewares, TInputValidator>,\n ): Promise<Awaited<TResponse>>\n}\n\n// Ideally, this type should just be `export type CustomFetch = typeof globalThis.fetch`, but that conflicts with the type overrides the `bun-types` package - a dependency of unplugin.\n// Relevant bun issues:\n// - https://github.com/oven-sh/bun/issues/23500\n// - https://github.com/oven-sh/bun/issues/23741\nexport type CustomFetch = typeof fetch extends (...args: infer A) => infer R\n ? (...args: A) => R\n : never\n\nexport type FetcherBaseOptions = {\n headers?: HeadersInit\n signal?: AbortSignal\n fetch?: CustomFetch\n}\n\nexport interface OptionalFetcherDataOptions<\n TMiddlewares,\n TInputValidator,\n> extends FetcherBaseOptions {\n data?: Expand<IntersectAllValidatorInputs<TMiddlewares, TInputValidator>>\n}\n\nexport interface RequiredFetcherDataOptions<\n TMiddlewares,\n TInputValidator,\n> extends FetcherBaseOptions {\n data: Expand<IntersectAllValidatorInputs<TMiddlewares, TInputValidator>>\n}\n\nexport type RscStream<T> = {\n __cacheState: T\n}\n\nexport type Method = 'GET' | 'POST'\n\nexport type ServerFnReturnType<\n TRegister,\n TResponse,\n TStrict extends ServerFnStrict = true,\n> =\n ServerFnStrictOutput<TStrict> extends false\n ? TResponse\n : TResponse extends PromiseLike<infer U>\n ? Promise<ServerFnReturnType<TRegister, U, TStrict>>\n : TResponse extends Response\n ? TResponse\n : ValidateSerializableInput<TRegister, TResponse>\n\nexport type ServerFn<\n TRegister,\n TMethod,\n TMiddlewares,\n TInputValidator,\n TResponse,\n TStrict extends ServerFnStrict = true,\n> = (\n ctx: ServerFnCtx<TRegister, TMethod, TMiddlewares, TInputValidator>,\n) => ServerFnReturnType<TRegister, TResponse, TStrict>\n\nexport interface ServerFnCtx<\n TRegister,\n TMethod,\n TMiddlewares,\n TInputValidator,\n> {\n data: Expand<IntersectAllValidatorOutputs<TMiddlewares, TInputValidator>>\n serverFnMeta: ServerFnMeta\n context: Expand<AssignAllServerFnContext<TRegister, TMiddlewares, {}>>\n method: TMethod\n}\n\nexport type CompiledFetcherFn<TRegister, TResponse> = {\n (\n opts: CompiledFetcherFnOptions & ServerFnBaseOptions<TRegister, Method>,\n ): Promise<TResponse>\n url: string\n serverFnMeta: ServerFnMeta\n}\n\nexport type ServerFnBaseOptions<\n TRegister,\n TMethod extends Method = 'GET',\n TResponse = unknown,\n TMiddlewares = unknown,\n TInputValidator = unknown,\n TStrict extends ServerFnStrict = true,\n> = {\n method: TMethod\n strict?: TStrict\n middleware?: Constrain<\n TMiddlewares,\n ReadonlyArray<AnyFunctionMiddleware | AnyRequestMiddleware>\n >\n validator?: ConstrainValidator<TRegister, TMethod, TInputValidator, TStrict>\n // TODO remove upon stable\n /** @deprecated Use `validator` instead. */\n inputValidator?: ConstrainValidator<\n TRegister,\n TMethod,\n TInputValidator,\n TStrict\n >\n extractedFn?: CompiledFetcherFn<TRegister, TResponse>\n serverFn?: ServerFn<\n TRegister,\n TMethod,\n TMiddlewares,\n TInputValidator,\n TResponse,\n TStrict\n >\n}\n\nexport type ValidateValidatorInput<\n TRegister,\n TMethod extends Method,\n TInputValidator,\n TStrict extends ServerFnStrict = true,\n> =\n ServerFnStrictInput<TStrict> extends false\n ? ResolveValidatorInput<TInputValidator>\n : TMethod extends 'POST'\n ? ResolveValidatorInput<TInputValidator> extends FormData\n ? ResolveValidatorInput<TInputValidator>\n : ValidateSerializable<\n ResolveValidatorInput<TInputValidator>,\n RegisteredSerializableInput<TRegister>\n >\n : ValidateSerializable<\n ResolveValidatorInput<TInputValidator>,\n RegisteredSerializableInput<TRegister>\n >\n\nexport type ValidateValidator<\n TRegister,\n TMethod extends Method,\n TInputValidator,\n TStrict extends ServerFnStrict = true,\n> =\n ValidateValidatorInput<\n TRegister,\n TMethod,\n TInputValidator,\n TStrict\n > extends infer TInput\n ? Validator<TInput, any>\n : never\n\nexport type ConstrainValidator<\n TRegister,\n TMethod extends Method,\n TInputValidator,\n TStrict extends ServerFnStrict = true,\n> =\n | (unknown extends TInputValidator\n ? TInputValidator\n : ResolveValidatorInput<TInputValidator> extends ValidateValidator<\n TRegister,\n TMethod,\n TInputValidator,\n TStrict\n >\n ? TInputValidator\n : never)\n | ValidateValidator<TRegister, TMethod, TInputValidator, TStrict>\n\nexport type AppendMiddlewares<TMiddlewares, TNewMiddlewares> =\n TMiddlewares extends ReadonlyArray<any>\n ? TNewMiddlewares extends ReadonlyArray<any>\n ? readonly [...TMiddlewares, ...TNewMiddlewares]\n : TMiddlewares\n : TNewMiddlewares\n\nexport interface ServerFnMiddleware<\n TRegister,\n TMethod extends Method,\n TMiddlewares,\n TInputValidator,\n TStrict extends ServerFnStrict,\n> {\n middleware: <const TNewMiddlewares>(\n middlewares: Constrain<\n TNewMiddlewares,\n ReadonlyArray<AnyFunctionMiddleware | AnyRequestMiddleware | AnyServerFn>\n >,\n ) => ServerFnAfterMiddleware<\n TRegister,\n TMethod,\n AppendMiddlewares<TMiddlewares, TNewMiddlewares>,\n TInputValidator,\n TStrict\n >\n}\n\nexport interface ServerFnAfterMiddleware<\n TRegister,\n TMethod extends Method,\n TMiddlewares,\n TInputValidator,\n TStrict extends ServerFnStrict,\n>\n extends\n ServerFnWithTypes<\n TRegister,\n TMethod,\n TMiddlewares,\n TInputValidator,\n undefined,\n TStrict\n >,\n ServerFnMiddleware<TRegister, TMethod, TMiddlewares, undefined, TStrict>,\n ServerFnValidator<TRegister, TMethod, TMiddlewares, TStrict>,\n ServerFnHandler<\n TRegister,\n TMethod,\n TMiddlewares,\n TInputValidator,\n TStrict\n > {\n <\n TNewMethod extends Method = TMethod,\n TNewStrict extends ServerFnStrict = TStrict,\n >(\n options?: ServerFnOptions<TNewMethod, TNewStrict>,\n ): ServerFnAfterMiddleware<\n TRegister,\n TNewMethod,\n TMiddlewares,\n TInputValidator,\n TNewStrict\n >\n}\n\nexport type ValidatorFn<\n TRegister,\n TMethod extends Method,\n TMiddlewares,\n TStrict extends ServerFnStrict,\n> = <TInputValidator>(\n validator: ConstrainValidator<TRegister, TMethod, TInputValidator, TStrict>,\n) => ServerFnAfterValidator<\n TRegister,\n TMethod,\n TMiddlewares,\n TInputValidator,\n TStrict\n>\n\nexport interface ServerFnValidator<\n TRegister,\n TMethod extends Method,\n TMiddlewares,\n TStrict extends ServerFnStrict,\n> {\n validator: ValidatorFn<TRegister, TMethod, TMiddlewares, TStrict>\n // TODO remove upon stable\n /** @deprecated Use `validator` instead. */\n inputValidator: ValidatorFn<TRegister, TMethod, TMiddlewares, TStrict>\n}\n\nexport interface ServerFnAfterValidator<\n TRegister,\n TMethod extends Method,\n TMiddlewares,\n TInputValidator,\n TStrict extends ServerFnStrict,\n>\n extends\n ServerFnWithTypes<\n TRegister,\n TMethod,\n TMiddlewares,\n TInputValidator,\n undefined,\n TStrict\n >,\n ServerFnMiddleware<\n TRegister,\n TMethod,\n TMiddlewares,\n TInputValidator,\n TStrict\n >,\n ServerFnHandler<\n TRegister,\n TMethod,\n TMiddlewares,\n TInputValidator,\n TStrict\n > {}\n\nexport interface ServerFnAfterTyper<\n TRegister,\n TMethod extends Method,\n TMiddlewares,\n TInputValidator,\n TStrict extends ServerFnStrict,\n>\n extends\n ServerFnWithTypes<\n TRegister,\n TMethod,\n TMiddlewares,\n TInputValidator,\n undefined,\n TStrict\n >,\n ServerFnHandler<\n TRegister,\n TMethod,\n TMiddlewares,\n TInputValidator,\n TStrict\n > {}\n\n// Handler\nexport interface ServerFnHandler<\n TRegister,\n TMethod extends Method,\n TMiddlewares,\n TInputValidator,\n TStrict extends ServerFnStrict,\n> {\n handler: <TNewResponse>(\n fn?: ServerFn<\n TRegister,\n TMethod,\n TMiddlewares,\n TInputValidator,\n TNewResponse,\n TStrict\n >,\n ) => Fetcher<TMiddlewares, TInputValidator, TNewResponse>\n}\n\nexport interface ServerFnBuilder<\n TRegister,\n TMethod extends Method = 'GET',\n TStrict extends ServerFnStrict = true,\n>\n extends\n ServerFnWithTypes<\n TRegister,\n TMethod,\n undefined,\n undefined,\n undefined,\n TStrict\n >,\n ServerFnMiddleware<TRegister, TMethod, undefined, undefined, TStrict>,\n ServerFnValidator<TRegister, TMethod, undefined, TStrict>,\n ServerFnHandler<TRegister, TMethod, undefined, undefined, TStrict> {\n <\n TNewMethod extends Method = TMethod,\n TNewStrict extends ServerFnStrict = TStrict,\n >(\n options?: ServerFnOptions<TNewMethod, TNewStrict>,\n ): ServerFnBuilder<TRegister, TNewMethod, TNewStrict>\n options: ServerFnBaseOptions<\n TRegister,\n TMethod,\n unknown,\n undefined,\n undefined,\n TStrict\n >\n}\n\nexport interface ServerFnWithTypes<\n in out TRegister,\n in out TMethod extends Method,\n in out TMiddlewares,\n in out TInputValidator,\n in out TResponse,\n in out TStrict extends ServerFnStrict,\n> {\n '~types': ServerFnTypes<\n TRegister,\n TMethod,\n TMiddlewares,\n TInputValidator,\n TResponse,\n TStrict\n >\n options: ServerFnBaseOptions<\n TRegister,\n TMethod,\n unknown,\n undefined,\n undefined,\n TStrict\n >\n [TSS_SERVER_FUNCTION_FACTORY]: true\n}\n\nexport type AnyServerFn = ServerFnWithTypes<any, any, any, any, any, any>\n\nexport interface ServerFnTypes<\n in out TRegister,\n in out TMethod extends Method,\n in out TMiddlewares,\n in out TInputValidator,\n in out TResponse,\n in out TStrict extends ServerFnStrict,\n> {\n method: TMethod\n strict: TStrict\n middlewares: TMiddlewares\n validator: TInputValidator\n // TODO remove upon stable\n /** @deprecated Use `validator` instead. */\n inputValidator: TInputValidator\n response: TResponse\n allServerContext: AssignAllServerFnContext<TRegister, TMiddlewares>\n allInput: IntersectAllValidatorInputs<TMiddlewares, TInputValidator>\n allOutput: IntersectAllValidatorOutputs<TMiddlewares, TInputValidator>\n}\n\nexport function flattenMiddlewares<\n T extends AnyFunctionMiddleware | AnyRequestMiddleware,\n>(middlewares: Array<T>, maxDepth: number = 100): Array<T> {\n const seen = new Set<T>()\n const flattened: Array<T> = []\n\n const recurse = (middleware: Array<T>, depth: number) => {\n if (depth > maxDepth) {\n throw new Error(\n `Middleware nesting depth exceeded maximum of ${maxDepth}. Check for circular references.`,\n )\n }\n middleware.forEach((m) => {\n if (m.options.middleware) {\n recurse(m.options.middleware as Array<T>, depth + 1)\n }\n\n if (!seen.has(m)) {\n seen.add(m)\n flattened.push(m)\n }\n })\n }\n\n recurse(middlewares, 0)\n\n return flattened\n}\n\nexport type ServerFnMiddlewareOptions = {\n method: Method\n data: any\n headers?: HeadersInit\n signal?: AbortSignal\n sendContext?: any\n context?: any\n serverFnMeta: ClientFnMeta\n fetch?: CustomFetch\n /** @internal - Preserves the call-site fetch to ensure it has highest priority over middleware */\n _callSiteFetch?: CustomFetch\n}\n\nexport type ServerFnMiddlewareResult = ServerFnMiddlewareOptions & {\n result?: unknown\n error?: unknown\n}\n\nexport type NextFn = (\n ctx: ServerFnMiddlewareResult,\n) => Promise<ServerFnMiddlewareResult>\n\nexport type MiddlewareFn = (\n ctx: ServerFnMiddlewareOptions & {\n next: NextFn\n },\n) => Promise<ServerFnMiddlewareResult>\n\nexport async function execValidator(\n validator: AnyValidator,\n input: unknown,\n): Promise<unknown> {\n if (validator == null) return {}\n\n if ('~standard' in validator) {\n const result = await validator['~standard'].validate(input)\n\n if (result.issues)\n throw new Error(JSON.stringify(result.issues, undefined, 2))\n\n return result.value\n }\n\n if ('parse' in validator) {\n return validator.parse(input)\n }\n\n if (typeof validator === 'function') {\n return validator(input)\n }\n\n throw new Error('Invalid validator type!')\n}\n\nfunction serverFnBaseToMiddleware(\n options: ServerFnBaseOptions<any, any, any, any, any>,\n): AnyFunctionMiddleware {\n // TODO remove upon stable\n const validator = options.validator ?? options.inputValidator\n\n return {\n '~types': undefined!,\n options: {\n // TODO remove upon stable\n inputValidator: validator,\n client: async ({ next, sendContext, fetch, ...ctx }) => {\n const payload = {\n ...ctx,\n // switch the sendContext over to context\n context: sendContext,\n fetch,\n }\n\n // Execute the extracted function\n // but not before serializing the context\n const res = await options.extractedFn?.(payload)\n\n return next(res)\n },\n server: async ({ next, ...ctx }) => {\n // Execute the server function\n const result = await options.serverFn?.(ctx)\n\n return next({\n ...ctx,\n result,\n } as any)\n },\n },\n }\n}\n"],"mappings":";;;;;;;AAyEA,IAAa,kBAA4C,SAAS,WAAW;CAC3E,MAAM,kBAAmB,UAAU,WAAW,CAAC;CAS/C,IAAI,OAAO,gBAAgB,WAAW,aACpC,gBAAgB,SAAS;CAG3B,MAAM,gBAAgB,cAAmB;EAOvC,OAAO,eAAe,KAAA,GAAW;GAJ/B,GAAG;GACH;GACA,gBAAgB;EAEe,CAAU;CAC7C;CAEA,MAAM,MAAyD;EAC7D,SAAS;EACT,aAAa,eAAe;GAI1B,MAAM,gBAAgB,CAAC,GAAI,gBAAgB,cAAc,CAAC,CAAE;GAC5D,WAAW,KAAK,MAAM;IACpB,IAAI,+BAA+B;SAC7B,EAAE,QAAQ,YACZ,cAAc,KAAK,GAAG,EAAE,QAAQ,UAAU;IAAA,OAG5C,cAAc,KAAK,CAAC;GAExB,CAAC;GAMD,MAAM,MAAM,eAAe,KAAA,GAAW;IAHpC,GAAG;IACH,YAAY;GAEwB,CAAU;GAChD,IAAI,+BAA+B;GACnC,OAAO;EACT;EACA,WAAW;EAEX,gBAAgB;EAChB,UAAU,GAAG,SAAS;GAIpB,MAAM,CAAC,aAAa,YAAY;GAOhC,MAAM,aAAa;IAAE,GAAG;IAAiB;IAAa;GAAS;GAE/D,MAAM,qBAAqB,CACzB,GAAI,WAAW,cAAc,CAAC,GAC9B,yBAAyB,UAAU,CACrC;GASC,YAAqB,SAAS,gBAAgB;GAE/C,OAAO,OAAO,OACZ,OAAO,SAAoC;IAEzC,MAAM,SAAS,MAAM,kBAAkB,oBAAoB,UAAU;KACnE,GAAG;KACH,GAAG;KACH,MAAM,MAAM;KACZ,SAAS,MAAM;KACf,QAAQ,MAAM;KACd,OAAO,MAAM;KACb,SAAS,sBAAsB;IACjC,CAAC;IAED,MAAM,WAAW,cAAc,OAAO,KAAK;IAC3C,IAAI,UACF,MAAM;IAGR,IAAI,OAAO,OAAO,MAAM,OAAO;IAC/B,OAAO,OAAO;GAChB,GACA;IAEE,GAAG;IAGH,QAAQ,gBAAgB;IAGxB,iBAAiB,OAAO,SAAc;KACpC,MAAM,eAAe,0BAA0B;KAC/C,MAAM,sCACJ,aAAa;KA2Bf,OAAO,MAXc,kBACnB,oBACA,UACA;MAjBA,GAAG;MACH,GAAG;MAIH,cAAc,YAAY;MAE1B,SAAS,gBACP,KAAK,SACL,mCACF;MACA,SAAS,aAAa;KAMtB,CACF,EAAE,MAAM,OAAO;MAEb,QAAQ,EAAE;MACV,OAAO,EAAE;MACT,SAAS,EAAE;KACb,EAAE;IAGJ;GACF,CACF;EACF;CACF;CACA,MAAM,OAAO,YAAsD;EAKjE,OAAO,eAAe,KAAA,GAAW;GAH/B,GAAG;GACH,GAAG;EAE4B,CAAU;CAC7C;CACA,OAAO,OAAO,OAAO,KAAK,GAAG;AAC/B;AAEA,eAAsB,kBACpB,aACA,KACA,MACmC;CAEnC,IAAI,uBAAuB,mBAAmB,CAC5C,GAFwB,gBAAgB,GAAG,sBAAsB,CAAC,GAGlE,GAAG,WACL,CAAC;CAID,IAAI,QAAQ,UAAU;EACpB,MAAM,eAAe,0BAA0B,EAAE,iBAAiB,MAAM,CAAC;EACzE,IAAI,cAAc,4BAChB,uBAAuB,qBAAqB,QACzC,MAAM,CAAC,aAAa,2BAA2B,IAAI,CAAC,CACvD;CAEJ;CAEA,MAAM,qBAA6B,OAAO,QAAQ;EAEhD,MAAM,iBAAiB,qBAAqB,MAAM;EAGlD,IAAI,CAAC,gBACH,OAAO;EAIT,IAAI;GACF,IAAI,YACF,eAAe,eAAe,UAC1B,eAAe,QAAQ,YACvB,KAAA;GAGN,IAAI,CAAC,aAAa,oBAAoB,eAAe,SACnD,YAAY,eAAe,QAAQ;GAGrC,IAAI,aAAa,QAAQ,UAEvB,IAAI,OAAO,MAAM,cAAc,WAA2B,IAAI,IAAI;GAGpE,IAAI,eAAyC,KAAA;GAC7C,IAAI,QAAQ;QACN,YAAY,eAAe,SAC7B,eAAe,eAAe,QAAQ;GAAA,OAMrC,IAAI,YAAY,eAAe,SAClC,eAAe,eAAe,QAAQ;GAGxC,IAAI,cAAc;IAChB,MAAM,WAAW,OACf,UAAgD,CAAC,MAC9C;KAoBH,MAAM,SAAS,MAAM,mBAAmB;MAhBtC,GAAG;MACH,GAAG;MACH,SAAS,gBAAgB,IAAI,SAAS,QAAQ,OAAO;MACrD,aAAa,gBAAgB,IAAI,aAAa,QAAQ,WAAW;MACjE,SAAS,aAAa,IAAI,SAAS,QAAQ,OAAO;MAClD,gBAAgB,IAAI;MACpB,OAAO,IAAI,kBAAkB,QAAQ,SAAS,IAAI;MAClD,QACE,QAAQ,WAAW,KAAA,IACf,QAAQ,SACR,mBAAmB,WACjB,UACC,IAAY;MACrB,OAAO,QAAQ,SAAU,IAAY;KAGC,CAAO;KAE/C,IAAI,OAAO,OACT,MAAM,OAAO;KAGf,OAAO;IACT;IAGA,MAAM,SAAS,MAAM,aAAa;KAChC,GAAG;KACH,MAAM;IACR,CAAC;IAID,IAAI,WAAW,MAAM,GACnB,OAAO;KACL,GAAG;KACH,OAAO;IACT;IAGF,IAAI,kBAAkB,UACpB,OAAO;KACL,GAAG;KACH;IACF;IAGF,IAAI,CAAE,QACJ,MAAM,IAAI,MACR,kGACF;IAGF,OAAO;GACT;GAEA,OAAO,mBAAmB,GAAG;EAC/B,SAAS,OAAY;GACnB,OAAO;IACL,GAAG;IACH;GACF;EACF;CACF;CAGA,OAAO,mBAAmB;EACxB,GAAG;EACH,SAAS,KAAK,WAAW,CAAC;EAC1B,aAAa,KAAK,eAAe,CAAC;EAClC,SAAS,KAAK,WAAW,sBAAsB;EAC/C,gBAAgB,KAAK;CACvB,CAAC;AACH;AAidA,SAAgB,mBAEd,aAAuB,WAAmB,KAAe;CACzD,MAAM,uBAAO,IAAI,IAAO;CACxB,MAAM,YAAsB,CAAC;CAE7B,MAAM,WAAW,YAAsB,UAAkB;EACvD,IAAI,QAAQ,UACV,MAAM,IAAI,MACR,gDAAgD,SAAS,iCAC3D;EAEF,WAAW,SAAS,MAAM;GACxB,IAAI,EAAE,QAAQ,YACZ,QAAQ,EAAE,QAAQ,YAAwB,QAAQ,CAAC;GAGrD,IAAI,CAAC,KAAK,IAAI,CAAC,GAAG;IAChB,KAAK,IAAI,CAAC;IACV,UAAU,KAAK,CAAC;GAClB;EACF,CAAC;CACH;CAEA,QAAQ,aAAa,CAAC;CAEtB,OAAO;AACT;AA8BA,eAAsB,cACpB,WACA,OACkB;CAClB,IAAI,aAAa,MAAM,OAAO,CAAC;CAE/B,IAAI,eAAe,WAAW;EAC5B,MAAM,SAAS,MAAM,UAAU,aAAa,SAAS,KAAK;EAE1D,IAAI,OAAO,QACT,MAAM,IAAI,MAAM,KAAK,UAAU,OAAO,QAAQ,KAAA,GAAW,CAAC,CAAC;EAE7D,OAAO,OAAO;CAChB;CAEA,IAAI,WAAW,WACb,OAAO,UAAU,MAAM,KAAK;CAG9B,IAAI,OAAO,cAAc,YACvB,OAAO,UAAU,KAAK;CAGxB,MAAM,IAAI,MAAM,yBAAyB;AAC3C;AAEA,SAAS,yBACP,SACuB;CAIvB,OAAO;EACL,UAAU,KAAA;EACV,SAAS;GAEP,gBANc,QAAQ,aAAa,QAAQ;GAO3C,QAAQ,OAAO,EAAE,MAAM,aAAa,OAAO,GAAG,UAAU;IACtD,MAAM,UAAU;KACd,GAAG;KAEH,SAAS;KACT;IACF;IAMA,OAAO,KAAK,MAFM,QAAQ,cAAc,OAAO,CAEhC;GACjB;GACA,QAAQ,OAAO,EAAE,MAAM,GAAG,UAAU;IAElC,MAAM,SAAS,MAAM,QAAQ,WAAW,GAAG;IAE3C,OAAO,KAAK;KACV,GAAG;KACH;IACF,CAAQ;GACV;EACF;CACF;AACF"} |
+1
-1
| { | ||
| "name": "@tanstack/start-client-core", | ||
| "version": "1.170.11", | ||
| "version": "1.170.12", | ||
| "description": "Modern and scalable routing for React applications", | ||
@@ -5,0 +5,0 @@ "author": "Tanner Linsley", |
@@ -34,3 +34,3 @@ --- | ||
| - Enforce auth in every server function, server route, or API endpoint that reads or writes private user, tenant, or account data. Use route `beforeLoad` for page UX, not as the data boundary. | ||
| - Use `.inputValidator()` on every server function that accepts input. | ||
| - Use `.validator()` on every server function that accepts input. | ||
| - Store sessions in `HttpOnly`, `Secure`, `SameSite` cookies. Do not store session tokens in `localStorage` or `sessionStorage`. | ||
@@ -143,3 +143,3 @@ - Hash passwords with bcrypt, scrypt, or Argon2. For missing users, verify against a dummy hash and return the same login/reset message. | ||
| export const login = createServerFn({ method: 'POST' }) | ||
| .inputValidator(z.object({ email: z.string().email(), password: z.string() })) | ||
| .validator(z.object({ email: z.string().email(), password: z.string() })) | ||
| .handler(async ({ data }) => { | ||
@@ -242,3 +242,3 @@ const user = await db.users.findByEmail(data.email) | ||
| export const requestPasswordReset = createServerFn({ method: 'POST' }) | ||
| .inputValidator(z.object({ email: z.string().email() })) | ||
| .validator(z.object({ email: z.string().email() })) | ||
| .handler(async ({ data }) => { | ||
@@ -375,3 +375,3 @@ const user = await db.users.findByEmail(data.email) | ||
| .middleware([authMiddleware]) | ||
| .inputValidator(z.object({ workspaceId: z.string().uuid() })) | ||
| .validator(z.object({ workspaceId: z.string().uuid() })) | ||
| .handler(async ({ context, data }) => { | ||
@@ -384,3 +384,3 @@ return db.workspaces.findById(data.workspaceId) // missing membership check! | ||
| .middleware([authMiddleware]) | ||
| .inputValidator(z.object({ workspaceId: z.string().uuid() })) | ||
| .validator(z.object({ workspaceId: z.string().uuid() })) | ||
| .handler(async ({ context, data }) => { | ||
@@ -387,0 +387,0 @@ const member = await db.memberships.find({ |
@@ -23,3 +23,3 @@ --- | ||
| > **CRITICAL**: TypeScript enforces method order: `middleware()` → `inputValidator()` → `client()` → `server()`. Wrong order causes type errors. | ||
| > **CRITICAL**: TypeScript enforces method order: `middleware()` → `validator()` → `client()` → `server()`. Wrong order causes type errors. | ||
| > **CRITICAL**: Validating the _shape_ of `sendContext` (e.g. `z.string().uuid().parse(...)`) is NOT authorization. A parsed identifier is a well-formed identifier, not an authorized one. Always re-check access against the session principal before using a client-sent ID as a query key, filter, or path parameter. | ||
@@ -33,3 +33,3 @@ | ||
| | Methods | `.server()` | `.client()`, `.server()` | | ||
| | Input validation | No | Yes (`.inputValidator()`) | | ||
| | Input validation | No | Yes (`.validator()`) | | ||
| | Client-side logic | No | Yes | | ||
@@ -165,3 +165,3 @@ | Created with | `createMiddleware()` | `createMiddleware({ type: 'function' })` | | ||
| const workspaceMiddleware = createMiddleware({ type: 'function' }) | ||
| .inputValidator(zodValidator(z.object({ workspaceId: z.string() }))) | ||
| .validator(zodValidator(z.object({ workspaceId: z.string() }))) | ||
| .server(async ({ next, data }) => { | ||
@@ -388,6 +388,6 @@ console.log('Workspace:', data.workspaceId) | ||
| // CORRECT — middleware → inputValidator → client → server | ||
| // CORRECT — middleware → validator → client → server | ||
| createMiddleware({ type: 'function' }) | ||
| .middleware([dep]) | ||
| .inputValidator(schema) | ||
| .validator(schema) | ||
| .client(({ next }) => next()) | ||
@@ -394,0 +394,0 @@ .server(({ next }) => next()) |
| --- | ||
| name: start-core/server-functions | ||
| description: >- | ||
| createServerFn (GET/POST), inputValidator (Zod or function), | ||
| createServerFn (GET/POST), validator (Zod or function), | ||
| useServerFn hook, server context utilities (getRequest, | ||
@@ -78,3 +78,3 @@ getRequestHeader, setResponseHeader, setResponseStatus), error | ||
| const deletePost = createServerFn({ method: 'POST' }) | ||
| .inputValidator((data: { id: string }) => data) | ||
| .validator((data: { id: string }) => data) | ||
| .handler(async ({ data }) => { | ||
@@ -102,3 +102,3 @@ await db.delete('posts').where({ id: data.id }) | ||
| const greetUser = createServerFn({ method: 'GET' }) | ||
| .inputValidator((data: { name: string }) => data) | ||
| .validator((data: { name: string }) => data) | ||
| .handler(async ({ data }) => { | ||
@@ -117,3 +117,3 @@ return `Hello, ${data.name}!` | ||
| const createUser = createServerFn({ method: 'POST' }) | ||
| .inputValidator( | ||
| .validator( | ||
| z.object({ | ||
@@ -133,3 +133,3 @@ name: z.string().min(1), | ||
| const submitForm = createServerFn({ method: 'POST' }) | ||
| .inputValidator((data) => { | ||
| .validator((data) => { | ||
| if (!(data instanceof FormData)) { | ||
@@ -184,3 +184,3 @@ throw new Error('Expected FormData') | ||
| const getPost = createServerFn() | ||
| .inputValidator((data: { id: string }) => data) | ||
| .validator((data: { id: string }) => data) | ||
| .handler(async ({ data }) => { | ||
@@ -266,3 +266,3 @@ const post = await db.findPost(data.id) | ||
| export const getUser = createServerFn({ method: 'GET' }) | ||
| .inputValidator((data: { id: string }) => data) | ||
| .validator((data: { id: string }) => data) | ||
| .handler(async ({ data }) => { | ||
@@ -269,0 +269,0 @@ return findUserById(data.id) |
@@ -39,2 +39,12 @@ import type { StartInstanceOptions } from './createStart' | ||
| } | ||
| const setValidator = (validator: any) => { | ||
| return createMiddleware( | ||
| {}, | ||
| Object.assign(resolvedOptions, { | ||
| validator, | ||
| // TODO remove upon stable | ||
| inputValidator: validator, | ||
| }), | ||
| ) | ||
| } | ||
@@ -49,8 +59,5 @@ return { | ||
| }, | ||
| inputValidator: (inputValidator: any) => { | ||
| return createMiddleware( | ||
| {}, | ||
| Object.assign(resolvedOptions, { inputValidator }), | ||
| ) | ||
| }, | ||
| validator: setValidator, | ||
| // TODO remove upon stable | ||
| inputValidator: setValidator, | ||
| client: (client: any) => { | ||
@@ -175,2 +182,5 @@ return createMiddleware({}, Object.assign(resolvedOptions, { client })) | ||
| > | ||
| validator: TInputValidator | ||
| // TODO remove upon stable | ||
| /** @deprecated Use `validator` instead. */ | ||
| inputValidator: TInputValidator | ||
@@ -385,2 +395,5 @@ } | ||
| middleware?: TMiddlewares | ||
| validator?: ConstrainValidator<TRegister, 'GET', TInputValidator> | ||
| // TODO remove upon stable | ||
| /** @deprecated Use `validator` instead. */ | ||
| inputValidator?: ConstrainValidator<TRegister, 'GET', TInputValidator> | ||
@@ -675,4 +688,9 @@ client?: FunctionMiddlewareClientFn< | ||
| export interface FunctionMiddlewareValidator<TRegister, TMiddlewares> { | ||
| validator: <TNewValidator>( | ||
| validator: ConstrainValidator<TRegister, 'GET', TNewValidator>, | ||
| ) => FunctionMiddlewareAfterValidator<TRegister, TMiddlewares, TNewValidator> | ||
| // TODO remove upon stable | ||
| /** @deprecated Use `validator` instead. */ | ||
| inputValidator: <TNewValidator>( | ||
| inputValidator: ConstrainValidator<TRegister, 'GET', TNewValidator>, | ||
| validator: ConstrainValidator<TRegister, 'GET', TNewValidator>, | ||
| ) => FunctionMiddlewareAfterValidator<TRegister, TMiddlewares, TNewValidator> | ||
@@ -679,0 +697,0 @@ } |
+40
-20
@@ -88,2 +88,12 @@ import { mergeHeaders } from '@tanstack/router-core/ssr/client' | ||
| const setValidator = (validator: any) => { | ||
| // TODO remove upon stable | ||
| const newOptions = { | ||
| ...resolvedOptions, | ||
| validator, | ||
| inputValidator: validator, | ||
| } | ||
| return createServerFn(undefined, newOptions) as any | ||
| } | ||
| const res: ServerFnBuilder<Register, Method, ServerFnStrict> = { | ||
@@ -114,6 +124,5 @@ options: resolvedOptions, | ||
| }, | ||
| inputValidator: (inputValidator) => { | ||
| const newOptions = { ...resolvedOptions, inputValidator } | ||
| return createServerFn(undefined, newOptions) as any | ||
| }, | ||
| validator: setValidator, | ||
| // TODO remove upon stable | ||
| inputValidator: setValidator, | ||
| handler: (...args) => { | ||
@@ -254,12 +263,15 @@ // This function signature changes due to AST transformations | ||
| try { | ||
| if ( | ||
| 'inputValidator' in nextMiddleware.options && | ||
| nextMiddleware.options.inputValidator && | ||
| env === 'server' | ||
| ) { | ||
| let validator = | ||
| 'validator' in nextMiddleware.options | ||
| ? nextMiddleware.options.validator | ||
| : undefined | ||
| // TODO remove upon stable | ||
| if (!validator && 'inputValidator' in nextMiddleware.options) { | ||
| validator = nextMiddleware.options.inputValidator | ||
| } | ||
| if (validator && env === 'server') { | ||
| // Execute the middleware's input function | ||
| ctx.data = await execValidator( | ||
| nextMiddleware.options.inputValidator, | ||
| ctx.data, | ||
| ) | ||
| ctx.data = await execValidator(validator as AnyValidator, ctx.data) | ||
| } | ||
@@ -500,2 +512,5 @@ | ||
| > | ||
| validator?: ConstrainValidator<TRegister, TMethod, TInputValidator, TStrict> | ||
| // TODO remove upon stable | ||
| /** @deprecated Use `validator` instead. */ | ||
| inputValidator?: ConstrainValidator< | ||
@@ -644,8 +659,3 @@ TRegister, | ||
| > = <TInputValidator>( | ||
| inputValidator: ConstrainValidator< | ||
| TRegister, | ||
| TMethod, | ||
| TInputValidator, | ||
| TStrict | ||
| >, | ||
| validator: ConstrainValidator<TRegister, TMethod, TInputValidator, TStrict>, | ||
| ) => ServerFnAfterValidator< | ||
@@ -665,2 +675,5 @@ TRegister, | ||
| > { | ||
| validator: ValidatorFn<TRegister, TMethod, TMiddlewares, TStrict> | ||
| // TODO remove upon stable | ||
| /** @deprecated Use `validator` instead. */ | ||
| inputValidator: ValidatorFn<TRegister, TMethod, TMiddlewares, TStrict> | ||
@@ -817,2 +830,5 @@ } | ||
| middlewares: TMiddlewares | ||
| validator: TInputValidator | ||
| // TODO remove upon stable | ||
| /** @deprecated Use `validator` instead. */ | ||
| inputValidator: TInputValidator | ||
@@ -911,6 +927,10 @@ response: TResponse | ||
| ): AnyFunctionMiddleware { | ||
| // TODO remove upon stable | ||
| const validator = options.validator ?? options.inputValidator | ||
| return { | ||
| '~types': undefined!, | ||
| options: { | ||
| inputValidator: options.inputValidator, | ||
| // TODO remove upon stable | ||
| inputValidator: validator, | ||
| client: async ({ next, sendContext, fetch, ...ctx }) => { | ||
@@ -917,0 +937,0 @@ const payload = { |
@@ -23,2 +23,4 @@ import { describe, expectTypeOf, test } from 'vitest' | ||
| expectTypeOf(createServerFn()).toHaveProperty('middleware') | ||
| expectTypeOf(createServerFn()).toHaveProperty('validator') | ||
| // TODO remove upon stable | ||
| expectTypeOf(createServerFn()).toHaveProperty('inputValidator') | ||
@@ -39,3 +41,3 @@ | ||
| method: 'GET', | ||
| }).inputValidator((input: { input: string }) => ({ | ||
| }).validator((input: { input: string }) => ({ | ||
| a: input.input, | ||
@@ -46,3 +48,3 @@ })) | ||
| expectTypeOf(fnAfterValidator).toHaveProperty('middleware') | ||
| expectTypeOf(fnAfterValidator).not.toHaveProperty('inputValidator') | ||
| expectTypeOf(fnAfterValidator).not.toHaveProperty('validator') | ||
@@ -73,7 +75,7 @@ const fn = fnAfterValidator.handler((options) => { | ||
| method: 'GET', | ||
| }).inputValidator((input: string) => Promise.resolve(input)) | ||
| }).validator((input: string) => Promise.resolve(input)) | ||
| expectTypeOf(fnAfterValidator).toHaveProperty('handler') | ||
| expectTypeOf(fnAfterValidator).toHaveProperty('middleware') | ||
| expectTypeOf(fnAfterValidator).not.toHaveProperty('inputValidator') | ||
| expectTypeOf(fnAfterValidator).not.toHaveProperty('validator') | ||
@@ -102,3 +104,3 @@ const fn = fnAfterValidator.handler((options) => { | ||
| method: 'GET', | ||
| }).inputValidator({ | ||
| }).validator({ | ||
| parse: (input: string) => input, | ||
@@ -109,3 +111,3 @@ }) | ||
| expectTypeOf(fnAfterValidator).toHaveProperty('middleware') | ||
| expectTypeOf(fnAfterValidator).not.toHaveProperty('inputValidator') | ||
| expectTypeOf(fnAfterValidator).not.toHaveProperty('validator') | ||
@@ -134,3 +136,3 @@ const fn = fnAfterValidator.handler((options) => { | ||
| method: 'GET', | ||
| }).inputValidator({ | ||
| }).validator({ | ||
| parse: (input: string) => Promise.resolve(input), | ||
@@ -141,3 +143,3 @@ }) | ||
| expectTypeOf(fnAfterValidator).toHaveProperty('middleware') | ||
| expectTypeOf(fnAfterValidator).not.toHaveProperty('inputValidator') | ||
| expectTypeOf(fnAfterValidator).not.toHaveProperty('validator') | ||
@@ -185,7 +187,7 @@ const fn = fnAfterValidator.handler((options) => { | ||
| method: 'GET', | ||
| }).inputValidator(validator) | ||
| }).validator(validator) | ||
| expectTypeOf(fnAfterValidator).toHaveProperty('handler') | ||
| expectTypeOf(fnAfterValidator).toHaveProperty('middleware') | ||
| expectTypeOf(fnAfterValidator).not.toHaveProperty('inputValidator') | ||
| expectTypeOf(fnAfterValidator).not.toHaveProperty('validator') | ||
@@ -234,7 +236,7 @@ const fn = fnAfterValidator.handler((options) => { | ||
| method: 'GET', | ||
| }).inputValidator(validator) | ||
| }).validator(validator) | ||
| expectTypeOf(fnAfterValidator).toHaveProperty('handler') | ||
| expectTypeOf(fnAfterValidator).toHaveProperty('middleware') | ||
| expectTypeOf(fnAfterValidator).not.toHaveProperty('inputValidator') | ||
| expectTypeOf(fnAfterValidator).not.toHaveProperty('validator') | ||
@@ -298,3 +300,3 @@ const fn = fnAfterValidator.handler((options) => { | ||
| expectTypeOf(fnWithMiddleware).toHaveProperty('handler') | ||
| expectTypeOf(fnWithMiddleware).toHaveProperty('inputValidator') | ||
| expectTypeOf(fnWithMiddleware).toHaveProperty('validator') | ||
@@ -317,3 +319,3 @@ fnWithMiddleware.handler((options) => { | ||
| describe('createServerFn with middleware and validator', () => { | ||
| const middleware1 = createMiddleware({ type: 'function' }).inputValidator( | ||
| const middleware1 = createMiddleware({ type: 'function' }).validator( | ||
| (input: { readonly inputA: 'inputA' }) => | ||
@@ -325,3 +327,3 @@ ({ | ||
| const middleware2 = createMiddleware({ type: 'function' }).inputValidator( | ||
| const middleware2 = createMiddleware({ type: 'function' }).validator( | ||
| (input: { readonly inputB: 'inputB' }) => | ||
@@ -341,3 +343,3 @@ ({ | ||
| .middleware([middleware3]) | ||
| .inputValidator( | ||
| .validator( | ||
| (input: { readonly inputC: 'inputC' }) => | ||
@@ -385,3 +387,3 @@ ({ | ||
| const middleware1 = createMiddleware({ type: 'function' }) | ||
| .inputValidator( | ||
| .validator( | ||
| () => | ||
@@ -411,3 +413,3 @@ ({ | ||
| .middleware([middleware1]) | ||
| .inputValidator( | ||
| .validator( | ||
| () => | ||
@@ -435,3 +437,3 @@ ({ | ||
| .middleware([middleware2]) | ||
| .inputValidator( | ||
| .validator( | ||
| () => | ||
@@ -452,3 +454,3 @@ ({ | ||
| createServerFn({ method: 'GET' }) | ||
| .inputValidator(() => 'c' as const) | ||
| .validator(() => 'c' as const) | ||
| .handler((options) => { | ||
@@ -466,3 +468,3 @@ expectTypeOf(options).toEqualTypeOf<{ | ||
| const fn = createServerFn({ method: 'GET' }) | ||
| .inputValidator((input: 'c' | undefined) => input) | ||
| .validator((input: 'c' | undefined) => input) | ||
| .handler((options) => { | ||
@@ -542,3 +544,3 @@ expectTypeOf(options).toEqualTypeOf<{ | ||
| const fn = createServerFn({ method: 'GET', strict: false }) | ||
| .inputValidator((input: { func: () => 'input' }) => ({ | ||
| .validator((input: { func: () => 'input' }) => ({ | ||
| output: input.func(), | ||
@@ -574,3 +576,3 @@ })) | ||
| const myServerFn = createServerFnWithoutSerializationCheck() | ||
| .inputValidator((input: { func: () => 'input' }) => ({ | ||
| .validator((input: { func: () => 'input' }) => ({ | ||
| output: input.func(), | ||
@@ -602,3 +604,3 @@ })) | ||
| const fn = createServerFn({ strict: { input: false } }) | ||
| .inputValidator((input: { func: () => 'input' }) => ({ | ||
| .validator((input: { func: () => 'input' }) => ({ | ||
| output: input.func(), | ||
@@ -669,3 +671,3 @@ })) | ||
| test('createServerFn cannot validate function', () => { | ||
| const validator = createServerFn().inputValidator< | ||
| const validator = createServerFn().validator< | ||
| (input: { func: () => 'string' }) => { output: 'string' } | ||
@@ -691,3 +693,3 @@ > | ||
| strict: { output: false }, | ||
| }).inputValidator<(input: { func: () => 'string' }) => { output: 'string' }> | ||
| }).validator<(input: { func: () => 'string' }) => { output: 'string' }> | ||
@@ -708,3 +710,3 @@ expectTypeOf(validator) | ||
| test('createServerFn can validate Date', () => { | ||
| const validator = createServerFn().inputValidator< | ||
| const validator = createServerFn().validator< | ||
| (input: Date) => { output: 'string' } | ||
@@ -721,3 +723,3 @@ > | ||
| test('createServerFn can validate FormData', () => { | ||
| const validator = createServerFn({ method: 'POST' }).inputValidator< | ||
| const validator = createServerFn({ method: 'POST' }).validator< | ||
| (input: FormData) => { output: 'string' } | ||
@@ -730,3 +732,3 @@ > | ||
| test('createServerFn cannot validate FormData for GET', () => { | ||
| const validator = createServerFn({ method: 'GET' }).inputValidator< | ||
| const validator = createServerFn({ method: 'GET' }).validator< | ||
| (input: FormData) => { output: 'string' } | ||
@@ -770,3 +772,3 @@ > | ||
| const serverFn = createServerFn() | ||
| .inputValidator((data: number) => data) | ||
| .validator((data: number) => data) | ||
| .handler(() => 'foo') | ||
@@ -788,3 +790,3 @@ | ||
| const fn = createServerFn() | ||
| .inputValidator((input) => { | ||
| .validator((input) => { | ||
| expectTypeOf(input).toEqualTypeOf<unknown>() | ||
@@ -839,3 +841,3 @@ | ||
| expectTypeOf(builderWithMw1).toHaveProperty('handler') | ||
| expectTypeOf(builderWithMw1).toHaveProperty('inputValidator') | ||
| expectTypeOf(builderWithMw1).toHaveProperty('validator') | ||
| expectTypeOf(builderWithMw1).toHaveProperty('middleware') | ||
@@ -860,3 +862,3 @@ | ||
| expectTypeOf(builderWithMw2).toHaveProperty('handler') | ||
| expectTypeOf(builderWithMw2).toHaveProperty('inputValidator') | ||
| expectTypeOf(builderWithMw2).toHaveProperty('validator') | ||
| expectTypeOf(builderWithMw2).toHaveProperty('middleware') | ||
@@ -882,3 +884,3 @@ | ||
| expectTypeOf(builderWithMw3).toHaveProperty('handler') | ||
| expectTypeOf(builderWithMw3).toHaveProperty('inputValidator') | ||
| expectTypeOf(builderWithMw3).toHaveProperty('validator') | ||
| expectTypeOf(builderWithMw3).toHaveProperty('middleware') | ||
@@ -951,3 +953,3 @@ | ||
| const funMw = createMiddleware({ type: 'function' }) | ||
| .inputValidator((x: string) => x) | ||
| .validator((x: string) => x) | ||
| .server(({ next }) => { | ||
@@ -963,3 +965,3 @@ return next({ context: { a: 'a' } as const }) | ||
| test('createServerFn with inputValidator and request middleware', () => { | ||
| test('createServerFn with validator and request middleware', () => { | ||
| const loggingMiddleware = createMiddleware().server(async ({ next }) => { | ||
@@ -973,3 +975,3 @@ console.log('Logging middleware executed on the server') | ||
| .middleware([loggingMiddleware]) | ||
| .inputValidator(({ userName }: { userName: string }) => { | ||
| .validator(({ userName }: { userName: string }) => { | ||
| return { userName } | ||
@@ -976,0 +978,0 @@ }) |
@@ -13,2 +13,4 @@ import { expectTypeOf, test } from 'vitest' | ||
| expectTypeOf(middleware).toHaveProperty('server') | ||
| expectTypeOf(middleware).toHaveProperty('validator') | ||
| // TODO remove upon stable | ||
| expectTypeOf(middleware).toHaveProperty('inputValidator') | ||
@@ -18,9 +20,10 @@ | ||
| expectTypeOf(middlewareAfterMiddleware).toHaveProperty('inputValidator') | ||
| expectTypeOf(middlewareAfterMiddleware).toHaveProperty('validator') | ||
| expectTypeOf(middlewareAfterMiddleware).toHaveProperty('server') | ||
| expectTypeOf(middlewareAfterMiddleware).not.toHaveProperty('middleware') | ||
| const middlewareAfterInput = middleware.inputValidator(() => {}) | ||
| const middlewareAfterInput = middleware.validator(() => {}) | ||
| expectTypeOf(middlewareAfterInput).toHaveProperty('server') | ||
| expectTypeOf(middlewareAfterInput).not.toHaveProperty('validator') | ||
| expectTypeOf(middlewareAfterInput).not.toHaveProperty('middleware') | ||
@@ -48,2 +51,3 @@ | ||
| expectTypeOf(middlewareAfterServer).not.toHaveProperty('input') | ||
| expectTypeOf(middlewareAfterServer).not.toHaveProperty('validator') | ||
| expectTypeOf(middlewareAfterServer).not.toHaveProperty('middleware') | ||
@@ -263,3 +267,3 @@ }) | ||
| const middleware1 = createMiddleware({ type: 'function' }) | ||
| .inputValidator(() => { | ||
| .validator(() => { | ||
| return { | ||
@@ -276,3 +280,3 @@ a: 'a', | ||
| .middleware([middleware1]) | ||
| .inputValidator(() => { | ||
| .validator(() => { | ||
| return { | ||
@@ -289,3 +293,3 @@ b: 'b', | ||
| .middleware([middleware2]) | ||
| .inputValidator(() => ({ c: 'c' }) as const) | ||
| .validator(() => ({ c: 'c' }) as const) | ||
| .server(({ next, data }) => { | ||
@@ -604,3 +608,3 @@ expectTypeOf(data).toEqualTypeOf<{ | ||
| test('createMiddleware cannot validate function', () => { | ||
| const validator = createMiddleware({ type: 'function' }).inputValidator< | ||
| const validator = createMiddleware({ type: 'function' }).validator< | ||
| (input: { func: () => 'string' }) => { output: 'string' } | ||
@@ -621,3 +625,3 @@ > | ||
| test('createMiddleware can validate Date', () => { | ||
| const validator = createMiddleware({ type: 'function' }).inputValidator< | ||
| const validator = createMiddleware({ type: 'function' }).validator< | ||
| (input: Date) => { output: 'string' } | ||
@@ -634,3 +638,3 @@ > | ||
| test('createMiddleware can validate FormData', () => { | ||
| const validator = createMiddleware({ type: 'function' }).inputValidator< | ||
| const validator = createMiddleware({ type: 'function' }).validator< | ||
| (input: FormData) => { output: 'string' } | ||
@@ -651,3 +655,3 @@ > | ||
| test('createMiddleware merging from parent with undefined validator', () => { | ||
| const middleware1 = createMiddleware({ type: 'function' }).inputValidator( | ||
| const middleware1 = createMiddleware({ type: 'function' }).validator( | ||
| (input: { test: string }) => input.test, | ||
@@ -667,3 +671,3 @@ ) | ||
| createMiddleware({ type: 'function' }) | ||
| .inputValidator((input) => { | ||
| .validator((input) => { | ||
| expectTypeOf(input).toEqualTypeOf<unknown>() | ||
@@ -670,0 +674,0 @@ |
534979
0.79%8420
0.74%