Skip to content
/ router Public

a typesafe router for plusnew

License

Notifications You must be signed in to change notification settings

plusnew/router

Repository files navigation

plusnew-router Build Status Coverage Status

This router makes complete typesafety possible. At compile and runtime the typesafety is guaranteed, for the Route-Components and also the Links.

import { createRootRoute, schema } from "@plusnew/router";

const rootRoute = createRootRoute(
  // Defines what parameter the route can have
  {
    size: schema.number({ default: 20 }), // This parameter is optional and has a default of 20
    sortOrder: schema.string({
      default: "desc",
      validate: (value) => value === "asc" || value === "desc", // Creates typesafety of only allowing the literals "asc" and "desc"
    }),
  },
);

const childRoute = rootRoute.createChildRoute("childRouteName", {
  nestedParameter: schema.object({
    id: schema.number(),
  }),
});

const url = childRoute.createPath({
  "/": {
    size: 20,
    sortOrder: null, // "asc" | "desc" | null
  },
  childRouteName: { nestedParameter: { id: 5 } },
});

document.body.innerHTML =
  rootRoute.map(url, ({ hasChildRouteActive, parameter }) =>
    hasChildRouteActive
      ? childRoute.map(url, ({ hasChildRouteActive, parameter }) =>
          hasChildRouteActive
            ? null
            : `${parameter["/"].size} + ${parameter.childRouteName.nestedParameter.id}`,
        )
      : `Welcome ${parameter["/"].size}`,
  ) ?? "not found";

About

a typesafe router for plusnew

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages