Code Monkey home page Code Monkey logo

route's Introduction

@izzyjs/route

GitHub Actions Status Coverage Status GitHub issues GitHub pull requests npm version License

Use your AdonisJs routes in JavaScript.

This package provides a JavaScript route() function that can be used to generate URLs for named routes defined in an AdonisJs application.

Installation

Install and configure the package using the following command :

node ace add @izzyjs/route

Configuration

To use the route() function in your JavaScript applications, you need to follow these steps:

Command

You can run a command to generate the route definitions from @izzyjs/routes with:

node ace izzy:routes

These type definitions are only needed in a development environment, so they can be generated automatically in the next step.

Assemble Hook

Add the following line to the adonisrc.ts file to register the () => import('@izzyjs/route/dev_hook') on onDevServerStarted array list.

{
  // rest of adonisrc.ts file
  unstable_assembler: {
    onBuildStarting: [() => import('@adonisjs/vite/build_hook')],
    onDevServerStarted: [() => import('@izzyjs/route/dev_hook')] // Add this line,
  }
}

View Helper

Add edge plugin in entry view file @routes to use the route() into javascript.

// resources/views/inertia_layout.edge

<!doctype html>
<html>
  <head>
    // rest of the file 
    @routes() // Add this line
    // rest of the file
  </head>

  <body>
    @inertia()
  </body>
</html>

Usage

Now that we've followed all the steps, we're ready to use route() on the client side to generate URLs for named routes.

import { route } from '@izzyjs/route/client'

const url = route('users.show', { id: '1' }) // /users/1

Route

Is a callback class with a parameter for route(), with information about the method, partern and path itself.

import { route } from '@izzyjs/route/client'

const url = route('users.show', { id: '1' }) // /users/1

url.method // GET
url.pattern // /users/:id
url.path // /users/1

Routes

Is a callback class withoout a parameter for route(), with information about the method, partern and path itself.

Current

The current method returns the current URL of the page or the URL of the page that the user is currently on.

import { route } from '@izzyjs/route/client'

// current /users/1

route().current() // /users/1
route().current('/users/1') // true
route().current('/users/2') // false
route().current('users.*') // true

Has

The has method returns a boolean value indicating whether the named route exists in the application.

// start/routes.ts

import router from '@adonisjs/core/services/router'

const usersConstroller = () => import('#app/controllers/users_controller')

router.get('/users', [usersConstroller, 'index']).as('users.index')
router.get('/users/:id', [usersConstroller, 'show']).as('users.show')
import { route } from '@izzyjs/route/client'

route().has('users.show') // true
route().has('users.*') // true
route().has('dashboard') // false

Params

The params method returns the parameters of the URL of the page that the user is currently on.

import { route } from '@izzyjs/route/client'

route().params() // { id: '1' }

These features enable seamless integration of AdonisJs routing within your JavaScript applications, enhancing flexibility and maintainability. By leveraging route(), you can easily manage and navigate your application routes with ease, ensuring a smooth user experience.

Contributing

Thank you for being interested in making this package better. We encourage everyone to help improve this project with new features, bug fixes, or performance improvements. Please take a little bit of your time to read our guide to make this process faster and easier.

Contribution Guidelines

To understand how to submit an issue, commit and create pull requests, check our Contribution Guidelines.

Code of Conduct

We expect you to follow our Code of Conduct. You can read it to understand what kind of behavior will and will not be tolerated.

License

MIT License © IzzyJs

Built with ❤︎ by Walaff Fernandes

route's People

Contributors

lncitador avatar

Stargazers

Julien Breton avatar Perfect Makanju avatar Okwukwe Ewurum avatar Shahjahan avatar Sonny avatar Nordine avatar  avatar  avatar wailroth avatar

route's Issues

Setup and generateRoutes fail in monorepo

Setting up the package in a monorepo leads to issues. The generateRoutes function in src/generate_routes.ts uses a node_modules/@izzyjs/route/build/src/client path which does not work for a monorepo because monorepos have their node_modules in the root and not in the package or app folder where adonis is being used and installed.

The initial setup command, node ace add @izzyjs/route also resulted in some weird issues initially when I run it in the app in my monorepo, it was somehow adding the package to the package.json file but not installing in node_modules. I manually installed the package by running yarn install in the root of the monorepo but when I run the initial setup command again, I realized that it actually deleted the package installed from the node_modules, not sure why.

So I manually installed the package and went through the configure.ts to configure the rest of the application and it worked only that now, I am not able to get the node ace izzy:routes command to work because of the node_modules issue.

For now, I might have to patch the package locally, and use a workaround of setting the baseDir this way: const baseDir = '../../node_modules/@izzyjs/route/build/src/client'; which allows me to point to my root node_modules.

No path property on the route returned object

Thank you for this helpful package.

Here are some issues I noticed when using it.

  1. The object returned from the route method does not have a path property.

    const url = route('users.show', { id: '1' }) // /users/1
    
    url.method // GET
    url.pattern // /users/:id
    url.path // the path property does not exist

    And if you check the Route class declaration, there is no path property on the class.

    export declare class Route extends String {
        readonly url: string;
        readonly method: Method;
        readonly params: string[] | undefined;
        readonly name: string;
        readonly pattern: string;
        readonly qs: URLSearchParams;
        private constructor();
        static replaceRouteParams(routePath: string, params: Record<string, string>): string;
        static new(routeName: unknown, params: unknown, qs?: unknown, prefix?: string): Route;
        static new(routeName: unknown): Route;
        static new(): Routes;
        static izzy(): {
            routes: SerializedRoute[];
            currentRoute: string;
        };
        toString(): string;
    }
  2. route('routename') returns an instance of the Route class. When using it in href of a tag or the inertia Link there is no issue, but when using it in the inertia form helper to submit request for example:

    form.post(route('signup.store'))

    it complains that Argument of type 'Route' is not assignable to parameter of type 'string' because the form helper expects a string. If the path property is available we could do form.post(route('signup.store').path) as a workaround.

Route filtering

This is not a bug but a feature request.

There is a feature in ziggy.js that allows filtering routes it output. This is a good feature; in case you have some routes you don't want to be publicly known.

If you can add this feature, that will a add more value and use to this package.

Also, thank you for creating this package. 👍👍👍

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.