@izzyjs/route
Version:
Use your AdonisJs routes in your Inertia.js application
155 lines (154 loc) • 4.59 kB
TypeScript
/**
* @izzyjs/route
*
* (c) IzzyJs - 2024
* For the full license information, please view the LICENSE file that was distributed with this source code.
*/
import type { GlobalIzzyJs, Method, SerializedRoute } from './types/manifest.js';
import type { RouteName } from './client/routes.js';
import type { ExcludeName, ExtractName, Params } from './types/routes.js';
export declare class Route extends String {
/**
* The complete URL with protocol and domain when baseUrl is configured
* @example
* ```ts
* const route = Route.new('user.show', { id: '1' })
* console.log(route.url) // https://example.com/users/1
* ```
*/
readonly url: string;
/**
* The HTTP method for the route
*/
readonly method: Method;
/**
* The route parameters as an array
* @example
* ```ts
* const route = Route.new('user.show', { id: '1' })
* console.log(route.params) // ['id']
* ```
*/
readonly params: string[] | undefined;
/**
* The route name as a string
* @example
* ```ts
* const route = Route.new('user.show', { id: '1' })
* console.log(route.name) // user.show
* ```
*/
readonly name: string;
/**
* The route pattern
* @example
* ```ts
* const route = Route.new('user.show', { id: '1' })
* console.log(route.pattern) // /users/:id
* ```
*/
readonly pattern: string;
/**
* The route path with parameters replaced
* @example
* ```ts
* const route = Route.new('user.show', { id: '1' })
* console.log(route.path) // /users/1
* ```
*/
readonly path: string;
/**
* The query string
* @example
* ```ts
* const route = Route.new('user.show', { id: '1' }, { page: 1 })
* console.log(route.qs.toString()) // page=1
* ```
*/
readonly qs: URLSearchParams;
private constructor();
/**
* Generate complete URL with protocol and domain when baseUrl is configured
*/
private generateCompleteUrl;
/**
* Get the configuration from the global Izzy object
*/
private static getConfig;
static replaceRouteParams(routePath: string, params: Record<string, string>): string;
/**
* Create a new `Route` instance
* @param routeName The route name
* @param params The route parameters
* @param qs The query string
* @param prefix The route prefix
*/
static new(routeName: unknown, params: unknown, qs?: unknown, prefix?: string): Route;
/**
* Create a new `Route` instance
* @param routeName The route name
*/
static new(routeName: unknown): Route;
/**
* Create a new `Routes` instance
*/
static new(): Routes;
/**
* Get the `Route` instance from the global Izzy object
* @returns { GlobalIzzyJs }
*/
static izzy(): GlobalIzzyJs;
toString(): string;
}
export declare class Routes {
private readonly currentRoute;
readonly routes: SerializedRoute[];
constructor();
/**
* Check if the current route matches the given route name and parameters
* @summary unstable
* @example
* ```ts
* route().current() // => "/users"
* ```
*/
current(): RouteName;
/**
* Check if the current route matches the given route name and parameters
* @summary unstable
* @param routeName route name
* @param params route parameters
* @example
* ```ts
* // current route is 'GET /users/1'
* route().current('users.show', { id: '1' }) // => true
* route().current('users.show', { id: '2' }) // => false
* ```
*/
current<Name extends ExtractName>(routeName: Name, params: Params<Name>): boolean;
/**
* Check if the current route matches the given route name
* @summary unstable
* @param routeName route name
* @example
* ```ts
* // current route is 'GET /users/1'
* route().current('users.show') // => true
* route().current('user.*') // => true
* route().current('todos.*') // => false
* ```
*/
current<Name extends ExcludeName['name']>(routeName: Name, params?: never): boolean;
/**
* Check if the current route has the given route name
* @summary unstable
* @param routeName route name
* @example
* ```ts
* route().has('users.index') // => true or false
* route().has('user.*') // => true or false
* ```
*/
has<T extends string>(routeName: T): boolean;
get params(): Record<string, string>;
}