Research
Security News
Kill Switch Hidden in npm Packages Typosquatting Chalk and Chokidar
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
route-codegen
Advanced tools
Route generator written in Typscript for React Router, NextJS, NodeJS and more
This library generates route modules which can be used to manage client-side ( e.g. react-router, NextJS, etc. ) and server-side routing ( normal <a />
).
Given a route pattern, automatically detect and generate link components to go to routes in the same app via client-side routing or routes in a different app via server-side routing. Typescript interfaces and helper functions / hooks are generated as well to make routing safe and easy.
This library can help you avoid routing errors like this:
If you only have one app, you can install at project root:
yarn add route-codegen
Or
npm i route-codegen
Add route-codegen.yml
to project root. Example:
apps:
client:
routes:
login: /login
logout: /logout
user: /user/:id/:subview(profile|pictures)?
routingType: ReactRouterV5 # "ReactRouterV5", "NextJS" or "Default" ( normal <a />)
destinationDir: client/src/routes
More details about config file.
If you have more than one app and want to manage all routes in one config file, you will need to run the cli command at project root. Run the following at project root or in a route manager package:
yarn add -D route-codegen
Or
npm i --save-dev route-codegen
The library contains some utility functions for the generated files. Therefore, it also needs to be installed in each app:
yarn add route-codegen
Or
npm i route-codegen
Add route-codegen.yml
to project root / route manager package. Example:
apps:
client:
routes:
login: /login
logout: /logout
user: /user/:id/:subview(profile|pictures)?
routingType: ReactRouterV5 # "ReactRouterV5", "NextJS" or "Default" ( normal <a />)
destinationDir: client/src/routes
client-seo:
routes:
home: /
routingType: NextJS
destinationDir: client-seo/src/routes
# An app without `routes` is still valid.
# In this case, this app can still generate url to other apps
# `generateLinkComponent`, `generateUseParams`, `generateRedirectComponent` and `generateUseRedirect` should be false to avoid generating unncessary files
express-server:
generateLinkComponent: false
generateRedirectComponent: false
generateUseParams: false
generateUseRedirect: false
destinationDir: server/src/routes
# Leave out `destinationDir` if no route needs to be generated.
# Other apps still generates routes to navigate to this app
legacy:
routes:
legacyApp: /legacy/app
# Origin can be used to prefix the URL path of certain apps.
# ${...} Can be used to pass environment variables to the config yml
externalApp:
origin: https://${SUB_DOMAIN}.external.com
routes:
externalAppHome: /
More details about config file.
Path parameter patterns are a subset of https://github.com/pillarjs/path-to-regexp:
:path
: This matches any string.:path?
: This matches an optional string.:path(enum1|enum2)
: This only matches if path value is enum1
or enum2
for React Router V5. For others, it matches any string.:path(enum1|enum2)?
: This only matches if path value is enum1
or enum2
for React Router V5. For others, it matches any string. This param is optional.If you have custom links ( e.g. to apply styling on top of underlying link components ), check out the link options doc.
yarn route-codegen
Or
npx route-codegen
Name | Default | Description |
---|---|---|
config | route-codegen.yml | The name of the config file. |
stacktrace | false | Turn on stack trace. Used to debug errors if they occur. |
verbose | false | Turn on infos and logs. Used to see more information about the current run. |
Example
yarn route-codegen --verbose --stacktrace --config path/to/routegen.yml
This file contains the pattern of a route and typescript interfaces that come with it.
This file contains a function to generate the URL of a particular route. Interfaces from the pattern files are used here to ensure type safety. This function is used in other components / functions of the route module to ensure URLs are generated the same way.
Each routing framework has different API for their link. The generated Link
component is an abstraction that handles:
https://domain.com
// react-router v5 ( client-side )
<Link to="/users/100/profile?from=home" />
// NextJS ( client-side )
<Link href="/users/100/profile?from=home" />
// Normal anchor ( server-side )
<a href="/users/100/profile?from=home" />
The generated Link component has the same props so you can do the following in any app:
// Works in any app
<LinkUser path={{ id: "100" }} urlQuery={{ from: "home" }} />
Or with origin:
<LinkUser path={{ id: "100" }} urlQuery={{ from: "home" }} origin="https://domain.com" />
Similar to the Link
component but redirects the user when mounted.
useParams
: Get dynamic params in the URL. Available for react-router
and NextJS
. Example
useRedirect
: Creates a function to redirect the user to a route. Example
If you want to see how the codegen works, check out the development guide.
FAQs
Route generator written in Typscript for React Router, NextJS, NodeJS and more
We found that route-codegen demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.