@tanstack/start-client-core 1.132.0-alpha.7 → 1.132.0-alpha.8
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/createMiddleware.d.ts +35 -36
- package/dist/esm/createMiddleware.js.map +1 -1
- package/dist/esm/createServerFn.d.ts +36 -37
- package/dist/esm/createServerFn.js.map +1 -1
- package/dist/esm/index.d.ts +0 -1
- package/dist/esm/serializer/ServerFunctionSerializationAdapter.d.ts +1 -1
- package/dist/esm/serializer/ServerFunctionSerializationAdapter.js +5 -1
- package/dist/esm/serializer/ServerFunctionSerializationAdapter.js.map +1 -1
- package/dist/esm/serializer/getDefaultSerovalPlugins.js.map +1 -1
- package/dist/esm/serverFnFetcher.js.map +1 -1
- package/package.json +3 -3
- package/src/createMiddleware.ts +101 -28
- package/src/createServerFn.ts +125 -39
- package/src/index.tsx +0 -9
- package/src/serializer/ServerFunctionSerializationAdapter.ts +7 -1
- package/src/serializer/getDefaultSerovalPlugins.ts +2 -2
- package/src/serverFnFetcher.ts +3 -2
- package/src/tests/createServerFn.test-d.ts +5 -3
- package/src/tests/createServerMiddleware.test-d.ts +11 -9
- package/dist/esm/serializer.d.ts +0 -16
- package/src/serializer.ts +0 -25
|
@@ -1,20 +1,19 @@
|
|
|
1
1
|
import { ConstrainValidator, Method, ServerFnResponseType } from './createServerFn.js';
|
|
2
|
-
import { AnyRouter, Assign, Constrain, Expand, IntersectAssign, RegisteredRouter, ResolveValidatorInput, ResolveValidatorOutput } from '@tanstack/router-core';
|
|
3
|
-
|
|
4
|
-
export declare function createMiddleware<TType extends MiddlewareType>(options: {
|
|
2
|
+
import { AnyRouter, Assign, Constrain, Expand, IntersectAssign, Register, RegisteredRouter, ResolveValidatorInput, ResolveValidatorOutput, ValidateSerializableInput } from '@tanstack/router-core';
|
|
3
|
+
export declare function createMiddleware<TRegister extends Register, TType extends MiddlewareType>(options: {
|
|
5
4
|
type: TType;
|
|
6
5
|
validateClient?: boolean;
|
|
7
|
-
}, __opts?: FunctionMiddlewareOptions<unknown, undefined, undefined, undefined, ServerFnResponseType>): CreateMiddlewareResult<TType>;
|
|
6
|
+
}, __opts?: FunctionMiddlewareOptions<TRegister, unknown, undefined, undefined, undefined, ServerFnResponseType>): CreateMiddlewareResult<TRegister, TType>;
|
|
8
7
|
export type MiddlewareType = 'request' | 'function';
|
|
9
|
-
export type CreateMiddlewareResult<TType extends MiddlewareType> = 'function' extends TType ? FunctionMiddleware<ServerFnResponseType> : RequestMiddleware;
|
|
10
|
-
export interface FunctionMiddleware<TServerFnResponseType extends ServerFnResponseType> extends FunctionMiddlewareAfterMiddleware<unknown, TServerFnResponseType> {
|
|
11
|
-
middleware: <const TNewMiddlewares = undefined>(middlewares: Constrain<TNewMiddlewares, ReadonlyArray<AnyFunctionMiddleware>>) => FunctionMiddlewareAfterMiddleware<TNewMiddlewares, TServerFnResponseType>;
|
|
8
|
+
export type CreateMiddlewareResult<TRegister extends Register, TType extends MiddlewareType> = 'function' extends TType ? FunctionMiddleware<TRegister, ServerFnResponseType> : RequestMiddleware;
|
|
9
|
+
export interface FunctionMiddleware<TRegister extends Register, TServerFnResponseType extends ServerFnResponseType> extends FunctionMiddlewareAfterMiddleware<TRegister, unknown, TServerFnResponseType> {
|
|
10
|
+
middleware: <const TNewMiddlewares = undefined>(middlewares: Constrain<TNewMiddlewares, ReadonlyArray<AnyFunctionMiddleware>>) => FunctionMiddlewareAfterMiddleware<TRegister, TNewMiddlewares, TServerFnResponseType>;
|
|
12
11
|
}
|
|
13
|
-
export interface FunctionMiddlewareAfterMiddleware<TMiddlewares, TServerFnResponseType extends ServerFnResponseType> extends FunctionMiddlewareWithTypes<TMiddlewares, undefined, undefined, undefined, undefined, undefined, TServerFnResponseType>, FunctionMiddlewareServer<TMiddlewares, undefined, undefined, undefined, TServerFnResponseType>, FunctionMiddlewareClient<TMiddlewares, undefined, TServerFnResponseType>, FunctionMiddlewareValidator<TMiddlewares, TServerFnResponseType> {
|
|
12
|
+
export interface FunctionMiddlewareAfterMiddleware<TRegister extends Register, TMiddlewares, TServerFnResponseType extends ServerFnResponseType> extends FunctionMiddlewareWithTypes<TRegister, TMiddlewares, undefined, undefined, undefined, undefined, undefined, TServerFnResponseType>, FunctionMiddlewareServer<TRegister, TMiddlewares, undefined, undefined, undefined, TServerFnResponseType>, FunctionMiddlewareClient<TRegister, TMiddlewares, undefined, TServerFnResponseType>, FunctionMiddlewareValidator<TRegister, TMiddlewares, TServerFnResponseType> {
|
|
14
13
|
}
|
|
15
|
-
export interface FunctionMiddlewareWithTypes<TMiddlewares, TValidator, TServerContext, TServerSendContext, TClientContext, TClientSendContext, TServerFnResponseType extends ServerFnResponseType> {
|
|
14
|
+
export interface FunctionMiddlewareWithTypes<TRegister extends Register, TMiddlewares, TValidator, TServerContext, TServerSendContext, TClientContext, TClientSendContext, TServerFnResponseType extends ServerFnResponseType> {
|
|
16
15
|
_types: FunctionMiddlewareTypes<TMiddlewares, TValidator, TServerContext, TServerSendContext, TClientContext, TClientSendContext>;
|
|
17
|
-
options: FunctionMiddlewareOptions<TMiddlewares, TValidator, TServerContext, TClientContext, TServerFnResponseType>;
|
|
16
|
+
options: FunctionMiddlewareOptions<TRegister, TMiddlewares, TValidator, TServerContext, TClientContext, TServerFnResponseType>;
|
|
18
17
|
}
|
|
19
18
|
export interface FunctionMiddlewareTypes<in out TMiddlewares, in out TValidator, in out TServerContext, in out TServerSendContext, in out TClientContext, in out TClientSendContext> {
|
|
20
19
|
type: 'function';
|
|
@@ -39,7 +38,7 @@ export interface FunctionMiddlewareTypes<in out TMiddlewares, in out TValidator,
|
|
|
39
38
|
*/
|
|
40
39
|
export type IntersectAllValidatorInputs<TMiddlewares, TValidator> = unknown extends TValidator ? TValidator : TValidator extends undefined ? IntersectAllMiddleware<TMiddlewares, 'allInput'> : IntersectAssign<IntersectAllMiddleware<TMiddlewares, 'allInput'>, ResolveValidatorInput<TValidator>>;
|
|
41
40
|
export type IntersectAllMiddleware<TMiddlewares, TType extends keyof AnyFunctionMiddleware['_types'] | keyof AnyRequestMiddleware['_types'], TAcc = undefined> = TMiddlewares extends readonly [infer TMiddleware, ...infer TRest] ? TMiddleware extends AnyFunctionMiddleware | AnyRequestMiddleware ? IntersectAllMiddleware<TRest, TType, IntersectAssign<TAcc, TMiddleware['_types'][TType & keyof TMiddleware['_types']]>> : TAcc : TAcc;
|
|
42
|
-
export type AnyFunctionMiddleware = FunctionMiddlewareWithTypes<any, any, any, any, any, any, any>;
|
|
41
|
+
export type AnyFunctionMiddleware = FunctionMiddlewareWithTypes<any, any, any, any, any, any, any, any>;
|
|
43
42
|
/**
|
|
44
43
|
* Recursively merge the output type produced by a sequence of middleware
|
|
45
44
|
*/
|
|
@@ -56,33 +55,33 @@ export type AssignAllServerSendContext<TMiddlewares, TSendContext = undefined> =
|
|
|
56
55
|
*/
|
|
57
56
|
export type AssignAllServerContext<TMiddlewares, TSendContext = undefined, TServerContext = undefined> = unknown extends TSendContext ? Assign<TSendContext, TServerContext> : Assign<AssignAllMiddleware<TMiddlewares, 'allServerContext'>, Assign<TSendContext, TServerContext>>;
|
|
58
57
|
export type AssignAllClientSendContext<TMiddlewares, TSendContext = undefined> = unknown extends TSendContext ? TSendContext : Assign<AssignAllMiddleware<TMiddlewares, 'allClientSendContext'>, TSendContext>;
|
|
59
|
-
export interface FunctionMiddlewareOptions<in out TMiddlewares, in out TValidator, in out TServerContext, in out TClientContext, in out TServerFnResponseType extends ServerFnResponseType> {
|
|
58
|
+
export interface FunctionMiddlewareOptions<in out TRegister extends Register, in out TMiddlewares, in out TValidator, in out TServerContext, in out TClientContext, in out TServerFnResponseType extends ServerFnResponseType> {
|
|
60
59
|
validateClient?: boolean;
|
|
61
60
|
middleware?: TMiddlewares;
|
|
62
|
-
validator?: ConstrainValidator<TValidator>;
|
|
63
|
-
client?: FunctionMiddlewareClientFn<TMiddlewares, TValidator, TServerContext, TClientContext, TServerFnResponseType>;
|
|
64
|
-
server?: FunctionMiddlewareServerFn<TMiddlewares, TValidator, TServerContext, unknown, unknown, TServerFnResponseType>;
|
|
61
|
+
validator?: ConstrainValidator<TRegister, TValidator>;
|
|
62
|
+
client?: FunctionMiddlewareClientFn<TRegister, TMiddlewares, TValidator, TServerContext, TClientContext, TServerFnResponseType>;
|
|
63
|
+
server?: FunctionMiddlewareServerFn<TRegister, TMiddlewares, TValidator, TServerContext, unknown, unknown, TServerFnResponseType>;
|
|
65
64
|
}
|
|
66
|
-
export type FunctionMiddlewareClientNextFn<TMiddlewares> = <TSendContext = undefined, TNewClientContext = undefined>(ctx?: {
|
|
65
|
+
export type FunctionMiddlewareClientNextFn<TRegister extends Register, TMiddlewares> = <TSendContext = undefined, TNewClientContext = undefined>(ctx?: {
|
|
67
66
|
context?: TNewClientContext;
|
|
68
|
-
sendContext?:
|
|
67
|
+
sendContext?: ValidateSerializableInput<TRegister, TSendContext>;
|
|
69
68
|
headers?: HeadersInit;
|
|
70
69
|
}) => Promise<FunctionClientResultWithContext<TMiddlewares, TSendContext, TNewClientContext>>;
|
|
71
|
-
export interface FunctionMiddlewareServer<TMiddlewares, TValidator, TServerSendContext, TClientContext, TServerFnResponseType extends ServerFnResponseType> {
|
|
72
|
-
server: <TNewServerContext = undefined, TSendContext = undefined>(server: FunctionMiddlewareServerFn<TMiddlewares, TValidator, TServerSendContext, TNewServerContext, TSendContext, TServerFnResponseType>) => FunctionMiddlewareAfterServer<TMiddlewares, TValidator, TNewServerContext, TServerSendContext, TClientContext, TSendContext, ServerFnResponseType>;
|
|
70
|
+
export interface FunctionMiddlewareServer<TRegister extends Register, TMiddlewares, TValidator, TServerSendContext, TClientContext, TServerFnResponseType extends ServerFnResponseType> {
|
|
71
|
+
server: <TNewServerContext = undefined, TSendContext = undefined>(server: FunctionMiddlewareServerFn<TRegister, TMiddlewares, TValidator, TServerSendContext, TNewServerContext, TSendContext, TServerFnResponseType>) => FunctionMiddlewareAfterServer<TRegister, TMiddlewares, TValidator, TNewServerContext, TServerSendContext, TClientContext, TSendContext, ServerFnResponseType>;
|
|
73
72
|
}
|
|
74
|
-
export type FunctionMiddlewareServerFn<TMiddlewares, TValidator, TServerSendContext, TNewServerContext, TSendContext, TServerFnResponseType extends ServerFnResponseType> = (options: FunctionMiddlewareServerFnOptions<TMiddlewares, TValidator, TServerSendContext, TServerFnResponseType>) => FunctionMiddlewareServerFnResult<TMiddlewares, TServerSendContext, TNewServerContext, TSendContext>;
|
|
75
|
-
export interface RequestMiddlewareServerFnOptions<in out TMiddlewares, in out TServerSendContext> {
|
|
73
|
+
export type FunctionMiddlewareServerFn<TRegister extends Register, TMiddlewares, TValidator, TServerSendContext, TNewServerContext, TSendContext, TServerFnResponseType extends ServerFnResponseType> = (options: FunctionMiddlewareServerFnOptions<TRegister, TMiddlewares, TValidator, TServerSendContext, TServerFnResponseType>) => FunctionMiddlewareServerFnResult<TMiddlewares, TServerSendContext, TNewServerContext, TSendContext>;
|
|
74
|
+
export interface RequestMiddlewareServerFnOptions<in out TRegister extends Register, in out TMiddlewares, in out TServerSendContext> {
|
|
76
75
|
request: Request;
|
|
77
76
|
context: Expand<AssignAllServerContext<TMiddlewares, TServerSendContext>>;
|
|
78
|
-
next: FunctionMiddlewareServerNextFn<TMiddlewares, TServerSendContext>;
|
|
77
|
+
next: FunctionMiddlewareServerNextFn<TRegister, TMiddlewares, TServerSendContext>;
|
|
79
78
|
response: Response;
|
|
80
79
|
method: Method;
|
|
81
80
|
signal: AbortSignal;
|
|
82
81
|
}
|
|
83
|
-
export type FunctionMiddlewareServerNextFn<TMiddlewares, TServerSendContext> = <TNewServerContext = undefined, TSendContext = undefined>(ctx?: {
|
|
82
|
+
export type FunctionMiddlewareServerNextFn<TRegister extends Register, TMiddlewares, TServerSendContext> = <TNewServerContext = undefined, TSendContext = undefined>(ctx?: {
|
|
84
83
|
context?: TNewServerContext;
|
|
85
|
-
sendContext?:
|
|
84
|
+
sendContext?: ValidateSerializableInput<TRegister, TSendContext>;
|
|
86
85
|
}) => Promise<FunctionServerResultWithContext<TMiddlewares, TServerSendContext, TNewServerContext, TSendContext>>;
|
|
87
86
|
export type FunctionServerResultWithContext<in out TMiddlewares, in out TServerSendContext, in out TServerContext, in out TSendContext> = {
|
|
88
87
|
'use functions must return the result of next()': true;
|
|
@@ -93,10 +92,10 @@ export type FunctionServerResultWithContext<in out TMiddlewares, in out TServerS
|
|
|
93
92
|
context: Expand<AssignAllServerContext<TMiddlewares, TServerSendContext, TServerContext>>;
|
|
94
93
|
sendContext: Expand<AssignAllClientSendContext<TMiddlewares, TSendContext>>;
|
|
95
94
|
};
|
|
96
|
-
export interface FunctionMiddlewareServerFnOptions<in out TMiddlewares, in out TValidator, in out TServerSendContext, in out TServerFnResponseType> {
|
|
95
|
+
export interface FunctionMiddlewareServerFnOptions<in out TRegister extends Register, in out TMiddlewares, in out TValidator, in out TServerSendContext, in out TServerFnResponseType> {
|
|
97
96
|
data: Expand<IntersectAllValidatorOutputs<TMiddlewares, TValidator>>;
|
|
98
97
|
context: Expand<AssignAllServerContext<TMiddlewares, TServerSendContext>>;
|
|
99
|
-
next: FunctionMiddlewareServerNextFn<TMiddlewares, TServerSendContext>;
|
|
98
|
+
next: FunctionMiddlewareServerNextFn<TRegister, TMiddlewares, TServerSendContext>;
|
|
100
99
|
response: TServerFnResponseType;
|
|
101
100
|
method: Method;
|
|
102
101
|
filename: string;
|
|
@@ -104,20 +103,20 @@ export interface FunctionMiddlewareServerFnOptions<in out TMiddlewares, in out T
|
|
|
104
103
|
signal: AbortSignal;
|
|
105
104
|
}
|
|
106
105
|
export type FunctionMiddlewareServerFnResult<TMiddlewares, TServerSendContext, TServerContext, TSendContext> = Promise<FunctionServerResultWithContext<TMiddlewares, TServerSendContext, TServerContext, TSendContext>> | FunctionServerResultWithContext<TMiddlewares, TServerSendContext, TServerContext, TSendContext>;
|
|
107
|
-
export interface FunctionMiddlewareAfterServer<TMiddlewares, TValidator, TServerContext, TServerSendContext, TClientContext, TClientSendContext, TServerFnResponseType extends ServerFnResponseType> extends FunctionMiddlewareWithTypes<TMiddlewares, TValidator, TServerContext, TServerSendContext, TClientContext, TClientSendContext, TServerFnResponseType> {
|
|
106
|
+
export interface FunctionMiddlewareAfterServer<TRegister extends Register, TMiddlewares, TValidator, TServerContext, TServerSendContext, TClientContext, TClientSendContext, TServerFnResponseType extends ServerFnResponseType> extends FunctionMiddlewareWithTypes<TRegister, TMiddlewares, TValidator, TServerContext, TServerSendContext, TClientContext, TClientSendContext, TServerFnResponseType> {
|
|
108
107
|
}
|
|
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>;
|
|
108
|
+
export interface FunctionMiddlewareClient<TRegister extends Register, TMiddlewares, TValidator, TServerFnResponseType extends ServerFnResponseType, TRouter extends AnyRouter = RegisteredRouter> {
|
|
109
|
+
client: <TSendServerContext = undefined, TNewClientContext = undefined>(client: FunctionMiddlewareClientFn<TRegister, TMiddlewares, TValidator, TSendServerContext, TNewClientContext, TServerFnResponseType, TRouter>) => FunctionMiddlewareAfterClient<TRegister, TMiddlewares, TValidator, TSendServerContext, TNewClientContext, ServerFnResponseType>;
|
|
111
110
|
}
|
|
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> {
|
|
111
|
+
export type FunctionMiddlewareClientFn<TRegister extends Register, TMiddlewares, TValidator, TSendContext, TClientContext, TServerFnResponseType extends ServerFnResponseType, TRouter extends AnyRouter = RegisteredRouter> = (options: FunctionMiddlewareClientFnOptions<TRegister, TMiddlewares, TValidator, TServerFnResponseType, TRouter>) => FunctionMiddlewareClientFnResult<TMiddlewares, TSendContext, TClientContext>;
|
|
112
|
+
export interface FunctionMiddlewareClientFnOptions<in out TRegister extends Register, in out TMiddlewares, in out TValidator, in out TServerFnResponseType extends ServerFnResponseType, in out TRouter extends AnyRouter> {
|
|
114
113
|
data: Expand<IntersectAllValidatorInputs<TMiddlewares, TValidator>>;
|
|
115
114
|
context: Expand<AssignAllClientContextBeforeNext<TMiddlewares>>;
|
|
116
115
|
sendContext: Expand<AssignAllServerSendContext<TMiddlewares>>;
|
|
117
116
|
method: Method;
|
|
118
117
|
response: TServerFnResponseType;
|
|
119
118
|
signal: AbortSignal;
|
|
120
|
-
next: FunctionMiddlewareClientNextFn<TMiddlewares>;
|
|
119
|
+
next: FunctionMiddlewareClientNextFn<TRegister, TMiddlewares>;
|
|
121
120
|
filename: string;
|
|
122
121
|
functionId: string;
|
|
123
122
|
router: TRouter;
|
|
@@ -129,12 +128,12 @@ export type FunctionClientResultWithContext<in out TMiddlewares, in out TSendCon
|
|
|
129
128
|
sendContext: Expand<AssignAllServerSendContext<TMiddlewares, TSendContext>>;
|
|
130
129
|
headers: HeadersInit;
|
|
131
130
|
};
|
|
132
|
-
export interface FunctionMiddlewareAfterClient<TMiddlewares, TValidator, TServerSendContext, TClientContext, TServerFnResponseType extends ServerFnResponseType> extends FunctionMiddlewareWithTypes<TMiddlewares, TValidator, undefined, TServerSendContext, TClientContext, undefined, TServerFnResponseType>, FunctionMiddlewareServer<TMiddlewares, TValidator, TServerSendContext, TClientContext, TServerFnResponseType> {
|
|
131
|
+
export interface FunctionMiddlewareAfterClient<TRegister extends Register, TMiddlewares, TValidator, TServerSendContext, TClientContext, TServerFnResponseType extends ServerFnResponseType> extends FunctionMiddlewareWithTypes<TRegister, TMiddlewares, TValidator, undefined, TServerSendContext, TClientContext, undefined, TServerFnResponseType>, FunctionMiddlewareServer<TRegister, TMiddlewares, TValidator, TServerSendContext, TClientContext, TServerFnResponseType> {
|
|
133
132
|
}
|
|
134
|
-
export interface FunctionMiddlewareValidator<TMiddlewares, TServerFnResponseType extends ServerFnResponseType> {
|
|
135
|
-
validator: <TNewValidator>(input: ConstrainValidator<TNewValidator>) => FunctionMiddlewareAfterValidator<TMiddlewares, TNewValidator, TServerFnResponseType>;
|
|
133
|
+
export interface FunctionMiddlewareValidator<TRegister extends Register, TMiddlewares, TServerFnResponseType extends ServerFnResponseType> {
|
|
134
|
+
validator: <TNewValidator>(input: ConstrainValidator<TRegister, TNewValidator>) => FunctionMiddlewareAfterValidator<TRegister, TMiddlewares, TNewValidator, TServerFnResponseType>;
|
|
136
135
|
}
|
|
137
|
-
export interface FunctionMiddlewareAfterValidator<TMiddlewares, TValidator, TServerFnResponseType extends ServerFnResponseType> extends FunctionMiddlewareWithTypes<TMiddlewares, TValidator, undefined, undefined, undefined, undefined, ServerFnResponseType>, FunctionMiddlewareServer<TMiddlewares, TValidator, undefined, undefined, TServerFnResponseType>, FunctionMiddlewareClient<TMiddlewares, TValidator, ServerFnResponseType> {
|
|
136
|
+
export interface FunctionMiddlewareAfterValidator<TRegister extends Register, TMiddlewares, TValidator, TServerFnResponseType extends ServerFnResponseType> extends FunctionMiddlewareWithTypes<TRegister, TMiddlewares, TValidator, undefined, undefined, undefined, undefined, ServerFnResponseType>, FunctionMiddlewareServer<TRegister, TMiddlewares, TValidator, undefined, undefined, TServerFnResponseType>, FunctionMiddlewareClient<TRegister, TMiddlewares, TValidator, ServerFnResponseType> {
|
|
138
137
|
}
|
|
139
138
|
export interface RequestMiddleware extends RequestMiddlewareAfterMiddleware<undefined> {
|
|
140
139
|
middleware: <const TMiddlewares = undefined>(middlewares: Constrain<TMiddlewares, ReadonlyArray<AnyRequestMiddleware>>) => RequestMiddlewareAfterMiddleware<TMiddlewares>;
|
|
@@ -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 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
|
+
{"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,12 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { AnyRouter, AnyValidator, Constrain, Expand, ResolveValidatorInput, Validator } from '@tanstack/router-core';
|
|
1
|
+
import { AnyRouter, AnyValidator, Constrain, Expand, Register, RegisteredSerializableInput, ResolveValidatorInput, ValidateSerializable, ValidateSerializableInput, ValidateSerializableInputResult, Validator } from '@tanstack/router-core';
|
|
3
2
|
import { JsonResponse } from '@tanstack/router-core/ssr/client';
|
|
4
3
|
import { Readable } from 'node:stream';
|
|
5
4
|
import { AnyFunctionMiddleware, AssignAllClientSendContext, AssignAllServerContext, IntersectAllValidatorInputs, IntersectAllValidatorOutputs } from './createMiddleware.js';
|
|
6
|
-
export declare function createServerFn<TMethod extends Method, TServerFnResponseType extends ServerFnResponseType = 'data', TResponse = unknown, TMiddlewares = undefined, TValidator = undefined>(options?: {
|
|
5
|
+
export declare function createServerFn<TRegister extends Register, TMethod extends Method, TServerFnResponseType extends ServerFnResponseType = 'data', TResponse = unknown, TMiddlewares = undefined, TValidator = undefined>(options?: {
|
|
7
6
|
method?: TMethod;
|
|
8
7
|
response?: TServerFnResponseType;
|
|
9
|
-
}, __opts?: ServerFnBaseOptions<TMethod, TServerFnResponseType, TResponse, TMiddlewares, TValidator>): ServerFnBuilder<TMethod, TServerFnResponseType>;
|
|
8
|
+
}, __opts?: ServerFnBaseOptions<TRegister, TMethod, TServerFnResponseType, TResponse, TMiddlewares, TValidator>): ServerFnBuilder<TRegister, TMethod, TServerFnResponseType>;
|
|
10
9
|
export declare function executeMiddleware(middlewares: Array<AnyFunctionMiddleware>, env: 'client' | 'server', opts: ServerFnMiddlewareOptions): Promise<ServerFnMiddlewareResult>;
|
|
11
10
|
export type CompiledFetcherFnOptions = {
|
|
12
11
|
method: Method;
|
|
@@ -16,7 +15,7 @@ export type CompiledFetcherFnOptions = {
|
|
|
16
15
|
signal?: AbortSignal;
|
|
17
16
|
context?: any;
|
|
18
17
|
};
|
|
19
|
-
export type Fetcher<TMiddlewares, TValidator, TResponse, TServerFnResponseType extends ServerFnResponseType> = undefined extends IntersectAllValidatorInputs<TMiddlewares, TValidator> ? OptionalFetcher<TMiddlewares, TValidator, TResponse, TServerFnResponseType> : RequiredFetcher<TMiddlewares, TValidator, TResponse, TServerFnResponseType>;
|
|
18
|
+
export type Fetcher<TRegister extends Register, TMiddlewares, TValidator, TResponse, TServerFnResponseType extends ServerFnResponseType> = undefined extends IntersectAllValidatorInputs<TMiddlewares, TValidator> ? OptionalFetcher<TRegister, TMiddlewares, TValidator, TResponse, TServerFnResponseType> : RequiredFetcher<TRegister, TMiddlewares, TValidator, TResponse, TServerFnResponseType>;
|
|
20
19
|
export interface FetcherBase {
|
|
21
20
|
url: string;
|
|
22
21
|
__executeServer: (opts: {
|
|
@@ -28,12 +27,12 @@ export interface FetcherBase {
|
|
|
28
27
|
signal: AbortSignal;
|
|
29
28
|
}) => Promise<unknown>;
|
|
30
29
|
}
|
|
31
|
-
export type FetchResult<TMiddlewares, TResponse, TServerFnResponseType extends ServerFnResponseType> = TServerFnResponseType extends 'raw' ? Promise<Response> : TServerFnResponseType extends 'full' ? Promise<FullFetcherData<TMiddlewares, TResponse>> : Promise<FetcherData<TResponse>>;
|
|
32
|
-
export interface OptionalFetcher<TMiddlewares, TValidator, TResponse, TServerFnResponseType extends ServerFnResponseType> extends FetcherBase {
|
|
33
|
-
(options?: OptionalFetcherDataOptions<TMiddlewares, TValidator>): FetchResult<TMiddlewares, TResponse, TServerFnResponseType>;
|
|
30
|
+
export type FetchResult<TRegister extends Register, TMiddlewares, TResponse, TServerFnResponseType extends ServerFnResponseType> = TServerFnResponseType extends 'raw' ? Promise<Response> : TServerFnResponseType extends 'full' ? Promise<FullFetcherData<TRegister, TMiddlewares, TResponse>> : Promise<FetcherData<TRegister, TResponse>>;
|
|
31
|
+
export interface OptionalFetcher<TRegister extends Register, TMiddlewares, TValidator, TResponse, TServerFnResponseType extends ServerFnResponseType> extends FetcherBase {
|
|
32
|
+
(options?: OptionalFetcherDataOptions<TMiddlewares, TValidator>): FetchResult<TRegister, TMiddlewares, TResponse, TServerFnResponseType>;
|
|
34
33
|
}
|
|
35
|
-
export interface RequiredFetcher<TMiddlewares, TValidator, TResponse, TServerFnResponseType extends ServerFnResponseType> extends FetcherBase {
|
|
36
|
-
(opts: RequiredFetcherDataOptions<TMiddlewares, TValidator>): FetchResult<TMiddlewares, TResponse, TServerFnResponseType>;
|
|
34
|
+
export interface RequiredFetcher<TRegister extends Register, TMiddlewares, TValidator, TResponse, TServerFnResponseType extends ServerFnResponseType> extends FetcherBase {
|
|
35
|
+
(opts: RequiredFetcherDataOptions<TMiddlewares, TValidator>): FetchResult<TRegister, TMiddlewares, TResponse, TServerFnResponseType>;
|
|
37
36
|
}
|
|
38
37
|
export type FetcherBaseOptions = {
|
|
39
38
|
headers?: HeadersInit;
|
|
@@ -45,20 +44,20 @@ export interface OptionalFetcherDataOptions<TMiddlewares, TValidator> extends Fe
|
|
|
45
44
|
export interface RequiredFetcherDataOptions<TMiddlewares, TValidator> extends FetcherBaseOptions {
|
|
46
45
|
data: Expand<IntersectAllValidatorInputs<TMiddlewares, TValidator>>;
|
|
47
46
|
}
|
|
48
|
-
export interface FullFetcherData<TMiddlewares, TResponse> {
|
|
47
|
+
export interface FullFetcherData<TRegister extends Register, TMiddlewares, TResponse> {
|
|
49
48
|
error: unknown;
|
|
50
|
-
result: FetcherData<TResponse>;
|
|
49
|
+
result: FetcherData<TRegister, TResponse>;
|
|
51
50
|
context: AssignAllClientSendContext<TMiddlewares>;
|
|
52
51
|
}
|
|
53
|
-
export type FetcherData<TResponse> = TResponse extends JsonResponse<any> ?
|
|
52
|
+
export type FetcherData<TRegister extends Register, TResponse> = TResponse extends JsonResponse<any> ? ValidateSerializableInputResult<TRegister, ReturnType<TResponse['json']>> : ValidateSerializableInputResult<TRegister, TResponse>;
|
|
54
53
|
export type RscStream<T> = {
|
|
55
54
|
__cacheState: T;
|
|
56
55
|
};
|
|
57
56
|
export type Method = 'GET' | 'POST';
|
|
58
57
|
export type ServerFnResponseType = 'data' | 'full' | 'raw';
|
|
59
58
|
export type RawResponse = Response | ReadableStream | Readable | null | string;
|
|
60
|
-
export type ServerFnReturnType<TServerFnResponseType extends ServerFnResponseType, TResponse> = TServerFnResponseType extends 'raw' ? RawResponse | Promise<RawResponse> : Promise<
|
|
61
|
-
export type ServerFn<TMethod, TServerFnResponseType extends ServerFnResponseType, TMiddlewares, TValidator, TResponse> = (ctx: ServerFnCtx<TMethod, TServerFnResponseType, TMiddlewares, TValidator>) => ServerFnReturnType<TServerFnResponseType, TResponse>;
|
|
59
|
+
export type ServerFnReturnType<TRegister extends Register, TServerFnResponseType extends ServerFnResponseType, TResponse> = TServerFnResponseType extends 'raw' ? RawResponse | Promise<RawResponse> : Promise<ValidateSerializableInput<TRegister, TResponse>> | ValidateSerializableInput<TRegister, TResponse>;
|
|
60
|
+
export type ServerFn<TRegister extends Register, TMethod, TServerFnResponseType extends ServerFnResponseType, TMiddlewares, TValidator, TResponse> = (ctx: ServerFnCtx<TMethod, TServerFnResponseType, TMiddlewares, TValidator>) => ServerFnReturnType<TRegister, TServerFnResponseType, TResponse>;
|
|
62
61
|
export interface ServerFnCtx<TMethod, TServerFnResponseType extends ServerFnResponseType, TMiddlewares, TValidator> {
|
|
63
62
|
method: TMethod;
|
|
64
63
|
response: TServerFnResponseType;
|
|
@@ -66,41 +65,41 @@ export interface ServerFnCtx<TMethod, TServerFnResponseType extends ServerFnResp
|
|
|
66
65
|
context: Expand<AssignAllServerContext<TMiddlewares>>;
|
|
67
66
|
signal: AbortSignal;
|
|
68
67
|
}
|
|
69
|
-
export type CompiledFetcherFn<TResponse, TServerFnResponseType extends ServerFnResponseType> = {
|
|
70
|
-
(opts: CompiledFetcherFnOptions & ServerFnBaseOptions<Method, TServerFnResponseType>): Promise<TResponse>;
|
|
68
|
+
export type CompiledFetcherFn<TRegister extends Register, TResponse, TServerFnResponseType extends ServerFnResponseType> = {
|
|
69
|
+
(opts: CompiledFetcherFnOptions & ServerFnBaseOptions<TRegister, Method, TServerFnResponseType>): Promise<TResponse>;
|
|
71
70
|
url: string;
|
|
72
71
|
};
|
|
73
|
-
export type ServerFnBaseOptions<TMethod extends Method = 'GET', TServerFnResponseType extends ServerFnResponseType = 'data', TResponse = unknown, TMiddlewares = unknown, TInput = unknown> = {
|
|
72
|
+
export type ServerFnBaseOptions<TRegister extends Register, TMethod extends Method = 'GET', TServerFnResponseType extends ServerFnResponseType = 'data', TResponse = unknown, TMiddlewares = unknown, TInput = unknown> = {
|
|
74
73
|
method: TMethod;
|
|
75
74
|
response?: TServerFnResponseType;
|
|
76
75
|
validateClient?: boolean;
|
|
77
76
|
middleware?: Constrain<TMiddlewares, ReadonlyArray<AnyFunctionMiddleware>>;
|
|
78
|
-
validator?: ConstrainValidator<TInput>;
|
|
79
|
-
extractedFn?: CompiledFetcherFn<TResponse, TServerFnResponseType>;
|
|
80
|
-
serverFn?: ServerFn<TMethod, TServerFnResponseType, TMiddlewares, TInput, TResponse>;
|
|
77
|
+
validator?: ConstrainValidator<TRegister, TInput>;
|
|
78
|
+
extractedFn?: CompiledFetcherFn<TRegister, TResponse, TServerFnResponseType>;
|
|
79
|
+
serverFn?: ServerFn<TRegister, TMethod, TServerFnResponseType, TMiddlewares, TInput, TResponse>;
|
|
81
80
|
functionId: string;
|
|
82
81
|
};
|
|
83
|
-
export type
|
|
84
|
-
export type
|
|
85
|
-
export type ConstrainValidator<TValidator> = (unknown extends TValidator ? TValidator : ResolveValidatorInput<TValidator> extends
|
|
86
|
-
export interface ServerFnMiddleware<TMethod extends Method, TServerFnResponseType extends ServerFnResponseType, TValidator> {
|
|
87
|
-
middleware: <const TNewMiddlewares = undefined>(middlewares: Constrain<TNewMiddlewares, ReadonlyArray<AnyFunctionMiddleware>>) => ServerFnAfterMiddleware<TMethod, TServerFnResponseType, TNewMiddlewares, TValidator>;
|
|
82
|
+
export type ValidateValidatorInput<TRegister extends Register, TValidator> = ValidateSerializable<ResolveValidatorInput<TValidator>, RegisteredSerializableInput<TRegister> | FormData>;
|
|
83
|
+
export type ValidateValidator<TRegister extends Register, TValidator> = ValidateValidatorInput<TRegister, TValidator> extends infer TInput ? Validator<TInput, any> : never;
|
|
84
|
+
export type ConstrainValidator<TRegister extends Register, TValidator> = (unknown extends TValidator ? TValidator : ResolveValidatorInput<TValidator> extends ValidateValidator<TRegister, TValidator> ? TValidator : never) | ValidateValidator<TRegister, TValidator>;
|
|
85
|
+
export interface ServerFnMiddleware<TRegister extends Register, TMethod extends Method, TServerFnResponseType extends ServerFnResponseType, TValidator> {
|
|
86
|
+
middleware: <const TNewMiddlewares = undefined>(middlewares: Constrain<TNewMiddlewares, ReadonlyArray<AnyFunctionMiddleware>>) => ServerFnAfterMiddleware<TRegister, TMethod, TServerFnResponseType, TNewMiddlewares, TValidator>;
|
|
88
87
|
}
|
|
89
|
-
export interface ServerFnAfterMiddleware<TMethod extends Method, TServerFnResponseType extends ServerFnResponseType, TMiddlewares, TValidator> extends ServerFnValidator<TMethod, TServerFnResponseType, TMiddlewares>, ServerFnHandler<TMethod, TServerFnResponseType, TMiddlewares, TValidator> {
|
|
88
|
+
export interface ServerFnAfterMiddleware<TRegister extends Register, TMethod extends Method, TServerFnResponseType extends ServerFnResponseType, TMiddlewares, TValidator> extends ServerFnValidator<TRegister, TMethod, TServerFnResponseType, TMiddlewares>, ServerFnHandler<TRegister, TMethod, TServerFnResponseType, TMiddlewares, TValidator> {
|
|
90
89
|
}
|
|
91
|
-
export type ValidatorFn<TMethod extends Method, TServerFnResponseType extends ServerFnResponseType, TMiddlewares> = <TValidator>(validator: ConstrainValidator<TValidator>) => ServerFnAfterValidator<TMethod, TServerFnResponseType, TMiddlewares, TValidator>;
|
|
92
|
-
export interface ServerFnValidator<TMethod extends Method, TServerFnResponseType extends ServerFnResponseType, TMiddlewares> {
|
|
93
|
-
validator: ValidatorFn<TMethod, TServerFnResponseType, TMiddlewares>;
|
|
90
|
+
export type ValidatorFn<TRegister extends Register, TMethod extends Method, TServerFnResponseType extends ServerFnResponseType, TMiddlewares> = <TValidator>(validator: ConstrainValidator<TRegister, TValidator>) => ServerFnAfterValidator<TRegister, TMethod, TServerFnResponseType, TMiddlewares, TValidator>;
|
|
91
|
+
export interface ServerFnValidator<TRegister extends Register, TMethod extends Method, TServerFnResponseType extends ServerFnResponseType, TMiddlewares> {
|
|
92
|
+
validator: ValidatorFn<TRegister, TMethod, TServerFnResponseType, TMiddlewares>;
|
|
94
93
|
}
|
|
95
|
-
export interface ServerFnAfterValidator<TMethod extends Method, TServerFnResponseType extends ServerFnResponseType, TMiddlewares, TValidator> extends ServerFnMiddleware<TMethod, TServerFnResponseType, TValidator>, ServerFnHandler<TMethod, TServerFnResponseType, TMiddlewares, TValidator> {
|
|
94
|
+
export interface ServerFnAfterValidator<TRegister extends Register, TMethod extends Method, TServerFnResponseType extends ServerFnResponseType, TMiddlewares, TValidator> extends ServerFnMiddleware<TRegister, TMethod, TServerFnResponseType, TValidator>, ServerFnHandler<TRegister, TMethod, TServerFnResponseType, TMiddlewares, TValidator> {
|
|
96
95
|
}
|
|
97
|
-
export interface ServerFnAfterTyper<TMethod extends Method, TServerFnResponseType extends ServerFnResponseType, TMiddlewares, TValidator> extends ServerFnHandler<TMethod, TServerFnResponseType, TMiddlewares, TValidator> {
|
|
96
|
+
export interface ServerFnAfterTyper<TRegister extends Register, TMethod extends Method, TServerFnResponseType extends ServerFnResponseType, TMiddlewares, TValidator> extends ServerFnHandler<TRegister, TMethod, TServerFnResponseType, TMiddlewares, TValidator> {
|
|
98
97
|
}
|
|
99
|
-
export interface ServerFnHandler<TMethod extends Method, TServerFnResponseType extends ServerFnResponseType, TMiddlewares, TValidator> {
|
|
100
|
-
handler: <TNewResponse>(fn?: ServerFn<TMethod, TServerFnResponseType, TMiddlewares, TValidator, TNewResponse>) => Fetcher<TMiddlewares, TValidator, TNewResponse, TServerFnResponseType>;
|
|
98
|
+
export interface ServerFnHandler<TRegister extends Register, TMethod extends Method, TServerFnResponseType extends ServerFnResponseType, TMiddlewares, TValidator> {
|
|
99
|
+
handler: <TNewResponse>(fn?: ServerFn<TRegister, TMethod, TServerFnResponseType, TMiddlewares, TValidator, TNewResponse>) => Fetcher<TRegister, TMiddlewares, TValidator, TNewResponse, TServerFnResponseType>;
|
|
101
100
|
}
|
|
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> {
|
|
103
|
-
options: ServerFnBaseOptions<TMethod, TServerFnResponseType, unknown, undefined, undefined>;
|
|
101
|
+
export interface ServerFnBuilder<TRegister extends Register, TMethod extends Method = 'GET', TServerFnResponseType extends ServerFnResponseType = 'data'> extends ServerFnMiddleware<TRegister, TMethod, TServerFnResponseType, undefined>, ServerFnValidator<TRegister, TMethod, TServerFnResponseType, undefined>, ServerFnHandler<TRegister, TMethod, TServerFnResponseType, undefined, undefined> {
|
|
102
|
+
options: ServerFnBaseOptions<TRegister, TMethod, TServerFnResponseType, unknown, undefined, undefined>;
|
|
104
103
|
}
|
|
105
104
|
export declare function flattenMiddlewares(middlewares: Array<AnyFunctionMiddleware>): Array<AnyFunctionMiddleware>;
|
|
106
105
|
export type ServerFnMiddlewareOptions = {
|
|
@@ -124,4 +123,4 @@ export type MiddlewareFn = (ctx: ServerFnMiddlewareOptions & {
|
|
|
124
123
|
}) => Promise<ServerFnMiddlewareResult>;
|
|
125
124
|
export declare const applyMiddleware: (middlewareFn: MiddlewareFn, ctx: ServerFnMiddlewareOptions, nextFn: NextFn) => Promise<ServerFnMiddlewareResult>;
|
|
126
125
|
export declare function execValidator(validator: AnyValidator, input: unknown): unknown;
|
|
127
|
-
export declare function serverFnBaseToMiddleware(options: ServerFnBaseOptions<any, any, any, any, any>): AnyFunctionMiddleware;
|
|
126
|
+
export declare function serverFnBaseToMiddleware(options: ServerFnBaseOptions<any, any, any, any, any, any>): AnyFunctionMiddleware;
|
|
@@ -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 SerializerParse,\n SerializerStringify,\n SerializerStringifyBy,\n} from './serializer'\nimport type {\n AnyRouter,\n AnyValidator,\n Constrain,\n Expand,\n ResolveValidatorInput,\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 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 TMethod,\n TServerFnResponseType,\n TResponse,\n TMiddlewares,\n TValidator\n >,\n): ServerFnBuilder<TMethod, TServerFnResponseType> {\n const resolvedOptions = (__opts || options || {}) as ServerFnBaseOptions<\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 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 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<TResponse, TServerFnResponseType>,\n ServerFn<\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 TMiddlewares,\n TValidator,\n TResponse,\n TServerFnResponseType extends ServerFnResponseType,\n> =\n undefined extends IntersectAllValidatorInputs<TMiddlewares, TValidator>\n ? OptionalFetcher<\n TMiddlewares,\n TValidator,\n TResponse,\n TServerFnResponseType\n >\n : RequiredFetcher<\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 TMiddlewares,\n TResponse,\n TServerFnResponseType extends ServerFnResponseType,\n> = TServerFnResponseType extends 'raw'\n ? Promise<Response>\n : TServerFnResponseType extends 'full'\n ? Promise<FullFetcherData<TMiddlewares, TResponse>>\n : Promise<FetcherData<TResponse>>\n\nexport interface OptionalFetcher<\n TMiddlewares,\n TValidator,\n TResponse,\n TServerFnResponseType extends ServerFnResponseType,\n> extends FetcherBase {\n (\n options?: OptionalFetcherDataOptions<TMiddlewares, TValidator>,\n ): FetchResult<TMiddlewares, TResponse, TServerFnResponseType>\n}\n\nexport interface RequiredFetcher<\n TMiddlewares,\n TValidator,\n TResponse,\n TServerFnResponseType extends ServerFnResponseType,\n> extends FetcherBase {\n (\n opts: RequiredFetcherDataOptions<TMiddlewares, TValidator>,\n ): FetchResult<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<TMiddlewares, TResponse> {\n error: unknown\n result: FetcherData<TResponse>\n context: AssignAllClientSendContext<TMiddlewares>\n}\n\nexport type FetcherData<TResponse> =\n TResponse extends JsonResponse<any>\n ? SerializerParse<ReturnType<TResponse['json']>>\n : SerializerParse<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 TServerFnResponseType extends ServerFnResponseType,\n TResponse,\n> = TServerFnResponseType extends 'raw'\n ? RawResponse | Promise<RawResponse>\n : Promise<SerializerStringify<TResponse>> | SerializerStringify<TResponse>\n\nexport type ServerFn<\n TMethod,\n TServerFnResponseType extends ServerFnResponseType,\n TMiddlewares,\n TValidator,\n TResponse,\n> = (\n ctx: ServerFnCtx<TMethod, TServerFnResponseType, TMiddlewares, TValidator>,\n) => ServerFnReturnType<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 TResponse,\n TServerFnResponseType extends ServerFnResponseType,\n> = {\n (\n opts: CompiledFetcherFnOptions &\n ServerFnBaseOptions<Method, TServerFnResponseType>,\n ): Promise<TResponse>\n url: string\n}\n\nexport type ServerFnBaseOptions<\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<TInput>\n extractedFn?: CompiledFetcherFn<TResponse, TServerFnResponseType>\n serverFn?: ServerFn<\n TMethod,\n TServerFnResponseType,\n TMiddlewares,\n TInput,\n TResponse\n >\n functionId: string\n}\n\nexport type ValidatorInputStringify<TValidator> = SerializerStringifyBy<\n ResolveValidatorInput<TValidator>,\n Date | undefined | FormData\n>\n\nexport type ValidatorSerializerStringify<TValidator> =\n ValidatorInputStringify<TValidator> extends infer TInput\n ? Validator<TInput, any>\n : never\n\nexport type ConstrainValidator<TValidator> =\n | (unknown extends TValidator\n ? TValidator\n : ResolveValidatorInput<TValidator> extends ValidatorInputStringify<TValidator>\n ? TValidator\n : never)\n | ValidatorSerializerStringify<TValidator>\n\nexport interface ServerFnMiddleware<\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 TMethod,\n TServerFnResponseType,\n TNewMiddlewares,\n TValidator\n >\n}\n\nexport interface ServerFnAfterMiddleware<\n TMethod extends Method,\n TServerFnResponseType extends ServerFnResponseType,\n TMiddlewares,\n TValidator,\n> extends ServerFnValidator<TMethod, TServerFnResponseType, TMiddlewares>,\n ServerFnHandler<TMethod, TServerFnResponseType, TMiddlewares, TValidator> {}\n\nexport type ValidatorFn<\n TMethod extends Method,\n TServerFnResponseType extends ServerFnResponseType,\n TMiddlewares,\n> = <TValidator>(\n validator: ConstrainValidator<TValidator>,\n) => ServerFnAfterValidator<\n TMethod,\n TServerFnResponseType,\n TMiddlewares,\n TValidator\n>\n\nexport interface ServerFnValidator<\n TMethod extends Method,\n TServerFnResponseType extends ServerFnResponseType,\n TMiddlewares,\n> {\n validator: ValidatorFn<TMethod, TServerFnResponseType, TMiddlewares>\n}\n\nexport interface ServerFnAfterValidator<\n TMethod extends Method,\n TServerFnResponseType extends ServerFnResponseType,\n TMiddlewares,\n TValidator,\n> extends ServerFnMiddleware<TMethod, TServerFnResponseType, TValidator>,\n ServerFnHandler<TMethod, TServerFnResponseType, TMiddlewares, TValidator> {}\n\nexport interface ServerFnAfterTyper<\n TMethod extends Method,\n TServerFnResponseType extends ServerFnResponseType,\n TMiddlewares,\n TValidator,\n> extends ServerFnHandler<\n TMethod,\n TServerFnResponseType,\n TMiddlewares,\n TValidator\n > {}\n\n// Handler\nexport interface ServerFnHandler<\n TMethod extends Method,\n TServerFnResponseType extends ServerFnResponseType,\n TMiddlewares,\n TValidator,\n> {\n handler: <TNewResponse>(\n fn?: ServerFn<\n TMethod,\n TServerFnResponseType,\n TMiddlewares,\n TValidator,\n TNewResponse\n >,\n ) => Fetcher<TMiddlewares, TValidator, TNewResponse, TServerFnResponseType>\n}\n\nexport interface ServerFnBuilder<\n TMethod extends Method = 'GET',\n TServerFnResponseType extends ServerFnResponseType = 'data',\n> extends ServerFnMiddleware<TMethod, TServerFnResponseType, undefined>,\n ServerFnValidator<TMethod, TServerFnResponseType, undefined>,\n ServerFnHandler<TMethod, TServerFnResponseType, undefined, undefined> {\n options: ServerFnBaseOptions<\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>,\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":"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"}
|
|
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":"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"}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
export type { DehydratedRouter, JsonResponse, } from '@tanstack/router-core/ssr/client';
|
|
2
2
|
export { hydrate, json, mergeHeaders } from '@tanstack/router-core/ssr/client';
|
|
3
|
-
export type { Serializable, SerializerParse, SerializerParseBy, SerializerStringify, SerializerStringifyBy, SerializerExtensions, } from './serializer.js';
|
|
4
3
|
export { createIsomorphicFn, type IsomorphicFn, type ServerOnlyFn, type ClientOnlyFn, type IsomorphicFnBase, } from './createIsomorphicFn.js';
|
|
5
4
|
export { createServerOnlyFn, createClientOnlyFn } from './envOnly.js';
|
|
6
5
|
export { createServerFn } from './createServerFn.js';
|
|
@@ -3,7 +3,11 @@ import { createClientRpc } from "../createClientRpc.js";
|
|
|
3
3
|
import { TSS_SERVER_FUNCTION } from "../constants.js";
|
|
4
4
|
const ServerFunctionSerializationAdapter = createSerializationAdapter({
|
|
5
5
|
key: "$TSS/serverfn",
|
|
6
|
-
test: (v) =>
|
|
6
|
+
test: (v) => {
|
|
7
|
+
if (typeof v !== "object" || v === null) return false;
|
|
8
|
+
if (!(TSS_SERVER_FUNCTION in v)) return false;
|
|
9
|
+
return !!v[TSS_SERVER_FUNCTION];
|
|
10
|
+
},
|
|
7
11
|
toSerializable: ({ functionId }) => ({ functionId }),
|
|
8
12
|
fromSerializable: ({ functionId }) => createClientRpc(functionId)
|
|
9
13
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ServerFunctionSerializationAdapter.js","sources":["../../../src/serializer/ServerFunctionSerializationAdapter.ts"],"sourcesContent":["import { createSerializationAdapter } from '@tanstack/router-core'\nimport { createClientRpc } from '../createClientRpc'\nimport { TSS_SERVER_FUNCTION } from '../constants'\n\nexport const ServerFunctionSerializationAdapter = createSerializationAdapter({\n key: '$TSS/serverfn',\n test: (v): v is { functionId: string } => v[TSS_SERVER_FUNCTION],\n toSerializable: ({ functionId }) => ({ functionId }),\n fromSerializable: ({ functionId }) => createClientRpc(functionId),\n})\n"],"names":[],"mappings":";;;AAIO,MAAM,qCAAqC,2BAA2B;AAAA,EAC3E,KAAK;AAAA,EACL,MAAM,CAAC,MAAmC,EAAE,mBAAmB;AAAA,
|
|
1
|
+
{"version":3,"file":"ServerFunctionSerializationAdapter.js","sources":["../../../src/serializer/ServerFunctionSerializationAdapter.ts"],"sourcesContent":["import { createSerializationAdapter } from '@tanstack/router-core'\nimport { createClientRpc } from '../createClientRpc'\nimport { TSS_SERVER_FUNCTION } from '../constants'\n\nexport const ServerFunctionSerializationAdapter = createSerializationAdapter({\n key: '$TSS/serverfn',\n test: (v): v is { functionId: string } => {\n if (typeof v !== 'object' || v === null) return false\n\n if (!(TSS_SERVER_FUNCTION in v)) return false\n\n return !!v[TSS_SERVER_FUNCTION]\n },\n toSerializable: ({ functionId }) => ({ functionId }),\n fromSerializable: ({ functionId }) => createClientRpc(functionId),\n})\n"],"names":[],"mappings":";;;AAIO,MAAM,qCAAqC,2BAA2B;AAAA,EAC3E,KAAK;AAAA,EACL,MAAM,CAAC,MAAmC;AACxC,QAAI,OAAO,MAAM,YAAY,MAAM,KAAM,QAAO;AAEhD,QAAI,EAAE,uBAAuB,GAAI,QAAO;AAExC,WAAO,CAAC,CAAC,EAAE,mBAAmB;AAAA,EAChC;AAAA,EACA,gBAAgB,CAAC,EAAE,kBAAkB,EAAE,WAAA;AAAA,EACvC,kBAAkB,CAAC,EAAE,WAAA,MAAiB,gBAAgB,UAAU;AAClE,CAAC;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getDefaultSerovalPlugins.js","sources":["../../../src/serializer/getDefaultSerovalPlugins.ts"],"sourcesContent":["import invariant from 'tiny-invariant'\nimport {\n makeSerovalPlugin,\n defaultSerovalPlugins as routerDefaultSerovalPlugins,\n} from '@tanstack/router-core'\nimport { FormDataPlugin } from 'seroval-plugins/web'\nimport { getRouterInstance } from '../getRouterInstance'\nimport type {
|
|
1
|
+
{"version":3,"file":"getDefaultSerovalPlugins.js","sources":["../../../src/serializer/getDefaultSerovalPlugins.ts"],"sourcesContent":["import invariant from 'tiny-invariant'\nimport {\n makeSerovalPlugin,\n defaultSerovalPlugins as routerDefaultSerovalPlugins,\n} from '@tanstack/router-core'\nimport { FormDataPlugin } from 'seroval-plugins/web'\nimport { getRouterInstance } from '../getRouterInstance'\nimport type { AnySerializationAdapter } from '@tanstack/router-core'\n\nimport type { Plugin } from 'seroval'\n\nexport const defaultSerovalPlugins = [\n ...routerDefaultSerovalPlugins,\n FormDataPlugin as Plugin<FormData, any>,\n]\n\nexport function getDefaultSerovalPlugins() {\n const router = getRouterInstance()\n invariant(router, 'Expected router instance to be available')\n const adapters = router.options.serializationAdapters as\n | Array<AnySerializationAdapter>\n | undefined\n return [...(adapters?.map(makeSerovalPlugin) ?? []), ...defaultSerovalPlugins]\n}\n"],"names":["routerDefaultSerovalPlugins"],"mappings":";;;;AAWO,MAAM,wBAAwB;AAAA,EACnC,GAAGA;AAAAA,EACH;AACF;AAEO,SAAS,2BAA2B;AACzC,QAAM,SAAS,kBAAA;AACf,YAAU,QAAQ,0CAA0C;AAC5D,QAAM,WAAW,OAAO,QAAQ;AAGhC,SAAO,CAAC,GAAI,UAAU,IAAI,iBAAiB,KAAK,CAAA,GAAK,GAAG,qBAAqB;AAC/E;"}
|