T
TanStack•7mo ago
robust-apricot

recommended way to redirect from api route?

i don't see any obvious way to do this. best thing i've come up with thus far without causing a 503 and/or just not executing the redirect is
return new Response(null, {
status: 302,
headers: {
Location: "/",
},
});
return new Response(null, {
status: 302,
headers: {
Location: "/",
},
});
is there any way to do this from within the start api itself?
15 Replies
modern-teal
modern-teal•7mo ago
i dont think we have any helper for that. you could maybe use https://h3.unjs.io/utils/response#sendredirectevent-location-code
Response - h3
Utilities to send response headers and data
robust-apricot
robust-apricotOP•7mo ago
Ok thanks. Sorry, I've read the docs but there's a lot of stuff going on beneath the hood- where does h3 fit into this? vite => vinxi => nitro => ?? => tanstack router => start? or something like this
modern-teal
modern-teal•7mo ago
start uses nitro which uses h3 but sending a response as you did above looks perfectly fine to me
robust-apricot
robust-apricotOP•7mo ago
yea i mostly just wanted to get typesafety like i can in serverFn's, but that's fine no big deal
modern-teal
modern-teal•7mo ago
inside of an API route you deal with "low level" stuff such as response, so that's ok there I'd say typesafety in which way?
robust-apricot
robust-apricotOP•7mo ago
i guess typesafety isn't the right word, type inference rather
modern-teal
modern-teal•7mo ago
but for what?
robust-apricot
robust-apricotOP•7mo ago
for the route to redirect to
modern-teal
modern-teal•7mo ago
what would you redirect to? a route that is handled by router/start?
robust-apricot
robust-apricotOP•7mo ago
yea i'm redirecting to my root route basically /
modern-teal
modern-teal•7mo ago
hm quite the niche use case i'd say. if you need to redirect to other routes than / (with typesafety) you could extract the route paths from the route tree
robust-apricot
robust-apricotOP•7mo ago
Yea it's not really a big deal it's mostly a matter of, an upside of the router is getting inference everywhere, and always falling back to a start/router api to do things like this so it feels odd to have this one case (api routes) where it's not possible but niche nonetheless
environmental-rose
environmental-rose•7mo ago
yeah i had that issue too. Needed to redirect to a route handled by Start with some search params. It gets quite messy 😅
yappiest-sapphire
yappiest-sapphire•7mo ago
Yeah me too
mute-gold
mute-gold•6mo ago
I'm facing this case with supabase auth. When a user register, we want to confirm the email, we need to redirect to an api route. So I'm doing something like this:
export const APIRoute = createAPIFileRoute("/api/confirm")({
GET: async ({ request, params }) => {
const url = new URL(request.url);
const token_hash = url.searchParams.get("token_hash");
const type = url.searchParams.get("type") as EmailOtpType;

if (token_hash && type) {
const supabase = getSupabaseServerClient();
const { error } = await supabase.auth.verifyOtp({
token_hash,
type: type,
});

if (error) {
return json({ error: error.message }, { status: 400 });
}
}

const redirect = new URL("/account", request.url);
return Response.redirect(redirect, 302);
},
});
export const APIRoute = createAPIFileRoute("/api/confirm")({
GET: async ({ request, params }) => {
const url = new URL(request.url);
const token_hash = url.searchParams.get("token_hash");
const type = url.searchParams.get("type") as EmailOtpType;

if (token_hash && type) {
const supabase = getSupabaseServerClient();
const { error } = await supabase.auth.verifyOtp({
token_hash,
type: type,
});

if (error) {
return json({ error: error.message }, { status: 400 });
}
}

const redirect = new URL("/account", request.url);
return Response.redirect(redirect, 302);
},
});
It works, but I don't know if its right. but I think it could be good to be able to use redirect from tanstack . in other framework it seems to be possible (next - RR7) I can also do this in a server function but I think api route is the way to go (at least this is how they do in supabase docs in next)

Did you find this page helpful?