npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

next-routify

v1.5.3

Published

Routing alternative for safe urls with typed parameters

Downloads

74

Readme

Next Routify

Build Status

Routing alternative for safe urls with typed parameters.

  • Define routes with or without parameters
  • Generate urls for links
  • Integrated request handler
  • Retrieve parameters from path separately from query

Setup with typescript

Check out the example app

Install it:

npm install --save next-routify ts-node

yarn add next-routify ts-node

Create routes.ts with your routes in root directory:

import { route, simple, bundle, router, routeLinkBuilder } from "next-routify";
const { routes, flattenRoutes } = router(
  // Define root route
  bundle(simple(""), {
    // Route with parameter id
    article: route<{ id: number }>("article/:id"),
    // Route without parameters
    user: simple("user"),
    // Nested routes
    admin: bundle(simple("admin"), {
      article: route<{ id: number }>("article/:id"),
      user: simple("user")
    })
  })
);

export const {
  // Alternative for Next Link
  RouteLink,
  // Redirect components that redirects on render
  Redirect,
  // Component that adds class to children component if is user on page
  IsUrlActive,
  // function to check whether is user on page
  isUrlActive
} = routifyBuilder(flattenRoutes);

Create server.ts in root directory:

import { createServer } from "http";
import * as next from "next";
import { requestHandler } from "next-routify";
import { flattenRoutes } from "./routes";

const app = next({
  dev: process.env.NODE_ENV !== "production"
});

app.prepare().then(() => {
  createServer(requestHandler(app, flattenRoutes))
    .addListener("error", err => {
      console.log(err);
      throw err;
    })
    .listen(3000, () => {
      console.log("> Ready on http://localhost:3000");
    });
});

Create tsconfig.server.json

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    "module": "commonjs",
    "target": "es2017",
    "lib": ["dom", "es2017"]
  }
}

Add script to your package.json:

{
  "scripts": {
    "next": "ts-node --project tsconfig.server.json server.ts"
  }
}

Now you can run server simply with:

yarn next

Get url

Get url as string:

// /
routes().url;

// /article/5
routes().article({ id: 5 }).url;

// /user
routes().user().url;

// /admin
routes().admin().url;

// /admin/article/5
routes()
  .admin()
  .article({ id: 5 }).url;

// /admin/user
routes()
  .admin()
  .user().url;

Create link to a route:

<RouteLink to={routes().article({ id: 5 })}>
  <a>Click here</a>
</RouteLink>

Retrieve parameters from path

interface WithRoutify<P = {}, Q = {}> extends WithRouterProps<Q> {
  parameters: P;
  query: Q;
}
withRouter(flattenRoutes)(Component);
import { withRoutify, WithRoutify } from "next-routify";
import { flattenRoutes, RouteLink, routes } from "routes";

// Every parameter is retrieved as string
const Component: React.FC<WithRoutify<{ id: string }>> = ({ parameters, query, router }) => {
  console.log(parameters.id);

  return (
    ...
  );
};

export default withRoutify(flattenRoutes)(Article);

Check whether an url is active

Inject class to its child:

<IsActive url={routes.url({}).article({ id: 5 })}>
  <li class="nav-item">
    <RouteLink to={routes.url({}).article({ id: 5 })}>
      <a>Article 5</a>
    </RouteLink>
  </li>
</IsActive>

API:

interface IsActiveProps {
  url: RouteUrl; // Url to check
  active?: string; // Class to inject (default: active)
  children: React.ReactElement; // Single react element
}

Use with Routelink

<RouteLink to={routes().article({ id: 5 })} active="active">
  <a>Click here</a>
</RouteLink>