Skip to content

Commit bbfab37

Browse files
feat: serverfn abstraction (#5040)
1 parent 840d280 commit bbfab37

File tree

27 files changed

+1307
-166
lines changed

27 files changed

+1307
-166
lines changed

e2e/react-start/server-functions/src/routeTree.gen.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { Route as AbortSignalRouteImport } from './routes/abort-signal'
2424
import { Route as IndexRouteImport } from './routes/index'
2525
import { Route as MiddlewareIndexRouteImport } from './routes/middleware/index'
2626
import { Route as FormdataRedirectIndexRouteImport } from './routes/formdata-redirect/index'
27+
import { Route as FactoryIndexRouteImport } from './routes/factory/index'
2728
import { Route as CookiesIndexRouteImport } from './routes/cookies/index'
2829
import { Route as MiddlewareSendServerFnRouteImport } from './routes/middleware/send-serverFn'
2930
import { Route as MiddlewareClientMiddlewareRouterRouteImport } from './routes/middleware/client-middleware-router'
@@ -105,6 +106,11 @@ const FormdataRedirectIndexRoute = FormdataRedirectIndexRouteImport.update({
105106
path: '/formdata-redirect/',
106107
getParentRoute: () => rootRouteImport,
107108
} as any)
109+
const FactoryIndexRoute = FactoryIndexRouteImport.update({
110+
id: '/factory/',
111+
path: '/factory/',
112+
getParentRoute: () => rootRouteImport,
113+
} as any)
108114
const CookiesIndexRoute = CookiesIndexRouteImport.update({
109115
id: '/cookies/',
110116
path: '/cookies/',
@@ -151,6 +157,7 @@ export interface FileRoutesByFullPath {
151157
'/middleware/client-middleware-router': typeof MiddlewareClientMiddlewareRouterRoute
152158
'/middleware/send-serverFn': typeof MiddlewareSendServerFnRoute
153159
'/cookies': typeof CookiesIndexRoute
160+
'/factory': typeof FactoryIndexRoute
154161
'/formdata-redirect': typeof FormdataRedirectIndexRoute
155162
'/middleware': typeof MiddlewareIndexRoute
156163
'/formdata-redirect/target/$name': typeof FormdataRedirectTargetNameRoute
@@ -173,6 +180,7 @@ export interface FileRoutesByTo {
173180
'/middleware/client-middleware-router': typeof MiddlewareClientMiddlewareRouterRoute
174181
'/middleware/send-serverFn': typeof MiddlewareSendServerFnRoute
175182
'/cookies': typeof CookiesIndexRoute
183+
'/factory': typeof FactoryIndexRoute
176184
'/formdata-redirect': typeof FormdataRedirectIndexRoute
177185
'/middleware': typeof MiddlewareIndexRoute
178186
'/formdata-redirect/target/$name': typeof FormdataRedirectTargetNameRoute
@@ -196,6 +204,7 @@ export interface FileRoutesById {
196204
'/middleware/client-middleware-router': typeof MiddlewareClientMiddlewareRouterRoute
197205
'/middleware/send-serverFn': typeof MiddlewareSendServerFnRoute
198206
'/cookies/': typeof CookiesIndexRoute
207+
'/factory/': typeof FactoryIndexRoute
199208
'/formdata-redirect/': typeof FormdataRedirectIndexRoute
200209
'/middleware/': typeof MiddlewareIndexRoute
201210
'/formdata-redirect/target/$name': typeof FormdataRedirectTargetNameRoute
@@ -220,6 +229,7 @@ export interface FileRouteTypes {
220229
| '/middleware/client-middleware-router'
221230
| '/middleware/send-serverFn'
222231
| '/cookies'
232+
| '/factory'
223233
| '/formdata-redirect'
224234
| '/middleware'
225235
| '/formdata-redirect/target/$name'
@@ -242,6 +252,7 @@ export interface FileRouteTypes {
242252
| '/middleware/client-middleware-router'
243253
| '/middleware/send-serverFn'
244254
| '/cookies'
255+
| '/factory'
245256
| '/formdata-redirect'
246257
| '/middleware'
247258
| '/formdata-redirect/target/$name'
@@ -264,6 +275,7 @@ export interface FileRouteTypes {
264275
| '/middleware/client-middleware-router'
265276
| '/middleware/send-serverFn'
266277
| '/cookies/'
278+
| '/factory/'
267279
| '/formdata-redirect/'
268280
| '/middleware/'
269281
| '/formdata-redirect/target/$name'
@@ -287,6 +299,7 @@ export interface RootRouteChildren {
287299
MiddlewareClientMiddlewareRouterRoute: typeof MiddlewareClientMiddlewareRouterRoute
288300
MiddlewareSendServerFnRoute: typeof MiddlewareSendServerFnRoute
289301
CookiesIndexRoute: typeof CookiesIndexRoute
302+
FactoryIndexRoute: typeof FactoryIndexRoute
290303
FormdataRedirectIndexRoute: typeof FormdataRedirectIndexRoute
291304
MiddlewareIndexRoute: typeof MiddlewareIndexRoute
292305
FormdataRedirectTargetNameRoute: typeof FormdataRedirectTargetNameRoute
@@ -399,6 +412,13 @@ declare module '@tanstack/react-router' {
399412
preLoaderRoute: typeof FormdataRedirectIndexRouteImport
400413
parentRoute: typeof rootRouteImport
401414
}
415+
'/factory/': {
416+
id: '/factory/'
417+
path: '/factory'
418+
fullPath: '/factory'
419+
preLoaderRoute: typeof FactoryIndexRouteImport
420+
parentRoute: typeof rootRouteImport
421+
}
402422
'/cookies/': {
403423
id: '/cookies/'
404424
path: '/cookies'
@@ -455,6 +475,7 @@ const rootRouteChildren: RootRouteChildren = {
455475
MiddlewareClientMiddlewareRouterRoute: MiddlewareClientMiddlewareRouterRoute,
456476
MiddlewareSendServerFnRoute: MiddlewareSendServerFnRoute,
457477
CookiesIndexRoute: CookiesIndexRoute,
478+
FactoryIndexRoute: FactoryIndexRoute,
458479
FormdataRedirectIndexRoute: FormdataRedirectIndexRoute,
459480
MiddlewareIndexRoute: MiddlewareIndexRoute,
460481
FormdataRedirectTargetNameRoute: FormdataRedirectTargetNameRoute,
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { createMiddleware } from '@tanstack/react-start'
2+
import { createFooServerFn } from './createFooServerFn'
3+
4+
const barMiddleware = createMiddleware({ type: 'function' }).server(
5+
({ next }) => {
6+
console.log('Bar middleware triggered')
7+
return next({
8+
context: { bar: 'bar' } as const,
9+
})
10+
},
11+
)
12+
13+
export const createBarServerFn = createFooServerFn().middleware([barMiddleware])
14+
15+
export const barFnInsideFactoryFile = createBarServerFn().handler(
16+
({ context }) => {
17+
return {
18+
name: 'barFnInsideFactoryFile',
19+
context,
20+
}
21+
},
22+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export function createFakeFn() {
2+
return {
3+
handler: (cb: () => Promise<any>) => cb,
4+
}
5+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { createMiddleware, createServerFn } from '@tanstack/react-start'
2+
3+
const fooMiddleware = createMiddleware({ type: 'function' }).server(
4+
({ next }) => {
5+
console.log('Foo middleware triggered')
6+
return next({
7+
context: { foo: 'foo' } as const,
8+
})
9+
},
10+
)
11+
12+
export const createFooServerFn = createServerFn().middleware([fooMiddleware])
13+
14+
export const fooFnInsideFactoryFile = createFooServerFn().handler(
15+
async ({ context, method }) => {
16+
console.log('fooFnInsideFactoryFile handler triggered', method)
17+
return {
18+
name: 'fooFnInsideFactoryFile',
19+
context,
20+
}
21+
},
22+
)
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { createMiddleware } from '@tanstack/react-start'
2+
import { createBarServerFn } from './createBarServerFn'
3+
import { createFooServerFn } from './createFooServerFn'
4+
import { createFakeFn } from './createFakeFn'
5+
6+
export const fooFn = createFooServerFn().handler(({ context }) => {
7+
return {
8+
name: 'fooFn',
9+
context,
10+
}
11+
})
12+
13+
export const fooFnPOST = createFooServerFn({ method: 'POST' }).handler(
14+
({ context }) => {
15+
return {
16+
name: 'fooFnPOST',
17+
context,
18+
}
19+
},
20+
)
21+
22+
export const barFn = createBarServerFn().handler(({ context }) => {
23+
return {
24+
name: 'barFn',
25+
context,
26+
}
27+
})
28+
29+
export const barFnPOST = createBarServerFn({ method: 'POST' }).handler(
30+
({ context }) => {
31+
return {
32+
name: 'barFnPOST',
33+
context,
34+
}
35+
},
36+
)
37+
38+
const localMiddleware = createMiddleware({ type: 'function' }).server(
39+
({ next }) => {
40+
console.log('local middleware triggered')
41+
return next({
42+
context: { local: 'local' } as const,
43+
})
44+
},
45+
)
46+
47+
const localFnFactory = createBarServerFn.middleware([localMiddleware])
48+
49+
const anotherMiddleware = createMiddleware({ type: 'function' }).server(
50+
({ next }) => {
51+
console.log('another middleware triggered')
52+
return next({
53+
context: { another: 'another' } as const,
54+
})
55+
},
56+
)
57+
58+
export const localFn = localFnFactory()
59+
.middleware([anotherMiddleware])
60+
.handler(({ context }) => {
61+
return {
62+
name: 'localFn',
63+
context,
64+
}
65+
})
66+
67+
export const localFnPOST = localFnFactory({ method: 'POST' })
68+
.middleware([anotherMiddleware])
69+
.handler(({ context }) => {
70+
return {
71+
name: 'localFnPOST',
72+
context,
73+
}
74+
})
75+
76+
export const fakeFn = createFakeFn().handler(async () => {
77+
return {
78+
name: 'fakeFn',
79+
window,
80+
}
81+
})

0 commit comments

Comments
 (0)