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.
The following command will install and configure everything automatically (provider, middleware, Japa plugin, config file config/izzyjs.ts
, and route generation):
node ace add @izzyjs/route
If you prefer manual setup, install the package with your package manager and then run the configure hook:
# npm
npm install @izzyjs/route
# yarn
yarn add @izzyjs/route
# pnpm
pnpm add @izzyjs/route
# then configure
node ace configure @izzyjs/route
The configure step will generate config/izzyjs.ts
, register the provider/middleware/Japa plugin, and trigger an initial routes generation.
To use the route()
function in your JavaScript applications, you need to follow these steps:
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.
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,
}
}
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>
@izzyjs/route supports filtering the list of routes it outputs, which is useful if you have certain routes that you don't want to be included and visible in your HTML source.
Important: Hiding routes from the output is not a replacement for thorough authentication and authorization. Routes that should not be accessible publicly should be protected by authentication whether they're filtered out or not.
To set up route filtering, create a config file in your app at config/izzyjs.ts
and add either an only
or except
key containing an array of route name patterns.
Note: You have to choose one or the other. Setting both only
and except
will disable filtering altogether and return all named routes.
// config/izzyjs.ts
import { defineConfig } from '@izzyjs/route'
export default defineConfig({
baseUrl: 'https://example.com',
routes: {
// Include only specific routes
only: ['home', 'posts.index', 'posts.show'],
// OR exclude specific routes
// except: ['_debugbar.*', 'horizon.*', 'admin.*'],
},
})
You can use asterisks as wildcards in route filters. In the example below, admin.*
will exclude routes named admin.login
, admin.register
, etc.:
// config/izzyjs.ts
import { defineConfig } from '@izzyjs/route'
export default defineConfig({
baseUrl: 'https://example.com',
routes: {
except: ['_debugbar.*', 'horizon.*', 'admin.*'],
},
})
You can also define groups of routes that you want to make available in different places in your app, using a groups
key in your config file:
// config/izzyjs.ts
import { defineConfig } from '@izzyjs/route'
export default defineConfig({
baseUrl: 'https://example.com',
routes: {
groups: {
admin: ['admin.*', 'users.*'],
author: ['posts.*'],
public: ['home', 'about', 'contact'],
},
},
})
When you configure baseUrl
in your config file, the route()
function automatically generates complete URLs with protocol, domain, and path. This is useful for:
- External links and redirects
- API calls to different domains
- Email templates and notifications
- Webhook URLs
- Cross-domain requests
// config/izzyjs.ts
import { defineConfig } from '@izzyjs/route'
export default defineConfig({
baseUrl: 'https://api.example.com',
routes: {
except: ['admin.*'],
},
})
Now when you use the route()
function, you get both the path and complete URL:
import { route } from '@izzyjs/route/client'
const userRoute = route('users.show', { id: '123' })
console.log(userRoute.path) // "/users/123"
console.log(userRoute.url) // "https://api.example.com/users/123"
// Use url for external requests
fetch(userRoute.url)
window.open(userRoute.url)
The baseUrl
can include:
- Protocol:
http://
orhttps://
- Domain:
example.com
orapi.example.com
- Port:
localhost:8080
- Subdomain:
admin.example.com
If the baseUrl
is invalid or not configured, url
falls back to just the path.
When your routes have different domains (not just 'root'), the system automatically uses the route's specific domain instead of the baseUrl.host
:
// config/izzyjs.ts
export default defineConfig({
baseUrl: 'https://example.com', // Protocol will be extracted from this
routes: { ... }
})
// Routes with different domains
const homeRoute = route('home') // domain: 'root'
const apiRoute = route('api.users.index') // domain: 'api.example.com'
const adminRoute = route('admin.dashboard') // domain: 'admin.example.com'
console.log(homeRoute.url) // "https://example.com/" (uses baseUrl.host)
console.log(apiRoute.url) // "https://api.example.com/users" (uses route domain)
console.log(adminRoute.url) // "https://admin.example.com/dashboard" (uses route domain)
This is useful for multi-domain applications where different routes need to point to different subdomains or domains.
When groups are configured, they will be available in your generated routes:
import { routes, groups } from '@izzyjs/route/client'
// Access all routes
console.log(routes)
// Access specific groups
console.log(groups.admin) // Admin routes only
console.log(groups.author) // Author routes only
console.log(groups.public) // Public routes only
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
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
url.url // "https://example.com/users/1" (when baseUrl is configured)
When baseUrl
is configured, you can access the complete URL with protocol and domain:
import { route } from '@izzyjs/route/client'
const userRoute = route('users.show', { id: '123' })
// Basic properties
console.log(userRoute.path) // "/users/123"
console.log(userRoute.url) // "https://api.example.com/users/123"
// Use url for external requests
fetch(userRoute.url)
window.open(userRoute.url)
// With query parameters
const postsRoute = route('posts.index', { qs: { page: '2' } })
console.log(postsRoute.url) // "https://api.example.com/posts?page=2"
// With prefix
const apiRoute = route('api.v1.users.index', { prefix: '/v1' })
console.log(apiRoute.url) // "https://api.example.com/v1/api/v1/users"
Is a callback class withoout a parameter for route(), with information about the method, partern and path itself.
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
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
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.
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.
To understand how to submit an issue, commit and create pull requests, check our Contribution Guidelines.
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.
MIT License © IzzyJs