@tanstack/start-client-core 1.132.0-alpha.8 → 1.132.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/{serializer → client}/ServerFunctionSerializationAdapter.js +2 -2
- package/dist/esm/client/ServerFunctionSerializationAdapter.js.map +1 -0
- package/dist/esm/client/createClientRpc.d.ts +6 -0
- package/dist/esm/{createClientRpc.js → client/createClientRpc.js} +4 -4
- package/dist/esm/client/createClientRpc.js.map +1 -0
- package/dist/esm/client/hydrateStart.d.ts +2 -0
- package/dist/esm/client/hydrateStart.js +29 -0
- package/dist/esm/client/hydrateStart.js.map +1 -0
- package/dist/esm/client/index.d.ts +2 -0
- package/dist/esm/client/index.js +7 -0
- package/dist/esm/client/index.js.map +1 -0
- package/dist/esm/{serverFnFetcher.js → client/serverFnFetcher.js} +6 -6
- package/dist/esm/client/serverFnFetcher.js.map +1 -0
- package/dist/esm/constants.d.ts +1 -0
- package/dist/esm/constants.js +4 -0
- package/dist/esm/constants.js.map +1 -1
- package/dist/esm/createMiddleware.d.ts +95 -86
- package/dist/esm/createMiddleware.js +5 -5
- package/dist/esm/createMiddleware.js.map +1 -1
- package/dist/esm/createServerFn.d.ts +64 -61
- package/dist/esm/createServerFn.js +57 -30
- package/dist/esm/createServerFn.js.map +1 -1
- package/dist/esm/createStart.d.ts +27 -0
- package/dist/esm/createStart.js +14 -0
- package/dist/esm/createStart.js.map +1 -0
- package/dist/esm/fake-start-entry.d.ts +5 -0
- package/dist/esm/fake-start-entry.js +11 -0
- package/dist/esm/fake-start-entry.js.map +1 -0
- package/dist/esm/getDefaultSerovalPlugins.d.ts +1 -0
- package/dist/esm/getDefaultSerovalPlugins.js +14 -0
- package/dist/esm/getDefaultSerovalPlugins.js.map +1 -0
- package/dist/esm/getGlobalStartContext.d.ts +3 -0
- package/dist/esm/getGlobalStartContext.js +15 -0
- package/dist/esm/getGlobalStartContext.js.map +1 -0
- package/dist/esm/getRouterInstance.d.ts +2 -1
- package/dist/esm/getRouterInstance.js +1 -1
- package/dist/esm/getRouterInstance.js.map +1 -1
- package/dist/esm/getServerContextAfterGlobalMiddlewares.d.ts +1 -0
- package/dist/esm/getServerContextAfterGlobalMiddlewares.js +10 -0
- package/dist/esm/getServerContextAfterGlobalMiddlewares.js.map +1 -0
- package/dist/esm/getStartOptions.d.ts +1 -0
- package/dist/esm/getStartOptions.js +7 -0
- package/dist/esm/getStartOptions.js.map +1 -0
- package/dist/esm/global.d.ts +7 -0
- package/dist/esm/index.d.ts +11 -6
- package/dist/esm/index.js +9 -9
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/serverRoute.d.ts +66 -0
- package/dist/esm/startEntry.d.ts +8 -0
- package/package.json +18 -5
- package/src/{serializer → client}/ServerFunctionSerializationAdapter.ts +2 -2
- package/src/{createClientRpc.ts → client/createClientRpc.ts} +5 -3
- package/src/client/hydrateStart.ts +33 -0
- package/src/client/index.ts +3 -0
- package/src/{serverFnFetcher.ts → client/serverFnFetcher.ts} +11 -16
- package/src/constants.ts +3 -0
- package/src/createMiddleware.ts +269 -255
- package/src/createServerFn.ts +277 -290
- package/src/createStart.ts +122 -0
- package/src/fake-start-entry.ts +4 -0
- package/src/getDefaultSerovalPlugins.ts +17 -0
- package/src/getGlobalStartContext.ts +18 -0
- package/src/getRouterInstance.ts +5 -4
- package/src/getServerContextAfterGlobalMiddlewares.ts +7 -0
- package/src/getStartOptions.ts +6 -0
- package/src/global.ts +9 -0
- package/src/index.tsx +12 -11
- package/src/serverRoute.ts +474 -0
- package/src/start-entry.d.ts +11 -0
- package/src/startEntry.ts +10 -0
- package/src/tests/createServerFn.test-d.ts +147 -123
- package/src/tests/createServerMiddleware.test-d.ts +22 -17
- package/dist/esm/createClientRpc.d.ts +0 -6
- package/dist/esm/createClientRpc.js.map +0 -1
- package/dist/esm/registerGlobalMiddleware.d.ts +0 -5
- package/dist/esm/registerGlobalMiddleware.js +0 -9
- package/dist/esm/registerGlobalMiddleware.js.map +0 -1
- package/dist/esm/serializer/ServerFunctionSerializationAdapter.js.map +0 -1
- package/dist/esm/serializer/getClientSerovalPlugins.d.ts +0 -3
- package/dist/esm/serializer/getClientSerovalPlugins.js +0 -13
- package/dist/esm/serializer/getClientSerovalPlugins.js.map +0 -1
- package/dist/esm/serializer/getDefaultSerovalPlugins.d.ts +0 -3
- package/dist/esm/serializer/getDefaultSerovalPlugins.js +0 -19
- package/dist/esm/serializer/getDefaultSerovalPlugins.js.map +0 -1
- package/dist/esm/serverFnFetcher.js.map +0 -1
- package/src/registerGlobalMiddleware.ts +0 -9
- package/src/serializer/getClientSerovalPlugins.ts +0 -10
- package/src/serializer/getDefaultSerovalPlugins.ts +0 -24
- /package/dist/esm/{serializer → client}/ServerFunctionSerializationAdapter.d.ts +0 -0
- /package/dist/esm/{serverFnFetcher.d.ts → client/serverFnFetcher.d.ts} +0 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createMiddleware.js","sources":["../../src/createMiddleware.ts"],"sourcesContent":["import type {\n ConstrainValidator,\n Method,\n ServerFnResponseType,\n} from './createServerFn'\nimport type {\n AnyRouter,\n Assign,\n Constrain,\n Expand,\n IntersectAssign,\n Register,\n RegisteredRouter,\n ResolveValidatorInput,\n ResolveValidatorOutput,\n ValidateSerializableInput,\n} from '@tanstack/router-core'\n\nexport function createMiddleware<\n TRegister extends Register,\n TType extends MiddlewareType,\n>(\n options: {\n type: TType\n validateClient?: boolean\n },\n __opts?: FunctionMiddlewareOptions<\n TRegister,\n unknown,\n undefined,\n undefined,\n undefined,\n ServerFnResponseType\n >,\n): CreateMiddlewareResult<TRegister, TType> {\n // const resolvedOptions = (__opts || options) as MiddlewareOptions<\n const resolvedOptions = {\n type: 'function',\n ...(__opts ||\n (options as FunctionMiddlewareOptions<\n TRegister,\n unknown,\n undefined,\n undefined,\n undefined,\n ServerFnResponseType\n >)),\n }\n\n return {\n options: resolvedOptions,\n middleware: (middleware: any) => {\n return createMiddleware(\n {} as any,\n Object.assign(resolvedOptions, { middleware }),\n ) as any\n },\n validator: (validator: any) => {\n return createMiddleware(\n {} as any,\n Object.assign(resolvedOptions, { validator }),\n ) as any\n },\n client: (client: any) => {\n return createMiddleware(\n {} as any,\n Object.assign(resolvedOptions, { client }),\n ) as any\n },\n server: (server: any) => {\n return createMiddleware(\n {} as any,\n Object.assign(resolvedOptions, { server }),\n ) as any\n },\n } as unknown as CreateMiddlewareResult<TRegister, TType>\n}\n\nexport type MiddlewareType = 'request' | 'function'\n\nexport type CreateMiddlewareResult<\n TRegister extends Register,\n TType extends MiddlewareType,\n> = 'function' extends TType\n ? FunctionMiddleware<TRegister, ServerFnResponseType>\n : RequestMiddleware\n\nexport interface FunctionMiddleware<\n TRegister extends Register,\n TServerFnResponseType extends ServerFnResponseType,\n> extends FunctionMiddlewareAfterMiddleware<\n TRegister,\n unknown,\n TServerFnResponseType\n > {\n middleware: <const TNewMiddlewares = undefined>(\n middlewares: Constrain<\n TNewMiddlewares,\n ReadonlyArray<AnyFunctionMiddleware>\n >,\n ) => FunctionMiddlewareAfterMiddleware<\n TRegister,\n TNewMiddlewares,\n TServerFnResponseType\n >\n}\n\nexport interface FunctionMiddlewareAfterMiddleware<\n TRegister extends Register,\n TMiddlewares,\n TServerFnResponseType extends ServerFnResponseType,\n> extends FunctionMiddlewareWithTypes<\n TRegister,\n TMiddlewares,\n undefined,\n undefined,\n undefined,\n undefined,\n undefined,\n TServerFnResponseType\n >,\n FunctionMiddlewareServer<\n TRegister,\n TMiddlewares,\n undefined,\n undefined,\n undefined,\n TServerFnResponseType\n >,\n FunctionMiddlewareClient<\n TRegister,\n TMiddlewares,\n undefined,\n TServerFnResponseType\n >,\n FunctionMiddlewareValidator<\n TRegister,\n TMiddlewares,\n TServerFnResponseType\n > {}\n\nexport interface FunctionMiddlewareWithTypes<\n TRegister extends Register,\n TMiddlewares,\n TValidator,\n TServerContext,\n TServerSendContext,\n TClientContext,\n TClientSendContext,\n TServerFnResponseType extends ServerFnResponseType,\n> {\n _types: FunctionMiddlewareTypes<\n TMiddlewares,\n TValidator,\n TServerContext,\n TServerSendContext,\n TClientContext,\n TClientSendContext\n >\n options: FunctionMiddlewareOptions<\n TRegister,\n TMiddlewares,\n TValidator,\n TServerContext,\n TClientContext,\n TServerFnResponseType\n >\n}\n\nexport interface FunctionMiddlewareTypes<\n in out TMiddlewares,\n in out TValidator,\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<TValidator>\n allInput: IntersectAllValidatorInputs<TMiddlewares, TValidator>\n output: ResolveValidatorOutput<TValidator>\n allOutput: IntersectAllValidatorOutputs<TMiddlewares, TValidator>\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: AssignAllServerContext<\n TMiddlewares,\n TServerSendContext,\n TServerContext\n >\n clientSendContext: TClientSendContext\n allClientSendContext: AssignAllClientSendContext<\n TMiddlewares,\n TClientSendContext\n >\n validator: TValidator\n}\n\n/**\n * Recursively resolve the input type produced by a sequence of middleware\n */\nexport type IntersectAllValidatorInputs<TMiddlewares, TValidator> =\n unknown extends TValidator\n ? TValidator\n : TValidator extends undefined\n ? IntersectAllMiddleware<TMiddlewares, 'allInput'>\n : IntersectAssign<\n IntersectAllMiddleware<TMiddlewares, 'allInput'>,\n ResolveValidatorInput<TValidator>\n >\n\nexport type IntersectAllMiddleware<\n TMiddlewares,\n TType extends\n | keyof AnyFunctionMiddleware['_types']\n | keyof AnyRequestMiddleware['_types'],\n TAcc = undefined,\n> = TMiddlewares extends readonly [infer TMiddleware, ...infer TRest]\n ? TMiddleware extends AnyFunctionMiddleware | AnyRequestMiddleware\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 any\n>\n\n/**\n * Recursively merge the output type produced by a sequence of middleware\n */\nexport type IntersectAllValidatorOutputs<TMiddlewares, TValidator> =\n unknown extends TValidator\n ? TValidator\n : TValidator extends undefined\n ? IntersectAllMiddleware<TMiddlewares, 'allOutput'>\n : IntersectAssign<\n IntersectAllMiddleware<TMiddlewares, 'allOutput'>,\n ResolveValidatorOutput<TValidator>\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 TAcc = undefined,\n> = TMiddlewares extends readonly [infer TMiddleware, ...infer TRest]\n ? TMiddleware extends AnyFunctionMiddleware | AnyRequestMiddleware\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\n/**\n * Recursively resolve the server context type produced by a sequence of middleware\n */\nexport type AssignAllServerContext<\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 extends Register,\n in out TMiddlewares,\n in out TValidator,\n in out TServerContext,\n in out TClientContext,\n in out TServerFnResponseType extends ServerFnResponseType,\n> {\n validateClient?: boolean\n middleware?: TMiddlewares\n validator?: ConstrainValidator<TRegister, TValidator>\n client?: FunctionMiddlewareClientFn<\n TRegister,\n TMiddlewares,\n TValidator,\n TServerContext,\n TClientContext,\n TServerFnResponseType\n >\n server?: FunctionMiddlewareServerFn<\n TRegister,\n TMiddlewares,\n TValidator,\n TServerContext,\n unknown,\n unknown,\n TServerFnResponseType\n >\n}\n\nexport type FunctionMiddlewareClientNextFn<\n TRegister extends Register,\n TMiddlewares,\n> = <TSendContext = undefined, TNewClientContext = undefined>(ctx?: {\n context?: TNewClientContext\n sendContext?: ValidateSerializableInput<TRegister, TSendContext>\n headers?: HeadersInit\n}) => Promise<\n FunctionClientResultWithContext<TMiddlewares, TSendContext, TNewClientContext>\n>\n\nexport interface FunctionMiddlewareServer<\n TRegister extends Register,\n TMiddlewares,\n TValidator,\n TServerSendContext,\n TClientContext,\n TServerFnResponseType extends ServerFnResponseType,\n> {\n server: <TNewServerContext = undefined, TSendContext = undefined>(\n server: FunctionMiddlewareServerFn<\n TRegister,\n TMiddlewares,\n TValidator,\n TServerSendContext,\n TNewServerContext,\n TSendContext,\n TServerFnResponseType\n >,\n ) => FunctionMiddlewareAfterServer<\n TRegister,\n TMiddlewares,\n TValidator,\n TNewServerContext,\n TServerSendContext,\n TClientContext,\n TSendContext,\n ServerFnResponseType\n >\n}\n\nexport type FunctionMiddlewareServerFn<\n TRegister extends Register,\n TMiddlewares,\n TValidator,\n TServerSendContext,\n TNewServerContext,\n TSendContext,\n TServerFnResponseType extends ServerFnResponseType,\n> = (\n options: FunctionMiddlewareServerFnOptions<\n TRegister,\n TMiddlewares,\n TValidator,\n TServerSendContext,\n TServerFnResponseType\n >,\n) => FunctionMiddlewareServerFnResult<\n TMiddlewares,\n TServerSendContext,\n TNewServerContext,\n TSendContext\n>\n\nexport interface RequestMiddlewareServerFnOptions<\n in out TRegister extends Register,\n in out TMiddlewares,\n in out TServerSendContext,\n> {\n request: Request\n context: Expand<AssignAllServerContext<TMiddlewares, TServerSendContext>>\n next: FunctionMiddlewareServerNextFn<\n TRegister,\n TMiddlewares,\n TServerSendContext\n >\n response: Response\n method: Method\n signal: AbortSignal\n}\n\nexport type FunctionMiddlewareServerNextFn<\n TRegister extends Register,\n TMiddlewares,\n TServerSendContext,\n> = <TNewServerContext = undefined, TSendContext = undefined>(ctx?: {\n context?: TNewServerContext\n sendContext?: ValidateSerializableInput<TRegister, TSendContext>\n}) => Promise<\n FunctionServerResultWithContext<\n TMiddlewares,\n TServerSendContext,\n TNewServerContext,\n TSendContext\n >\n>\n\nexport type FunctionServerResultWithContext<\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 AssignAllServerContext<TMiddlewares, TServerSendContext, TServerContext>\n >\n sendContext: Expand<AssignAllClientSendContext<TMiddlewares, TSendContext>>\n}\n\nexport interface FunctionMiddlewareServerFnOptions<\n in out TRegister extends Register,\n in out TMiddlewares,\n in out TValidator,\n in out TServerSendContext,\n in out TServerFnResponseType,\n> {\n data: Expand<IntersectAllValidatorOutputs<TMiddlewares, TValidator>>\n context: Expand<AssignAllServerContext<TMiddlewares, TServerSendContext>>\n next: FunctionMiddlewareServerNextFn<\n TRegister,\n TMiddlewares,\n TServerSendContext\n >\n response: TServerFnResponseType\n method: Method\n filename: string\n functionId: string\n signal: AbortSignal\n}\n\nexport type FunctionMiddlewareServerFnResult<\n TMiddlewares,\n TServerSendContext,\n TServerContext,\n TSendContext,\n> =\n | Promise<\n FunctionServerResultWithContext<\n TMiddlewares,\n TServerSendContext,\n TServerContext,\n TSendContext\n >\n >\n | FunctionServerResultWithContext<\n TMiddlewares,\n TServerSendContext,\n TServerContext,\n TSendContext\n >\n\nexport interface FunctionMiddlewareAfterServer<\n TRegister extends Register,\n TMiddlewares,\n TValidator,\n TServerContext,\n TServerSendContext,\n TClientContext,\n TClientSendContext,\n TServerFnResponseType extends ServerFnResponseType,\n> extends FunctionMiddlewareWithTypes<\n TRegister,\n TMiddlewares,\n TValidator,\n TServerContext,\n TServerSendContext,\n TClientContext,\n TClientSendContext,\n TServerFnResponseType\n > {}\n\nexport interface FunctionMiddlewareClient<\n TRegister extends Register,\n TMiddlewares,\n TValidator,\n TServerFnResponseType extends ServerFnResponseType,\n TRouter extends AnyRouter = RegisteredRouter,\n> {\n client: <TSendServerContext = undefined, TNewClientContext = undefined>(\n client: FunctionMiddlewareClientFn<\n TRegister,\n TMiddlewares,\n TValidator,\n TSendServerContext,\n TNewClientContext,\n TServerFnResponseType,\n TRouter\n >,\n ) => FunctionMiddlewareAfterClient<\n TRegister,\n TMiddlewares,\n TValidator,\n TSendServerContext,\n TNewClientContext,\n ServerFnResponseType\n >\n}\n\nexport type FunctionMiddlewareClientFn<\n TRegister extends Register,\n TMiddlewares,\n TValidator,\n TSendContext,\n TClientContext,\n TServerFnResponseType extends ServerFnResponseType,\n TRouter extends AnyRouter = RegisteredRouter,\n> = (\n options: FunctionMiddlewareClientFnOptions<\n TRegister,\n TMiddlewares,\n TValidator,\n TServerFnResponseType,\n TRouter\n >,\n) => FunctionMiddlewareClientFnResult<\n TMiddlewares,\n TSendContext,\n TClientContext\n>\n\nexport interface FunctionMiddlewareClientFnOptions<\n in out TRegister extends Register,\n in out TMiddlewares,\n in out TValidator,\n in out TServerFnResponseType extends ServerFnResponseType,\n in out TRouter extends AnyRouter,\n> {\n data: Expand<IntersectAllValidatorInputs<TMiddlewares, TValidator>>\n context: Expand<AssignAllClientContextBeforeNext<TMiddlewares>>\n sendContext: Expand<AssignAllServerSendContext<TMiddlewares>>\n method: Method\n response: TServerFnResponseType\n signal: AbortSignal\n next: FunctionMiddlewareClientNextFn<TRegister, TMiddlewares>\n filename: string\n functionId: string\n router: TRouter\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}\n\nexport interface FunctionMiddlewareAfterClient<\n TRegister extends Register,\n TMiddlewares,\n TValidator,\n TServerSendContext,\n TClientContext,\n TServerFnResponseType extends ServerFnResponseType,\n> extends FunctionMiddlewareWithTypes<\n TRegister,\n TMiddlewares,\n TValidator,\n undefined,\n TServerSendContext,\n TClientContext,\n undefined,\n TServerFnResponseType\n >,\n FunctionMiddlewareServer<\n TRegister,\n TMiddlewares,\n TValidator,\n TServerSendContext,\n TClientContext,\n TServerFnResponseType\n > {}\n\nexport interface FunctionMiddlewareValidator<\n TRegister extends Register,\n TMiddlewares,\n TServerFnResponseType extends ServerFnResponseType,\n> {\n validator: <TNewValidator>(\n input: ConstrainValidator<TRegister, TNewValidator>,\n ) => FunctionMiddlewareAfterValidator<\n TRegister,\n TMiddlewares,\n TNewValidator,\n TServerFnResponseType\n >\n}\n\nexport interface FunctionMiddlewareAfterValidator<\n TRegister extends Register,\n TMiddlewares,\n TValidator,\n TServerFnResponseType extends ServerFnResponseType,\n> extends FunctionMiddlewareWithTypes<\n TRegister,\n TMiddlewares,\n TValidator,\n undefined,\n undefined,\n undefined,\n undefined,\n ServerFnResponseType\n >,\n FunctionMiddlewareServer<\n TRegister,\n TMiddlewares,\n TValidator,\n undefined,\n undefined,\n TServerFnResponseType\n >,\n FunctionMiddlewareClient<\n TRegister,\n TMiddlewares,\n TValidator,\n ServerFnResponseType\n > {}\n\nexport interface RequestMiddleware\n extends RequestMiddlewareAfterMiddleware<undefined> {\n middleware: <const TMiddlewares = undefined>(\n middlewares: Constrain<TMiddlewares, ReadonlyArray<AnyRequestMiddleware>>,\n ) => RequestMiddlewareAfterMiddleware<TMiddlewares>\n}\n\nexport type AnyRequestMiddleware = RequestMiddlewareWithTypes<any, any>\n\nexport interface RequestMiddlewareWithTypes<TMiddlewares, TServerContext> {\n _types: RequestMiddlewareTypes<TMiddlewares, TServerContext>\n}\n\nexport interface RequestMiddlewareTypes<TMiddlewares, TServerContext> {\n type: 'request'\n middlewares: TMiddlewares\n serverContext: TServerContext\n allServerContext: AssignAllServerContext<\n TMiddlewares,\n undefined,\n TServerContext\n >\n}\n\nexport interface RequestMiddlewareAfterMiddleware<TMiddlewares>\n extends RequestMiddlewareWithTypes<TMiddlewares, undefined>,\n RequestMiddlewareServer<TMiddlewares> {}\n\nexport interface RequestMiddlewareServer<TMiddlewares> {\n server: <TServerContext = undefined>(\n fn: RequestServerFn<TMiddlewares, TServerContext>,\n ) => RequestMiddlewareAfterServer<TMiddlewares, TServerContext>\n}\n\nexport type RequestServerFn<TMiddlewares, TServerContext> = (\n options: RequestServerOptions<TMiddlewares>,\n) => RequestMiddlewareServerFnResult<TMiddlewares, TServerContext>\n\nexport interface RequestServerOptions<TMiddlewares> {\n request: Request\n pathname: string\n context: AssignAllServerContext<TMiddlewares>\n next: RequestServerNextFn<TMiddlewares>\n}\n\nexport type RequestServerNextFn<TMiddlewares> = <TServerContext = undefined>(\n options?: RequestServerNextFnOptions<TServerContext>,\n) => RequestMiddlewareServerFnResult<TMiddlewares, TServerContext>\n\nexport interface RequestServerNextFnOptions<TServerContext> {\n context?: TServerContext\n}\n\nexport type RequestMiddlewareServerFnResult<TMiddlewares, TServerContext> =\n | Promise<RequestServerResult<TMiddlewares, TServerContext>>\n | RequestServerResult<TMiddlewares, TServerContext>\n\nexport interface RequestServerResult<TMiddlewares, TServerContext> {\n request: Request\n pathname: string\n context: Expand<\n AssignAllServerContext<TMiddlewares, undefined, TServerContext>\n >\n response: Response\n}\n\nexport interface RequestMiddlewareAfterServer<TMiddlewares, TServerContext>\n extends RequestMiddlewareWithTypes<TMiddlewares, TServerContext> {}\n"],"names":[],"mappings":"AAkBO,SAAS,iBAId,SAIA,QAQ0C;AAE1C,QAAM,kBAAkB;AAAA,IACtB,MAAM;AAAA,IACN,GAAI,UACD;AAAA,EAAA;AAUL,SAAO;AAAA,IACL,SAAS;AAAA,IACT,YAAY,CAAC,eAAoB;AAC/B,aAAO;AAAA,QACL,CAAA;AAAA,QACA,OAAO,OAAO,iBAAiB,EAAE,YAAY;AAAA,MAAA;AAAA,IAEjD;AAAA,IACA,WAAW,CAAC,cAAmB;AAC7B,aAAO;AAAA,QACL,CAAA;AAAA,QACA,OAAO,OAAO,iBAAiB,EAAE,WAAW;AAAA,MAAA;AAAA,IAEhD;AAAA,IACA,QAAQ,CAAC,WAAgB;AACvB,aAAO;AAAA,QACL,CAAA;AAAA,QACA,OAAO,OAAO,iBAAiB,EAAE,QAAQ;AAAA,MAAA;AAAA,IAE7C;AAAA,IACA,QAAQ,CAAC,WAAgB;AACvB,aAAO;AAAA,QACL,CAAA;AAAA,QACA,OAAO,OAAO,iBAAiB,EAAE,QAAQ;AAAA,MAAA;AAAA,IAE7C;AAAA,EAAA;AAEJ;"}
|
|
1
|
+
{"version":3,"file":"createMiddleware.js","sources":["../../src/createMiddleware.ts"],"sourcesContent":["import type { StartInstanceOptions } from './createStart'\nimport type { AnyServerFn, ConstrainValidator, Method } from './createServerFn'\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 {} as any,\n Object.assign(resolvedOptions, { middleware }),\n ) as any\n },\n inputValidator: (inputValidator: any) => {\n return createMiddleware(\n {} as any,\n Object.assign(resolvedOptions, { inputValidator }),\n ) as any\n },\n client: (client: any) => {\n return createMiddleware(\n {} as any,\n Object.assign(resolvedOptions, { client }),\n ) as any\n },\n server: (server: any) => {\n return createMiddleware(\n {} as any,\n Object.assign(resolvedOptions, { server }),\n ) as any\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<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 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 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 // AnyContext,\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 __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}) => 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 filename: string\n functionId: string\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 next: FunctionMiddlewareClientNextFn<TRegister, TMiddlewares>\n filename: string\n functionId: string\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}\n\nexport interface FunctionMiddlewareAfterClient<\n TRegister,\n TMiddlewares,\n TInputValidator,\n TServerSendContext,\n TClientContext,\n> extends 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> extends 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<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 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 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\nexport type RequestServerNextFn<TRegister, TMiddlewares> = <\n TServerContext = undefined,\n>(\n options?: RequestServerNextFnOptions<TServerContext>,\n) => RequestMiddlewareServerFnResult<TRegister, TMiddlewares, TServerContext>\n\nexport interface RequestServerNextFnOptions<TServerContext> {\n context?: TServerContext\n}\n\nexport type RequestMiddlewareServerFnResult<\n TRegister,\n TMiddlewares,\n TServerContext,\n> =\n | Promise<RequestServerResult<TRegister, TMiddlewares, TServerContext>>\n | RequestServerResult<TRegister, TMiddlewares, TServerContext>\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"],"names":[],"mappings":"AA2BO,MAAM,mBAA2C,CAAC,SAAS,WAAW;AAC3E,QAAM,kBAAkB;AAAA,IACtB,MAAM;AAAA,IACN,GAAI,UAAU;AAAA,EAAA;AAGhB,SAAO;AAAA,IACL,SAAS;AAAA,IACT,YAAY,CAAC,eAAoB;AAC/B,aAAO;AAAA,QACL,CAAA;AAAA,QACA,OAAO,OAAO,iBAAiB,EAAE,YAAY;AAAA,MAAA;AAAA,IAEjD;AAAA,IACA,gBAAgB,CAAC,mBAAwB;AACvC,aAAO;AAAA,QACL,CAAA;AAAA,QACA,OAAO,OAAO,iBAAiB,EAAE,gBAAgB;AAAA,MAAA;AAAA,IAErD;AAAA,IACA,QAAQ,CAAC,WAAgB;AACvB,aAAO;AAAA,QACL,CAAA;AAAA,QACA,OAAO,OAAO,iBAAiB,EAAE,QAAQ;AAAA,MAAA;AAAA,IAE7C;AAAA,IACA,QAAQ,CAAC,WAAgB;AACvB,aAAO;AAAA,QACL,CAAA;AAAA,QACA,OAAO,OAAO,iBAAiB,EAAE,QAAQ;AAAA,MAAA;AAAA,IAE7C;AAAA,EAAA;AAEJ;"}
|
|
@@ -1,117 +1,121 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { TSS_SERVER_FUNCTION_FACTORY } from './constants.js';
|
|
2
|
+
import { AnyValidator, Constrain, Expand, Register, RegisteredSerializableInput, ResolveValidatorInput, ValidateSerializable, ValidateSerializableInput, ValidateSerializableInputResult, Validator } from '@tanstack/router-core';
|
|
2
3
|
import { JsonResponse } from '@tanstack/router-core/ssr/client';
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
export declare function createServerFn<TRegister extends Register, TMethod extends Method, TServerFnResponseType extends ServerFnResponseType = 'data', TResponse = unknown, TMiddlewares = undefined, TValidator = undefined>(options?: {
|
|
4
|
+
import { AnyFunctionMiddleware, AnyRequestMiddleware, AssignAllServerFnContext, IntersectAllValidatorInputs, IntersectAllValidatorOutputs } from './createMiddleware.js';
|
|
5
|
+
export type CreateServerFn<TRegister> = <TMethod extends Method, TResponse = unknown, TMiddlewares = undefined, TInputValidator = undefined>(options?: {
|
|
6
6
|
method?: TMethod;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
export declare function executeMiddleware(middlewares: Array<AnyFunctionMiddleware>, env: 'client' | 'server', opts: ServerFnMiddlewareOptions): Promise<ServerFnMiddlewareResult>;
|
|
7
|
+
}, __opts?: ServerFnBaseOptions<TRegister, TMethod, TResponse, TMiddlewares, TInputValidator>) => ServerFnBuilder<TRegister, TMethod>;
|
|
8
|
+
export declare const createServerFn: CreateServerFn<Register>;
|
|
9
|
+
export declare function executeMiddleware(middlewares: Array<AnyFunctionMiddleware | AnyRequestMiddleware>, env: 'client' | 'server', opts: ServerFnMiddlewareOptions): Promise<ServerFnMiddlewareResult>;
|
|
10
10
|
export type CompiledFetcherFnOptions = {
|
|
11
11
|
method: Method;
|
|
12
12
|
data: unknown;
|
|
13
|
-
response?: ServerFnResponseType;
|
|
14
13
|
headers?: HeadersInit;
|
|
15
14
|
signal?: AbortSignal;
|
|
16
15
|
context?: any;
|
|
17
16
|
};
|
|
18
|
-
export type Fetcher<TRegister
|
|
17
|
+
export type Fetcher<TRegister, TMiddlewares, TInputValidator, TResponse> = undefined extends IntersectAllValidatorInputs<TMiddlewares, TInputValidator> ? OptionalFetcher<TRegister, TMiddlewares, TInputValidator, TResponse> : RequiredFetcher<TRegister, TMiddlewares, TInputValidator, TResponse>;
|
|
19
18
|
export interface FetcherBase {
|
|
20
19
|
url: string;
|
|
21
20
|
__executeServer: (opts: {
|
|
22
21
|
method: Method;
|
|
23
|
-
response?: ServerFnResponseType;
|
|
24
22
|
data: unknown;
|
|
25
23
|
headers?: HeadersInit;
|
|
26
24
|
context?: any;
|
|
27
25
|
signal: AbortSignal;
|
|
28
26
|
}) => Promise<unknown>;
|
|
29
27
|
}
|
|
30
|
-
export
|
|
31
|
-
|
|
32
|
-
(options?: OptionalFetcherDataOptions<TMiddlewares, TValidator>): FetchResult<TRegister, TMiddlewares, TResponse, TServerFnResponseType>;
|
|
28
|
+
export interface OptionalFetcher<TRegister, TMiddlewares, TInputValidator, TResponse> extends FetcherBase {
|
|
29
|
+
(options?: OptionalFetcherDataOptions<TMiddlewares, TInputValidator>): Promise<FetcherData<TRegister, TResponse>>;
|
|
33
30
|
}
|
|
34
|
-
export interface RequiredFetcher<TRegister
|
|
35
|
-
(opts: RequiredFetcherDataOptions<TMiddlewares,
|
|
31
|
+
export interface RequiredFetcher<TRegister, TMiddlewares, TInputValidator, TResponse> extends FetcherBase {
|
|
32
|
+
(opts: RequiredFetcherDataOptions<TMiddlewares, TInputValidator>): Promise<FetcherData<TRegister, TResponse>>;
|
|
36
33
|
}
|
|
37
34
|
export type FetcherBaseOptions = {
|
|
38
35
|
headers?: HeadersInit;
|
|
39
36
|
signal?: AbortSignal;
|
|
40
37
|
};
|
|
41
|
-
export interface OptionalFetcherDataOptions<TMiddlewares,
|
|
42
|
-
data?: Expand<IntersectAllValidatorInputs<TMiddlewares,
|
|
38
|
+
export interface OptionalFetcherDataOptions<TMiddlewares, TInputValidator> extends FetcherBaseOptions {
|
|
39
|
+
data?: Expand<IntersectAllValidatorInputs<TMiddlewares, TInputValidator>>;
|
|
43
40
|
}
|
|
44
|
-
export interface RequiredFetcherDataOptions<TMiddlewares,
|
|
45
|
-
data: Expand<IntersectAllValidatorInputs<TMiddlewares,
|
|
41
|
+
export interface RequiredFetcherDataOptions<TMiddlewares, TInputValidator> extends FetcherBaseOptions {
|
|
42
|
+
data: Expand<IntersectAllValidatorInputs<TMiddlewares, TInputValidator>>;
|
|
46
43
|
}
|
|
47
|
-
export
|
|
48
|
-
error: unknown;
|
|
49
|
-
result: FetcherData<TRegister, TResponse>;
|
|
50
|
-
context: AssignAllClientSendContext<TMiddlewares>;
|
|
51
|
-
}
|
|
52
|
-
export type FetcherData<TRegister extends Register, TResponse> = TResponse extends JsonResponse<any> ? ValidateSerializableInputResult<TRegister, ReturnType<TResponse['json']>> : ValidateSerializableInputResult<TRegister, TResponse>;
|
|
44
|
+
export type FetcherData<TRegister, TResponse> = TResponse extends Response ? Response : TResponse extends JsonResponse<any> ? ValidateSerializableInputResult<TRegister, ReturnType<TResponse['json']>> : ValidateSerializableInputResult<TRegister, TResponse>;
|
|
53
45
|
export type RscStream<T> = {
|
|
54
46
|
__cacheState: T;
|
|
55
47
|
};
|
|
56
48
|
export type Method = 'GET' | 'POST';
|
|
57
|
-
export type
|
|
58
|
-
export type
|
|
59
|
-
export
|
|
60
|
-
export type ServerFn<TRegister extends Register, TMethod, TServerFnResponseType extends ServerFnResponseType, TMiddlewares, TValidator, TResponse> = (ctx: ServerFnCtx<TMethod, TServerFnResponseType, TMiddlewares, TValidator>) => ServerFnReturnType<TRegister, TServerFnResponseType, TResponse>;
|
|
61
|
-
export interface ServerFnCtx<TMethod, TServerFnResponseType extends ServerFnResponseType, TMiddlewares, TValidator> {
|
|
49
|
+
export type ServerFnReturnType<TRegister, TResponse> = Response | Promise<ValidateSerializableInput<TRegister, TResponse>> | ValidateSerializableInput<TRegister, TResponse>;
|
|
50
|
+
export type ServerFn<TRegister, TMethod, TMiddlewares, TInputValidator, TResponse> = (ctx: ServerFnCtx<TRegister, TMethod, TMiddlewares, TInputValidator>) => ServerFnReturnType<TRegister, TResponse>;
|
|
51
|
+
export interface ServerFnCtx<TRegister, TMethod, TMiddlewares, TInputValidator> {
|
|
62
52
|
method: TMethod;
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
context: Expand<AssignAllServerContext<TMiddlewares>>;
|
|
53
|
+
data: Expand<IntersectAllValidatorOutputs<TMiddlewares, TInputValidator>>;
|
|
54
|
+
context: Expand<AssignAllServerFnContext<TRegister, TMiddlewares, {}>>;
|
|
66
55
|
signal: AbortSignal;
|
|
67
56
|
}
|
|
68
|
-
export type CompiledFetcherFn<TRegister
|
|
69
|
-
(opts: CompiledFetcherFnOptions & ServerFnBaseOptions<TRegister, Method
|
|
57
|
+
export type CompiledFetcherFn<TRegister, TResponse> = {
|
|
58
|
+
(opts: CompiledFetcherFnOptions & ServerFnBaseOptions<TRegister, Method>): Promise<TResponse>;
|
|
70
59
|
url: string;
|
|
71
60
|
};
|
|
72
|
-
export type ServerFnBaseOptions<TRegister
|
|
61
|
+
export type ServerFnBaseOptions<TRegister, TMethod extends Method = 'GET', TResponse = unknown, TMiddlewares = unknown, TInputValidator = unknown> = {
|
|
73
62
|
method: TMethod;
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
extractedFn?: CompiledFetcherFn<TRegister, TResponse, TServerFnResponseType>;
|
|
79
|
-
serverFn?: ServerFn<TRegister, TMethod, TServerFnResponseType, TMiddlewares, TInput, TResponse>;
|
|
63
|
+
middleware?: Constrain<TMiddlewares, ReadonlyArray<AnyFunctionMiddleware | AnyRequestMiddleware>>;
|
|
64
|
+
inputValidator?: ConstrainValidator<TRegister, TMethod, TInputValidator>;
|
|
65
|
+
extractedFn?: CompiledFetcherFn<TRegister, TResponse>;
|
|
66
|
+
serverFn?: ServerFn<TRegister, TMethod, TMiddlewares, TInputValidator, TResponse>;
|
|
80
67
|
functionId: string;
|
|
81
68
|
};
|
|
82
|
-
export type ValidateValidatorInput<TRegister extends
|
|
83
|
-
export type ValidateValidator<TRegister extends
|
|
84
|
-
export type ConstrainValidator<TRegister extends
|
|
85
|
-
export
|
|
86
|
-
|
|
69
|
+
export type ValidateValidatorInput<TRegister, TMethod extends Method, TInputValidator> = TMethod extends 'POST' ? ResolveValidatorInput<TInputValidator> extends FormData ? ResolveValidatorInput<TInputValidator> : ValidateSerializable<ResolveValidatorInput<TInputValidator>, RegisteredSerializableInput<TRegister>> : ValidateSerializable<ResolveValidatorInput<TInputValidator>, RegisteredSerializableInput<TRegister>>;
|
|
70
|
+
export type ValidateValidator<TRegister, TMethod extends Method, TInputValidator> = ValidateValidatorInput<TRegister, TMethod, TInputValidator> extends infer TInput ? Validator<TInput, any> : never;
|
|
71
|
+
export type ConstrainValidator<TRegister, TMethod extends Method, TInputValidator> = (unknown extends TInputValidator ? TInputValidator : ResolveValidatorInput<TInputValidator> extends ValidateValidator<TRegister, TMethod, TInputValidator> ? TInputValidator : never) | ValidateValidator<TRegister, TMethod, TInputValidator>;
|
|
72
|
+
export type AppendMiddlewares<TMiddlewares, TNewMiddlewares> = TMiddlewares extends ReadonlyArray<any> ? TNewMiddlewares extends ReadonlyArray<any> ? readonly [...TMiddlewares, ...TNewMiddlewares] : TMiddlewares : TNewMiddlewares;
|
|
73
|
+
export interface ServerFnMiddleware<TRegister, TMethod extends Method, TMiddlewares, TInputValidator> {
|
|
74
|
+
middleware: <const TNewMiddlewares>(middlewares: Constrain<TNewMiddlewares, ReadonlyArray<AnyFunctionMiddleware | AnyRequestMiddleware | AnyServerFn>>) => ServerFnAfterMiddleware<TRegister, TMethod, AppendMiddlewares<TMiddlewares, TNewMiddlewares>, TInputValidator>;
|
|
75
|
+
}
|
|
76
|
+
export interface ServerFnAfterMiddleware<TRegister, TMethod extends Method, TMiddlewares, TInputValidator> extends ServerFnWithTypes<TRegister, TMethod, TMiddlewares, TInputValidator, undefined>, ServerFnMiddleware<TRegister, TMethod, TMiddlewares, undefined>, ServerFnValidator<TRegister, TMethod, TMiddlewares>, ServerFnHandler<TRegister, TMethod, TMiddlewares, TInputValidator> {
|
|
77
|
+
<TNewMethod extends Method = TMethod>(options?: {
|
|
78
|
+
method?: TNewMethod;
|
|
79
|
+
}): ServerFnAfterMiddleware<TRegister, TNewMethod, TMiddlewares, TInputValidator>;
|
|
87
80
|
}
|
|
88
|
-
export
|
|
81
|
+
export type ValidatorFn<TRegister, TMethod extends Method, TMiddlewares> = <TInputValidator>(inputValidator: ConstrainValidator<TRegister, TMethod, TInputValidator>) => ServerFnAfterValidator<TRegister, TMethod, TMiddlewares, TInputValidator>;
|
|
82
|
+
export interface ServerFnValidator<TRegister, TMethod extends Method, TMiddlewares> {
|
|
83
|
+
inputValidator: ValidatorFn<TRegister, TMethod, TMiddlewares>;
|
|
89
84
|
}
|
|
90
|
-
export
|
|
91
|
-
export interface ServerFnValidator<TRegister extends Register, TMethod extends Method, TServerFnResponseType extends ServerFnResponseType, TMiddlewares> {
|
|
92
|
-
validator: ValidatorFn<TRegister, TMethod, TServerFnResponseType, TMiddlewares>;
|
|
85
|
+
export interface ServerFnAfterValidator<TRegister, TMethod extends Method, TMiddlewares, TInputValidator> extends ServerFnWithTypes<TRegister, TMethod, TMiddlewares, TInputValidator, undefined>, ServerFnMiddleware<TRegister, TMethod, TMiddlewares, TInputValidator>, ServerFnHandler<TRegister, TMethod, TMiddlewares, TInputValidator> {
|
|
93
86
|
}
|
|
94
|
-
export interface
|
|
87
|
+
export interface ServerFnAfterTyper<TRegister, TMethod extends Method, TMiddlewares, TInputValidator> extends ServerFnWithTypes<TRegister, TMethod, TMiddlewares, TInputValidator, undefined>, ServerFnHandler<TRegister, TMethod, TMiddlewares, TInputValidator> {
|
|
95
88
|
}
|
|
96
|
-
export interface
|
|
89
|
+
export interface ServerFnHandler<TRegister, TMethod extends Method, TMiddlewares, TInputValidator> {
|
|
90
|
+
handler: <TNewResponse>(fn?: ServerFn<TRegister, TMethod, TMiddlewares, TInputValidator, TNewResponse>) => Fetcher<TRegister, TMiddlewares, TInputValidator, TNewResponse>;
|
|
97
91
|
}
|
|
98
|
-
export interface
|
|
99
|
-
|
|
92
|
+
export interface ServerFnBuilder<TRegister, TMethod extends Method = 'GET'> extends ServerFnWithTypes<TRegister, TMethod, undefined, undefined, undefined>, ServerFnMiddleware<TRegister, TMethod, undefined, undefined>, ServerFnValidator<TRegister, TMethod, undefined>, ServerFnHandler<TRegister, TMethod, undefined, undefined> {
|
|
93
|
+
options: ServerFnBaseOptions<TRegister, TMethod, unknown, undefined, undefined>;
|
|
100
94
|
}
|
|
101
|
-
export interface
|
|
102
|
-
|
|
95
|
+
export interface ServerFnWithTypes<in out TRegister, in out TMethod extends Method, in out TMiddlewares, in out TInputValidator, in out TResponse> {
|
|
96
|
+
_types: ServerFnTypes<TRegister, TMethod, TMiddlewares, TInputValidator, TResponse>;
|
|
97
|
+
options: ServerFnBaseOptions<TRegister, TMethod, unknown, undefined, undefined>;
|
|
98
|
+
[TSS_SERVER_FUNCTION_FACTORY]: true;
|
|
99
|
+
}
|
|
100
|
+
export type AnyServerFn = ServerFnWithTypes<any, any, any, any, any>;
|
|
101
|
+
export interface ServerFnTypes<in out TRegister, in out TMethod extends Method, in out TMiddlewares, in out TInputValidator, in out TResponse> {
|
|
102
|
+
method: TMethod;
|
|
103
|
+
middlewares: TMiddlewares;
|
|
104
|
+
inputValidator: TInputValidator;
|
|
105
|
+
response: TResponse;
|
|
106
|
+
allServerContext: AssignAllServerFnContext<TRegister, TMiddlewares>;
|
|
107
|
+
allInput: IntersectAllValidatorInputs<TMiddlewares, TInputValidator>;
|
|
108
|
+
allOutput: IntersectAllValidatorOutputs<TMiddlewares, TInputValidator>;
|
|
103
109
|
}
|
|
104
|
-
export declare function flattenMiddlewares(middlewares: Array<AnyFunctionMiddleware>): Array<AnyFunctionMiddleware>;
|
|
110
|
+
export declare function flattenMiddlewares(middlewares: Array<AnyFunctionMiddleware | AnyRequestMiddleware>): Array<AnyFunctionMiddleware | AnyRequestMiddleware>;
|
|
105
111
|
export type ServerFnMiddlewareOptions = {
|
|
106
112
|
method: Method;
|
|
107
|
-
response?: ServerFnResponseType;
|
|
108
113
|
data: any;
|
|
109
114
|
headers?: HeadersInit;
|
|
110
115
|
signal?: AbortSignal;
|
|
111
116
|
sendContext?: any;
|
|
112
117
|
context?: any;
|
|
113
118
|
functionId: string;
|
|
114
|
-
router?: AnyRouter;
|
|
115
119
|
};
|
|
116
120
|
export type ServerFnMiddlewareResult = ServerFnMiddlewareOptions & {
|
|
117
121
|
result?: unknown;
|
|
@@ -123,4 +127,3 @@ export type MiddlewareFn = (ctx: ServerFnMiddlewareOptions & {
|
|
|
123
127
|
}) => Promise<ServerFnMiddlewareResult>;
|
|
124
128
|
export declare const applyMiddleware: (middlewareFn: MiddlewareFn, ctx: ServerFnMiddlewareOptions, nextFn: NextFn) => Promise<ServerFnMiddlewareResult>;
|
|
125
129
|
export declare function execValidator(validator: AnyValidator, input: unknown): unknown;
|
|
126
|
-
export declare function serverFnBaseToMiddleware(options: ServerFnBaseOptions<any, any, any, any, any, any>): AnyFunctionMiddleware;
|
|
@@ -1,45 +1,55 @@
|
|
|
1
1
|
import { isRedirect, isNotFound } from "@tanstack/router-core";
|
|
2
2
|
import { mergeHeaders } from "@tanstack/router-core/ssr/client";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
3
|
+
import { TSS_SERVER_FUNCTION_FACTORY } from "./constants.js";
|
|
4
|
+
import { getServerContextAfterGlobalMiddlewares } from "./getServerContextAfterGlobalMiddlewares.js";
|
|
5
|
+
import { getStartOptions } from "./getStartOptions.js";
|
|
6
|
+
const createServerFn = (options, __opts) => {
|
|
6
7
|
const resolvedOptions = __opts || options || {};
|
|
7
8
|
if (typeof resolvedOptions.method === "undefined") {
|
|
8
9
|
resolvedOptions.method = "GET";
|
|
9
10
|
}
|
|
10
|
-
|
|
11
|
+
const res = {
|
|
11
12
|
options: resolvedOptions,
|
|
12
13
|
middleware: (middleware) => {
|
|
13
|
-
|
|
14
|
+
const newMiddleware = [...resolvedOptions.middleware || []];
|
|
15
|
+
middleware.map((m) => {
|
|
16
|
+
if (TSS_SERVER_FUNCTION_FACTORY in m) {
|
|
17
|
+
if (m.options.middleware) {
|
|
18
|
+
newMiddleware.push(...m.options.middleware);
|
|
19
|
+
}
|
|
20
|
+
} else {
|
|
21
|
+
newMiddleware.push(m);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
const newOptions = {
|
|
25
|
+
...resolvedOptions,
|
|
26
|
+
middleware: newMiddleware
|
|
27
|
+
};
|
|
28
|
+
const res2 = createServerFn(void 0, newOptions);
|
|
29
|
+
res2[TSS_SERVER_FUNCTION_FACTORY] = true;
|
|
30
|
+
return res2;
|
|
14
31
|
},
|
|
15
|
-
|
|
16
|
-
|
|
32
|
+
inputValidator: (inputValidator) => {
|
|
33
|
+
const newOptions = { ...resolvedOptions, inputValidator };
|
|
34
|
+
return createServerFn(void 0, newOptions);
|
|
17
35
|
},
|
|
18
36
|
handler: (...args) => {
|
|
19
37
|
const [extractedFn, serverFn] = args;
|
|
20
|
-
|
|
21
|
-
...extractedFn,
|
|
22
|
-
extractedFn,
|
|
23
|
-
serverFn
|
|
24
|
-
});
|
|
38
|
+
const newOptions = { ...resolvedOptions, extractedFn, serverFn };
|
|
25
39
|
const resolvedMiddleware = [
|
|
26
|
-
...
|
|
27
|
-
serverFnBaseToMiddleware(
|
|
40
|
+
...newOptions.middleware || [],
|
|
41
|
+
serverFnBaseToMiddleware(newOptions)
|
|
28
42
|
];
|
|
29
43
|
return Object.assign(
|
|
30
44
|
async (opts) => {
|
|
31
45
|
return executeMiddleware(resolvedMiddleware, "client", {
|
|
32
46
|
...extractedFn,
|
|
33
|
-
...
|
|
47
|
+
...newOptions,
|
|
34
48
|
data: opts?.data,
|
|
35
49
|
headers: opts?.headers,
|
|
36
50
|
signal: opts?.signal,
|
|
37
|
-
context: {}
|
|
38
|
-
router: getRouterInstance()
|
|
51
|
+
context: {}
|
|
39
52
|
}).then((d) => {
|
|
40
|
-
if (resolvedOptions.response === "full") {
|
|
41
|
-
return d;
|
|
42
|
-
}
|
|
43
53
|
if (d.error) throw d.error;
|
|
44
54
|
return d.result;
|
|
45
55
|
});
|
|
@@ -50,9 +60,14 @@ function createServerFn(options, __opts) {
|
|
|
50
60
|
// The extracted function on the server-side calls
|
|
51
61
|
// this function
|
|
52
62
|
__executeServer: async (opts, signal) => {
|
|
63
|
+
const serverContextAfterGlobalMiddlewares = getServerContextAfterGlobalMiddlewares();
|
|
53
64
|
const ctx = {
|
|
54
65
|
...extractedFn,
|
|
55
66
|
...opts,
|
|
67
|
+
context: {
|
|
68
|
+
...serverContextAfterGlobalMiddlewares,
|
|
69
|
+
...opts.context
|
|
70
|
+
},
|
|
56
71
|
signal
|
|
57
72
|
};
|
|
58
73
|
return executeMiddleware(resolvedMiddleware, "server", ctx).then(
|
|
@@ -68,10 +83,21 @@ function createServerFn(options, __opts) {
|
|
|
68
83
|
);
|
|
69
84
|
}
|
|
70
85
|
};
|
|
71
|
-
|
|
86
|
+
const fun = (options2) => {
|
|
87
|
+
return {
|
|
88
|
+
...res,
|
|
89
|
+
options: {
|
|
90
|
+
...res.options,
|
|
91
|
+
...options2
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
};
|
|
95
|
+
return Object.assign(fun, res);
|
|
96
|
+
};
|
|
72
97
|
async function executeMiddleware(middlewares, env, opts) {
|
|
98
|
+
const globalMiddlewares = getStartOptions().functionMiddleware || [];
|
|
73
99
|
const flattenedMiddlewares = flattenMiddlewares([
|
|
74
|
-
...
|
|
100
|
+
...globalMiddlewares,
|
|
75
101
|
...middlewares
|
|
76
102
|
]);
|
|
77
103
|
const next = async (ctx) => {
|
|
@@ -79,10 +105,13 @@ async function executeMiddleware(middlewares, env, opts) {
|
|
|
79
105
|
if (!nextMiddleware) {
|
|
80
106
|
return ctx;
|
|
81
107
|
}
|
|
82
|
-
if (nextMiddleware.options.
|
|
83
|
-
ctx.data = await execValidator(
|
|
108
|
+
if ("inputValidator" in nextMiddleware.options && nextMiddleware.options.inputValidator && env === "server") {
|
|
109
|
+
ctx.data = await execValidator(
|
|
110
|
+
nextMiddleware.options.inputValidator,
|
|
111
|
+
ctx.data
|
|
112
|
+
);
|
|
84
113
|
}
|
|
85
|
-
const middlewareFn = env === "client" ? nextMiddleware.options.client : nextMiddleware.options.server;
|
|
114
|
+
const middlewareFn = env === "client" && "client" in nextMiddleware.options ? nextMiddleware.options.client : nextMiddleware.options.server;
|
|
86
115
|
if (middlewareFn) {
|
|
87
116
|
return applyMiddleware(middlewareFn, ctx, async (newCtx) => {
|
|
88
117
|
return next(newCtx).catch((error) => {
|
|
@@ -138,7 +167,7 @@ const applyMiddleware = async (middlewareFn, ctx, nextFn) => {
|
|
|
138
167
|
...userCtx.sendContext ?? {}
|
|
139
168
|
},
|
|
140
169
|
headers: mergeHeaders(ctx.headers, userCtx.headers),
|
|
141
|
-
result: userCtx.result !== void 0 ? userCtx.result :
|
|
170
|
+
result: userCtx.result !== void 0 ? userCtx.result : userCtx instanceof Response ? userCtx : ctx.result,
|
|
142
171
|
error: userCtx.error ?? ctx.error
|
|
143
172
|
});
|
|
144
173
|
}
|
|
@@ -166,8 +195,7 @@ function serverFnBaseToMiddleware(options) {
|
|
|
166
195
|
return {
|
|
167
196
|
_types: void 0,
|
|
168
197
|
options: {
|
|
169
|
-
|
|
170
|
-
validateClient: options.validateClient,
|
|
198
|
+
inputValidator: options.inputValidator,
|
|
171
199
|
client: async ({ next, sendContext, ...ctx }) => {
|
|
172
200
|
const payload = {
|
|
173
201
|
...ctx,
|
|
@@ -192,7 +220,6 @@ export {
|
|
|
192
220
|
createServerFn,
|
|
193
221
|
execValidator,
|
|
194
222
|
executeMiddleware,
|
|
195
|
-
flattenMiddlewares
|
|
196
|
-
serverFnBaseToMiddleware
|
|
223
|
+
flattenMiddlewares
|
|
197
224
|
};
|
|
198
225
|
//# sourceMappingURL=createServerFn.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createServerFn.js","sources":["../../src/createServerFn.ts"],"sourcesContent":["import { isNotFound, isRedirect } from '@tanstack/router-core'\nimport { mergeHeaders } from '@tanstack/router-core/ssr/client'\nimport { globalMiddleware } from './registerGlobalMiddleware'\n\nimport { getRouterInstance } from './getRouterInstance'\nimport type {\n AnyRouter,\n AnyValidator,\n Constrain,\n Expand,\n Register,\n RegisteredSerializableInput,\n ResolveValidatorInput,\n ValidateSerializable,\n ValidateSerializableInput,\n ValidateSerializableInputResult,\n Validator,\n} from '@tanstack/router-core'\nimport type { JsonResponse } from '@tanstack/router-core/ssr/client'\nimport type { Readable } from 'node:stream'\nimport type {\n AnyFunctionMiddleware,\n AssignAllClientSendContext,\n AssignAllServerContext,\n FunctionMiddlewareClientFnResult,\n FunctionMiddlewareServerFnResult,\n IntersectAllValidatorInputs,\n IntersectAllValidatorOutputs,\n} from './createMiddleware'\n\ntype TODO = any\n\nexport function createServerFn<\n TRegister extends Register,\n TMethod extends Method,\n TServerFnResponseType extends ServerFnResponseType = 'data',\n TResponse = unknown,\n TMiddlewares = undefined,\n TValidator = undefined,\n>(\n options?: {\n method?: TMethod\n response?: TServerFnResponseType\n },\n __opts?: ServerFnBaseOptions<\n TRegister,\n TMethod,\n TServerFnResponseType,\n TResponse,\n TMiddlewares,\n TValidator\n >,\n): ServerFnBuilder<TRegister, TMethod, TServerFnResponseType> {\n const resolvedOptions = (__opts || options || {}) as ServerFnBaseOptions<\n TRegister,\n TMethod,\n ServerFnResponseType,\n TResponse,\n TMiddlewares,\n TValidator\n >\n\n if (typeof resolvedOptions.method === 'undefined') {\n resolvedOptions.method = 'GET' as TMethod\n }\n\n return {\n options: resolvedOptions as any,\n middleware: (middleware) => {\n return createServerFn<\n TRegister,\n TMethod,\n ServerFnResponseType,\n TResponse,\n TMiddlewares,\n TValidator\n >(undefined, Object.assign(resolvedOptions, { middleware })) as any\n },\n validator: (validator) => {\n return createServerFn<\n TRegister,\n TMethod,\n ServerFnResponseType,\n TResponse,\n TMiddlewares,\n TValidator\n >(undefined, Object.assign(resolvedOptions, { validator })) 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<TRegister, TResponse, TServerFnResponseType>,\n ServerFn<\n TRegister,\n TMethod,\n TServerFnResponseType,\n TMiddlewares,\n TValidator,\n TResponse\n >,\n ]\n\n // Keep the original function around so we can use it\n // in the server environment\n Object.assign(resolvedOptions, {\n ...extractedFn,\n extractedFn,\n serverFn,\n })\n\n const resolvedMiddleware = [\n ...(resolvedOptions.middleware || []),\n serverFnBaseToMiddleware(resolvedOptions),\n ]\n\n // We want to make sure the new function has the same\n // properties as the original function\n\n return Object.assign(\n async (opts?: CompiledFetcherFnOptions) => {\n // Start by executing the client-side middleware chain\n return executeMiddleware(resolvedMiddleware, 'client', {\n ...extractedFn,\n ...resolvedOptions,\n data: opts?.data as any,\n headers: opts?.headers,\n signal: opts?.signal,\n context: {},\n router: getRouterInstance(),\n }).then((d) => {\n if (resolvedOptions.response === 'full') {\n return d\n }\n if (d.error) throw d.error\n return d.result\n })\n },\n {\n // This copies over the URL, function ID\n ...extractedFn,\n // The extracted function on the server-side calls\n // this function\n __executeServer: async (opts: any, signal: AbortSignal) => {\n const ctx = {\n ...extractedFn,\n ...opts,\n signal,\n }\n\n return executeMiddleware(resolvedMiddleware, 'server', ctx).then(\n (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 },\n },\n ) as any\n },\n }\n}\n\nexport async function executeMiddleware(\n middlewares: Array<AnyFunctionMiddleware>,\n env: 'client' | 'server',\n opts: ServerFnMiddlewareOptions,\n): Promise<ServerFnMiddlewareResult> {\n const flattenedMiddlewares = flattenMiddlewares([\n ...globalMiddleware,\n ...middlewares,\n ])\n\n const next: 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 if (\n nextMiddleware.options.validator &&\n (env === 'client' ? nextMiddleware.options.validateClient : true)\n ) {\n // Execute the middleware's input function\n ctx.data = await execValidator(nextMiddleware.options.validator, ctx.data)\n }\n\n const middlewareFn = (\n env === 'client'\n ? nextMiddleware.options.client\n : nextMiddleware.options.server\n ) as MiddlewareFn | undefined\n\n if (middlewareFn) {\n // Execute the middleware\n return applyMiddleware(middlewareFn, ctx, async (newCtx) => {\n return next(newCtx).catch((error: any) => {\n if (isRedirect(error) || isNotFound(error)) {\n return {\n ...newCtx,\n error,\n }\n }\n\n throw error\n })\n })\n }\n\n return next(ctx)\n }\n\n // Start the middleware chain\n return next({\n ...opts,\n headers: opts.headers || {},\n sendContext: opts.sendContext || {},\n context: opts.context || {},\n })\n}\n\nexport type CompiledFetcherFnOptions = {\n method: Method\n data: unknown\n response?: ServerFnResponseType\n headers?: HeadersInit\n signal?: AbortSignal\n context?: any\n // router?: AnyRouter\n}\n\nexport type Fetcher<\n TRegister extends Register,\n TMiddlewares,\n TValidator,\n TResponse,\n TServerFnResponseType extends ServerFnResponseType,\n> =\n undefined extends IntersectAllValidatorInputs<TMiddlewares, TValidator>\n ? OptionalFetcher<\n TRegister,\n TMiddlewares,\n TValidator,\n TResponse,\n TServerFnResponseType\n >\n : RequiredFetcher<\n TRegister,\n TMiddlewares,\n TValidator,\n TResponse,\n TServerFnResponseType\n >\n\nexport interface FetcherBase {\n url: string\n __executeServer: (opts: {\n method: Method\n response?: ServerFnResponseType\n data: unknown\n headers?: HeadersInit\n context?: any\n signal: AbortSignal\n }) => Promise<unknown>\n}\n\nexport type FetchResult<\n TRegister extends Register,\n TMiddlewares,\n TResponse,\n TServerFnResponseType extends ServerFnResponseType,\n> = TServerFnResponseType extends 'raw'\n ? Promise<Response>\n : TServerFnResponseType extends 'full'\n ? Promise<FullFetcherData<TRegister, TMiddlewares, TResponse>>\n : Promise<FetcherData<TRegister, TResponse>>\n\nexport interface OptionalFetcher<\n TRegister extends Register,\n TMiddlewares,\n TValidator,\n TResponse,\n TServerFnResponseType extends ServerFnResponseType,\n> extends FetcherBase {\n (\n options?: OptionalFetcherDataOptions<TMiddlewares, TValidator>,\n ): FetchResult<TRegister, TMiddlewares, TResponse, TServerFnResponseType>\n}\n\nexport interface RequiredFetcher<\n TRegister extends Register,\n TMiddlewares,\n TValidator,\n TResponse,\n TServerFnResponseType extends ServerFnResponseType,\n> extends FetcherBase {\n (\n opts: RequiredFetcherDataOptions<TMiddlewares, TValidator>,\n ): FetchResult<TRegister, TMiddlewares, TResponse, TServerFnResponseType>\n}\n\nexport type FetcherBaseOptions = {\n headers?: HeadersInit\n signal?: AbortSignal\n}\n\nexport interface OptionalFetcherDataOptions<TMiddlewares, TValidator>\n extends FetcherBaseOptions {\n data?: Expand<IntersectAllValidatorInputs<TMiddlewares, TValidator>>\n}\n\nexport interface RequiredFetcherDataOptions<TMiddlewares, TValidator>\n extends FetcherBaseOptions {\n data: Expand<IntersectAllValidatorInputs<TMiddlewares, TValidator>>\n}\n\nexport interface FullFetcherData<\n TRegister extends Register,\n TMiddlewares,\n TResponse,\n> {\n error: unknown\n result: FetcherData<TRegister, TResponse>\n context: AssignAllClientSendContext<TMiddlewares>\n}\n\nexport type FetcherData<TRegister extends Register, TResponse> =\n TResponse extends JsonResponse<any>\n ? ValidateSerializableInputResult<TRegister, ReturnType<TResponse['json']>>\n : ValidateSerializableInputResult<TRegister, TResponse>\n\nexport type RscStream<T> = {\n __cacheState: T\n}\n\nexport type Method = 'GET' | 'POST'\nexport type ServerFnResponseType = 'data' | 'full' | 'raw'\n\n// see https://h3.unjs.io/guide/event-handler#responses-types\nexport type RawResponse = Response | ReadableStream | Readable | null | string\n\nexport type ServerFnReturnType<\n TRegister extends Register,\n TServerFnResponseType extends ServerFnResponseType,\n TResponse,\n> = TServerFnResponseType extends 'raw'\n ? RawResponse | Promise<RawResponse>\n :\n | Promise<ValidateSerializableInput<TRegister, TResponse>>\n | ValidateSerializableInput<TRegister, TResponse>\n\nexport type ServerFn<\n TRegister extends Register,\n TMethod,\n TServerFnResponseType extends ServerFnResponseType,\n TMiddlewares,\n TValidator,\n TResponse,\n> = (\n ctx: ServerFnCtx<TMethod, TServerFnResponseType, TMiddlewares, TValidator>,\n) => ServerFnReturnType<TRegister, TServerFnResponseType, TResponse>\n\nexport interface ServerFnCtx<\n TMethod,\n TServerFnResponseType extends ServerFnResponseType,\n TMiddlewares,\n TValidator,\n> {\n method: TMethod\n response: TServerFnResponseType\n data: Expand<IntersectAllValidatorOutputs<TMiddlewares, TValidator>>\n context: Expand<AssignAllServerContext<TMiddlewares>>\n signal: AbortSignal\n}\n\nexport type CompiledFetcherFn<\n TRegister extends Register,\n TResponse,\n TServerFnResponseType extends ServerFnResponseType,\n> = {\n (\n opts: CompiledFetcherFnOptions &\n ServerFnBaseOptions<TRegister, Method, TServerFnResponseType>,\n ): Promise<TResponse>\n url: string\n}\n\nexport type ServerFnBaseOptions<\n TRegister extends Register,\n TMethod extends Method = 'GET',\n TServerFnResponseType extends ServerFnResponseType = 'data',\n TResponse = unknown,\n TMiddlewares = unknown,\n TInput = unknown,\n> = {\n method: TMethod\n response?: TServerFnResponseType\n validateClient?: boolean\n middleware?: Constrain<TMiddlewares, ReadonlyArray<AnyFunctionMiddleware>>\n validator?: ConstrainValidator<TRegister, TInput>\n extractedFn?: CompiledFetcherFn<TRegister, TResponse, TServerFnResponseType>\n serverFn?: ServerFn<\n TRegister,\n TMethod,\n TServerFnResponseType,\n TMiddlewares,\n TInput,\n TResponse\n >\n functionId: string\n}\n\nexport type ValidateValidatorInput<\n TRegister extends Register,\n TValidator,\n> = ValidateSerializable<\n ResolveValidatorInput<TValidator>,\n RegisteredSerializableInput<TRegister> | FormData\n>\n\nexport type ValidateValidator<TRegister extends Register, TValidator> =\n ValidateValidatorInput<TRegister, TValidator> extends infer TInput\n ? Validator<TInput, any>\n : never\n\nexport type ConstrainValidator<TRegister extends Register, TValidator> =\n | (unknown extends TValidator\n ? TValidator\n : ResolveValidatorInput<TValidator> extends ValidateValidator<\n TRegister,\n TValidator\n >\n ? TValidator\n : never)\n | ValidateValidator<TRegister, TValidator>\n\nexport interface ServerFnMiddleware<\n TRegister extends Register,\n TMethod extends Method,\n TServerFnResponseType extends ServerFnResponseType,\n TValidator,\n> {\n middleware: <const TNewMiddlewares = undefined>(\n middlewares: Constrain<\n TNewMiddlewares,\n ReadonlyArray<AnyFunctionMiddleware>\n >,\n ) => ServerFnAfterMiddleware<\n TRegister,\n TMethod,\n TServerFnResponseType,\n TNewMiddlewares,\n TValidator\n >\n}\n\nexport interface ServerFnAfterMiddleware<\n TRegister extends Register,\n TMethod extends Method,\n TServerFnResponseType extends ServerFnResponseType,\n TMiddlewares,\n TValidator,\n> extends ServerFnValidator<\n TRegister,\n TMethod,\n TServerFnResponseType,\n TMiddlewares\n >,\n ServerFnHandler<\n TRegister,\n TMethod,\n TServerFnResponseType,\n TMiddlewares,\n TValidator\n > {}\n\nexport type ValidatorFn<\n TRegister extends Register,\n TMethod extends Method,\n TServerFnResponseType extends ServerFnResponseType,\n TMiddlewares,\n> = <TValidator>(\n validator: ConstrainValidator<TRegister, TValidator>,\n) => ServerFnAfterValidator<\n TRegister,\n TMethod,\n TServerFnResponseType,\n TMiddlewares,\n TValidator\n>\n\nexport interface ServerFnValidator<\n TRegister extends Register,\n TMethod extends Method,\n TServerFnResponseType extends ServerFnResponseType,\n TMiddlewares,\n> {\n validator: ValidatorFn<\n TRegister,\n TMethod,\n TServerFnResponseType,\n TMiddlewares\n >\n}\n\nexport interface ServerFnAfterValidator<\n TRegister extends Register,\n TMethod extends Method,\n TServerFnResponseType extends ServerFnResponseType,\n TMiddlewares,\n TValidator,\n> extends ServerFnMiddleware<\n TRegister,\n TMethod,\n TServerFnResponseType,\n TValidator\n >,\n ServerFnHandler<\n TRegister,\n TMethod,\n TServerFnResponseType,\n TMiddlewares,\n TValidator\n > {}\n\nexport interface ServerFnAfterTyper<\n TRegister extends Register,\n TMethod extends Method,\n TServerFnResponseType extends ServerFnResponseType,\n TMiddlewares,\n TValidator,\n> extends ServerFnHandler<\n TRegister,\n TMethod,\n TServerFnResponseType,\n TMiddlewares,\n TValidator\n > {}\n\n// Handler\nexport interface ServerFnHandler<\n TRegister extends Register,\n TMethod extends Method,\n TServerFnResponseType extends ServerFnResponseType,\n TMiddlewares,\n TValidator,\n> {\n handler: <TNewResponse>(\n fn?: ServerFn<\n TRegister,\n TMethod,\n TServerFnResponseType,\n TMiddlewares,\n TValidator,\n TNewResponse\n >,\n ) => Fetcher<\n TRegister,\n TMiddlewares,\n TValidator,\n TNewResponse,\n TServerFnResponseType\n >\n}\n\nexport interface ServerFnBuilder<\n TRegister extends Register,\n TMethod extends Method = 'GET',\n TServerFnResponseType extends ServerFnResponseType = 'data',\n> extends ServerFnMiddleware<\n TRegister,\n TMethod,\n TServerFnResponseType,\n undefined\n >,\n ServerFnValidator<TRegister, TMethod, TServerFnResponseType, undefined>,\n ServerFnHandler<\n TRegister,\n TMethod,\n TServerFnResponseType,\n undefined,\n undefined\n > {\n options: ServerFnBaseOptions<\n TRegister,\n TMethod,\n TServerFnResponseType,\n unknown,\n undefined,\n undefined\n >\n}\n\nexport function flattenMiddlewares(\n middlewares: Array<AnyFunctionMiddleware>,\n): Array<AnyFunctionMiddleware> {\n const seen = new Set<AnyFunctionMiddleware>()\n const flattened: Array<AnyFunctionMiddleware> = []\n\n const recurse = (middleware: Array<AnyFunctionMiddleware>) => {\n middleware.forEach((m) => {\n if (m.options.middleware) {\n recurse(m.options.middleware)\n }\n\n if (!seen.has(m)) {\n seen.add(m)\n flattened.push(m)\n }\n })\n }\n\n recurse(middlewares)\n\n return flattened\n}\n\nexport type ServerFnMiddlewareOptions = {\n method: Method\n response?: ServerFnResponseType\n data: any\n headers?: HeadersInit\n signal?: AbortSignal\n sendContext?: any\n context?: any\n functionId: string\n router?: AnyRouter\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 const applyMiddleware = async (\n middlewareFn: MiddlewareFn,\n ctx: ServerFnMiddlewareOptions,\n nextFn: NextFn,\n) => {\n return middlewareFn({\n ...ctx,\n next: (async (\n userCtx: ServerFnMiddlewareResult | undefined = {} as any,\n ) => {\n // Return the next middleware\n return nextFn({\n ...ctx,\n ...userCtx,\n context: {\n ...ctx.context,\n ...userCtx.context,\n },\n sendContext: {\n ...ctx.sendContext,\n ...(userCtx.sendContext ?? {}),\n },\n headers: mergeHeaders(ctx.headers, userCtx.headers),\n result:\n userCtx.result !== undefined\n ? userCtx.result\n : ctx.response === 'raw'\n ? userCtx\n : (ctx as any).result,\n error: userCtx.error ?? (ctx as any).error,\n })\n }) as any,\n } as any)\n}\n\nexport function execValidator(\n validator: AnyValidator,\n input: unknown,\n): unknown {\n if (validator == null) return {}\n\n if ('~standard' in validator) {\n const result = validator['~standard'].validate(input)\n\n if (result instanceof Promise)\n throw new Error('Async validation not supported')\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\nexport function serverFnBaseToMiddleware(\n options: ServerFnBaseOptions<any, any, any, any, any, any>,\n): AnyFunctionMiddleware {\n return {\n _types: undefined!,\n options: {\n validator: options.validator,\n validateClient: options.validateClient,\n client: async ({ next, sendContext, ...ctx }) => {\n const payload = {\n ...ctx,\n // switch the sendContext over to context\n context: sendContext,\n } as any\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) as unknown as FunctionMiddlewareClientFnResult<\n any,\n any,\n any\n >\n },\n server: async ({ next, ...ctx }) => {\n // Execute the server function\n const result = await options.serverFn?.(ctx as TODO)\n\n return next({\n ...ctx,\n result,\n } as any) as unknown as FunctionMiddlewareServerFnResult<\n any,\n any,\n any,\n any\n >\n },\n },\n }\n}\n"],"names":[],"mappings":";;;;AAgCO,SAAS,eAQd,SAIA,QAQ4D;AAC5D,QAAM,kBAAmB,UAAU,WAAW,CAAA;AAS9C,MAAI,OAAO,gBAAgB,WAAW,aAAa;AACjD,oBAAgB,SAAS;AAAA,EAC3B;AAEA,SAAO;AAAA,IACL,SAAS;AAAA,IACT,YAAY,CAAC,eAAe;AAC1B,aAAO,eAOL,QAAW,OAAO,OAAO,iBAAiB,EAAE,WAAA,CAAY,CAAC;AAAA,IAC7D;AAAA,IACA,WAAW,CAAC,cAAc;AACxB,aAAO,eAOL,QAAW,OAAO,OAAO,iBAAiB,EAAE,UAAA,CAAW,CAAC;AAAA,IAC5D;AAAA,IACA,SAAS,IAAI,SAAS;AAIpB,YAAM,CAAC,aAAa,QAAQ,IAAI;AAchC,aAAO,OAAO,iBAAiB;AAAA,QAC7B,GAAG;AAAA,QACH;AAAA,QACA;AAAA,MAAA,CACD;AAED,YAAM,qBAAqB;AAAA,QACzB,GAAI,gBAAgB,cAAc,CAAA;AAAA,QAClC,yBAAyB,eAAe;AAAA,MAAA;AAM1C,aAAO,OAAO;AAAA,QACZ,OAAO,SAAoC;AAEzC,iBAAO,kBAAkB,oBAAoB,UAAU;AAAA,YACrD,GAAG;AAAA,YACH,GAAG;AAAA,YACH,MAAM,MAAM;AAAA,YACZ,SAAS,MAAM;AAAA,YACf,QAAQ,MAAM;AAAA,YACd,SAAS,CAAA;AAAA,YACT,QAAQ,kBAAA;AAAA,UAAkB,CAC3B,EAAE,KAAK,CAAC,MAAM;AACb,gBAAI,gBAAgB,aAAa,QAAQ;AACvC,qBAAO;AAAA,YACT;AACA,gBAAI,EAAE,MAAO,OAAM,EAAE;AACrB,mBAAO,EAAE;AAAA,UACX,CAAC;AAAA,QACH;AAAA,QACA;AAAA;AAAA,UAEE,GAAG;AAAA;AAAA;AAAA,UAGH,iBAAiB,OAAO,MAAW,WAAwB;AACzD,kBAAM,MAAM;AAAA,cACV,GAAG;AAAA,cACH,GAAG;AAAA,cACH;AAAA,YAAA;AAGF,mBAAO,kBAAkB,oBAAoB,UAAU,GAAG,EAAE;AAAA,cAC1D,CAAC,OAAO;AAAA;AAAA,gBAEN,QAAQ,EAAE;AAAA,gBACV,OAAO,EAAE;AAAA,gBACT,SAAS,EAAE;AAAA,cAAA;AAAA,YACb;AAAA,UAEJ;AAAA,QAAA;AAAA,MACF;AAAA,IAEJ;AAAA,EAAA;AAEJ;AAEA,eAAsB,kBACpB,aACA,KACA,MACmC;AACnC,QAAM,uBAAuB,mBAAmB;AAAA,IAC9C,GAAG;AAAA,IACH,GAAG;AAAA,EAAA,CACJ;AAED,QAAM,OAAe,OAAO,QAAQ;AAElC,UAAM,iBAAiB,qBAAqB,MAAA;AAG5C,QAAI,CAAC,gBAAgB;AACnB,aAAO;AAAA,IACT;AAEA,QACE,eAAe,QAAQ,cACtB,QAAQ,WAAW,eAAe,QAAQ,iBAAiB,OAC5D;AAEA,UAAI,OAAO,MAAM,cAAc,eAAe,QAAQ,WAAW,IAAI,IAAI;AAAA,IAC3E;AAEA,UAAM,eACJ,QAAQ,WACJ,eAAe,QAAQ,SACvB,eAAe,QAAQ;AAG7B,QAAI,cAAc;AAEhB,aAAO,gBAAgB,cAAc,KAAK,OAAO,WAAW;AAC1D,eAAO,KAAK,MAAM,EAAE,MAAM,CAAC,UAAe;AACxC,cAAI,WAAW,KAAK,KAAK,WAAW,KAAK,GAAG;AAC1C,mBAAO;AAAA,cACL,GAAG;AAAA,cACH;AAAA,YAAA;AAAA,UAEJ;AAEA,gBAAM;AAAA,QACR,CAAC;AAAA,MACH,CAAC;AAAA,IACH;AAEA,WAAO,KAAK,GAAG;AAAA,EACjB;AAGA,SAAO,KAAK;AAAA,IACV,GAAG;AAAA,IACH,SAAS,KAAK,WAAW,CAAA;AAAA,IACzB,aAAa,KAAK,eAAe,CAAA;AAAA,IACjC,SAAS,KAAK,WAAW,CAAA;AAAA,EAAC,CAC3B;AACH;AAsXO,SAAS,mBACd,aAC8B;AAC9B,QAAM,2BAAW,IAAA;AACjB,QAAM,YAA0C,CAAA;AAEhD,QAAM,UAAU,CAAC,eAA6C;AAC5D,eAAW,QAAQ,CAAC,MAAM;AACxB,UAAI,EAAE,QAAQ,YAAY;AACxB,gBAAQ,EAAE,QAAQ,UAAU;AAAA,MAC9B;AAEA,UAAI,CAAC,KAAK,IAAI,CAAC,GAAG;AAChB,aAAK,IAAI,CAAC;AACV,kBAAU,KAAK,CAAC;AAAA,MAClB;AAAA,IACF,CAAC;AAAA,EACH;AAEA,UAAQ,WAAW;AAEnB,SAAO;AACT;AA6BO,MAAM,kBAAkB,OAC7B,cACA,KACA,WACG;AACH,SAAO,aAAa;AAAA,IAClB,GAAG;AAAA,IACH,MAAO,OACL,UAAgD,OAC7C;AAEH,aAAO,OAAO;AAAA,QACZ,GAAG;AAAA,QACH,GAAG;AAAA,QACH,SAAS;AAAA,UACP,GAAG,IAAI;AAAA,UACP,GAAG,QAAQ;AAAA,QAAA;AAAA,QAEb,aAAa;AAAA,UACX,GAAG,IAAI;AAAA,UACP,GAAI,QAAQ,eAAe,CAAA;AAAA,QAAC;AAAA,QAE9B,SAAS,aAAa,IAAI,SAAS,QAAQ,OAAO;AAAA,QAClD,QACE,QAAQ,WAAW,SACf,QAAQ,SACR,IAAI,aAAa,QACf,UACC,IAAY;AAAA,QACrB,OAAO,QAAQ,SAAU,IAAY;AAAA,MAAA,CACtC;AAAA,IACH;AAAA,EAAA,CACM;AACV;AAEO,SAAS,cACd,WACA,OACS;AACT,MAAI,aAAa,KAAM,QAAO,CAAA;AAE9B,MAAI,eAAe,WAAW;AAC5B,UAAM,SAAS,UAAU,WAAW,EAAE,SAAS,KAAK;AAEpD,QAAI,kBAAkB;AACpB,YAAM,IAAI,MAAM,gCAAgC;AAElD,QAAI,OAAO;AACT,YAAM,IAAI,MAAM,KAAK,UAAU,OAAO,QAAQ,QAAW,CAAC,CAAC;AAE7D,WAAO,OAAO;AAAA,EAChB;AAEA,MAAI,WAAW,WAAW;AACxB,WAAO,UAAU,MAAM,KAAK;AAAA,EAC9B;AAEA,MAAI,OAAO,cAAc,YAAY;AACnC,WAAO,UAAU,KAAK;AAAA,EACxB;AAEA,QAAM,IAAI,MAAM,yBAAyB;AAC3C;AAEO,SAAS,yBACd,SACuB;AACvB,SAAO;AAAA,IACL,QAAQ;AAAA,IACR,SAAS;AAAA,MACP,WAAW,QAAQ;AAAA,MACnB,gBAAgB,QAAQ;AAAA,MACxB,QAAQ,OAAO,EAAE,MAAM,aAAa,GAAG,UAAU;AAC/C,cAAM,UAAU;AAAA,UACd,GAAG;AAAA;AAAA,UAEH,SAAS;AAAA,QAAA;AAKX,cAAM,MAAM,MAAM,QAAQ,cAAc,OAAO;AAE/C,eAAO,KAAK,GAAG;AAAA,MAKjB;AAAA,MACA,QAAQ,OAAO,EAAE,MAAM,GAAG,UAAU;AAElC,cAAM,SAAS,MAAM,QAAQ,WAAW,GAAW;AAEnD,eAAO,KAAK;AAAA,UACV,GAAG;AAAA,UACH;AAAA,QAAA,CACM;AAAA,MAMV;AAAA,IAAA;AAAA,EACF;AAEJ;"}
|
|
1
|
+
{"version":3,"file":"createServerFn.js","sources":["../../src/createServerFn.ts"],"sourcesContent":["import { isNotFound, isRedirect } from '@tanstack/router-core'\nimport { mergeHeaders } from '@tanstack/router-core/ssr/client'\n\nimport { TSS_SERVER_FUNCTION_FACTORY } from './constants'\nimport { getServerContextAfterGlobalMiddlewares } from './getServerContextAfterGlobalMiddlewares'\nimport { getStartOptions } from './getStartOptions'\nimport type {\n AnyValidator,\n Constrain,\n Expand,\n Register,\n RegisteredSerializableInput,\n ResolveValidatorInput,\n ValidateSerializable,\n ValidateSerializableInput,\n ValidateSerializableInputResult,\n Validator,\n} from '@tanstack/router-core'\nimport type { JsonResponse } from '@tanstack/router-core/ssr/client'\nimport type {\n AnyFunctionMiddleware,\n AnyRequestMiddleware,\n AssignAllServerFnContext,\n FunctionMiddlewareClientFnResult,\n FunctionMiddlewareServerFnResult,\n IntersectAllValidatorInputs,\n IntersectAllValidatorOutputs,\n} from './createMiddleware'\n\ntype TODO = any\n\nexport type CreateServerFn<TRegister> = <\n TMethod extends Method,\n TResponse = unknown,\n TMiddlewares = undefined,\n TInputValidator = undefined,\n>(\n options?: {\n method?: TMethod\n },\n __opts?: ServerFnBaseOptions<\n TRegister,\n TMethod,\n TResponse,\n TMiddlewares,\n TInputValidator\n >,\n) => ServerFnBuilder<TRegister, TMethod>\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 >\n\n if (typeof resolvedOptions.method === 'undefined') {\n resolvedOptions.method = 'GET' as Method\n }\n\n const res: ServerFnBuilder<Register, Method> = {\n options: resolvedOptions as any,\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) as any\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 return Object.assign(\n async (opts?: CompiledFetcherFnOptions) => {\n // Start by executing the client-side middleware chain\n return executeMiddleware(resolvedMiddleware, 'client', {\n ...extractedFn,\n ...newOptions,\n data: opts?.data as any,\n headers: opts?.headers,\n signal: opts?.signal,\n context: {},\n }).then((d) => {\n if (d.error) throw d.error\n return d.result\n })\n },\n {\n // This copies over the URL, function ID\n ...extractedFn,\n // The extracted function on the server-side calls\n // this function\n __executeServer: async (opts: any, signal: AbortSignal) => {\n const serverContextAfterGlobalMiddlewares =\n getServerContextAfterGlobalMiddlewares()\n const ctx = {\n ...extractedFn,\n ...opts,\n context: {\n ...serverContextAfterGlobalMiddlewares,\n ...opts.context,\n },\n signal,\n }\n\n return executeMiddleware(resolvedMiddleware, 'server', ctx).then(\n (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 },\n },\n ) as any\n },\n } as ServerFnBuilder<Register, Method>\n const fun = (options?: { method?: Method }) => {\n return {\n ...res,\n options: {\n ...res.options,\n ...options,\n },\n }\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 const flattenedMiddlewares = flattenMiddlewares([\n ...globalMiddlewares,\n ...middlewares,\n ])\n\n const next: 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 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 const middlewareFn = (\n env === 'client' && 'client' in nextMiddleware.options\n ? nextMiddleware.options.client\n : nextMiddleware.options.server\n ) as MiddlewareFn | undefined\n\n if (middlewareFn) {\n // Execute the middleware\n return applyMiddleware(middlewareFn, ctx, async (newCtx) => {\n return next(newCtx).catch((error: any) => {\n if (isRedirect(error) || isNotFound(error)) {\n return {\n ...newCtx,\n error,\n }\n }\n\n throw error\n })\n })\n }\n\n return next(ctx)\n }\n\n // Start the middleware chain\n return next({\n ...opts,\n headers: opts.headers || {},\n sendContext: opts.sendContext || {},\n context: opts.context || {},\n })\n}\n\nexport type CompiledFetcherFnOptions = {\n method: Method\n data: unknown\n headers?: HeadersInit\n signal?: AbortSignal\n context?: any\n}\n\nexport type Fetcher<TRegister, TMiddlewares, TInputValidator, TResponse> =\n undefined extends IntersectAllValidatorInputs<TMiddlewares, TInputValidator>\n ? OptionalFetcher<TRegister, TMiddlewares, TInputValidator, TResponse>\n : RequiredFetcher<TRegister, TMiddlewares, TInputValidator, TResponse>\n\nexport interface FetcherBase {\n url: string\n __executeServer: (opts: {\n method: Method\n data: unknown\n headers?: HeadersInit\n context?: any\n signal: AbortSignal\n }) => Promise<unknown>\n}\n\nexport interface OptionalFetcher<\n TRegister,\n TMiddlewares,\n TInputValidator,\n TResponse,\n> extends FetcherBase {\n (\n options?: OptionalFetcherDataOptions<TMiddlewares, TInputValidator>,\n ): Promise<FetcherData<TRegister, TResponse>>\n}\n\nexport interface RequiredFetcher<\n TRegister,\n TMiddlewares,\n TInputValidator,\n TResponse,\n> extends FetcherBase {\n (\n opts: RequiredFetcherDataOptions<TMiddlewares, TInputValidator>,\n ): Promise<FetcherData<TRegister, TResponse>>\n}\n\nexport type FetcherBaseOptions = {\n headers?: HeadersInit\n signal?: AbortSignal\n}\n\nexport interface OptionalFetcherDataOptions<TMiddlewares, TInputValidator>\n extends FetcherBaseOptions {\n data?: Expand<IntersectAllValidatorInputs<TMiddlewares, TInputValidator>>\n}\n\nexport interface RequiredFetcherDataOptions<TMiddlewares, TInputValidator>\n extends FetcherBaseOptions {\n data: Expand<IntersectAllValidatorInputs<TMiddlewares, TInputValidator>>\n}\n\nexport type FetcherData<TRegister, TResponse> = TResponse extends Response\n ? Response\n : TResponse extends JsonResponse<any>\n ? ValidateSerializableInputResult<TRegister, ReturnType<TResponse['json']>>\n : ValidateSerializableInputResult<TRegister, TResponse>\n\nexport type RscStream<T> = {\n __cacheState: T\n}\n\nexport type Method = 'GET' | 'POST'\n\nexport type ServerFnReturnType<TRegister, TResponse> =\n | Response\n | Promise<ValidateSerializableInput<TRegister, TResponse>>\n | ValidateSerializableInput<TRegister, TResponse>\n\nexport type ServerFn<\n TRegister,\n TMethod,\n TMiddlewares,\n TInputValidator,\n TResponse,\n> = (\n ctx: ServerFnCtx<TRegister, TMethod, TMiddlewares, TInputValidator>,\n) => ServerFnReturnType<TRegister, TResponse>\n\nexport interface ServerFnCtx<\n TRegister,\n TMethod,\n TMiddlewares,\n TInputValidator,\n> {\n method: TMethod\n data: Expand<IntersectAllValidatorOutputs<TMiddlewares, TInputValidator>>\n context: Expand<AssignAllServerFnContext<TRegister, TMiddlewares, {}>>\n signal: AbortSignal\n}\n\nexport type CompiledFetcherFn<TRegister, TResponse> = {\n (\n opts: CompiledFetcherFnOptions & ServerFnBaseOptions<TRegister, Method>,\n ): Promise<TResponse>\n url: string\n}\n\nexport type ServerFnBaseOptions<\n TRegister,\n TMethod extends Method = 'GET',\n TResponse = unknown,\n TMiddlewares = unknown,\n TInputValidator = unknown,\n> = {\n method: TMethod\n middleware?: Constrain<\n TMiddlewares,\n ReadonlyArray<AnyFunctionMiddleware | AnyRequestMiddleware>\n >\n inputValidator?: ConstrainValidator<TRegister, TMethod, TInputValidator>\n extractedFn?: CompiledFetcherFn<TRegister, TResponse>\n serverFn?: ServerFn<\n TRegister,\n TMethod,\n TMiddlewares,\n TInputValidator,\n TResponse\n >\n functionId: string\n}\n\nexport type ValidateValidatorInput<\n TRegister,\n TMethod extends Method,\n 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> =\n ValidateValidatorInput<\n TRegister,\n TMethod,\n TInputValidator\n > extends infer TInput\n ? Validator<TInput, any>\n : never\n\nexport type ConstrainValidator<\n TRegister,\n TMethod extends Method,\n TInputValidator,\n> =\n | (unknown extends TInputValidator\n ? TInputValidator\n : ResolveValidatorInput<TInputValidator> extends ValidateValidator<\n TRegister,\n TMethod,\n TInputValidator\n >\n ? TInputValidator\n : never)\n | ValidateValidator<TRegister, TMethod, TInputValidator>\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> {\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 >\n}\n\nexport interface ServerFnAfterMiddleware<\n TRegister,\n TMethod extends Method,\n TMiddlewares,\n TInputValidator,\n> extends ServerFnWithTypes<\n TRegister,\n TMethod,\n TMiddlewares,\n TInputValidator,\n undefined\n >,\n ServerFnMiddleware<TRegister, TMethod, TMiddlewares, undefined>,\n ServerFnValidator<TRegister, TMethod, TMiddlewares>,\n ServerFnHandler<TRegister, TMethod, TMiddlewares, TInputValidator> {\n <TNewMethod extends Method = TMethod>(options?: {\n method?: TNewMethod\n }): ServerFnAfterMiddleware<\n TRegister,\n TNewMethod,\n TMiddlewares,\n TInputValidator\n >\n}\n\nexport type ValidatorFn<TRegister, TMethod extends Method, TMiddlewares> = <\n TInputValidator,\n>(\n inputValidator: ConstrainValidator<TRegister, TMethod, TInputValidator>,\n) => ServerFnAfterValidator<TRegister, TMethod, TMiddlewares, TInputValidator>\n\nexport interface ServerFnValidator<\n TRegister,\n TMethod extends Method,\n TMiddlewares,\n> {\n inputValidator: ValidatorFn<TRegister, TMethod, TMiddlewares>\n}\n\nexport interface ServerFnAfterValidator<\n TRegister,\n TMethod extends Method,\n TMiddlewares,\n TInputValidator,\n> extends ServerFnWithTypes<\n TRegister,\n TMethod,\n TMiddlewares,\n TInputValidator,\n undefined\n >,\n ServerFnMiddleware<TRegister, TMethod, TMiddlewares, TInputValidator>,\n ServerFnHandler<TRegister, TMethod, TMiddlewares, TInputValidator> {}\n\nexport interface ServerFnAfterTyper<\n TRegister,\n TMethod extends Method,\n TMiddlewares,\n TInputValidator,\n> extends ServerFnWithTypes<\n TRegister,\n TMethod,\n TMiddlewares,\n TInputValidator,\n undefined\n >,\n ServerFnHandler<TRegister, TMethod, TMiddlewares, TInputValidator> {}\n\n// Handler\nexport interface ServerFnHandler<\n TRegister,\n TMethod extends Method,\n TMiddlewares,\n TInputValidator,\n> {\n handler: <TNewResponse>(\n fn?: ServerFn<\n TRegister,\n TMethod,\n TMiddlewares,\n TInputValidator,\n TNewResponse\n >,\n ) => Fetcher<TRegister, TMiddlewares, TInputValidator, TNewResponse>\n}\n\nexport interface ServerFnBuilder<TRegister, TMethod extends Method = 'GET'>\n extends ServerFnWithTypes<\n TRegister,\n TMethod,\n undefined,\n undefined,\n undefined\n >,\n ServerFnMiddleware<TRegister, TMethod, undefined, undefined>,\n ServerFnValidator<TRegister, TMethod, undefined>,\n ServerFnHandler<TRegister, TMethod, undefined, undefined> {\n options: ServerFnBaseOptions<\n TRegister,\n TMethod,\n unknown,\n undefined,\n undefined\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> {\n _types: ServerFnTypes<\n TRegister,\n TMethod,\n TMiddlewares,\n TInputValidator,\n TResponse\n >\n options: ServerFnBaseOptions<\n TRegister,\n TMethod,\n unknown,\n undefined,\n undefined\n >\n [TSS_SERVER_FUNCTION_FACTORY]: true\n}\n\nexport type AnyServerFn = ServerFnWithTypes<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> {\n method: TMethod\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 middlewares: Array<AnyFunctionMiddleware | AnyRequestMiddleware>,\n): Array<AnyFunctionMiddleware | AnyRequestMiddleware> {\n const seen = new Set<AnyFunctionMiddleware | AnyRequestMiddleware>()\n const flattened: Array<AnyFunctionMiddleware | AnyRequestMiddleware> = []\n\n const recurse = (\n middleware: Array<AnyFunctionMiddleware | AnyRequestMiddleware>,\n ) => {\n middleware.forEach((m) => {\n if (m.options.middleware) {\n recurse(m.options.middleware)\n }\n\n if (!seen.has(m)) {\n seen.add(m)\n flattened.push(m)\n }\n })\n }\n\n recurse(middlewares)\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 functionId: string\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 const applyMiddleware = async (\n middlewareFn: MiddlewareFn,\n ctx: ServerFnMiddlewareOptions,\n nextFn: NextFn,\n) => {\n return middlewareFn({\n ...ctx,\n next: (async (\n userCtx: ServerFnMiddlewareResult | undefined = {} as any,\n ) => {\n // Return the next middleware\n return nextFn({\n ...ctx,\n ...userCtx,\n context: {\n ...ctx.context,\n ...userCtx.context,\n },\n sendContext: {\n ...ctx.sendContext,\n ...(userCtx.sendContext ?? {}),\n },\n headers: mergeHeaders(ctx.headers, userCtx.headers),\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 }) as any,\n } as any)\n}\n\nexport function execValidator(\n validator: AnyValidator,\n input: unknown,\n): unknown {\n if (validator == null) return {}\n\n if ('~standard' in validator) {\n const result = validator['~standard'].validate(input)\n\n if (result instanceof Promise)\n throw new Error('Async validation not supported')\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, ...ctx }) => {\n const payload = {\n ...ctx,\n // switch the sendContext over to context\n context: sendContext,\n } as any\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) as unknown as FunctionMiddlewareClientFnResult<\n any,\n any,\n any\n >\n },\n server: async ({ next, ...ctx }) => {\n // Execute the server function\n const result = await options.serverFn?.(ctx as TODO)\n\n return next({\n ...ctx,\n result,\n } as any) as unknown as FunctionMiddlewareServerFnResult<\n any,\n any,\n any,\n any,\n any\n >\n },\n },\n }\n}\n"],"names":["res","options"],"mappings":"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"}
|