T
TanStack3mo ago
helpful-purple

createServerFileRoute with different middleware for GET and POST

I am trying to create an app with an API that uses a different middleware for GET and POST. I am using the example code from the docs. My code looks like this
export const ServerRoute = createServerFileRoute(
"/api/actor/$id"
).methods((api) => ({
GET: api
.middleware([checkApiAccess(AccessLevel.Read)])
.handler(async ({ params }) => {
let companies = await getActor(params.id);
return json(companies);
}),
}));
export const ServerRoute = createServerFileRoute(
"/api/actor/$id"
).methods((api) => ({
GET: api
.middleware([checkApiAccess(AccessLevel.Read)])
.handler(async ({ params }) => {
let companies = await getActor(params.id);
return json(companies);
}),
}));
I have removed the POST for brevity. When I use this code, I get the error TypeError: handler is not a function. Can somebody explain what I am doing wrong because I feel that having different middlewares to control access to GET and POST endpoints should be a regular use-case.
Server Routes | TanStack Start React Docs
// TODO: Add redirect from api-routes to server-routes Server routes are a powerful feature of TanStack Start that allow you to create server-side endpoints in your application and are useful for hand...
3 Replies
optimistic-gold
optimistic-gold3mo ago
might be a bug can you please create a github issue including a reproducer?
helpful-purple
helpful-purpleOP3mo ago
Sure, I'll try and get something up now. Give me a moment.
helpful-purple
helpful-purpleOP3mo ago
I have created https://github.com/TanStack/router/issues/4490, hopefully I can get some help with this soon.
GitHub
TanStack/router
🤖 Fully typesafe Router for React (and friends) w/ built-in caching, 1st class search-param APIs, client-side cache integration and isomorphic rendering. - TanStack/router

Did you find this page helpful?