56 lines
2.3 KiB
Plaintext
56 lines
2.3 KiB
Plaintext
//#region src/redirect.ts
|
|
/**
|
|
* Create a redirect Response understood by TanStack Router.
|
|
*
|
|
* Use from route `loader`/`beforeLoad` or server functions to trigger a
|
|
* navigation. If `throw: true` is set, the redirect is thrown instead of
|
|
* returned. When an absolute `href` is supplied and `reloadDocument` is not
|
|
* set, a full-document navigation is inferred.
|
|
*
|
|
* @param opts Options for the redirect. Common fields:
|
|
* - `href`: absolute URL for external redirects; infers `reloadDocument`.
|
|
* - `statusCode`: HTTP status code to use (defaults to 307).
|
|
* - `headers`: additional headers to include on the Response.
|
|
* - Standard navigation options like `to`, `params`, `search`, `replace`,
|
|
* and `reloadDocument` for internal redirects.
|
|
* @returns A Response augmented with router navigation options.
|
|
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/redirectFunction
|
|
*/
|
|
function redirect(opts) {
|
|
opts.statusCode = opts.statusCode || opts.code || 307;
|
|
if (!opts._builtLocation && !opts.reloadDocument && typeof opts.href === "string") try {
|
|
new URL(opts.href);
|
|
opts.reloadDocument = true;
|
|
} catch {}
|
|
const headers = new Headers(opts.headers);
|
|
if (opts.href && headers.get("Location") === null) headers.set("Location", opts.href);
|
|
const response = new Response(null, {
|
|
status: opts.statusCode,
|
|
headers
|
|
});
|
|
response.options = opts;
|
|
if (opts.throw) throw response;
|
|
return response;
|
|
}
|
|
/** Check whether a value is a TanStack Router redirect Response. */
|
|
/** Check whether a value is a TanStack Router redirect Response. */
|
|
function isRedirect(obj) {
|
|
return obj instanceof Response && !!obj.options;
|
|
}
|
|
/** True if value is a redirect with a resolved `href` location. */
|
|
/** True if value is a redirect with a resolved `href` location. */
|
|
function isResolvedRedirect(obj) {
|
|
return isRedirect(obj) && !!obj.options.href;
|
|
}
|
|
/** Parse a serialized redirect object back into a redirect Response. */
|
|
/** Parse a serialized redirect object back into a redirect Response. */
|
|
function parseRedirect(obj) {
|
|
if (obj !== null && typeof obj === "object" && obj.isSerializedRedirect) return redirect(obj);
|
|
}
|
|
//#endregion
|
|
exports.isRedirect = isRedirect;
|
|
exports.isResolvedRedirect = isResolvedRedirect;
|
|
exports.parseRedirect = parseRedirect;
|
|
exports.redirect = redirect;
|
|
|
|
//# sourceMappingURL=redirect.cjs.map |