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

@juliushuck/generate-route

v3.0.0

Published

This is a function that can generate all kinds of routes and sub routes. It can be used to generate routes for express routers, react routers or to generate urls for api calls.

Downloads

13

Readme

generate-route

This package can generate route structures and build routes. It supports sub routes, parameter placeholders and query parameters. It can be used for APIs, SPAs, apps, etc.

All features

  • Generate route structures
  • Build routes with parameter placeholders
  • Build routes with values for the parameter placeholders
  • Build not to the lowest level
  • Build not to the highest level
  • Build routes with query parameters

Get started

1. Install the package

npm i @juliushuck/generate-route

2. Import the generate function

import { generateRoute } from "@juliushuck/generate-route";
// or
const { generateRoute } = require("@juliushuck/generate-route");

3. Generate your route structure

app.com // Highest level route
    └── posts
        ├── (getAll)
        └── :postId (getById)
            └── comments
                └── :commentId (getById) // Lowest level route
const routes = generateRoute(["app.com"], { // Highest level route
  posts: generateRoute(["posts"], {
    getAll: generateRoute(),
    getById: generateRoute([":postId"], {
      comments: generateRoute(["comments"], {
        getById: generateRoute([":commentId"]), // Lowest level route
      }),
    }),
  }),
})(); // <-- Do not forget to call the first generate route function

The generate route function accepts segments and subroutes. Segments can be a static ones or parameter placeholders. A parameter placeholder starts with :.

When you look closely, you can see that posts has an additional route called getAll. This route has no segments and is not really needed. But I always add it anyways in API routes, because functions should describe actions and for that need a verb.

4. Build routes

// Build routes without values for the parameter placeholders
// app.com/posts/:postId/comments/:commentId
routes().posts().getById().comments().getById().build();

// Build routes with values for the parameter placeholders
// app.com/posts/123/comments/456
routes()
  .posts()
  .getById(false, { postId: 123 })
  .comments()
  .getById(false, { commentId: 456 })
  .build();

// Build not to the lowest level route, by calling build on one of the higher level routes
// app.com/posts/:postId
routes().posts().getById().build();

// Build not to the highest level route, by setting isRootsParent to true on one of the lower level routes
// /:postId/comments/:commentId
routes().posts(true).getById().comments().getById().build();

// Add query parameters
// app.com/posts?page=abc&pageSize=abc
routes().posts().build({ page: "abc", pageSize: "abc" });

For more examples, please have a look at the test/tests.js file.

When you have a frontend and a backend, I recommend you to create two files. Then generate the routes for the frontend in one file and for the backend in the other one. Then export the routes from the files and share the files between the two projects in a shared git submodule or so.

When you need examples for the usage in express or react, please create an issue on GitHub. But basically you generate the routes not to the highest level and do not pass values for the parameter placeholders.

Roadmap

  • Cover multiple parameter placeholders in one route with tests.
  • Add meaningfull error messages for generating the route structure and for building routes.
  • Maybe: Support back slashes for path generation in Windows.