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

@dabrowskif/typed-routes

v0.6.3

Published

Typed Routes provides a route generation tool for Frontend meta frameworks that use file-based routing projects, such as SvelteKit or NextJS. It simplifies the process of creating typed route structures by automatically converting file system-based routin

Downloads

5

Readme

Typed Route Generator for Frontend Frameworks

Typed Routes provides a route generation tool for Frontend meta frameworks that use file-based routing projects, such as SvelteKit or NextJS. It simplifies the process of creating typed route structures by automatically converting file system-based routing into a JavaScript object, enhancing type safety and improving developer experience.

Features

  • Automated Route Generation: Converts the routes (ex. /routes in SvelteKit) directory into a JavaScript object, mirroring the file system structure
  • Dynamic Route Support: Handles dynamic, optional and rest route parameters
  • Command-Line Interface: Easy-to-use CLI for quick and seamless route generation

Supported Frameworks

  • [x] SvelteKit
  • [ ] Next.js
  • [ ] Nuxt.js
  • [ ] Astro
  • [ ] Other frameworks (coming soon)

Installation

Install the package using npm:

npm install --save-dev @dabrowskif/typed-routes

Or using yarn:

yarn add --dev @dabrowskif/typed-routes

Usage

Here are the available options you can use with the Typed Route Generator:

Options

| Option | Description | Required | Default | Possible Values | |---------------------------------|--------------------------------------------------------|----------|-------------|-------------------------| | -r, --root-directory <string> | Directory where all routes all stored | Yes | None | Any valid path | | -f, --framework <Framework> | Framework for which to generate routes | Yes | None | sveltekit, nextjs | | -od, --output-directory <string> | Directory where the generated routes file is saved | No | Current dir | Any valid path | | -of, --output-file-name <string> | Name of the file with generated routes | No | routes.js | Any valid file name | | -fn, --function-name <name> | Function name that will be used to get url path for given file | No | getRoute | Any valid function name | | -m, --module-system <ModuleSystem> | Module system to use (commonjs or es6) | No | es6 | es6, commonjs | | -v, --verbose <boolean> | Enable verbose logging | No | false | true, false |

CLI Usage

To use this tool via the command line, you can run it directly with npx:

npx @dabrowskif/typed-routes -fr SvelteKit -r src/routes

Alternatively, if you have installed it as a dependency in your project, you can add a script to your package.json:

scripts": {
    ...
    "generate-typesafe-routes": "typed-routes -f sveltekit -r src/routes"
    ...
}

Then, you can generate routes runnint this script:

npm run generate-routes

Sample output

export const routes = {
  home: {
    _get: () => `/home`,
    "[profileId]": { _get: (profileId: string) => `/home/${profileId}` },
  },
};

Actual usage in your app

routes.home["[profileId]"]._get("id-123");

For a more integrated development experience, consider running this script concurrently with your framework's development server. Tools like nodemon or concurrently can be used to run multiple npm scripts simultaneously.

Example with nodemon

(keep in mind you have to have nodemon installed)

scripts": {
    ...
    "generate-typed-routes": "typed-routes -f sveltekit -r src/routes",
    "generate-typed-routes:watch": "nodemon --exec npm run generate-typesafe-routes --watch src/routes"
    ...
}

TODO

The following features are planned:

  1. More Frameworks Support

  2. Native watch

  3. Type-Only Route Definitions

    • Evolve the current route functions to support type-only definitions that adds minimal runtime overhead.
      • Simple functions with typed parameters for general use.
      • Framework-specific enhancements, such as type-safe wrappers for SvelteKit's goto function or Next.js's config hook
  4. SvelteKit Segments Enhancement

    • Incorporate support for SvelteKit's advanced routing features, including segment sorting and parameter encoding. This will align the Typed Route Generator with SvelteKit's latest routing capabilities, allowing for more sophisticated route structures.
  5. Custom Strategy Provider

    • Implement functionality to allow defining custom strategies for route generation. This feature aims to provide greater flexibility and adaptability to specific project requirements.
  6. Support for object-based arguments, instead of multiple arguments inside one function

    • Passing many arguments into heavily nested dynamic routes one by one is more or less an anti-pattern, so creating an option to make getter function a one-argument based with multiple properties is a good alternative.
  7. Support for passing a query string

    • Add a simple option to pass a query string into the function invocation in order to produce full and final route for usage.
    • This actually can be even typed, after creating specific file to look for, or scanning a main route file.

Contributing

Contributions are welcome! If you have a suggestion or enhancement, feel free to fork the repo and create a pull request.

License

This project is licensed under the MIT License.