import { createFileRoute } from '@tanstack/react-router'
import type { AnyRoute, Register } from '@tanstack/react-router'
import type { RouteMethodHandlerCtx } from '@tanstack/react-start'
const handlerFactory = <
TCtx extends RouteMethodHandlerCtx<
Register,
AnyRoute,
'/demo/api/endpoint-factory',
unknown,
any
>,
>(
handler: (ctx: TCtx, headerId: string) => Response | Promise<Response>,
) => {
return async (ctx: TCtx) => {
const headerId = ctx.request.headers.get('x-custom-id') || 'no-id'
return handler(ctx, headerId)
}
}
export const Route = createFileRoute('/demo/api/endpoint-factory')({
server: {
handlers: {
POST: (ctx) => {
const headerId = ctx.request.headers.get('x-custom-id') || 'no-id'
return new Response(
`Hello from endpoint factory! Header ID: ${headerId}`,
)
},
GET: handlerFactory((ctx, headerId) => {
return new Response(
`Hello from endpoint factory! Header ID: ${headerId}`,
)
}),
},
},
})
import { createFileRoute } from '@tanstack/react-router'
import type { AnyRoute, Register } from '@tanstack/react-router'
import type { RouteMethodHandlerCtx } from '@tanstack/react-start'
const handlerFactory = <
TCtx extends RouteMethodHandlerCtx<
Register,
AnyRoute,
'/demo/api/endpoint-factory',
unknown,
any
>,
>(
handler: (ctx: TCtx, headerId: string) => Response | Promise<Response>,
) => {
return async (ctx: TCtx) => {
const headerId = ctx.request.headers.get('x-custom-id') || 'no-id'
return handler(ctx, headerId)
}
}
export const Route = createFileRoute('/demo/api/endpoint-factory')({
server: {
handlers: {
POST: (ctx) => {
const headerId = ctx.request.headers.get('x-custom-id') || 'no-id'
return new Response(
`Hello from endpoint factory! Header ID: ${headerId}`,
)
},
GET: handlerFactory((ctx, headerId) => {
return new Response(
`Hello from endpoint factory! Header ID: ${headerId}`,
)
}),
},
},
})