2.0.2 • Published 3 years ago

@juliushuck/gen-route v2.0.2

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

gen-route

GenRoute is a function that can define and 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.

Get started

1. Install the package

npm i @juliushuck/gen-route

2. Import the genRoute function

import genRoute from "@juliushuck/gen-route";
// or
var genRoute = require("@juliushuck/gen-route");

3. Generate your routes structure with sub routes

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

The genRoute function accepts a segement and subroutes. A segment can be a static one or a parameter placeholder. A parameter placeholder starts with :. You can also pass multiple segments to one route like this: genRoute(["app.com", "posts", ":postId"]. In addition you can have multiple parameter placeholders in one segments array.

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
console.log(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 one of the lower level routes.
// /:postId/comments/:commentId
routes().posts(true).getById().comments().getById().build();

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

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

When you have a frontend and a backend, I recommend you to create two files. Then define 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 use the buildForRouter function.

All features

  • Generate your routes structure with sub routes.
  • Use parameter placeholders.
    • Build routes with values for the parameter placeholders.
    • Build routes without values for the parameter placeholders.
  • Build not to the lowest level.
  • Build not to the highest level.
  • Add a query string.

Roadmap

  • Check the performance impact on express routers.
  • Cover multiple parameter placeholders in one route with tests.
  • Add meaningfull error messages for generating the routes structure and for building routes.
  • Maybe: By default get the segment string for the route from its key in the parents object.
  • Maybe: Support back slashes for path generation in Windows.
  • Maybe: Switch to a single build function and just require the developer to not pass arguments .to the function for it to return the route for routers.
2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago