@tanstack/start-client-core 1.121.0-alpha.27 → 1.121.0-alpha.28
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/createIsomorphicFn.js +6 -1
- package/dist/esm/createIsomorphicFn.js.map +1 -1
- package/dist/esm/createMiddleware.d.ts +8 -7
- package/dist/esm/createMiddleware.js.map +1 -1
- package/dist/esm/createServerFn.d.ts +7 -32
- package/dist/esm/createServerFn.js +15 -147
- package/dist/esm/createServerFn.js.map +1 -1
- package/dist/esm/envOnly.js.map +1 -1
- package/dist/esm/index.d.ts +6 -6
- package/dist/esm/index.js +2 -5
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/registerGlobalMiddleware.js.map +1 -1
- package/dist/esm/serializer.d.ts +22 -1
- package/dist/esm/serializer.js.map +1 -1
- package/package.json +3 -6
- package/src/createIsomorphicFn.ts +6 -1
- package/src/createMiddleware.ts +11 -10
- package/src/createServerFn.ts +28 -328
- package/src/index.tsx +19 -19
- package/src/serializer.ts +32 -3
- package/src/tests/createServerFn.test-d.ts +0 -6
- package/src/tests/{transformer.test.tsx → serializer.test.tsx} +36 -32
- package/dist/cjs/createIsomorphicFn.cjs +0 -7
- package/dist/cjs/createIsomorphicFn.cjs.map +0 -1
- package/dist/cjs/createIsomorphicFn.d.cts +0 -12
- package/dist/cjs/createMiddleware.cjs +0 -37
- package/dist/cjs/createMiddleware.cjs.map +0 -1
- package/dist/cjs/createMiddleware.d.cts +0 -175
- package/dist/cjs/createServerFn.cjs +0 -378
- package/dist/cjs/createServerFn.cjs.map +0 -1
- package/dist/cjs/createServerFn.d.cts +0 -159
- package/dist/cjs/envOnly.cjs +0 -7
- package/dist/cjs/envOnly.cjs.map +0 -1
- package/dist/cjs/envOnly.d.cts +0 -4
- package/dist/cjs/headers.cjs +0 -30
- package/dist/cjs/headers.cjs.map +0 -1
- package/dist/cjs/headers.d.cts +0 -5
- package/dist/cjs/index.cjs +0 -33
- package/dist/cjs/index.cjs.map +0 -1
- package/dist/cjs/index.d.cts +0 -11
- package/dist/cjs/json.cjs +0 -14
- package/dist/cjs/json.cjs.map +0 -1
- package/dist/cjs/json.d.cts +0 -2
- package/dist/cjs/registerGlobalMiddleware.cjs +0 -9
- package/dist/cjs/registerGlobalMiddleware.cjs.map +0 -1
- package/dist/cjs/registerGlobalMiddleware.d.cts +0 -5
- package/dist/cjs/serializer.cjs +0 -152
- package/dist/cjs/serializer.cjs.map +0 -1
- package/dist/cjs/serializer.d.cts +0 -2
- package/dist/cjs/ssr-client.cjs +0 -131
- package/dist/cjs/ssr-client.cjs.map +0 -1
- package/dist/cjs/ssr-client.d.cts +0 -65
- package/dist/cjs/tests/createServerFn.test-d.d.cts +0 -1
- package/dist/cjs/tests/createServerMiddleware.test-d.d.cts +0 -1
- package/dist/cjs/tests/envOnly.test-d.d.cts +0 -1
- package/dist/cjs/tests/json.test.d.cts +0 -1
- package/dist/cjs/tests/transformer.test.d.cts +0 -1
- package/dist/esm/headers.d.ts +0 -5
- package/dist/esm/headers.js +0 -30
- package/dist/esm/headers.js.map +0 -1
- package/dist/esm/json.d.ts +0 -2
- package/dist/esm/json.js +0 -14
- package/dist/esm/json.js.map +0 -1
- package/dist/esm/ssr-client.d.ts +0 -65
- package/dist/esm/ssr-client.js +0 -131
- package/dist/esm/ssr-client.js.map +0 -1
- package/dist/esm/tests/json.test.d.ts +0 -1
- package/dist/esm/tests/transformer.test.d.ts +0 -1
- package/src/headers.ts +0 -50
- package/src/json.ts +0 -15
- package/src/ssr-client.tsx +0 -249
- package/src/tests/json.test.ts +0 -37
- /package/dist/{cjs/tests/createIsomorphicFn.test-d.d.cts → esm/tests/serializer.test.d.ts} +0 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createIsomorphicFn.js","sources":["../../src/createIsomorphicFn.ts"],"sourcesContent":["// a function that can have different implementations on the client and server.\n// implementations not provided will default to a no-op function.\n\nexport type IsomorphicFn<\n TArgs extends Array<any> = [],\n TServer = undefined,\n TClient = undefined,\n> = (...args: TArgs) => TServer | TClient\n\nexport interface ServerOnlyFn<TArgs extends Array<any>, TServer>\n extends IsomorphicFn<TArgs, TServer> {\n client: <TClient>(\n clientImpl: (...args: TArgs) => TClient,\n ) => IsomorphicFn<TArgs, TServer, TClient>\n}\n\nexport interface ClientOnlyFn<TArgs extends Array<any>, TClient>\n extends IsomorphicFn<TArgs, undefined, TClient> {\n server: <TServer>(\n serverImpl: (...args: TArgs) => TServer,\n ) => IsomorphicFn<TArgs, TServer, TClient>\n}\n\nexport interface IsomorphicFnBase extends IsomorphicFn {\n server: <TArgs extends Array<any>, TServer>(\n serverImpl: (...args: TArgs) => TServer,\n ) => ServerOnlyFn<TArgs, TServer>\n client: <TArgs extends Array<any>, TClient>(\n clientImpl: (...args: TArgs) => TClient,\n ) => ClientOnlyFn<TArgs, TClient>\n}\n\n// this is a dummy function, it will be replaced by the transformer\nexport function createIsomorphicFn(): IsomorphicFnBase {\n return
|
|
1
|
+
{"version":3,"file":"createIsomorphicFn.js","sources":["../../src/createIsomorphicFn.ts"],"sourcesContent":["// a function that can have different implementations on the client and server.\n// implementations not provided will default to a no-op function.\n\nexport type IsomorphicFn<\n TArgs extends Array<any> = [],\n TServer = undefined,\n TClient = undefined,\n> = (...args: TArgs) => TServer | TClient\n\nexport interface ServerOnlyFn<TArgs extends Array<any>, TServer>\n extends IsomorphicFn<TArgs, TServer> {\n client: <TClient>(\n clientImpl: (...args: TArgs) => TClient,\n ) => IsomorphicFn<TArgs, TServer, TClient>\n}\n\nexport interface ClientOnlyFn<TArgs extends Array<any>, TClient>\n extends IsomorphicFn<TArgs, undefined, TClient> {\n server: <TServer>(\n serverImpl: (...args: TArgs) => TServer,\n ) => IsomorphicFn<TArgs, TServer, TClient>\n}\n\nexport interface IsomorphicFnBase extends IsomorphicFn {\n server: <TArgs extends Array<any>, TServer>(\n serverImpl: (...args: TArgs) => TServer,\n ) => ServerOnlyFn<TArgs, TServer>\n client: <TArgs extends Array<any>, TClient>(\n clientImpl: (...args: TArgs) => TClient,\n ) => ClientOnlyFn<TArgs, TClient>\n}\n\n// this is a dummy function, it will be replaced by the transformer\n// if we use `createIsomorphicFn` in this library itself, vite tries to execute it before the transformer runs\n// therefore we must return a dummy function that allows calling `server` and `client` method chains.\nexport function createIsomorphicFn(): IsomorphicFnBase {\n return {\n server: () => ({ client: () => () => {} }),\n client: () => ({ server: () => () => {} }),\n } as any\n}\n"],"names":[],"mappings":"AAmCO,SAAS,qBAAuC;AACrD,SAAO;AAAA,IACL,QAAQ,OAAO,EAAE,QAAQ,MAAM,MAAM;AAAA,IAAC;IACtC,QAAQ,OAAO,EAAE,QAAQ,MAAM,MAAM;AAAA,IAAC,EAAA;AAAA,EAAE;AAE5C;"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { ConstrainValidator, Method, ServerFnResponseType
|
|
2
|
-
import { Assign, Constrain, Expand, IntersectAssign, ResolveValidatorInput, ResolveValidatorOutput
|
|
1
|
+
import { ConstrainValidator, Method, ServerFnResponseType } from './createServerFn.js';
|
|
2
|
+
import { AnyRouter, Assign, Constrain, Expand, IntersectAssign, RegisteredRouter, ResolveValidatorInput, ResolveValidatorOutput } from '@tanstack/router-core';
|
|
3
|
+
import { SerializerStringify } from './serializer.js';
|
|
3
4
|
export declare function createMiddleware<TType extends MiddlewareType>(options: {
|
|
4
5
|
type: TType;
|
|
5
6
|
validateClient?: boolean;
|
|
@@ -105,11 +106,11 @@ export interface FunctionMiddlewareServerFnOptions<in out TMiddlewares, in out T
|
|
|
105
106
|
export type FunctionMiddlewareServerFnResult<TMiddlewares, TServerSendContext, TServerContext, TSendContext> = Promise<FunctionServerResultWithContext<TMiddlewares, TServerSendContext, TServerContext, TSendContext>> | FunctionServerResultWithContext<TMiddlewares, TServerSendContext, TServerContext, TSendContext>;
|
|
106
107
|
export interface FunctionMiddlewareAfterServer<TMiddlewares, TValidator, TServerContext, TServerSendContext, TClientContext, TClientSendContext, TServerFnResponseType extends ServerFnResponseType> extends FunctionMiddlewareWithTypes<TMiddlewares, TValidator, TServerContext, TServerSendContext, TClientContext, TClientSendContext, TServerFnResponseType> {
|
|
107
108
|
}
|
|
108
|
-
export interface FunctionMiddlewareClient<TMiddlewares, TValidator, TServerFnResponseType extends ServerFnResponseType> {
|
|
109
|
-
client: <TSendServerContext = undefined, TNewClientContext = undefined>(client: FunctionMiddlewareClientFn<TMiddlewares, TValidator, TSendServerContext, TNewClientContext, TServerFnResponseType>) => FunctionMiddlewareAfterClient<TMiddlewares, TValidator, TSendServerContext, TNewClientContext, ServerFnResponseType>;
|
|
109
|
+
export interface FunctionMiddlewareClient<TMiddlewares, TValidator, TServerFnResponseType extends ServerFnResponseType, TRouter extends AnyRouter = RegisteredRouter> {
|
|
110
|
+
client: <TSendServerContext = undefined, TNewClientContext = undefined>(client: FunctionMiddlewareClientFn<TMiddlewares, TValidator, TSendServerContext, TNewClientContext, TServerFnResponseType, TRouter>) => FunctionMiddlewareAfterClient<TMiddlewares, TValidator, TSendServerContext, TNewClientContext, ServerFnResponseType>;
|
|
110
111
|
}
|
|
111
|
-
export type FunctionMiddlewareClientFn<TMiddlewares, TValidator, TSendContext, TClientContext, TServerFnResponseType extends ServerFnResponseType> = (options: FunctionMiddlewareClientFnOptions<TMiddlewares, TValidator, TServerFnResponseType>) => FunctionMiddlewareClientFnResult<TMiddlewares, TSendContext, TClientContext>;
|
|
112
|
-
export interface FunctionMiddlewareClientFnOptions<in out TMiddlewares, in out TValidator, in out TServerFnResponseType extends ServerFnResponseType> {
|
|
112
|
+
export type FunctionMiddlewareClientFn<TMiddlewares, TValidator, TSendContext, TClientContext, TServerFnResponseType extends ServerFnResponseType, TRouter extends AnyRouter = RegisteredRouter> = (options: FunctionMiddlewareClientFnOptions<TMiddlewares, TValidator, TServerFnResponseType, TRouter>) => FunctionMiddlewareClientFnResult<TMiddlewares, TSendContext, TClientContext>;
|
|
113
|
+
export interface FunctionMiddlewareClientFnOptions<in out TMiddlewares, in out TValidator, in out TServerFnResponseType extends ServerFnResponseType, in out TRouter extends AnyRouter> {
|
|
113
114
|
data: Expand<IntersectAllValidatorInputs<TMiddlewares, TValidator>>;
|
|
114
115
|
context: Expand<AssignAllClientContextBeforeNext<TMiddlewares>>;
|
|
115
116
|
sendContext: Expand<AssignAllServerSendContext<TMiddlewares>>;
|
|
@@ -119,7 +120,7 @@ export interface FunctionMiddlewareClientFnOptions<in out TMiddlewares, in out T
|
|
|
119
120
|
next: FunctionMiddlewareClientNextFn<TMiddlewares>;
|
|
120
121
|
filename: string;
|
|
121
122
|
functionId: string;
|
|
122
|
-
|
|
123
|
+
router: TRouter;
|
|
123
124
|
}
|
|
124
125
|
export type FunctionMiddlewareClientFnResult<TMiddlewares, TSendContext, TClientContext> = Promise<FunctionClientResultWithContext<TMiddlewares, TSendContext, TClientContext>> | FunctionClientResultWithContext<TMiddlewares, TSendContext, TClientContext>;
|
|
125
126
|
export type FunctionClientResultWithContext<in out TMiddlewares, in out TSendContext, in out TClientContext> = {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createMiddleware.js","sources":["../../src/createMiddleware.ts"],"sourcesContent":["import type {\n ConstrainValidator,\n Method,\n ServerFnResponseType,\n ServerFnTypeOrTypeFn,\n} from './createServerFn'\nimport type {\n Assign,\n Constrain,\n Expand,\n IntersectAssign,\n ResolveValidatorInput,\n ResolveValidatorOutput,\n SerializerStringify,\n} from '@tanstack/router-core'\n\nexport function createMiddleware<TType extends MiddlewareType>(\n options: {\n type: TType\n validateClient?: boolean\n },\n __opts?: FunctionMiddlewareOptions<\n unknown,\n undefined,\n undefined,\n undefined,\n ServerFnResponseType\n >,\n): CreateMiddlewareResult<TType> {\n // const resolvedOptions = (__opts || options) as MiddlewareOptions<\n const resolvedOptions = {\n type: 'function',\n ...(__opts ||\n (options as FunctionMiddlewareOptions<\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<TType>\n}\n\nexport type MiddlewareType = 'request' | 'function'\n\nexport type CreateMiddlewareResult<TType extends MiddlewareType> =\n 'function' extends TType\n ? FunctionMiddleware<ServerFnResponseType>\n : RequestMiddleware\n\nexport interface FunctionMiddleware<\n TServerFnResponseType extends ServerFnResponseType,\n> extends FunctionMiddlewareAfterMiddleware<unknown, TServerFnResponseType> {\n middleware: <const TNewMiddlewares = undefined>(\n middlewares: Constrain<\n TNewMiddlewares,\n ReadonlyArray<AnyFunctionMiddleware>\n >,\n ) => FunctionMiddlewareAfterMiddleware<TNewMiddlewares, TServerFnResponseType>\n}\n\nexport interface FunctionMiddlewareAfterMiddleware<\n TMiddlewares,\n TServerFnResponseType extends ServerFnResponseType,\n> extends FunctionMiddlewareWithTypes<\n TMiddlewares,\n undefined,\n undefined,\n undefined,\n undefined,\n undefined,\n TServerFnResponseType\n >,\n FunctionMiddlewareServer<\n TMiddlewares,\n undefined,\n undefined,\n undefined,\n TServerFnResponseType\n >,\n FunctionMiddlewareClient<TMiddlewares, undefined, TServerFnResponseType>,\n FunctionMiddlewareValidator<TMiddlewares, TServerFnResponseType> {}\n\nexport interface FunctionMiddlewareWithTypes<\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 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>\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 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<TValidator>\n client?: FunctionMiddlewareClientFn<\n TMiddlewares,\n TValidator,\n TServerContext,\n TClientContext,\n TServerFnResponseType\n >\n server?: FunctionMiddlewareServerFn<\n TMiddlewares,\n TValidator,\n TServerContext,\n unknown,\n unknown,\n TServerFnResponseType\n >\n}\n\nexport type FunctionMiddlewareClientNextFn<TMiddlewares> = <\n TSendContext = undefined,\n TNewClientContext = undefined,\n>(ctx?: {\n context?: TNewClientContext\n sendContext?: SerializerStringify<TSendContext>\n headers?: HeadersInit\n}) => Promise<\n FunctionClientResultWithContext<TMiddlewares, TSendContext, TNewClientContext>\n>\n\nexport interface FunctionMiddlewareServer<\n TMiddlewares,\n TValidator,\n TServerSendContext,\n TClientContext,\n TServerFnResponseType extends ServerFnResponseType,\n> {\n server: <TNewServerContext = undefined, TSendContext = undefined>(\n server: FunctionMiddlewareServerFn<\n TMiddlewares,\n TValidator,\n TServerSendContext,\n TNewServerContext,\n TSendContext,\n TServerFnResponseType\n >,\n ) => FunctionMiddlewareAfterServer<\n TMiddlewares,\n TValidator,\n TNewServerContext,\n TServerSendContext,\n TClientContext,\n TSendContext,\n ServerFnResponseType\n >\n}\nexport type FunctionMiddlewareServerFn<\n TMiddlewares,\n TValidator,\n TServerSendContext,\n TNewServerContext,\n TSendContext,\n TServerFnResponseType extends ServerFnResponseType,\n> = (\n options: FunctionMiddlewareServerFnOptions<\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 TMiddlewares,\n in out TServerSendContext,\n> {\n request: Request\n context: Expand<AssignAllServerContext<TMiddlewares, TServerSendContext>>\n next: FunctionMiddlewareServerNextFn<TMiddlewares, TServerSendContext>\n response: Response\n method: Method\n signal: AbortSignal\n}\n\nexport type FunctionMiddlewareServerNextFn<TMiddlewares, TServerSendContext> = <\n TNewServerContext = undefined,\n TSendContext = undefined,\n>(ctx?: {\n context?: TNewServerContext\n sendContext?: SerializerStringify<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 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<TMiddlewares, TServerSendContext>\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 TMiddlewares,\n TValidator,\n TServerContext,\n TServerSendContext,\n TClientContext,\n TClientSendContext,\n TServerFnResponseType extends ServerFnResponseType,\n> extends FunctionMiddlewareWithTypes<\n TMiddlewares,\n TValidator,\n TServerContext,\n TServerSendContext,\n TClientContext,\n TClientSendContext,\n TServerFnResponseType\n > {}\n\nexport interface FunctionMiddlewareClient<\n TMiddlewares,\n TValidator,\n TServerFnResponseType extends ServerFnResponseType,\n> {\n client: <TSendServerContext = undefined, TNewClientContext = undefined>(\n client: FunctionMiddlewareClientFn<\n TMiddlewares,\n TValidator,\n TSendServerContext,\n TNewClientContext,\n TServerFnResponseType\n >,\n ) => FunctionMiddlewareAfterClient<\n TMiddlewares,\n TValidator,\n TSendServerContext,\n TNewClientContext,\n ServerFnResponseType\n >\n}\n\nexport type FunctionMiddlewareClientFn<\n TMiddlewares,\n TValidator,\n TSendContext,\n TClientContext,\n TServerFnResponseType extends ServerFnResponseType,\n> = (\n options: FunctionMiddlewareClientFnOptions<\n TMiddlewares,\n TValidator,\n TServerFnResponseType\n >,\n) => FunctionMiddlewareClientFnResult<\n TMiddlewares,\n TSendContext,\n TClientContext\n>\n\nexport interface FunctionMiddlewareClientFnOptions<\n in out TMiddlewares,\n in out TValidator,\n in out TServerFnResponseType extends ServerFnResponseType,\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<TMiddlewares>\n filename: string\n functionId: string\n type: ServerFnTypeOrTypeFn<\n Method,\n TServerFnResponseType,\n TMiddlewares,\n TValidator\n >\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 TMiddlewares,\n TValidator,\n TServerSendContext,\n TClientContext,\n TServerFnResponseType extends ServerFnResponseType,\n> extends FunctionMiddlewareWithTypes<\n TMiddlewares,\n TValidator,\n undefined,\n TServerSendContext,\n TClientContext,\n undefined,\n TServerFnResponseType\n >,\n FunctionMiddlewareServer<\n TMiddlewares,\n TValidator,\n TServerSendContext,\n TClientContext,\n TServerFnResponseType\n > {}\n\nexport interface FunctionMiddlewareValidator<\n TMiddlewares,\n TServerFnResponseType extends ServerFnResponseType,\n> {\n validator: <TNewValidator>(\n input: ConstrainValidator<TNewValidator>,\n ) => FunctionMiddlewareAfterValidator<\n TMiddlewares,\n TNewValidator,\n TServerFnResponseType\n >\n}\n\nexport interface FunctionMiddlewareAfterValidator<\n TMiddlewares,\n TValidator,\n TServerFnResponseType extends ServerFnResponseType,\n> extends FunctionMiddlewareWithTypes<\n TMiddlewares,\n TValidator,\n undefined,\n undefined,\n undefined,\n undefined,\n ServerFnResponseType\n >,\n FunctionMiddlewareServer<\n TMiddlewares,\n TValidator,\n undefined,\n undefined,\n TServerFnResponseType\n >,\n FunctionMiddlewareClient<TMiddlewares, TValidator, ServerFnResponseType> {}\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":"AAgBgB,SAAA,iBACd,SAIA,QAO+B;AAE/B,QAAM,kBAAkB;AAAA,IACtB,MAAM;AAAA,IACN,GAAI,UACD;AAAA,EAOL;AAEO,SAAA;AAAA,IACL,SAAS;AAAA,IACT,YAAY,CAAC,eAAoB;AACxB,aAAA;AAAA,QACL,CAAC;AAAA,QACD,OAAO,OAAO,iBAAiB,EAAE,WAAY,CAAA;AAAA,MAC/C;AAAA,IACF;AAAA,IACA,WAAW,CAAC,cAAmB;AACtB,aAAA;AAAA,QACL,CAAC;AAAA,QACD,OAAO,OAAO,iBAAiB,EAAE,UAAW,CAAA;AAAA,MAC9C;AAAA,IACF;AAAA,IACA,QAAQ,CAAC,WAAgB;AAChB,aAAA;AAAA,QACL,CAAC;AAAA,QACD,OAAO,OAAO,iBAAiB,EAAE,OAAQ,CAAA;AAAA,MAC3C;AAAA,IACF;AAAA,IACA,QAAQ,CAAC,WAAgB;AAChB,aAAA;AAAA,QACL,CAAC;AAAA,QACD,OAAO,OAAO,iBAAiB,EAAE,OAAQ,CAAA;AAAA,MAC3C;AAAA,IAAA;AAAA,EAEJ;AACF;"}
|
|
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 RegisteredRouter,\n ResolveValidatorInput,\n ResolveValidatorOutput,\n} from '@tanstack/router-core'\nimport type { SerializerStringify } from './serializer'\n\nexport function createMiddleware<TType extends MiddlewareType>(\n options: {\n type: TType\n validateClient?: boolean\n },\n __opts?: FunctionMiddlewareOptions<\n unknown,\n undefined,\n undefined,\n undefined,\n ServerFnResponseType\n >,\n): CreateMiddlewareResult<TType> {\n // const resolvedOptions = (__opts || options) as MiddlewareOptions<\n const resolvedOptions = {\n type: 'function',\n ...(__opts ||\n (options as FunctionMiddlewareOptions<\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<TType>\n}\n\nexport type MiddlewareType = 'request' | 'function'\n\nexport type CreateMiddlewareResult<TType extends MiddlewareType> =\n 'function' extends TType\n ? FunctionMiddleware<ServerFnResponseType>\n : RequestMiddleware\n\nexport interface FunctionMiddleware<\n TServerFnResponseType extends ServerFnResponseType,\n> extends FunctionMiddlewareAfterMiddleware<unknown, TServerFnResponseType> {\n middleware: <const TNewMiddlewares = undefined>(\n middlewares: Constrain<\n TNewMiddlewares,\n ReadonlyArray<AnyFunctionMiddleware>\n >,\n ) => FunctionMiddlewareAfterMiddleware<TNewMiddlewares, TServerFnResponseType>\n}\n\nexport interface FunctionMiddlewareAfterMiddleware<\n TMiddlewares,\n TServerFnResponseType extends ServerFnResponseType,\n> extends FunctionMiddlewareWithTypes<\n TMiddlewares,\n undefined,\n undefined,\n undefined,\n undefined,\n undefined,\n TServerFnResponseType\n >,\n FunctionMiddlewareServer<\n TMiddlewares,\n undefined,\n undefined,\n undefined,\n TServerFnResponseType\n >,\n FunctionMiddlewareClient<TMiddlewares, undefined, TServerFnResponseType>,\n FunctionMiddlewareValidator<TMiddlewares, TServerFnResponseType> {}\n\nexport interface FunctionMiddlewareWithTypes<\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 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>\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 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<TValidator>\n client?: FunctionMiddlewareClientFn<\n TMiddlewares,\n TValidator,\n TServerContext,\n TClientContext,\n TServerFnResponseType\n >\n server?: FunctionMiddlewareServerFn<\n TMiddlewares,\n TValidator,\n TServerContext,\n unknown,\n unknown,\n TServerFnResponseType\n >\n}\n\nexport type FunctionMiddlewareClientNextFn<TMiddlewares> = <\n TSendContext = undefined,\n TNewClientContext = undefined,\n>(ctx?: {\n context?: TNewClientContext\n sendContext?: SerializerStringify<TSendContext>\n headers?: HeadersInit\n}) => Promise<\n FunctionClientResultWithContext<TMiddlewares, TSendContext, TNewClientContext>\n>\n\nexport interface FunctionMiddlewareServer<\n TMiddlewares,\n TValidator,\n TServerSendContext,\n TClientContext,\n TServerFnResponseType extends ServerFnResponseType,\n> {\n server: <TNewServerContext = undefined, TSendContext = undefined>(\n server: FunctionMiddlewareServerFn<\n TMiddlewares,\n TValidator,\n TServerSendContext,\n TNewServerContext,\n TSendContext,\n TServerFnResponseType\n >,\n ) => FunctionMiddlewareAfterServer<\n TMiddlewares,\n TValidator,\n TNewServerContext,\n TServerSendContext,\n TClientContext,\n TSendContext,\n ServerFnResponseType\n >\n}\nexport type FunctionMiddlewareServerFn<\n TMiddlewares,\n TValidator,\n TServerSendContext,\n TNewServerContext,\n TSendContext,\n TServerFnResponseType extends ServerFnResponseType,\n> = (\n options: FunctionMiddlewareServerFnOptions<\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 TMiddlewares,\n in out TServerSendContext,\n> {\n request: Request\n context: Expand<AssignAllServerContext<TMiddlewares, TServerSendContext>>\n next: FunctionMiddlewareServerNextFn<TMiddlewares, TServerSendContext>\n response: Response\n method: Method\n signal: AbortSignal\n}\n\nexport type FunctionMiddlewareServerNextFn<TMiddlewares, TServerSendContext> = <\n TNewServerContext = undefined,\n TSendContext = undefined,\n>(ctx?: {\n context?: TNewServerContext\n sendContext?: SerializerStringify<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 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<TMiddlewares, TServerSendContext>\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 TMiddlewares,\n TValidator,\n TServerContext,\n TServerSendContext,\n TClientContext,\n TClientSendContext,\n TServerFnResponseType extends ServerFnResponseType,\n> extends FunctionMiddlewareWithTypes<\n TMiddlewares,\n TValidator,\n TServerContext,\n TServerSendContext,\n TClientContext,\n TClientSendContext,\n TServerFnResponseType\n > {}\n\nexport interface FunctionMiddlewareClient<\n TMiddlewares,\n TValidator,\n TServerFnResponseType extends ServerFnResponseType,\n TRouter extends AnyRouter = RegisteredRouter,\n> {\n client: <TSendServerContext = undefined, TNewClientContext = undefined>(\n client: FunctionMiddlewareClientFn<\n TMiddlewares,\n TValidator,\n TSendServerContext,\n TNewClientContext,\n TServerFnResponseType,\n TRouter\n >,\n ) => FunctionMiddlewareAfterClient<\n TMiddlewares,\n TValidator,\n TSendServerContext,\n TNewClientContext,\n ServerFnResponseType\n >\n}\n\nexport type FunctionMiddlewareClientFn<\n TMiddlewares,\n TValidator,\n TSendContext,\n TClientContext,\n TServerFnResponseType extends ServerFnResponseType,\n TRouter extends AnyRouter = RegisteredRouter,\n> = (\n options: FunctionMiddlewareClientFnOptions<\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 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<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 TMiddlewares,\n TValidator,\n TServerSendContext,\n TClientContext,\n TServerFnResponseType extends ServerFnResponseType,\n> extends FunctionMiddlewareWithTypes<\n TMiddlewares,\n TValidator,\n undefined,\n TServerSendContext,\n TClientContext,\n undefined,\n TServerFnResponseType\n >,\n FunctionMiddlewareServer<\n TMiddlewares,\n TValidator,\n TServerSendContext,\n TClientContext,\n TServerFnResponseType\n > {}\n\nexport interface FunctionMiddlewareValidator<\n TMiddlewares,\n TServerFnResponseType extends ServerFnResponseType,\n> {\n validator: <TNewValidator>(\n input: ConstrainValidator<TNewValidator>,\n ) => FunctionMiddlewareAfterValidator<\n TMiddlewares,\n TNewValidator,\n TServerFnResponseType\n >\n}\n\nexport interface FunctionMiddlewareAfterValidator<\n TMiddlewares,\n TValidator,\n TServerFnResponseType extends ServerFnResponseType,\n> extends FunctionMiddlewareWithTypes<\n TMiddlewares,\n TValidator,\n undefined,\n undefined,\n undefined,\n undefined,\n ServerFnResponseType\n >,\n FunctionMiddlewareServer<\n TMiddlewares,\n TValidator,\n undefined,\n undefined,\n TServerFnResponseType\n >,\n FunctionMiddlewareClient<TMiddlewares, TValidator, ServerFnResponseType> {}\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":"AAiBO,SAAS,iBACd,SAIA,QAO+B;AAE/B,QAAM,kBAAkB;AAAA,IACtB,MAAM;AAAA,IACN,GAAI,UACD;AAAA,EAAA;AASL,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,15 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { SerializerParse, SerializerStringify, SerializerStringifyBy } from './serializer.js';
|
|
2
|
+
import { AnyRouter, AnyValidator, Constrain, Expand, ResolveValidatorInput, Validator } from '@tanstack/router-core';
|
|
3
|
+
import { JsonResponse } from '@tanstack/router-core/ssr/client';
|
|
2
4
|
import { Readable } from 'node:stream';
|
|
3
5
|
import { AnyFunctionMiddleware, AssignAllClientSendContext, AssignAllServerContext, IntersectAllValidatorInputs, IntersectAllValidatorOutputs } from './createMiddleware.js';
|
|
4
6
|
export declare function createServerFn<TMethod extends Method, TServerFnResponseType extends ServerFnResponseType = 'data', TResponse = unknown, TMiddlewares = undefined, TValidator = undefined>(options?: {
|
|
5
7
|
method?: TMethod;
|
|
6
8
|
response?: TServerFnResponseType;
|
|
7
|
-
type?: ServerFnType;
|
|
8
9
|
}, __opts?: ServerFnBaseOptions<TMethod, TServerFnResponseType, TResponse, TMiddlewares, TValidator>): ServerFnBuilder<TMethod, TServerFnResponseType>;
|
|
9
10
|
export declare function executeMiddleware(middlewares: Array<AnyFunctionMiddleware>, env: 'client' | 'server', opts: ServerFnMiddlewareOptions): Promise<ServerFnMiddlewareResult>;
|
|
10
|
-
export interface JsonResponse<TData> extends Response {
|
|
11
|
-
json: () => Promise<TData>;
|
|
12
|
-
}
|
|
13
11
|
export type CompiledFetcherFnOptions = {
|
|
14
12
|
method: Method;
|
|
15
13
|
data: unknown;
|
|
@@ -39,10 +37,8 @@ export interface RequiredFetcher<TMiddlewares, TValidator, TResponse, TServerFnR
|
|
|
39
37
|
}
|
|
40
38
|
export type FetcherBaseOptions = {
|
|
41
39
|
headers?: HeadersInit;
|
|
42
|
-
type?: ServerFnType;
|
|
43
40
|
signal?: AbortSignal;
|
|
44
41
|
};
|
|
45
|
-
export type ServerFnType = 'static' | 'dynamic';
|
|
46
42
|
export interface OptionalFetcherDataOptions<TMiddlewares, TValidator> extends FetcherBaseOptions {
|
|
47
43
|
data?: Expand<IntersectAllValidatorInputs<TMiddlewares, TValidator>>;
|
|
48
44
|
}
|
|
@@ -83,7 +79,6 @@ export type ServerFnBaseOptions<TMethod extends Method = 'GET', TServerFnRespons
|
|
|
83
79
|
extractedFn?: CompiledFetcherFn<TResponse, TServerFnResponseType>;
|
|
84
80
|
serverFn?: ServerFn<TMethod, TServerFnResponseType, TMiddlewares, TInput, TResponse>;
|
|
85
81
|
functionId: string;
|
|
86
|
-
type: ServerFnTypeOrTypeFn<TMethod, TServerFnResponseType, TMiddlewares, AnyValidator>;
|
|
87
82
|
};
|
|
88
83
|
export type ValidatorInputStringify<TValidator> = SerializerStringifyBy<ResolveValidatorInput<TValidator>, Date | undefined | FormData>;
|
|
89
84
|
export type ValidatorSerializerStringify<TValidator> = ValidatorInputStringify<TValidator> extends infer TInput ? Validator<TInput, any> : never;
|
|
@@ -91,41 +86,22 @@ export type ConstrainValidator<TValidator> = (unknown extends TValidator ? TVali
|
|
|
91
86
|
export interface ServerFnMiddleware<TMethod extends Method, TServerFnResponseType extends ServerFnResponseType, TValidator> {
|
|
92
87
|
middleware: <const TNewMiddlewares = undefined>(middlewares: Constrain<TNewMiddlewares, ReadonlyArray<AnyFunctionMiddleware>>) => ServerFnAfterMiddleware<TMethod, TServerFnResponseType, TNewMiddlewares, TValidator>;
|
|
93
88
|
}
|
|
94
|
-
export interface ServerFnAfterMiddleware<TMethod extends Method, TServerFnResponseType extends ServerFnResponseType, TMiddlewares, TValidator> extends ServerFnValidator<TMethod, TServerFnResponseType, TMiddlewares>,
|
|
89
|
+
export interface ServerFnAfterMiddleware<TMethod extends Method, TServerFnResponseType extends ServerFnResponseType, TMiddlewares, TValidator> extends ServerFnValidator<TMethod, TServerFnResponseType, TMiddlewares>, ServerFnHandler<TMethod, TServerFnResponseType, TMiddlewares, TValidator> {
|
|
95
90
|
}
|
|
96
91
|
export type ValidatorFn<TMethod extends Method, TServerFnResponseType extends ServerFnResponseType, TMiddlewares> = <TValidator>(validator: ConstrainValidator<TValidator>) => ServerFnAfterValidator<TMethod, TServerFnResponseType, TMiddlewares, TValidator>;
|
|
97
92
|
export interface ServerFnValidator<TMethod extends Method, TServerFnResponseType extends ServerFnResponseType, TMiddlewares> {
|
|
98
93
|
validator: ValidatorFn<TMethod, TServerFnResponseType, TMiddlewares>;
|
|
99
94
|
}
|
|
100
|
-
export interface ServerFnAfterValidator<TMethod extends Method, TServerFnResponseType extends ServerFnResponseType, TMiddlewares, TValidator> extends ServerFnMiddleware<TMethod, TServerFnResponseType, TValidator>,
|
|
101
|
-
}
|
|
102
|
-
export interface ServerFnTyper<TMethod extends Method, TServerFnResponseType extends ServerFnResponseType, TMiddlewares, TValidator> {
|
|
103
|
-
type: (typer: ServerFnTypeOrTypeFn<TMethod, TServerFnResponseType, TMiddlewares, TValidator>) => ServerFnAfterTyper<TMethod, TServerFnResponseType, TMiddlewares, TValidator>;
|
|
95
|
+
export interface ServerFnAfterValidator<TMethod extends Method, TServerFnResponseType extends ServerFnResponseType, TMiddlewares, TValidator> extends ServerFnMiddleware<TMethod, TServerFnResponseType, TValidator>, ServerFnHandler<TMethod, TServerFnResponseType, TMiddlewares, TValidator> {
|
|
104
96
|
}
|
|
105
|
-
export type ServerFnTypeOrTypeFn<TMethod extends Method, TServerFnResponseType extends ServerFnResponseType, TMiddlewares, TValidator> = ServerFnType | ((ctx: ServerFnCtx<TMethod, TServerFnResponseType, TMiddlewares, TValidator>) => ServerFnType);
|
|
106
97
|
export interface ServerFnAfterTyper<TMethod extends Method, TServerFnResponseType extends ServerFnResponseType, TMiddlewares, TValidator> extends ServerFnHandler<TMethod, TServerFnResponseType, TMiddlewares, TValidator> {
|
|
107
98
|
}
|
|
108
99
|
export interface ServerFnHandler<TMethod extends Method, TServerFnResponseType extends ServerFnResponseType, TMiddlewares, TValidator> {
|
|
109
100
|
handler: <TNewResponse>(fn?: ServerFn<TMethod, TServerFnResponseType, TMiddlewares, TValidator, TNewResponse>) => Fetcher<TMiddlewares, TValidator, TNewResponse, TServerFnResponseType>;
|
|
110
101
|
}
|
|
111
|
-
export interface ServerFnBuilder<TMethod extends Method = 'GET', TServerFnResponseType extends ServerFnResponseType = 'data'> extends ServerFnMiddleware<TMethod, TServerFnResponseType, undefined>, ServerFnValidator<TMethod, TServerFnResponseType, undefined>,
|
|
102
|
+
export interface ServerFnBuilder<TMethod extends Method = 'GET', TServerFnResponseType extends ServerFnResponseType = 'data'> extends ServerFnMiddleware<TMethod, TServerFnResponseType, undefined>, ServerFnValidator<TMethod, TServerFnResponseType, undefined>, ServerFnHandler<TMethod, TServerFnResponseType, undefined, undefined> {
|
|
112
103
|
options: ServerFnBaseOptions<TMethod, TServerFnResponseType, unknown, undefined, undefined>;
|
|
113
104
|
}
|
|
114
|
-
export type StaticCachedResult = {
|
|
115
|
-
ctx?: {
|
|
116
|
-
result: any;
|
|
117
|
-
context: any;
|
|
118
|
-
};
|
|
119
|
-
error?: any;
|
|
120
|
-
};
|
|
121
|
-
export type ServerFnStaticCache = {
|
|
122
|
-
getItem: (ctx: ServerFnMiddlewareResult) => StaticCachedResult | Promise<StaticCachedResult | undefined>;
|
|
123
|
-
setItem: (ctx: ServerFnMiddlewareResult, response: StaticCachedResult) => Promise<void>;
|
|
124
|
-
fetchItem: (ctx: ServerFnMiddlewareResult) => StaticCachedResult | Promise<StaticCachedResult | undefined>;
|
|
125
|
-
};
|
|
126
|
-
export declare let serverFnStaticCache: ServerFnStaticCache | undefined;
|
|
127
|
-
export declare function setServerFnStaticCache(cache?: ServerFnStaticCache | (() => ServerFnStaticCache | undefined)): () => void;
|
|
128
|
-
export declare function createServerFnStaticCache(serverFnStaticCache: ServerFnStaticCache): ServerFnStaticCache;
|
|
129
105
|
export declare function extractFormDataContext(formData: FormData): {
|
|
130
106
|
context: unknown;
|
|
131
107
|
data: FormData;
|
|
@@ -142,13 +118,12 @@ export type ServerFnMiddlewareOptions = {
|
|
|
142
118
|
signal?: AbortSignal;
|
|
143
119
|
sendContext?: any;
|
|
144
120
|
context?: any;
|
|
145
|
-
type: ServerFnTypeOrTypeFn<any, any, any, any>;
|
|
146
121
|
functionId: string;
|
|
122
|
+
router?: AnyRouter;
|
|
147
123
|
};
|
|
148
124
|
export type ServerFnMiddlewareResult = ServerFnMiddlewareOptions & {
|
|
149
125
|
result?: unknown;
|
|
150
126
|
error?: unknown;
|
|
151
|
-
type: ServerFnTypeOrTypeFn<any, any, any, any>;
|
|
152
127
|
};
|
|
153
128
|
export type NextFn = (ctx: ServerFnMiddlewareResult) => Promise<ServerFnMiddlewareResult>;
|
|
154
129
|
export type MiddlewareFn = (ctx: ServerFnMiddlewareOptions & {
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import invariant from "tiny-invariant";
|
|
2
|
-
import warning from "tiny-warning";
|
|
3
1
|
import { isRedirect, isNotFound } from "@tanstack/router-core";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
2
|
+
import { mergeHeaders } from "@tanstack/router-core/ssr/client";
|
|
3
|
+
import { getStartContext } from "@tanstack/start-storage-context";
|
|
6
4
|
import { globalMiddleware } from "./registerGlobalMiddleware.js";
|
|
5
|
+
import { startSerializer } from "./serializer.js";
|
|
6
|
+
import { createIsomorphicFn } from "./createIsomorphicFn.js";
|
|
7
|
+
const getRouterInstance = createIsomorphicFn().client(() => window.__TSR_ROUTER__).server(() => getStartContext({ throwIfNotFound: false })?.router);
|
|
7
8
|
function createServerFn(options, __opts) {
|
|
8
9
|
const resolvedOptions = __opts || options || {};
|
|
9
10
|
if (typeof resolvedOptions.method === "undefined") {
|
|
@@ -17,9 +18,6 @@ function createServerFn(options, __opts) {
|
|
|
17
18
|
validator: (validator) => {
|
|
18
19
|
return createServerFn(void 0, Object.assign(resolvedOptions, { validator }));
|
|
19
20
|
},
|
|
20
|
-
type: (type) => {
|
|
21
|
-
return createServerFn(void 0, Object.assign(resolvedOptions, { type }));
|
|
22
|
-
},
|
|
23
21
|
handler: (...args) => {
|
|
24
22
|
const [extractedFn, serverFn] = args;
|
|
25
23
|
Object.assign(resolvedOptions, {
|
|
@@ -36,10 +34,11 @@ function createServerFn(options, __opts) {
|
|
|
36
34
|
return executeMiddleware(resolvedMiddleware, "client", {
|
|
37
35
|
...extractedFn,
|
|
38
36
|
...resolvedOptions,
|
|
39
|
-
data: opts
|
|
40
|
-
headers: opts
|
|
41
|
-
signal: opts
|
|
42
|
-
context: {}
|
|
37
|
+
data: opts?.data,
|
|
38
|
+
headers: opts?.headers,
|
|
39
|
+
signal: opts?.signal,
|
|
40
|
+
context: {},
|
|
41
|
+
router: getRouterInstance()
|
|
43
42
|
}).then((d) => {
|
|
44
43
|
if (resolvedOptions.response === "full") {
|
|
45
44
|
return d;
|
|
@@ -55,13 +54,12 @@ function createServerFn(options, __opts) {
|
|
|
55
54
|
// this function
|
|
56
55
|
__executeServer: async (opts_, signal) => {
|
|
57
56
|
const opts = opts_ instanceof FormData ? extractFormDataContext(opts_) : opts_;
|
|
58
|
-
opts.type = typeof resolvedOptions.type === "function" ? resolvedOptions.type(opts) : resolvedOptions.type;
|
|
59
57
|
const ctx = {
|
|
60
58
|
...extractedFn,
|
|
61
59
|
...opts,
|
|
62
60
|
signal
|
|
63
61
|
};
|
|
64
|
-
|
|
62
|
+
return executeMiddleware(resolvedMiddleware, "server", ctx).then(
|
|
65
63
|
(d) => ({
|
|
66
64
|
// Only send the result and sendContext back to the client
|
|
67
65
|
result: d.result,
|
|
@@ -69,37 +67,6 @@ function createServerFn(options, __opts) {
|
|
|
69
67
|
context: d.sendContext
|
|
70
68
|
})
|
|
71
69
|
);
|
|
72
|
-
if (ctx.type === "static") {
|
|
73
|
-
let response;
|
|
74
|
-
if (serverFnStaticCache == null ? void 0 : serverFnStaticCache.getItem) {
|
|
75
|
-
response = await serverFnStaticCache.getItem(ctx);
|
|
76
|
-
}
|
|
77
|
-
if (!response) {
|
|
78
|
-
response = await run().then((d) => {
|
|
79
|
-
return {
|
|
80
|
-
ctx: d,
|
|
81
|
-
error: null
|
|
82
|
-
};
|
|
83
|
-
}).catch((e) => {
|
|
84
|
-
return {
|
|
85
|
-
ctx: void 0,
|
|
86
|
-
error: e
|
|
87
|
-
};
|
|
88
|
-
});
|
|
89
|
-
if (serverFnStaticCache == null ? void 0 : serverFnStaticCache.setItem) {
|
|
90
|
-
await serverFnStaticCache.setItem(ctx, response);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
invariant(
|
|
94
|
-
response,
|
|
95
|
-
"No response from both server and static cache!"
|
|
96
|
-
);
|
|
97
|
-
if (response.error) {
|
|
98
|
-
throw response.error;
|
|
99
|
-
}
|
|
100
|
-
return response.ctx;
|
|
101
|
-
}
|
|
102
|
-
return run();
|
|
103
70
|
}
|
|
104
71
|
}
|
|
105
72
|
);
|
|
@@ -142,82 +109,6 @@ async function executeMiddleware(middlewares, env, opts) {
|
|
|
142
109
|
context: opts.context || {}
|
|
143
110
|
});
|
|
144
111
|
}
|
|
145
|
-
let serverFnStaticCache;
|
|
146
|
-
function setServerFnStaticCache(cache) {
|
|
147
|
-
const previousCache = serverFnStaticCache;
|
|
148
|
-
serverFnStaticCache = typeof cache === "function" ? cache() : cache;
|
|
149
|
-
return () => {
|
|
150
|
-
serverFnStaticCache = previousCache;
|
|
151
|
-
};
|
|
152
|
-
}
|
|
153
|
-
function createServerFnStaticCache(serverFnStaticCache2) {
|
|
154
|
-
return serverFnStaticCache2;
|
|
155
|
-
}
|
|
156
|
-
async function sha1Hash(message) {
|
|
157
|
-
const msgBuffer = new TextEncoder().encode(message);
|
|
158
|
-
const hashBuffer = await crypto.subtle.digest("SHA-1", msgBuffer);
|
|
159
|
-
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
|
160
|
-
const hashHex = hashArray.map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
161
|
-
return hashHex;
|
|
162
|
-
}
|
|
163
|
-
setServerFnStaticCache(() => {
|
|
164
|
-
const getStaticCacheUrl = async (options, hash) => {
|
|
165
|
-
const filename = await sha1Hash(`${options.functionId}__${hash}`);
|
|
166
|
-
return `/__tsr/staticServerFnCache/${filename}.json`;
|
|
167
|
-
};
|
|
168
|
-
const jsonToFilenameSafeString = (json) => {
|
|
169
|
-
const sortedKeysReplacer = (key, value) => value && typeof value === "object" && !Array.isArray(value) ? Object.keys(value).sort().reduce((acc, curr) => {
|
|
170
|
-
acc[curr] = value[curr];
|
|
171
|
-
return acc;
|
|
172
|
-
}, {}) : value;
|
|
173
|
-
const jsonString = JSON.stringify(json ?? "", sortedKeysReplacer);
|
|
174
|
-
return jsonString.replace(/[/\\?%*:|"<>]/g, "-").replace(/\s+/g, "_");
|
|
175
|
-
};
|
|
176
|
-
const staticClientCache = typeof document !== "undefined" ? /* @__PURE__ */ new Map() : null;
|
|
177
|
-
return createServerFnStaticCache({
|
|
178
|
-
getItem: async (ctx) => {
|
|
179
|
-
if (typeof document === "undefined") {
|
|
180
|
-
const hash = jsonToFilenameSafeString(ctx.data);
|
|
181
|
-
const url = await getStaticCacheUrl(ctx, hash);
|
|
182
|
-
const publicUrl = process.env.TSS_OUTPUT_PUBLIC_DIR;
|
|
183
|
-
const { promises: fs } = await import("node:fs");
|
|
184
|
-
const path = await import("node:path");
|
|
185
|
-
const filePath = path.join(publicUrl, url);
|
|
186
|
-
const [cachedResult, readError] = await fs.readFile(filePath, "utf-8").then((c) => [
|
|
187
|
-
startSerializer.parse(c),
|
|
188
|
-
null
|
|
189
|
-
]).catch((e) => [null, e]);
|
|
190
|
-
if (readError && readError.code !== "ENOENT") {
|
|
191
|
-
throw readError;
|
|
192
|
-
}
|
|
193
|
-
return cachedResult;
|
|
194
|
-
}
|
|
195
|
-
return void 0;
|
|
196
|
-
},
|
|
197
|
-
setItem: async (ctx, response) => {
|
|
198
|
-
const { promises: fs } = await import("node:fs");
|
|
199
|
-
const path = await import("node:path");
|
|
200
|
-
const hash = jsonToFilenameSafeString(ctx.data);
|
|
201
|
-
const url = await getStaticCacheUrl(ctx, hash);
|
|
202
|
-
const publicUrl = process.env.TSS_OUTPUT_PUBLIC_DIR;
|
|
203
|
-
const filePath = path.join(publicUrl, url);
|
|
204
|
-
await fs.mkdir(path.dirname(filePath), { recursive: true });
|
|
205
|
-
await fs.writeFile(filePath, startSerializer.stringify(response));
|
|
206
|
-
},
|
|
207
|
-
fetchItem: async (ctx) => {
|
|
208
|
-
const hash = jsonToFilenameSafeString(ctx.data);
|
|
209
|
-
const url = await getStaticCacheUrl(ctx, hash);
|
|
210
|
-
let result = staticClientCache == null ? void 0 : staticClientCache.get(url);
|
|
211
|
-
if (!result) {
|
|
212
|
-
result = await fetch(url, {
|
|
213
|
-
method: "GET"
|
|
214
|
-
}).then((r) => r.text()).then((d) => startSerializer.parse(d));
|
|
215
|
-
staticClientCache == null ? void 0 : staticClientCache.set(url, result);
|
|
216
|
-
}
|
|
217
|
-
return result;
|
|
218
|
-
}
|
|
219
|
-
});
|
|
220
|
-
});
|
|
221
112
|
function extractFormDataContext(formData) {
|
|
222
113
|
const serializedContext = formData.get("__TSR_CONTEXT");
|
|
223
114
|
formData.delete("__TSR_CONTEXT");
|
|
@@ -303,36 +194,16 @@ function serverFnBaseToMiddleware(options) {
|
|
|
303
194
|
validator: options.validator,
|
|
304
195
|
validateClient: options.validateClient,
|
|
305
196
|
client: async ({ next, sendContext, ...ctx }) => {
|
|
306
|
-
var _a;
|
|
307
197
|
const payload = {
|
|
308
198
|
...ctx,
|
|
309
199
|
// switch the sendContext over to context
|
|
310
|
-
context: sendContext
|
|
311
|
-
type: typeof ctx.type === "function" ? ctx.type(ctx) : ctx.type
|
|
200
|
+
context: sendContext
|
|
312
201
|
};
|
|
313
|
-
|
|
314
|
-
invariant(
|
|
315
|
-
serverFnStaticCache,
|
|
316
|
-
"serverFnStaticCache.fetchItem is not available!"
|
|
317
|
-
);
|
|
318
|
-
const result = await serverFnStaticCache.fetchItem(payload);
|
|
319
|
-
if (result) {
|
|
320
|
-
if (result.error) {
|
|
321
|
-
throw result.error;
|
|
322
|
-
}
|
|
323
|
-
return next(result.ctx);
|
|
324
|
-
}
|
|
325
|
-
warning(
|
|
326
|
-
result,
|
|
327
|
-
`No static cache item found for ${payload.functionId}__${JSON.stringify(payload.data)}, falling back to server function...`
|
|
328
|
-
);
|
|
329
|
-
}
|
|
330
|
-
const res = await ((_a = options.extractedFn) == null ? void 0 : _a.call(options, payload));
|
|
202
|
+
const res = await options.extractedFn?.(payload);
|
|
331
203
|
return next(res);
|
|
332
204
|
},
|
|
333
205
|
server: async ({ next, ...ctx }) => {
|
|
334
|
-
|
|
335
|
-
const result = await ((_a = options.serverFn) == null ? void 0 : _a.call(options, ctx));
|
|
206
|
+
const result = await options.serverFn?.(ctx);
|
|
336
207
|
return next({
|
|
337
208
|
...ctx,
|
|
338
209
|
result
|
|
@@ -344,13 +215,10 @@ function serverFnBaseToMiddleware(options) {
|
|
|
344
215
|
export {
|
|
345
216
|
applyMiddleware,
|
|
346
217
|
createServerFn,
|
|
347
|
-
createServerFnStaticCache,
|
|
348
218
|
execValidator,
|
|
349
219
|
executeMiddleware,
|
|
350
220
|
extractFormDataContext,
|
|
351
221
|
flattenMiddlewares,
|
|
352
|
-
serverFnBaseToMiddleware
|
|
353
|
-
serverFnStaticCache,
|
|
354
|
-
setServerFnStaticCache
|
|
222
|
+
serverFnBaseToMiddleware
|
|
355
223
|
};
|
|
356
224
|
//# sourceMappingURL=createServerFn.js.map
|