@tanstack/router-core
Version:
Modern and scalable routing for React applications
95 lines (94 loc) • 2.76 kB
JavaScript
import { joinPaths, trimPathLeft, trimPathRight } from "./path.js";
import { notFound } from "./not-found.js";
import { rootRouteId } from "./root.js";
import { redirect } from "./redirect.js";
import invariant from "tiny-invariant";
//#region src/route.ts
var BaseRoute = class {
get to() {
return this._to;
}
get id() {
return this._id;
}
get path() {
return this._path;
}
get fullPath() {
return this._fullPath;
}
constructor(options) {
this.init = (opts) => {
this.originalIndex = opts.originalIndex;
const options = this.options;
const isRoot = !options?.path && !options?.id;
this.parentRoute = this.options.getParentRoute?.();
if (isRoot) this._path = rootRouteId;
else if (!this.parentRoute) invariant(false, `Child Route instances must pass a 'getParentRoute: () => ParentRoute' option that returns a Route instance.`);
let path = isRoot ? rootRouteId : options?.path;
if (path && path !== "/") path = trimPathLeft(path);
const customId = options?.id || path;
let id = isRoot ? rootRouteId : joinPaths([this.parentRoute.id === "__root__" ? "" : this.parentRoute.id, customId]);
if (path === "__root__") path = "/";
if (id !== "__root__") id = joinPaths(["/", id]);
const fullPath = id === "__root__" ? "/" : joinPaths([this.parentRoute.fullPath, path]);
this._path = path;
this._id = id;
this._fullPath = fullPath;
this._to = trimPathRight(fullPath);
};
this.addChildren = (children) => {
return this._addFileChildren(children);
};
this._addFileChildren = (children) => {
if (Array.isArray(children)) this.children = children;
if (typeof children === "object" && children !== null) this.children = Object.values(children);
return this;
};
this._addFileTypes = () => {
return this;
};
this.updateLoader = (options) => {
Object.assign(this.options, options);
return this;
};
this.update = (options) => {
Object.assign(this.options, options);
return this;
};
this.lazy = (lazyFn) => {
this.lazyFn = lazyFn;
return this;
};
this.redirect = (opts) => redirect({
from: this.fullPath,
...opts
});
this.options = options || {};
this.isRoot = !options?.getParentRoute;
if (options?.id && options?.path) throw new Error(`Route cannot have both an 'id' and a 'path' option.`);
}
};
var BaseRouteApi = class {
constructor({ id }) {
this.notFound = (opts) => {
return notFound({
routeId: this.id,
...opts
});
};
this.redirect = (opts) => redirect({
from: this.id,
...opts
});
this.id = id;
}
};
var BaseRootRoute = class extends BaseRoute {
constructor(options) {
super(options);
}
};
//#endregion
export { BaseRootRoute, BaseRoute, BaseRouteApi };
//# sourceMappingURL=route.js.map