T
TanStack3mo ago
secure-lavender

how to get router in server function

I need build a typesafe url to be sended by email in a server function in a component I can use useRouter().buildLocation({ to: "/auth/sign-up/confirm" }).href; but how to do that in a server function? thanks
2 Replies
exotic-emerald
exotic-emerald3mo ago
we have not yet exposed such an API but we will add it
secure-lavender
secure-lavenderOP3mo ago
thanks, I let a work around to solve this
import type { LinkProps, RegisteredRouter } from "@tanstack/react-router";

type CreateUrl<TTo extends string> = LinkProps<
unknown,
RegisteredRouter,
string,
TTo,
string,
string
> & {
to: TTo;
};

export function createUrl<TTo extends string>(props: CreateUrl<TTo>) {
const domain = process.env.DOMAIN;

const url = new URL(props.to, domain);
Object.entries(props.search ?? {}).forEach(([key, value]) => {
url.searchParams.set(key, value);
});
return url.toString();
}
import type { LinkProps, RegisteredRouter } from "@tanstack/react-router";

type CreateUrl<TTo extends string> = LinkProps<
unknown,
RegisteredRouter,
string,
TTo,
string,
string
> & {
to: TTo;
};

export function createUrl<TTo extends string>(props: CreateUrl<TTo>) {
const domain = process.env.DOMAIN;

const url = new URL(props.to, domain);
Object.entries(props.search ?? {}).forEach(([key, value]) => {
url.searchParams.set(key, value);
});
return url.toString();
}

Did you find this page helpful?