@tanstack/start-client-core 1.132.0-alpha.9 → 1.132.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/{serializer → client}/ServerFunctionSerializationAdapter.js +2 -2
- package/dist/esm/client/ServerFunctionSerializationAdapter.js.map +1 -0
- package/dist/esm/client/createClientRpc.d.ts +6 -0
- package/dist/esm/{createClientRpc.js → client/createClientRpc.js} +4 -4
- package/dist/esm/client/createClientRpc.js.map +1 -0
- package/dist/esm/client/hydrateStart.d.ts +2 -0
- package/dist/esm/client/hydrateStart.js +30 -0
- package/dist/esm/client/hydrateStart.js.map +1 -0
- package/dist/esm/client/index.d.ts +2 -0
- package/dist/esm/client/index.js +7 -0
- package/dist/esm/client/index.js.map +1 -0
- package/dist/esm/{serverFnFetcher.js → client/serverFnFetcher.js} +6 -6
- package/dist/esm/client/serverFnFetcher.js.map +1 -0
- package/dist/esm/constants.d.ts +1 -0
- package/dist/esm/constants.js +4 -0
- package/dist/esm/constants.js.map +1 -1
- package/dist/esm/createMiddleware.d.ts +95 -86
- package/dist/esm/createMiddleware.js +5 -5
- package/dist/esm/createMiddleware.js.map +1 -1
- package/dist/esm/createServerFn.d.ts +63 -69
- package/dist/esm/createServerFn.js +38 -22
- package/dist/esm/createServerFn.js.map +1 -1
- package/dist/esm/createStart.d.ts +27 -0
- package/dist/esm/createStart.js +14 -0
- package/dist/esm/createStart.js.map +1 -0
- package/dist/esm/fake-start-entry.d.ts +5 -0
- package/dist/esm/fake-start-entry.js +11 -0
- package/dist/esm/fake-start-entry.js.map +1 -0
- package/dist/esm/getDefaultSerovalPlugins.d.ts +1 -0
- package/dist/esm/getDefaultSerovalPlugins.js +14 -0
- package/dist/esm/getDefaultSerovalPlugins.js.map +1 -0
- package/dist/esm/getGlobalStartContext.d.ts +3 -0
- package/dist/esm/getGlobalStartContext.js +15 -0
- package/dist/esm/getGlobalStartContext.js.map +1 -0
- package/dist/esm/getRouterInstance.d.ts +2 -1
- package/dist/esm/getRouterInstance.js +1 -1
- package/dist/esm/getRouterInstance.js.map +1 -1
- package/dist/esm/getServerContextAfterGlobalMiddlewares.d.ts +1 -0
- package/dist/esm/getServerContextAfterGlobalMiddlewares.js +10 -0
- package/dist/esm/getServerContextAfterGlobalMiddlewares.js.map +1 -0
- package/dist/esm/getStartOptions.d.ts +1 -0
- package/dist/esm/getStartOptions.js +7 -0
- package/dist/esm/getStartOptions.js.map +1 -0
- package/dist/esm/global.d.ts +7 -0
- package/dist/esm/index.d.ts +11 -6
- package/dist/esm/index.js +9 -9
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/serverRoute.d.ts +66 -0
- package/dist/esm/startEntry.d.ts +8 -0
- package/package.json +18 -5
- package/src/{serializer → client}/ServerFunctionSerializationAdapter.ts +2 -2
- package/src/{createClientRpc.ts → client/createClientRpc.ts} +5 -3
- package/src/client/hydrateStart.ts +35 -0
- package/src/client/index.ts +3 -0
- package/src/{serverFnFetcher.ts → client/serverFnFetcher.ts} +11 -16
- package/src/constants.ts +3 -0
- package/src/createMiddleware.ts +269 -255
- package/src/createServerFn.ts +248 -300
- package/src/createStart.ts +122 -0
- package/src/fake-start-entry.ts +4 -0
- package/src/getDefaultSerovalPlugins.ts +17 -0
- package/src/getGlobalStartContext.ts +18 -0
- package/src/getRouterInstance.ts +5 -4
- package/src/getServerContextAfterGlobalMiddlewares.ts +7 -0
- package/src/getStartOptions.ts +6 -0
- package/src/global.ts +9 -0
- package/src/index.tsx +12 -11
- package/src/serverRoute.ts +474 -0
- package/src/start-entry.d.ts +11 -0
- package/src/startEntry.ts +10 -0
- package/src/tests/createServerFn.test-d.ts +68 -128
- package/src/tests/createServerMiddleware.test-d.ts +22 -17
- package/dist/esm/createClientRpc.d.ts +0 -6
- package/dist/esm/createClientRpc.js.map +0 -1
- package/dist/esm/registerGlobalMiddleware.d.ts +0 -5
- package/dist/esm/registerGlobalMiddleware.js +0 -9
- package/dist/esm/registerGlobalMiddleware.js.map +0 -1
- package/dist/esm/serializer/ServerFunctionSerializationAdapter.js.map +0 -1
- package/dist/esm/serializer/getClientSerovalPlugins.d.ts +0 -3
- package/dist/esm/serializer/getClientSerovalPlugins.js +0 -13
- package/dist/esm/serializer/getClientSerovalPlugins.js.map +0 -1
- package/dist/esm/serializer/getDefaultSerovalPlugins.d.ts +0 -3
- package/dist/esm/serializer/getDefaultSerovalPlugins.js +0 -19
- package/dist/esm/serializer/getDefaultSerovalPlugins.js.map +0 -1
- package/dist/esm/serverFnFetcher.js.map +0 -1
- package/src/registerGlobalMiddleware.ts +0 -9
- package/src/serializer/getClientSerovalPlugins.ts +0 -10
- package/src/serializer/getDefaultSerovalPlugins.ts +0 -24
- /package/dist/esm/{serializer → client}/ServerFunctionSerializationAdapter.d.ts +0 -0
- /package/dist/esm/{serverFnFetcher.d.ts → client/serverFnFetcher.d.ts} +0 -0
package/src/createServerFn.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { isNotFound, isRedirect } from '@tanstack/router-core'
|
|
2
2
|
import { mergeHeaders } from '@tanstack/router-core/ssr/client'
|
|
3
|
-
import { globalMiddleware } from './registerGlobalMiddleware'
|
|
4
3
|
|
|
5
|
-
import {
|
|
4
|
+
import { TSS_SERVER_FUNCTION_FACTORY } from './constants'
|
|
5
|
+
import { getServerContextAfterGlobalMiddlewares } from './getServerContextAfterGlobalMiddlewares'
|
|
6
|
+
import { getStartOptions } from './getStartOptions'
|
|
6
7
|
import type {
|
|
7
|
-
AnyRouter,
|
|
8
8
|
AnyValidator,
|
|
9
9
|
Constrain,
|
|
10
10
|
Expand,
|
|
@@ -17,11 +17,10 @@ import type {
|
|
|
17
17
|
Validator,
|
|
18
18
|
} from '@tanstack/router-core'
|
|
19
19
|
import type { JsonResponse } from '@tanstack/router-core/ssr/client'
|
|
20
|
-
import type { Readable } from 'node:stream'
|
|
21
20
|
import type {
|
|
22
21
|
AnyFunctionMiddleware,
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
AnyRequestMiddleware,
|
|
23
|
+
AssignAllServerFnContext,
|
|
25
24
|
FunctionMiddlewareClientFnResult,
|
|
26
25
|
FunctionMiddlewareServerFnResult,
|
|
27
26
|
IntersectAllValidatorInputs,
|
|
@@ -30,83 +29,73 @@ import type {
|
|
|
30
29
|
|
|
31
30
|
type TODO = any
|
|
32
31
|
|
|
33
|
-
export
|
|
34
|
-
TRegister extends Register,
|
|
32
|
+
export type CreateServerFn<TRegister> = <
|
|
35
33
|
TMethod extends Method,
|
|
36
|
-
TServerFnResponseType extends ServerFnResponseType = 'data',
|
|
37
34
|
TResponse = unknown,
|
|
38
35
|
TMiddlewares = undefined,
|
|
39
|
-
|
|
36
|
+
TInputValidator = undefined,
|
|
40
37
|
>(
|
|
41
38
|
options?: {
|
|
42
39
|
method?: TMethod
|
|
43
|
-
response?: TServerFnResponseType
|
|
44
40
|
},
|
|
45
41
|
__opts?: ServerFnBaseOptions<
|
|
46
42
|
TRegister,
|
|
47
43
|
TMethod,
|
|
48
|
-
TServerFnResponseType,
|
|
49
44
|
TResponse,
|
|
50
45
|
TMiddlewares,
|
|
51
|
-
|
|
46
|
+
TInputValidator
|
|
52
47
|
>,
|
|
53
|
-
)
|
|
48
|
+
) => ServerFnBuilder<TRegister, TMethod>
|
|
49
|
+
|
|
50
|
+
export const createServerFn: CreateServerFn<Register> = (options, __opts) => {
|
|
54
51
|
const resolvedOptions = (__opts || options || {}) as ServerFnBaseOptions<
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
TValidator
|
|
52
|
+
any,
|
|
53
|
+
any,
|
|
54
|
+
any,
|
|
55
|
+
any,
|
|
56
|
+
any
|
|
61
57
|
>
|
|
62
58
|
|
|
63
59
|
if (typeof resolvedOptions.method === 'undefined') {
|
|
64
|
-
resolvedOptions.method = 'GET' as
|
|
60
|
+
resolvedOptions.method = 'GET' as Method
|
|
65
61
|
}
|
|
66
62
|
|
|
67
|
-
const res: ServerFnBuilder<
|
|
63
|
+
const res: ServerFnBuilder<Register, Method> = {
|
|
68
64
|
options: resolvedOptions as any,
|
|
69
65
|
middleware: (middleware) => {
|
|
70
66
|
// multiple calls to `middleware()` merge the middlewares with the previously supplied ones
|
|
71
67
|
// this is primarily useful for letting users create their own abstractions on top of `createServerFn`
|
|
68
|
+
|
|
69
|
+
const newMiddleware = [...(resolvedOptions.middleware || [])]
|
|
70
|
+
middleware.map((m) => {
|
|
71
|
+
if (TSS_SERVER_FUNCTION_FACTORY in m) {
|
|
72
|
+
if (m.options.middleware) {
|
|
73
|
+
newMiddleware.push(...m.options.middleware)
|
|
74
|
+
}
|
|
75
|
+
} else {
|
|
76
|
+
newMiddleware.push(m)
|
|
77
|
+
}
|
|
78
|
+
})
|
|
79
|
+
|
|
72
80
|
const newOptions = {
|
|
73
81
|
...resolvedOptions,
|
|
74
|
-
middleware:
|
|
82
|
+
middleware: newMiddleware,
|
|
75
83
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
ServerFnResponseType,
|
|
80
|
-
TResponse,
|
|
81
|
-
TMiddlewares,
|
|
82
|
-
TValidator
|
|
83
|
-
>(undefined, newOptions) as any
|
|
84
|
+
const res = createServerFn(undefined, newOptions) as any
|
|
85
|
+
res[TSS_SERVER_FUNCTION_FACTORY] = true
|
|
86
|
+
return res
|
|
84
87
|
},
|
|
85
|
-
|
|
86
|
-
const newOptions = { ...resolvedOptions,
|
|
87
|
-
return createServerFn
|
|
88
|
-
TRegister,
|
|
89
|
-
TMethod,
|
|
90
|
-
ServerFnResponseType,
|
|
91
|
-
TResponse,
|
|
92
|
-
TMiddlewares,
|
|
93
|
-
TValidator
|
|
94
|
-
>(undefined, newOptions) as any
|
|
88
|
+
inputValidator: (inputValidator) => {
|
|
89
|
+
const newOptions = { ...resolvedOptions, inputValidator }
|
|
90
|
+
return createServerFn(undefined, newOptions) as any
|
|
95
91
|
},
|
|
96
92
|
handler: (...args) => {
|
|
97
93
|
// This function signature changes due to AST transformations
|
|
98
94
|
// in the babel plugin. We need to cast it to the correct
|
|
99
95
|
// function signature post-transformation
|
|
100
96
|
const [extractedFn, serverFn] = args as unknown as [
|
|
101
|
-
CompiledFetcherFn<
|
|
102
|
-
ServerFn<
|
|
103
|
-
TRegister,
|
|
104
|
-
TMethod,
|
|
105
|
-
TServerFnResponseType,
|
|
106
|
-
TMiddlewares,
|
|
107
|
-
TValidator,
|
|
108
|
-
TResponse
|
|
109
|
-
>,
|
|
97
|
+
CompiledFetcherFn<Register, any>,
|
|
98
|
+
ServerFn<Register, Method, any, any, any>,
|
|
110
99
|
]
|
|
111
100
|
|
|
112
101
|
// Keep the original function around so we can use it
|
|
@@ -131,11 +120,7 @@ export function createServerFn<
|
|
|
131
120
|
headers: opts?.headers,
|
|
132
121
|
signal: opts?.signal,
|
|
133
122
|
context: {},
|
|
134
|
-
router: getRouterInstance(),
|
|
135
123
|
}).then((d) => {
|
|
136
|
-
if (newOptions.response === 'full') {
|
|
137
|
-
return d
|
|
138
|
-
}
|
|
139
124
|
if (d.error) throw d.error
|
|
140
125
|
return d.result
|
|
141
126
|
})
|
|
@@ -146,9 +131,15 @@ export function createServerFn<
|
|
|
146
131
|
// The extracted function on the server-side calls
|
|
147
132
|
// this function
|
|
148
133
|
__executeServer: async (opts: any, signal: AbortSignal) => {
|
|
134
|
+
const serverContextAfterGlobalMiddlewares =
|
|
135
|
+
getServerContextAfterGlobalMiddlewares()
|
|
149
136
|
const ctx = {
|
|
150
137
|
...extractedFn,
|
|
151
138
|
...opts,
|
|
139
|
+
context: {
|
|
140
|
+
...serverContextAfterGlobalMiddlewares,
|
|
141
|
+
...opts.context,
|
|
142
|
+
},
|
|
152
143
|
signal,
|
|
153
144
|
}
|
|
154
145
|
|
|
@@ -164,11 +155,8 @@ export function createServerFn<
|
|
|
164
155
|
},
|
|
165
156
|
) as any
|
|
166
157
|
},
|
|
167
|
-
}
|
|
168
|
-
const fun = (options?: {
|
|
169
|
-
method?: TMethod
|
|
170
|
-
response?: TServerFnResponseType
|
|
171
|
-
}) => {
|
|
158
|
+
} as ServerFnBuilder<Register, Method>
|
|
159
|
+
const fun = (options?: { method?: Method }) => {
|
|
172
160
|
return {
|
|
173
161
|
...res,
|
|
174
162
|
options: {
|
|
@@ -177,16 +165,17 @@ export function createServerFn<
|
|
|
177
165
|
},
|
|
178
166
|
}
|
|
179
167
|
}
|
|
180
|
-
return Object.assign(fun, res)
|
|
168
|
+
return Object.assign(fun, res) as any
|
|
181
169
|
}
|
|
182
170
|
|
|
183
171
|
export async function executeMiddleware(
|
|
184
|
-
middlewares: Array<AnyFunctionMiddleware>,
|
|
172
|
+
middlewares: Array<AnyFunctionMiddleware | AnyRequestMiddleware>,
|
|
185
173
|
env: 'client' | 'server',
|
|
186
174
|
opts: ServerFnMiddlewareOptions,
|
|
187
175
|
): Promise<ServerFnMiddlewareResult> {
|
|
176
|
+
const globalMiddlewares = getStartOptions().functionMiddleware || []
|
|
188
177
|
const flattenedMiddlewares = flattenMiddlewares([
|
|
189
|
-
...
|
|
178
|
+
...globalMiddlewares,
|
|
190
179
|
...middlewares,
|
|
191
180
|
])
|
|
192
181
|
|
|
@@ -200,15 +189,19 @@ export async function executeMiddleware(
|
|
|
200
189
|
}
|
|
201
190
|
|
|
202
191
|
if (
|
|
203
|
-
nextMiddleware.options
|
|
204
|
-
|
|
192
|
+
'inputValidator' in nextMiddleware.options &&
|
|
193
|
+
nextMiddleware.options.inputValidator &&
|
|
194
|
+
env === 'server'
|
|
205
195
|
) {
|
|
206
196
|
// Execute the middleware's input function
|
|
207
|
-
ctx.data = await execValidator(
|
|
197
|
+
ctx.data = await execValidator(
|
|
198
|
+
nextMiddleware.options.inputValidator,
|
|
199
|
+
ctx.data,
|
|
200
|
+
)
|
|
208
201
|
}
|
|
209
202
|
|
|
210
203
|
const middlewareFn = (
|
|
211
|
-
env === 'client'
|
|
204
|
+
env === 'client' && 'client' in nextMiddleware.options
|
|
212
205
|
? nextMiddleware.options.client
|
|
213
206
|
: nextMiddleware.options.server
|
|
214
207
|
) as MiddlewareFn | undefined
|
|
@@ -244,41 +237,20 @@ export async function executeMiddleware(
|
|
|
244
237
|
export type CompiledFetcherFnOptions = {
|
|
245
238
|
method: Method
|
|
246
239
|
data: unknown
|
|
247
|
-
response?: ServerFnResponseType
|
|
248
240
|
headers?: HeadersInit
|
|
249
241
|
signal?: AbortSignal
|
|
250
242
|
context?: any
|
|
251
|
-
// router?: AnyRouter
|
|
252
243
|
}
|
|
253
244
|
|
|
254
|
-
export type Fetcher<
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
TResponse,
|
|
259
|
-
TServerFnResponseType extends ServerFnResponseType,
|
|
260
|
-
> =
|
|
261
|
-
undefined extends IntersectAllValidatorInputs<TMiddlewares, TValidator>
|
|
262
|
-
? OptionalFetcher<
|
|
263
|
-
TRegister,
|
|
264
|
-
TMiddlewares,
|
|
265
|
-
TValidator,
|
|
266
|
-
TResponse,
|
|
267
|
-
TServerFnResponseType
|
|
268
|
-
>
|
|
269
|
-
: RequiredFetcher<
|
|
270
|
-
TRegister,
|
|
271
|
-
TMiddlewares,
|
|
272
|
-
TValidator,
|
|
273
|
-
TResponse,
|
|
274
|
-
TServerFnResponseType
|
|
275
|
-
>
|
|
245
|
+
export type Fetcher<TRegister, TMiddlewares, TInputValidator, TResponse> =
|
|
246
|
+
undefined extends IntersectAllValidatorInputs<TMiddlewares, TInputValidator>
|
|
247
|
+
? OptionalFetcher<TRegister, TMiddlewares, TInputValidator, TResponse>
|
|
248
|
+
: RequiredFetcher<TRegister, TMiddlewares, TInputValidator, TResponse>
|
|
276
249
|
|
|
277
250
|
export interface FetcherBase {
|
|
278
251
|
url: string
|
|
279
252
|
__executeServer: (opts: {
|
|
280
253
|
method: Method
|
|
281
|
-
response?: ServerFnResponseType
|
|
282
254
|
data: unknown
|
|
283
255
|
headers?: HeadersInit
|
|
284
256
|
context?: any
|
|
@@ -286,39 +258,26 @@ export interface FetcherBase {
|
|
|
286
258
|
}) => Promise<unknown>
|
|
287
259
|
}
|
|
288
260
|
|
|
289
|
-
export type FetchResult<
|
|
290
|
-
TRegister extends Register,
|
|
291
|
-
TMiddlewares,
|
|
292
|
-
TResponse,
|
|
293
|
-
TServerFnResponseType extends ServerFnResponseType,
|
|
294
|
-
> = TServerFnResponseType extends 'raw'
|
|
295
|
-
? Promise<Response>
|
|
296
|
-
: TServerFnResponseType extends 'full'
|
|
297
|
-
? Promise<FullFetcherData<TRegister, TMiddlewares, TResponse>>
|
|
298
|
-
: Promise<FetcherData<TRegister, TResponse>>
|
|
299
|
-
|
|
300
261
|
export interface OptionalFetcher<
|
|
301
|
-
TRegister
|
|
262
|
+
TRegister,
|
|
302
263
|
TMiddlewares,
|
|
303
|
-
|
|
264
|
+
TInputValidator,
|
|
304
265
|
TResponse,
|
|
305
|
-
TServerFnResponseType extends ServerFnResponseType,
|
|
306
266
|
> extends FetcherBase {
|
|
307
267
|
(
|
|
308
|
-
options?: OptionalFetcherDataOptions<TMiddlewares,
|
|
309
|
-
):
|
|
268
|
+
options?: OptionalFetcherDataOptions<TMiddlewares, TInputValidator>,
|
|
269
|
+
): Promise<FetcherData<TRegister, TResponse>>
|
|
310
270
|
}
|
|
311
271
|
|
|
312
272
|
export interface RequiredFetcher<
|
|
313
|
-
TRegister
|
|
273
|
+
TRegister,
|
|
314
274
|
TMiddlewares,
|
|
315
|
-
|
|
275
|
+
TInputValidator,
|
|
316
276
|
TResponse,
|
|
317
|
-
TServerFnResponseType extends ServerFnResponseType,
|
|
318
277
|
> extends FetcherBase {
|
|
319
278
|
(
|
|
320
|
-
opts: RequiredFetcherDataOptions<TMiddlewares,
|
|
321
|
-
):
|
|
279
|
+
opts: RequiredFetcherDataOptions<TMiddlewares, TInputValidator>,
|
|
280
|
+
): Promise<FetcherData<TRegister, TResponse>>
|
|
322
281
|
}
|
|
323
282
|
|
|
324
283
|
export type FetcherBaseOptions = {
|
|
@@ -326,28 +285,19 @@ export type FetcherBaseOptions = {
|
|
|
326
285
|
signal?: AbortSignal
|
|
327
286
|
}
|
|
328
287
|
|
|
329
|
-
export interface OptionalFetcherDataOptions<TMiddlewares,
|
|
288
|
+
export interface OptionalFetcherDataOptions<TMiddlewares, TInputValidator>
|
|
330
289
|
extends FetcherBaseOptions {
|
|
331
|
-
data?: Expand<IntersectAllValidatorInputs<TMiddlewares,
|
|
290
|
+
data?: Expand<IntersectAllValidatorInputs<TMiddlewares, TInputValidator>>
|
|
332
291
|
}
|
|
333
292
|
|
|
334
|
-
export interface RequiredFetcherDataOptions<TMiddlewares,
|
|
293
|
+
export interface RequiredFetcherDataOptions<TMiddlewares, TInputValidator>
|
|
335
294
|
extends FetcherBaseOptions {
|
|
336
|
-
data: Expand<IntersectAllValidatorInputs<TMiddlewares,
|
|
295
|
+
data: Expand<IntersectAllValidatorInputs<TMiddlewares, TInputValidator>>
|
|
337
296
|
}
|
|
338
297
|
|
|
339
|
-
export
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
TResponse,
|
|
343
|
-
> {
|
|
344
|
-
error: unknown
|
|
345
|
-
result: FetcherData<TRegister, TResponse>
|
|
346
|
-
context: AssignAllClientSendContext<TMiddlewares>
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
export type FetcherData<TRegister extends Register, TResponse> =
|
|
350
|
-
TResponse extends JsonResponse<any>
|
|
298
|
+
export type FetcherData<TRegister, TResponse> = TResponse extends Response
|
|
299
|
+
? Response
|
|
300
|
+
: TResponse extends JsonResponse<any>
|
|
351
301
|
? ValidateSerializableInputResult<TRegister, ReturnType<TResponse['json']>>
|
|
352
302
|
: ValidateSerializableInputResult<TRegister, TResponse>
|
|
353
303
|
|
|
@@ -356,293 +306,293 @@ export type RscStream<T> = {
|
|
|
356
306
|
}
|
|
357
307
|
|
|
358
308
|
export type Method = 'GET' | 'POST'
|
|
359
|
-
export type ServerFnResponseType = 'data' | 'full' | 'raw'
|
|
360
309
|
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
TRegister extends Register,
|
|
366
|
-
TServerFnResponseType extends ServerFnResponseType,
|
|
367
|
-
TResponse,
|
|
368
|
-
> = TServerFnResponseType extends 'raw'
|
|
369
|
-
? RawResponse | Promise<RawResponse>
|
|
370
|
-
:
|
|
371
|
-
| Promise<ValidateSerializableInput<TRegister, TResponse>>
|
|
372
|
-
| ValidateSerializableInput<TRegister, TResponse>
|
|
310
|
+
export type ServerFnReturnType<TRegister, TResponse> =
|
|
311
|
+
| Response
|
|
312
|
+
| Promise<ValidateSerializableInput<TRegister, TResponse>>
|
|
313
|
+
| ValidateSerializableInput<TRegister, TResponse>
|
|
373
314
|
|
|
374
315
|
export type ServerFn<
|
|
375
|
-
TRegister
|
|
316
|
+
TRegister,
|
|
376
317
|
TMethod,
|
|
377
|
-
TServerFnResponseType extends ServerFnResponseType,
|
|
378
318
|
TMiddlewares,
|
|
379
|
-
|
|
319
|
+
TInputValidator,
|
|
380
320
|
TResponse,
|
|
381
321
|
> = (
|
|
382
|
-
ctx: ServerFnCtx<
|
|
383
|
-
) => ServerFnReturnType<TRegister,
|
|
322
|
+
ctx: ServerFnCtx<TRegister, TMethod, TMiddlewares, TInputValidator>,
|
|
323
|
+
) => ServerFnReturnType<TRegister, TResponse>
|
|
384
324
|
|
|
385
325
|
export interface ServerFnCtx<
|
|
326
|
+
TRegister,
|
|
386
327
|
TMethod,
|
|
387
|
-
TServerFnResponseType extends ServerFnResponseType,
|
|
388
328
|
TMiddlewares,
|
|
389
|
-
|
|
329
|
+
TInputValidator,
|
|
390
330
|
> {
|
|
391
331
|
method: TMethod
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
context: Expand<AssignAllServerContext<TMiddlewares>>
|
|
332
|
+
data: Expand<IntersectAllValidatorOutputs<TMiddlewares, TInputValidator>>
|
|
333
|
+
context: Expand<AssignAllServerFnContext<TRegister, TMiddlewares, {}>>
|
|
395
334
|
signal: AbortSignal
|
|
396
335
|
}
|
|
397
336
|
|
|
398
|
-
export type CompiledFetcherFn<
|
|
399
|
-
TRegister extends Register,
|
|
400
|
-
TResponse,
|
|
401
|
-
TServerFnResponseType extends ServerFnResponseType,
|
|
402
|
-
> = {
|
|
337
|
+
export type CompiledFetcherFn<TRegister, TResponse> = {
|
|
403
338
|
(
|
|
404
|
-
opts: CompiledFetcherFnOptions &
|
|
405
|
-
ServerFnBaseOptions<TRegister, Method, TServerFnResponseType>,
|
|
339
|
+
opts: CompiledFetcherFnOptions & ServerFnBaseOptions<TRegister, Method>,
|
|
406
340
|
): Promise<TResponse>
|
|
407
341
|
url: string
|
|
408
342
|
}
|
|
409
343
|
|
|
410
344
|
export type ServerFnBaseOptions<
|
|
411
|
-
TRegister
|
|
345
|
+
TRegister,
|
|
412
346
|
TMethod extends Method = 'GET',
|
|
413
|
-
TServerFnResponseType extends ServerFnResponseType = 'data',
|
|
414
347
|
TResponse = unknown,
|
|
415
348
|
TMiddlewares = unknown,
|
|
416
|
-
|
|
349
|
+
TInputValidator = unknown,
|
|
417
350
|
> = {
|
|
418
351
|
method: TMethod
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
352
|
+
middleware?: Constrain<
|
|
353
|
+
TMiddlewares,
|
|
354
|
+
ReadonlyArray<AnyFunctionMiddleware | AnyRequestMiddleware>
|
|
355
|
+
>
|
|
356
|
+
inputValidator?: ConstrainValidator<TRegister, TMethod, TInputValidator>
|
|
357
|
+
extractedFn?: CompiledFetcherFn<TRegister, TResponse>
|
|
424
358
|
serverFn?: ServerFn<
|
|
425
359
|
TRegister,
|
|
426
360
|
TMethod,
|
|
427
|
-
TServerFnResponseType,
|
|
428
361
|
TMiddlewares,
|
|
429
|
-
|
|
362
|
+
TInputValidator,
|
|
430
363
|
TResponse
|
|
431
364
|
>
|
|
432
365
|
functionId: string
|
|
433
366
|
}
|
|
434
367
|
|
|
435
368
|
export type ValidateValidatorInput<
|
|
436
|
-
TRegister
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
>
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
369
|
+
TRegister,
|
|
370
|
+
TMethod extends Method,
|
|
371
|
+
TInputValidator,
|
|
372
|
+
> = TMethod extends 'POST'
|
|
373
|
+
? ResolveValidatorInput<TInputValidator> extends FormData
|
|
374
|
+
? ResolveValidatorInput<TInputValidator>
|
|
375
|
+
: ValidateSerializable<
|
|
376
|
+
ResolveValidatorInput<TInputValidator>,
|
|
377
|
+
RegisteredSerializableInput<TRegister>
|
|
378
|
+
>
|
|
379
|
+
: ValidateSerializable<
|
|
380
|
+
ResolveValidatorInput<TInputValidator>,
|
|
381
|
+
RegisteredSerializableInput<TRegister>
|
|
382
|
+
>
|
|
383
|
+
|
|
384
|
+
export type ValidateValidator<
|
|
385
|
+
TRegister,
|
|
386
|
+
TMethod extends Method,
|
|
387
|
+
TInputValidator,
|
|
388
|
+
> =
|
|
389
|
+
ValidateValidatorInput<
|
|
390
|
+
TRegister,
|
|
391
|
+
TMethod,
|
|
392
|
+
TInputValidator
|
|
393
|
+
> extends infer TInput
|
|
445
394
|
? Validator<TInput, any>
|
|
446
395
|
: never
|
|
447
396
|
|
|
448
|
-
export type ConstrainValidator<
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
397
|
+
export type ConstrainValidator<
|
|
398
|
+
TRegister,
|
|
399
|
+
TMethod extends Method,
|
|
400
|
+
TInputValidator,
|
|
401
|
+
> =
|
|
402
|
+
| (unknown extends TInputValidator
|
|
403
|
+
? TInputValidator
|
|
404
|
+
: ResolveValidatorInput<TInputValidator> extends ValidateValidator<
|
|
452
405
|
TRegister,
|
|
453
|
-
|
|
406
|
+
TMethod,
|
|
407
|
+
TInputValidator
|
|
454
408
|
>
|
|
455
|
-
?
|
|
409
|
+
? TInputValidator
|
|
456
410
|
: never)
|
|
457
|
-
| ValidateValidator<TRegister,
|
|
411
|
+
| ValidateValidator<TRegister, TMethod, TInputValidator>
|
|
458
412
|
|
|
459
|
-
type
|
|
460
|
-
|
|
413
|
+
export type AppendMiddlewares<TMiddlewares, TNewMiddlewares> =
|
|
414
|
+
TMiddlewares extends ReadonlyArray<any>
|
|
415
|
+
? TNewMiddlewares extends ReadonlyArray<any>
|
|
416
|
+
? readonly [...TMiddlewares, ...TNewMiddlewares]
|
|
417
|
+
: TMiddlewares
|
|
418
|
+
: TNewMiddlewares
|
|
461
419
|
|
|
462
420
|
export interface ServerFnMiddleware<
|
|
463
|
-
TRegister
|
|
421
|
+
TRegister,
|
|
464
422
|
TMethod extends Method,
|
|
465
|
-
TServerFnResponseType extends ServerFnResponseType,
|
|
466
423
|
TMiddlewares,
|
|
467
|
-
|
|
424
|
+
TInputValidator,
|
|
468
425
|
> {
|
|
469
|
-
middleware: <const TNewMiddlewares
|
|
426
|
+
middleware: <const TNewMiddlewares>(
|
|
470
427
|
middlewares: Constrain<
|
|
471
428
|
TNewMiddlewares,
|
|
472
|
-
ReadonlyArray<AnyFunctionMiddleware>
|
|
429
|
+
ReadonlyArray<AnyFunctionMiddleware | AnyRequestMiddleware | AnyServerFn>
|
|
473
430
|
>,
|
|
474
431
|
) => ServerFnAfterMiddleware<
|
|
475
432
|
TRegister,
|
|
476
433
|
TMethod,
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
TValidator
|
|
434
|
+
AppendMiddlewares<TMiddlewares, TNewMiddlewares>,
|
|
435
|
+
TInputValidator
|
|
480
436
|
>
|
|
481
437
|
}
|
|
482
438
|
|
|
483
439
|
export interface ServerFnAfterMiddleware<
|
|
484
|
-
TRegister
|
|
440
|
+
TRegister,
|
|
485
441
|
TMethod extends Method,
|
|
486
|
-
TServerFnResponseType extends ServerFnResponseType,
|
|
487
442
|
TMiddlewares,
|
|
488
|
-
|
|
489
|
-
> extends
|
|
443
|
+
TInputValidator,
|
|
444
|
+
> extends ServerFnWithTypes<
|
|
490
445
|
TRegister,
|
|
491
446
|
TMethod,
|
|
492
|
-
TServerFnResponseType,
|
|
493
447
|
TMiddlewares,
|
|
448
|
+
TInputValidator,
|
|
494
449
|
undefined
|
|
495
450
|
>,
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
TServerFnResponseType,
|
|
501
|
-
TMiddlewares,
|
|
502
|
-
TValidator
|
|
503
|
-
> {
|
|
504
|
-
<
|
|
505
|
-
TNewMethod extends Method = TMethod,
|
|
506
|
-
TNewServerFnResponseType extends
|
|
507
|
-
ServerFnResponseType = TServerFnResponseType,
|
|
508
|
-
>(options?: {
|
|
451
|
+
ServerFnMiddleware<TRegister, TMethod, TMiddlewares, undefined>,
|
|
452
|
+
ServerFnValidator<TRegister, TMethod, TMiddlewares>,
|
|
453
|
+
ServerFnHandler<TRegister, TMethod, TMiddlewares, TInputValidator> {
|
|
454
|
+
<TNewMethod extends Method = TMethod>(options?: {
|
|
509
455
|
method?: TNewMethod
|
|
510
|
-
response?: TNewServerFnResponseType
|
|
511
456
|
}): ServerFnAfterMiddleware<
|
|
512
457
|
TRegister,
|
|
513
458
|
TNewMethod,
|
|
514
|
-
TNewServerFnResponseType,
|
|
515
459
|
TMiddlewares,
|
|
516
|
-
|
|
460
|
+
TInputValidator
|
|
517
461
|
>
|
|
518
462
|
}
|
|
519
463
|
|
|
520
|
-
export type ValidatorFn<
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
> = <TValidator>(
|
|
526
|
-
validator: ConstrainValidator<TRegister, TValidator>,
|
|
527
|
-
) => ServerFnAfterValidator<
|
|
528
|
-
TRegister,
|
|
529
|
-
TMethod,
|
|
530
|
-
TServerFnResponseType,
|
|
531
|
-
TMiddlewares,
|
|
532
|
-
TValidator
|
|
533
|
-
>
|
|
464
|
+
export type ValidatorFn<TRegister, TMethod extends Method, TMiddlewares> = <
|
|
465
|
+
TInputValidator,
|
|
466
|
+
>(
|
|
467
|
+
inputValidator: ConstrainValidator<TRegister, TMethod, TInputValidator>,
|
|
468
|
+
) => ServerFnAfterValidator<TRegister, TMethod, TMiddlewares, TInputValidator>
|
|
534
469
|
|
|
535
470
|
export interface ServerFnValidator<
|
|
536
|
-
TRegister
|
|
471
|
+
TRegister,
|
|
537
472
|
TMethod extends Method,
|
|
538
|
-
TServerFnResponseType extends ServerFnResponseType,
|
|
539
473
|
TMiddlewares,
|
|
540
474
|
> {
|
|
541
|
-
|
|
542
|
-
TRegister,
|
|
543
|
-
TMethod,
|
|
544
|
-
TServerFnResponseType,
|
|
545
|
-
TMiddlewares
|
|
546
|
-
>
|
|
475
|
+
inputValidator: ValidatorFn<TRegister, TMethod, TMiddlewares>
|
|
547
476
|
}
|
|
548
477
|
|
|
549
478
|
export interface ServerFnAfterValidator<
|
|
550
|
-
TRegister
|
|
479
|
+
TRegister,
|
|
551
480
|
TMethod extends Method,
|
|
552
|
-
TServerFnResponseType extends ServerFnResponseType,
|
|
553
481
|
TMiddlewares,
|
|
554
|
-
|
|
555
|
-
> extends
|
|
482
|
+
TInputValidator,
|
|
483
|
+
> extends ServerFnWithTypes<
|
|
556
484
|
TRegister,
|
|
557
485
|
TMethod,
|
|
558
|
-
TServerFnResponseType,
|
|
559
486
|
TMiddlewares,
|
|
560
|
-
|
|
487
|
+
TInputValidator,
|
|
488
|
+
undefined
|
|
561
489
|
>,
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
TMethod,
|
|
565
|
-
TServerFnResponseType,
|
|
566
|
-
TMiddlewares,
|
|
567
|
-
TValidator
|
|
568
|
-
> {}
|
|
490
|
+
ServerFnMiddleware<TRegister, TMethod, TMiddlewares, TInputValidator>,
|
|
491
|
+
ServerFnHandler<TRegister, TMethod, TMiddlewares, TInputValidator> {}
|
|
569
492
|
|
|
570
493
|
export interface ServerFnAfterTyper<
|
|
571
|
-
TRegister
|
|
494
|
+
TRegister,
|
|
572
495
|
TMethod extends Method,
|
|
573
|
-
TServerFnResponseType extends ServerFnResponseType,
|
|
574
496
|
TMiddlewares,
|
|
575
|
-
|
|
576
|
-
> extends
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
497
|
+
TInputValidator,
|
|
498
|
+
> extends ServerFnWithTypes<
|
|
499
|
+
TRegister,
|
|
500
|
+
TMethod,
|
|
501
|
+
TMiddlewares,
|
|
502
|
+
TInputValidator,
|
|
503
|
+
undefined
|
|
504
|
+
>,
|
|
505
|
+
ServerFnHandler<TRegister, TMethod, TMiddlewares, TInputValidator> {}
|
|
583
506
|
|
|
584
507
|
// Handler
|
|
585
508
|
export interface ServerFnHandler<
|
|
586
|
-
TRegister
|
|
509
|
+
TRegister,
|
|
587
510
|
TMethod extends Method,
|
|
588
|
-
TServerFnResponseType extends ServerFnResponseType,
|
|
589
511
|
TMiddlewares,
|
|
590
|
-
|
|
512
|
+
TInputValidator,
|
|
591
513
|
> {
|
|
592
514
|
handler: <TNewResponse>(
|
|
593
515
|
fn?: ServerFn<
|
|
594
516
|
TRegister,
|
|
595
517
|
TMethod,
|
|
596
|
-
TServerFnResponseType,
|
|
597
518
|
TMiddlewares,
|
|
598
|
-
|
|
519
|
+
TInputValidator,
|
|
599
520
|
TNewResponse
|
|
600
521
|
>,
|
|
601
|
-
) => Fetcher<
|
|
602
|
-
TRegister,
|
|
603
|
-
TMiddlewares,
|
|
604
|
-
TValidator,
|
|
605
|
-
TNewResponse,
|
|
606
|
-
TServerFnResponseType
|
|
607
|
-
>
|
|
522
|
+
) => Fetcher<TRegister, TMiddlewares, TInputValidator, TNewResponse>
|
|
608
523
|
}
|
|
609
524
|
|
|
610
|
-
export interface ServerFnBuilder<
|
|
611
|
-
|
|
612
|
-
TMethod extends Method = 'GET',
|
|
613
|
-
TServerFnResponseType extends ServerFnResponseType = 'data',
|
|
614
|
-
> extends ServerFnMiddleware<
|
|
525
|
+
export interface ServerFnBuilder<TRegister, TMethod extends Method = 'GET'>
|
|
526
|
+
extends ServerFnWithTypes<
|
|
615
527
|
TRegister,
|
|
616
528
|
TMethod,
|
|
617
|
-
TServerFnResponseType,
|
|
618
529
|
undefined,
|
|
619
|
-
undefined
|
|
620
|
-
>,
|
|
621
|
-
ServerFnValidator<TRegister, TMethod, TServerFnResponseType, undefined>,
|
|
622
|
-
ServerFnHandler<
|
|
623
|
-
TRegister,
|
|
624
|
-
TMethod,
|
|
625
|
-
TServerFnResponseType,
|
|
626
530
|
undefined,
|
|
627
531
|
undefined
|
|
628
|
-
|
|
532
|
+
>,
|
|
533
|
+
ServerFnMiddleware<TRegister, TMethod, undefined, undefined>,
|
|
534
|
+
ServerFnValidator<TRegister, TMethod, undefined>,
|
|
535
|
+
ServerFnHandler<TRegister, TMethod, undefined, undefined> {
|
|
629
536
|
options: ServerFnBaseOptions<
|
|
630
537
|
TRegister,
|
|
631
538
|
TMethod,
|
|
632
|
-
TServerFnResponseType,
|
|
633
539
|
unknown,
|
|
634
540
|
undefined,
|
|
635
541
|
undefined
|
|
636
542
|
>
|
|
637
543
|
}
|
|
638
544
|
|
|
639
|
-
export
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
545
|
+
export interface ServerFnWithTypes<
|
|
546
|
+
in out TRegister,
|
|
547
|
+
in out TMethod extends Method,
|
|
548
|
+
in out TMiddlewares,
|
|
549
|
+
in out TInputValidator,
|
|
550
|
+
in out TResponse,
|
|
551
|
+
> {
|
|
552
|
+
_types: ServerFnTypes<
|
|
553
|
+
TRegister,
|
|
554
|
+
TMethod,
|
|
555
|
+
TMiddlewares,
|
|
556
|
+
TInputValidator,
|
|
557
|
+
TResponse
|
|
558
|
+
>
|
|
559
|
+
options: ServerFnBaseOptions<
|
|
560
|
+
TRegister,
|
|
561
|
+
TMethod,
|
|
562
|
+
unknown,
|
|
563
|
+
undefined,
|
|
564
|
+
undefined
|
|
565
|
+
>
|
|
566
|
+
[TSS_SERVER_FUNCTION_FACTORY]: true
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
export type AnyServerFn = ServerFnWithTypes<any, any, any, any, any>
|
|
644
570
|
|
|
645
|
-
|
|
571
|
+
export interface ServerFnTypes<
|
|
572
|
+
in out TRegister,
|
|
573
|
+
in out TMethod extends Method,
|
|
574
|
+
in out TMiddlewares,
|
|
575
|
+
in out TInputValidator,
|
|
576
|
+
in out TResponse,
|
|
577
|
+
> {
|
|
578
|
+
method: TMethod
|
|
579
|
+
middlewares: TMiddlewares
|
|
580
|
+
inputValidator: TInputValidator
|
|
581
|
+
response: TResponse
|
|
582
|
+
allServerContext: AssignAllServerFnContext<TRegister, TMiddlewares>
|
|
583
|
+
allInput: IntersectAllValidatorInputs<TMiddlewares, TInputValidator>
|
|
584
|
+
allOutput: IntersectAllValidatorOutputs<TMiddlewares, TInputValidator>
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
export function flattenMiddlewares(
|
|
588
|
+
middlewares: Array<AnyFunctionMiddleware | AnyRequestMiddleware>,
|
|
589
|
+
): Array<AnyFunctionMiddleware | AnyRequestMiddleware> {
|
|
590
|
+
const seen = new Set<AnyFunctionMiddleware | AnyRequestMiddleware>()
|
|
591
|
+
const flattened: Array<AnyFunctionMiddleware | AnyRequestMiddleware> = []
|
|
592
|
+
|
|
593
|
+
const recurse = (
|
|
594
|
+
middleware: Array<AnyFunctionMiddleware | AnyRequestMiddleware>,
|
|
595
|
+
) => {
|
|
646
596
|
middleware.forEach((m) => {
|
|
647
597
|
if (m.options.middleware) {
|
|
648
598
|
recurse(m.options.middleware)
|
|
@@ -662,14 +612,12 @@ export function flattenMiddlewares(
|
|
|
662
612
|
|
|
663
613
|
export type ServerFnMiddlewareOptions = {
|
|
664
614
|
method: Method
|
|
665
|
-
response?: ServerFnResponseType
|
|
666
615
|
data: any
|
|
667
616
|
headers?: HeadersInit
|
|
668
617
|
signal?: AbortSignal
|
|
669
618
|
sendContext?: any
|
|
670
619
|
context?: any
|
|
671
620
|
functionId: string
|
|
672
|
-
router?: AnyRouter
|
|
673
621
|
}
|
|
674
622
|
|
|
675
623
|
export type ServerFnMiddlewareResult = ServerFnMiddlewareOptions & {
|
|
@@ -713,7 +661,7 @@ export const applyMiddleware = async (
|
|
|
713
661
|
result:
|
|
714
662
|
userCtx.result !== undefined
|
|
715
663
|
? userCtx.result
|
|
716
|
-
:
|
|
664
|
+
: userCtx instanceof Response
|
|
717
665
|
? userCtx
|
|
718
666
|
: (ctx as any).result,
|
|
719
667
|
error: userCtx.error ?? (ctx as any).error,
|
|
@@ -751,14 +699,13 @@ export function execValidator(
|
|
|
751
699
|
throw new Error('Invalid validator type!')
|
|
752
700
|
}
|
|
753
701
|
|
|
754
|
-
|
|
755
|
-
options: ServerFnBaseOptions<any, any, any, any, any
|
|
702
|
+
function serverFnBaseToMiddleware(
|
|
703
|
+
options: ServerFnBaseOptions<any, any, any, any, any>,
|
|
756
704
|
): AnyFunctionMiddleware {
|
|
757
705
|
return {
|
|
758
706
|
_types: undefined!,
|
|
759
707
|
options: {
|
|
760
|
-
|
|
761
|
-
validateClient: options.validateClient,
|
|
708
|
+
inputValidator: options.inputValidator,
|
|
762
709
|
client: async ({ next, sendContext, ...ctx }) => {
|
|
763
710
|
const payload = {
|
|
764
711
|
...ctx,
|
|
@@ -787,6 +734,7 @@ export function serverFnBaseToMiddleware(
|
|
|
787
734
|
any,
|
|
788
735
|
any,
|
|
789
736
|
any,
|
|
737
|
+
any,
|
|
790
738
|
any
|
|
791
739
|
>
|
|
792
740
|
},
|