Giter VIP home page Giter VIP logo

routes-gen's Introduction

routes-gen @routes-gen/remix @routes-gen/solid-start License Twitter

About

routes-gen is a framework agnostic CLI tool for routes parsing and generation of a type-safe helper for safe route usage. Think of it as Prisma, but for routes.

Installation

First, you have to install the routes generator itself:

yarn add routes-gen

Official Drivers

The generator works with "drivers", which are route parsers for different frameworks.

Driver Installation
Remix yarn add @routes-gen/remix
SolidStart yarn add @routes-gen/solid-start

Usage Example

You can simply run:

yarn routes-gen -d @routes-gen/remix

It will parse and export the routes, based on the driver that you've provided.

For example, the @routes-gen/remix driver will export the routes by default to /app/routes.d.ts.

Note that you can change the output path via the --output or -o flag.

Now you can import the generated route helper anywhere and enjoy the typings:

import { route } from "routes-gen";

// Compiles to /products
route("/products");

// Compiles to /products/1337
route("/products/:productId", {
    productId: "1337",
});

Params Types Usage

You can use the RouteParams type to add typings to your dynamic route parameters/segments. Example:

import { RouteParams } from "routes-gen";

const { productId } = params as RouteParams["/products/:productId"];

Remix example:

import { LoaderFunction, useParams } from "remix";
import { RouteParams } from "routes-gen";

export const loader: LoaderFunction = async ({ params }) => {
  const { productId } = params as RouteParams["/products/:productId"];
};

export default function Product() {
  const { productId } = useParams<RouteParams["/products/:productId"]>();

  return <div />;
}

CLI Options

Option Alias Description
--help Print the help message and exit
--version -v Print the CLI version and exit
--output -o The path for routes export
--driver -d The driver of handling routes parsing
--watch -w Watch for changes
--post-export Execute a command after routes export

Writing Your Driver

If there is no driver for your preferred framework, you can write your own. For example, create a simple driver.js file in your project, with the following content:

module.exports = {
    // Where to export the typings if the "output" flag was not provided.
    defaultOutputPath: "src/routes.d.ts",
    
    // The routes parser. Must export and array of routes matching the { path: string } interface.
    routes: async () => {
        return [
            {
                path: "/products",
            },
            {
                path: "/products/:productId", // Note that the dynamic segments must match the :myVar pattern.
            },
        ];
    },

    // The paths to be watched for changes. Must return and array of relative paths.
    watchPaths: async () => {
        return ["/my-routes/**/*.{ts,tsx,js,jsx}"];
    },
}

Now you can easily use it:

routes-gen -d driver.js

You can also publish it to npm, install it, and use it as a package:

routes-gen -d my-driver-package

Please consider submitting your drivers to this repository.

routes-gen's People

Contributors

aniravi24 avatar br0p0p avatar brandonpittman avatar cliffordfajardo avatar itsmapleleaf avatar jmezzacappa avatar nirtamir2 avatar sandulat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

routes-gen's Issues

--output doesn't work with remix (would be nice so it could work with vite)

Describe the bug

--output parameter does not work without a remix.config.js

Your Example Website or App

N/A

Steps to Reproduce the Bug or Issue

N/A

Expected behavior

if an output parameter is specified, it'd be nice to use route-gen without needing a remix.config.js

Screenshots or Videos

No response

Platform

  • OS: [e.g. macOS, Windows, Linux]
  • Browser: [e.g. Chrome, Safari, Firefox]
  • Version: [e.g. 91.1]

Additional context

No response

Autocomplete shows non-relevant params

Describe the bug

image

When missing a param, autocomplete shows too many options

Your Example Website or App

Steps to Reproduce the Bug or Issue

Expected behavior

Screenshots or Videos

No response

Platform

  • OS: MacOS
  • Version: latest

Additional context

No response

Command failed: remix routes --json | tee in Windows Powershell

Describe the bug

Followed the instructions up to this line yarn routes-gen -d @routes-gen/remix and received the following error:

image

I believe this occurs because on Windows it defaults to use command prompt instead of Powershell, where the command would have worked.

Your Example Website or App

n/a

Steps to Reproduce the Bug or Issue

  1. Open existing remix repo on VSCode Windows
  2. yarn add routes-gen @routes-gen/remix
  3. yarn routes-gen -d @routes-gen/remix

Expected behavior

It will parse and export the routes, based on the driver that you've provided.

Screenshots or Videos

No response

Platform

  • OS: Windows 10 Powershell
  • IDE: VSCode

Additional context

No response

escaped sequences not mapped correctly

Describe the bug

I have a route called contacts.$id[.vcard].tsx

the route string is properly generated by the routes.d.ts but the runtime handling of it is invalid.

the produced URL of the route function looks as follows:

route('/contacts/:id.vcard', {
  id: '1',
})

and the output looks like this:

/contacts/:id.vcard

where :id should be replaced with the actual parameter

Your Example Website or App

N/A

Steps to Reproduce the Bug or Issue

outlined above

Expected behavior

I'd expect the correct runtime value

Screenshots or Videos

No response

Platform

N/A

Additional context

No response

Is there a limit to the number of routes

Describe the bug

In a Remix app, we are struggling to add any additional routes. We are getting the following error:

SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at /Users/my-appl/node_modules/@routes-gen/remix/dist/index.js:52:30
    at ChildProcess.exithandler (child_process.js:310:7)
    at ChildProcess.emit (events.js:376:20)
    at maybeClose (internal/child_process.js:1055:16)
    at Socket.<anonymous> (internal/child_process.js:441:11)
    at Socket.emit (events.js:376:20)
    at Pipe.<anonymous> (net.js:673:12)

The only way around it, is to delete an existing route in order to create a new one.

I have tested on a fresh remix app and limit appears to be 68 routes and issues shows up when I create any additional routes.

Your Example Website or App

Local app

Steps to Reproduce the Bug or Issue

  1. Create 70 routes on a fresh remix app
  2. Try to run the command routes-gen -d @routes-gen/remix in a Remix App

Expected behavior

To be able to generate an updated /routes.d.ts file

Screenshots or Videos

No response

Platform

  • OS: macOS

Additional context

No response

Error [ERR_REQUIRE_ESM] When using with an esm based npm project.

Describe the bug

When running routes-gen with a setup that is all ESM based I get the following error.

Error [ERR_REQUIRE_ESM]: require() of ES Module /<myproject_path>/remix.config.js from /<myproject_path>/node_modules/@routes-gen/remix/dist/index.js not supported.
Instead change the require of remix.config.js in /<myproject_path>/node_modules/@routes-gen/remix/dist/index.js to a dynamic import() which is available in all CommonJS modules.
    at loadRemixConfig (/<myproject_path>/node_modules/@routes-gen/remix/dist/index.js:46:14)
    at Object.<anonymous> (/<myproject_path>/node_modules/@routes-gen/remix/dist/index.js:59:21)
    at //<myproject_path>/node_modules/routes-gen/dist/cli.js:142:118
    at async bootstrap (/<myproject_path>/node_modules/routes-gen/dist/cli.js:142:14) {
  code: 'ERR_REQUIRE_ESM'
}
Failed to load the Remix config at "remix.config.js". Falling back to the following defaults: {"appDirectory":"app"}.
[SUCCESS] Exported 100 routes to "app/routes.d.ts".

So it doesn't actually fail but I was able to easily remedy this with changing a couple lines of the built remix driver in the dist index.js file to use dynamic import instead.

...
    try {
      const configFile = await import(file);
      return configFile.default;
    } catch (e) {
...

I believe changing this

return require(file) as {
to be a dynamic import would solve it although I am unsure if that would have ramifications for other users sticking strictly to commonjs. Looking at the error it shouldn't.

Happy to help work on the problem and test the solution out if needed.

Your Example Website or App

https://stackblitz.com/edit/remix-run-remix-j9fqdd

Steps to Reproduce the Bug or Issue

  1. run npm run gen-routes from the terminal in the example app and you can see the error

Expected behavior

clean output e.g.

npm run gen-routes

> gen-routes
> routes-gen -d @routes-gen/remix

[SUCCESS] Exported 100 routes to "app/routes.d.ts".

Screenshots or Videos

No response

Platform

  • OS: macOS
  • Browser: Chrome
  • Version: "routes-gen": "^0.6.1", "@routes-gen/remix": "^0.3.7",

Additional context

No response

Invalid driver package name or file path

Describe the bug

I'm running routes-gen on postinstall on Github Actions, and I'm getting this error:

[ERROR] Invalid driver package name or file path.

It's running on node:16-bullseye-slim image, and it's running without any issues on my local machine. I tested it even after deleting node_modules.

The relevant parts of my scripts:

    "postinstall": "remix setup node && npm run routes-gen",
    "routes-gen": "routes-gen -d @routes-gen/remix"

and relevant devDependencies:

    "@routes-gen/remix": "^0.3.0",
    "routes-gen": "^0.4.0",

Your Example Website or App

a private repo

Steps to Reproduce the Bug or Issue

  1. Run npm install --production=false on Github Actions under Docker node:16-bullseye-slim image

Expected behavior

The driver should resolve correctly

Screenshots or Videos

No response

Platform

  • OS: [Linux]
  • Version: [0.4.0]

Additional context

No response

Routes gen remix requires use of --legacy-peer-deps when used with Remix 2.x

Describe the bug

After updating remix to v2.x, and installing packages, I get the following error:

npm ERR! code ERESOLVE
npm ERR! ERESOLVE could not resolve
npm ERR! 
npm ERR! While resolving: @routes-gen/[email protected]
npm ERR! Found: @remix-run/[email protected]
npm ERR! node_modules/@remix-run/dev
npm ERR!   dev @remix-run/dev@"2.1.0" from the root project
npm ERR! 
npm ERR! Could not resolve dependency:
npm ERR! peer @remix-run/dev@"^1.0.3" from @routes-gen/[email protected]
npm ERR! node_modules/@routes-gen/remix
npm ERR!   dev @routes-gen/remix@"^0.3.6" from the root project
npm ERR! 
npm ERR! Conflicting peer dependency: @remix-run/[email protected]
npm ERR! node_modules/@remix-run/dev
npm ERR!   peer @remix-run/dev@"^1.0.3" from @routes-gen/[email protected]
npm ERR!   node_modules/@routes-gen/remix
npm ERR!     dev @routes-gen/remix@"^0.3.6" from the root project
npm ERR! 
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resol

Due to the above outdated peer dependencies for @routes-gen/remix it is now required to use --legacy-peer-deps with every npm install or uninstall command.

Would it be possible to update the peer dependencies?

Your Example Website or App

https://stackblitz.com/edit/remix-run-remix-yqj3cg?file=package.json

Steps to Reproduce the Bug or Issue

  1. Run npm i -D routes-gen @routes-gen/remix
  2. Error shown above displays

Expected behavior

Run npm install or uninstall without errors.

Screenshots or Videos

No response

Platform

  • OS: macOS

Additional context

No response

Invalid driver package name or file path.

Describe the bug

I installed the dependencies using npm add routes-gen @routes-gen/remix and run routes-gen -d @routes-gen/remix and I get the following error

CleanShot 2023-01-09 at 02 02 38

Your Example Website or App

Private Repo

Steps to Reproduce the Bug or Issue

  1. Install dependencies with npm
  2. Run the script with npm

Expected behavior

Generate the routes

Screenshots or Videos

No response

Platform

  • OS: macOS
  • Browser: Chrome
  • Version: 108.0

Additional context

No response

Optional Remix route creates type error when supplying dynamic segments object

Describe the bug

If you have an optional Remix route, a type error occurs when supplying the dynamic segments object.

Your Example Website or App

https://stackblitz.com/edit/node-jcqyxc?file=app/routes.d.ts

Steps to Reproduce the Bug or Issue

  1. Create an optional route
  2. Call a route like route("/reports/:type?/:id/", {id: userId})
  3. The route type includes the ? from the optional route as part of the param's name, not its type definition.

Expected behavior

I should be able to use an optional route and be able to optionally pass the optional param.

Screenshots or Videos

CleanShot 2023-01-04 at 10 31 37@2x

Platform

  • OS: macOS
  • Browser: Chrome
  • Version: 108

Additional context

No response

doesn't work with vite

Describe the bug

the now stable remix plugin for vite doesn't seem to be supported

Your Example Website or App

N/A

Steps to Reproduce the Bug or Issue

  1. create a new remix project
  2. run routes gen
  3. see generator fail

Expected behavior

As a user, I expect to be able to use routes-gen with remix and vite

Screenshots or Videos

No response

Platform

  • OS: MacOS
  • Browser: Chrome
  • Version: Latest

Additional context

No response

[FEATURE REQUEST] Add query string support

Describe the bug

Discussions are not enabled so I am using this.

There is a similar library to this, Remix Routes, that supports adding query parameters to URLs with the path helper. Could this functionality be added to this package?

Your Example Website or App

https://github.com/yesmeck/remix-routes

Steps to Reproduce the Bug or Issue

N/A

Expected behavior

Query parameters can be added to the path with the helper.

Screenshots or Videos

No response

Platform

  • OS: [e.g. macOS, Windows, Linux]
  • Browser: [e.g. Chrome, Safari, Firefox]
  • Version: [e.g. 91.1]

Additional context

No response

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.