@amazing-router/react 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Adel Chafic Gannem
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # amazing-router-react
@@ -0,0 +1,122 @@
1
+ import * as react from 'react';
2
+ import { ReactNode } from 'react';
3
+ import { RouteNode } from '@amazing-router/core';
4
+ import { RouteObject } from 'react-router';
5
+ export * from 'react-router';
6
+ import * as react_jsx_runtime from 'react/jsx-runtime';
7
+
8
+ /**
9
+ * Custom hook that provides a simplified navigation API built on top of React Router.
10
+ *
11
+ * @returns An object with the current pathname and navigation methods:
12
+ * - `pathname` — the current URL path.
13
+ * - `push(path)` — navigates to the given path, pushing a new entry onto the history stack.
14
+ * - `replace(path)` — navigates to the given path, replacing the current history entry.
15
+ * - `back()` — navigates back one entry in the history stack.
16
+ */
17
+ declare const useAmazingRouter: () => {
18
+ pathname: string;
19
+ push: (path: string) => void | Promise<void>;
20
+ replace: (path: string) => void | Promise<void>;
21
+ back: () => void | Promise<void>;
22
+ };
23
+
24
+ /**
25
+ * Custom hook that reads the meta information attached to the currently active route.
26
+ * Meta is injected into the route's `handle` property during route transformation.
27
+ *
28
+ * @returns An object containing the resolved `title`, `description`, and any additional
29
+ * meta fields defined on the matched route. Falls back to default values if no meta is found.
30
+ */
31
+ declare const useAmazingMeta: () => {
32
+ title: string;
33
+ description: string;
34
+ image?: string;
35
+ keywords?: string[];
36
+ canonical?: string;
37
+ breadcrumb?: string | react.ReactNode;
38
+ icon?: string | react.ReactNode;
39
+ hideInMenu?: boolean;
40
+ roles?: string[];
41
+ requiresAuth?: boolean;
42
+ };
43
+
44
+ /**
45
+ * Metadata that can be attached to any route node.
46
+ * This information is read at runtime by the {@link useAmazingMeta} hook.
47
+ */
48
+ interface MetaProps {
49
+ /** The page title displayed in the browser tab and used for SEO. */
50
+ title?: string;
51
+ /** A short description of the page, used for SEO meta tags. */
52
+ description?: string;
53
+ /** URL of the Open Graph / social share image. */
54
+ image?: string;
55
+ /** List of keywords associated with the page for SEO purposes. */
56
+ keywords?: string[];
57
+ /** The canonical URL for the page, used to avoid duplicate content issues. */
58
+ canonical?: string;
59
+ /** Breadcrumb label shown in navigation hierarchies. */
60
+ breadcrumb?: string | ReactNode;
61
+ /** Icon associated with this route, used in menus or tabs. */
62
+ icon?: string | ReactNode;
63
+ /** When `true`, hides this route from automatically generated menus. */
64
+ hideInMenu?: boolean;
65
+ /** List of roles allowed to access this route, used for authorization checks. */
66
+ roles?: string[];
67
+ /** When `true`, the route requires the user to be authenticated. */
68
+ requiresAuth?: boolean;
69
+ /** Allows any additional custom metadata fields. */
70
+ [key: string]: any;
71
+ }
72
+
73
+ /**
74
+ * Extends React Router's `RouteObject` with Amazing Router specific fields.
75
+ * Used as the internal representation of each route after transformation.
76
+ */
77
+ interface AmazingRouteObject extends Omit<RouteObject, "children"> {
78
+ /** Unique identifier for the route, derived from the file-system path. */
79
+ id: string;
80
+ /** The URL path segment for this route. */
81
+ path?: string;
82
+ /** The React element rendered when this route is active. */
83
+ element?: React.ReactNode;
84
+ /** Optional metadata attached to this route, accessible via `useAmazingMeta`. */
85
+ meta?: MetaProps;
86
+ /** Nested child routes. */
87
+ children?: AmazingRouteObject[];
88
+ }
89
+
90
+ /**
91
+ * Recursively transforms a tree of RouteNodes into React Router compatible objects.
92
+ *
93
+ * Key behaviors:
94
+ * - Top-level routes use absolute paths ("/about")
95
+ * - Nested routes use relative paths ("about") as required by React Router
96
+ * - Pathless group layout wrappers have NO path property (not even undefined)
97
+ * - Middleware wraps the page element when present
98
+ *
99
+ * @param nodes - Array of nodes from routes.json
100
+ * @param isNested - Whether these nodes are children of another route
101
+ * @returns Array of AmazingRouteObject
102
+ */
103
+ declare function transformRoutes(nodes: RouteNode[], routeFiles: Record<string, () => Promise<any>>, isNested?: boolean): AmazingRouteObject[];
104
+
105
+ /**
106
+ * Props for the {@link AmazingProvider} component.
107
+ */
108
+ interface AmazingProviderProps {
109
+ /** Optional React node to display while the route configuration is being loaded. */
110
+ loadingElement?: React.ReactNode;
111
+ }
112
+ /**
113
+ * Root provider component for Amazing Router.
114
+ * Dynamically loads the generated `routes.json` and `routeFiles.ts` artifacts
115
+ * produced by the Amazing Router Vite/Webpack plugin, transforms them into a
116
+ * React Router-compatible browser router, and renders the application.
117
+ *
118
+ * @param props - {@link AmazingProviderProps}
119
+ */
120
+ declare const AmazingProvider: ({ loadingElement, }: AmazingProviderProps) => string | number | bigint | boolean | Iterable<react.ReactNode> | Promise<string | number | bigint | boolean | react.ReactPortal | react.ReactElement<unknown, string | react.JSXElementConstructor<any>> | Iterable<react.ReactNode> | null | undefined> | react_jsx_runtime.JSX.Element | null;
121
+
122
+ export { AmazingProvider, type AmazingProviderProps, type AmazingRouteObject, type MetaProps, transformRoutes, useAmazingMeta, useAmazingRouter };
@@ -0,0 +1,122 @@
1
+ import * as react from 'react';
2
+ import { ReactNode } from 'react';
3
+ import { RouteNode } from '@amazing-router/core';
4
+ import { RouteObject } from 'react-router';
5
+ export * from 'react-router';
6
+ import * as react_jsx_runtime from 'react/jsx-runtime';
7
+
8
+ /**
9
+ * Custom hook that provides a simplified navigation API built on top of React Router.
10
+ *
11
+ * @returns An object with the current pathname and navigation methods:
12
+ * - `pathname` — the current URL path.
13
+ * - `push(path)` — navigates to the given path, pushing a new entry onto the history stack.
14
+ * - `replace(path)` — navigates to the given path, replacing the current history entry.
15
+ * - `back()` — navigates back one entry in the history stack.
16
+ */
17
+ declare const useAmazingRouter: () => {
18
+ pathname: string;
19
+ push: (path: string) => void | Promise<void>;
20
+ replace: (path: string) => void | Promise<void>;
21
+ back: () => void | Promise<void>;
22
+ };
23
+
24
+ /**
25
+ * Custom hook that reads the meta information attached to the currently active route.
26
+ * Meta is injected into the route's `handle` property during route transformation.
27
+ *
28
+ * @returns An object containing the resolved `title`, `description`, and any additional
29
+ * meta fields defined on the matched route. Falls back to default values if no meta is found.
30
+ */
31
+ declare const useAmazingMeta: () => {
32
+ title: string;
33
+ description: string;
34
+ image?: string;
35
+ keywords?: string[];
36
+ canonical?: string;
37
+ breadcrumb?: string | react.ReactNode;
38
+ icon?: string | react.ReactNode;
39
+ hideInMenu?: boolean;
40
+ roles?: string[];
41
+ requiresAuth?: boolean;
42
+ };
43
+
44
+ /**
45
+ * Metadata that can be attached to any route node.
46
+ * This information is read at runtime by the {@link useAmazingMeta} hook.
47
+ */
48
+ interface MetaProps {
49
+ /** The page title displayed in the browser tab and used for SEO. */
50
+ title?: string;
51
+ /** A short description of the page, used for SEO meta tags. */
52
+ description?: string;
53
+ /** URL of the Open Graph / social share image. */
54
+ image?: string;
55
+ /** List of keywords associated with the page for SEO purposes. */
56
+ keywords?: string[];
57
+ /** The canonical URL for the page, used to avoid duplicate content issues. */
58
+ canonical?: string;
59
+ /** Breadcrumb label shown in navigation hierarchies. */
60
+ breadcrumb?: string | ReactNode;
61
+ /** Icon associated with this route, used in menus or tabs. */
62
+ icon?: string | ReactNode;
63
+ /** When `true`, hides this route from automatically generated menus. */
64
+ hideInMenu?: boolean;
65
+ /** List of roles allowed to access this route, used for authorization checks. */
66
+ roles?: string[];
67
+ /** When `true`, the route requires the user to be authenticated. */
68
+ requiresAuth?: boolean;
69
+ /** Allows any additional custom metadata fields. */
70
+ [key: string]: any;
71
+ }
72
+
73
+ /**
74
+ * Extends React Router's `RouteObject` with Amazing Router specific fields.
75
+ * Used as the internal representation of each route after transformation.
76
+ */
77
+ interface AmazingRouteObject extends Omit<RouteObject, "children"> {
78
+ /** Unique identifier for the route, derived from the file-system path. */
79
+ id: string;
80
+ /** The URL path segment for this route. */
81
+ path?: string;
82
+ /** The React element rendered when this route is active. */
83
+ element?: React.ReactNode;
84
+ /** Optional metadata attached to this route, accessible via `useAmazingMeta`. */
85
+ meta?: MetaProps;
86
+ /** Nested child routes. */
87
+ children?: AmazingRouteObject[];
88
+ }
89
+
90
+ /**
91
+ * Recursively transforms a tree of RouteNodes into React Router compatible objects.
92
+ *
93
+ * Key behaviors:
94
+ * - Top-level routes use absolute paths ("/about")
95
+ * - Nested routes use relative paths ("about") as required by React Router
96
+ * - Pathless group layout wrappers have NO path property (not even undefined)
97
+ * - Middleware wraps the page element when present
98
+ *
99
+ * @param nodes - Array of nodes from routes.json
100
+ * @param isNested - Whether these nodes are children of another route
101
+ * @returns Array of AmazingRouteObject
102
+ */
103
+ declare function transformRoutes(nodes: RouteNode[], routeFiles: Record<string, () => Promise<any>>, isNested?: boolean): AmazingRouteObject[];
104
+
105
+ /**
106
+ * Props for the {@link AmazingProvider} component.
107
+ */
108
+ interface AmazingProviderProps {
109
+ /** Optional React node to display while the route configuration is being loaded. */
110
+ loadingElement?: React.ReactNode;
111
+ }
112
+ /**
113
+ * Root provider component for Amazing Router.
114
+ * Dynamically loads the generated `routes.json` and `routeFiles.ts` artifacts
115
+ * produced by the Amazing Router Vite/Webpack plugin, transforms them into a
116
+ * React Router-compatible browser router, and renders the application.
117
+ *
118
+ * @param props - {@link AmazingProviderProps}
119
+ */
120
+ declare const AmazingProvider: ({ loadingElement, }: AmazingProviderProps) => string | number | bigint | boolean | Iterable<react.ReactNode> | Promise<string | number | bigint | boolean | react.ReactPortal | react.ReactElement<unknown, string | react.JSXElementConstructor<any>> | Iterable<react.ReactNode> | null | undefined> | react_jsx_runtime.JSX.Element | null;
121
+
122
+ export { AmazingProvider, type AmazingProviderProps, type AmazingRouteObject, type MetaProps, transformRoutes, useAmazingMeta, useAmazingRouter };
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";var R=Object.defineProperty;var M=Object.getOwnPropertyDescriptor;var N=Object.getOwnPropertyNames;var O=Object.prototype.hasOwnProperty;var j=(e,t)=>{for(var r in t)R(e,r,{get:t[r],enumerable:!0})},g=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of N(t))!O.call(e,o)&&o!==r&&R(e,o,{get:()=>t[o],enumerable:!(n=M(t,o))||n.enumerable});return e},u=(e,t,r)=>(g(e,t,"default"),r&&g(r,t,"default"));var v=e=>g(R({},"__esModule",{value:!0}),e);var i={};j(i,{AmazingProvider:()=>S,transformRoutes:()=>d,useAmazingMeta:()=>F,useAmazingRouter:()=>k});module.exports=v(i);var p=require("react-router"),k=()=>{let e=(0,p.useNavigate)();return{pathname:(0,p.useLocation)().pathname,push:r=>e(r),replace:r=>e(r,{replace:!0}),back:()=>e(-1)}};var P=require("react-router"),F=()=>{let e=(0,P.useMatches)(),r=e[e.length-1]?.handle;return{title:r?.meta?.title||"React - Amazing Router",description:r?.meta?.description||"",...r?.meta}};var c=require("react"),z=require("react-router"),m=require("react/jsx-runtime");function L(e,t){let r=e.replace(/\\/g,"/"),n=t[r];return n?(0,c.lazy)(n):(console.warn(`[Amazing Router] Missing route file for: ${r}`),null)}function $(e,t){let r=e.replace(/\\/g,"/"),n=t[r];return n?(0,c.lazy)(()=>n().then(o=>({default:o.middleware}))):(console.warn(`[Amazing Router] Missing middleware file for: ${r}`),null)}function y(e,t,r=!1){let n=r?e.pagePath:e.layoutPath||e.pagePath;if(!n)return(0,m.jsx)(z.Outlet,{});let o=L(n,t);if(!o)return(0,m.jsx)(z.Outlet,{});let s=(0,m.jsx)(c.Suspense,{fallback:null,children:(0,m.jsx)(o,{})});if(e.middlewarePath){let a=$(e.middlewarePath,t);if(a)return(0,m.jsx)(c.Suspense,{fallback:null,children:(0,m.jsx)(a,{})})}return s}function d(e,t,r=!1){let n=[];for(let o of e){let s=r&&o.path?o.path.replace(/^\//,""):o.path,a={id:o.id,element:y(o,t),handle:{meta:o.meta},children:[],...s!==void 0?{path:s}:{}};o.layoutPath&&o.pagePath&&a.children.push({id:`${o.id}-index`,index:!0,element:y({...o,layoutPath:void 0},t),handle:{meta:o.meta}}),o.children&&o.children.length>0&&a.children.push(...d(o.children,t,!0)),a.children&&a.children.length===0&&delete a.children,n.push(a)}return n}var l=require("react"),f=require("react-router");var x=require("react/jsx-runtime"),S=({loadingElement:e=null})=>{let[t,r]=(0,l.useState)([]),[n,o]=(0,l.useState)({});(0,l.useEffect)(()=>{let a=Date.now(),A=`/.amazing-router/routes.json?t=${a}`,b=`/.amazing-router/routeFiles.ts?t=${a}`;Promise.all([import(A),import(b)]).then(([h,w])=>{r(h.default||[]),o(w.routeFiles||{})}).catch(h=>{console.warn("[Amazing Router] Could not find the generated route files. Did you run the core build or forgot to configure the Vite/Webpack plugin?",h)})},[]);let s=(0,l.useMemo)(()=>{if(!t||t.length===0||Object.keys(n).length===0)return null;let a=d(t,n);return(0,f.createBrowserRouter)(a)},[t,n]);return s?(0,x.jsx)(f.RouterProvider,{router:s}):e};u(i,require("react-router"),module.exports);0&&(module.exports={AmazingProvider,transformRoutes,useAmazingMeta,useAmazingRouter,...require("react-router")});
package/dist/index.mjs ADDED
@@ -0,0 +1 @@
1
+ import{useNavigate as z,useLocation as P}from"react-router";var v=()=>{let t=z();return{pathname:P().pathname,push:r=>t(r),replace:r=>t(r,{replace:!0}),back:()=>t(-1)}};import{useMatches as y}from"react-router";var L=()=>{let t=y(),r=t[t.length-1]?.handle;return{title:r?.meta?.title||"React - Amazing Router",description:r?.meta?.description||"",...r?.meta}};import{lazy as d,Suspense as l}from"react";import{Outlet as c}from"react-router";import{jsx as s}from"react/jsx-runtime";function x(t,o){let r=t.replace(/\\/g,"/"),n=o[r];return n?d(n):(console.warn(`[Amazing Router] Missing route file for: ${r}`),null)}function A(t,o){let r=t.replace(/\\/g,"/"),n=o[r];return n?d(()=>n().then(e=>({default:e.middleware}))):(console.warn(`[Amazing Router] Missing middleware file for: ${r}`),null)}function p(t,o,r=!1){let n=r?t.pagePath:t.layoutPath||t.pagePath;if(!n)return s(c,{});let e=x(n,o);if(!e)return s(c,{});let i=s(l,{fallback:null,children:s(e,{})});if(t.middlewarePath){let a=A(t.middlewarePath,o);if(a)return s(l,{fallback:null,children:s(a,{})})}return i}function m(t,o,r=!1){let n=[];for(let e of t){let i=r&&e.path?e.path.replace(/^\//,""):e.path,a={id:e.id,element:p(e,o),handle:{meta:e.meta},children:[],...i!==void 0?{path:i}:{}};e.layoutPath&&e.pagePath&&a.children.push({id:`${e.id}-index`,index:!0,element:p({...e,layoutPath:void 0},o),handle:{meta:e.meta}}),e.children&&e.children.length>0&&a.children.push(...m(e.children,o,!0)),a.children&&a.children.length===0&&delete a.children,n.push(a)}return n}import{useState as f,useEffect as b,useMemo as w}from"react";import{createBrowserRouter as M,RouterProvider as N}from"react-router";import{jsx as O}from"react/jsx-runtime";var U=({loadingElement:t=null})=>{let[o,r]=f([]),[n,e]=f({});b(()=>{let a=Date.now(),h=`/.amazing-router/routes.json?t=${a}`,g=`/.amazing-router/routeFiles.ts?t=${a}`;Promise.all([import(h),import(g)]).then(([u,R])=>{r(u.default||[]),e(R.routeFiles||{})}).catch(u=>{console.warn("[Amazing Router] Could not find the generated route files. Did you run the core build or forgot to configure the Vite/Webpack plugin?",u)})},[]);let i=w(()=>{if(!o||o.length===0||Object.keys(n).length===0)return null;let a=m(o,n);return M(a)},[o,n]);return i?O(N,{router:i}):t};export*from"react-router";export{U as AmazingProvider,m as transformRoutes,L as useAmazingMeta,v as useAmazingRouter};
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "@amazing-router/react",
3
+ "version": "1.1.0",
4
+ "description": "React adapter for Amazing Router Core using React Router under the hood",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "import": "./dist/index.mjs",
11
+ "require": "./dist/index.js",
12
+ "types": "./dist/index.d.ts"
13
+ }
14
+ },
15
+ "scripts": {
16
+ "build": "tsup src/index.ts --format cjs,esm --dts --clean --external react",
17
+ "dev": "tsup src/index.ts --format cjs,esm --watch --dts --external react",
18
+ "lint": "eslint src",
19
+ "test": "vitest run",
20
+ "test:ui": "vitest"
21
+ },
22
+ "peerDependencies": {
23
+ "react": "^19.0.0",
24
+ "react-dom": "^19.0.0",
25
+ "react-router": "^7.0.0"
26
+ },
27
+ "dependencies": {
28
+ "@amazing-router/core": "^1.5.4"
29
+ },
30
+ "devDependencies": {
31
+ "@testing-library/jest-dom": "^6.9.1",
32
+ "@testing-library/react": "^16.3.2",
33
+ "@types/react": "^19.0.0",
34
+ "@types/react-dom": "^19.0.0",
35
+ "@vitejs/plugin-react": "^6.0.1",
36
+ "jsdom": "^29.0.1",
37
+ "react": "^19.0.0",
38
+ "react-dom": "^19.0.0",
39
+ "react-router": "^7.0.0",
40
+ "tsup": "^8.3.5",
41
+ "typescript": "^5.7.3",
42
+ "vitest": "^4.1.2"
43
+ },
44
+ "files": [
45
+ "dist"
46
+ ],
47
+ "keywords": [
48
+ "react",
49
+ "router",
50
+ "amazing-router",
51
+ "file-system-routing"
52
+ ],
53
+ "author": "AdelGann",
54
+ "license": "MIT"
55
+ }