@arielgonzaguer/michi-router 1.0.4 → 1.2.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/README.md +275 -277
- package/package.json +40 -40
package/README.md
CHANGED
|
@@ -1,277 +1,275 @@
|
|
|
1
|
-
# Michi Router
|
|
2
|
-
|
|
3
|
-

|
|
4
|
-

|
|
5
|
-

|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
]
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
MIT
|
|
1
|
+
# Michi Router
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+

|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
# ¡IMPORTANTE!
|
|
9
|
+
|
|
10
|
+
Las versiones 1.1.x publicadas antes del 18/marzo/2025 están rotas, contienen errores y han sido eliminadas.
|
|
11
|
+
La versión estable es la 1.2.0.
|
|
12
|
+
Si tienes alguna de estas versiones instaladas (1.1.x), actualízala a la versión 1.2.0 ejecutando el siguiente comando:
|
|
13
|
+
```bash
|
|
14
|
+
npm install @arielgonzaguer/michi-router@latest
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
O:
|
|
18
|
+
```bash
|
|
19
|
+
npm install @arielgonzaguer/michi-router@1.2.0
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Gracias por tu comprensión 😸
|
|
23
|
+
|
|
24
|
+
El router minimalista y simple para React.
|
|
25
|
+
El objetivo principal de esta herramienta es proporcionar la funcionalidad básica de enrutamiento.
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
## Características
|
|
29
|
+
|
|
30
|
+
- Es ideal para proyectos pequeños o que solo necesitan una funcionalidad básica de enrutamiento.
|
|
31
|
+
- No requiere de ninguna configuración adicional.
|
|
32
|
+
- No requiere de ninguna dependencia externa.
|
|
33
|
+
- Creado con TypeScript.
|
|
34
|
+
- Ultra ligero.
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
## Compatibilidad
|
|
38
|
+
|
|
39
|
+
- React 16.8+.
|
|
40
|
+
- Funciona con proyectos creados con Vite, Next.js, etc.
|
|
41
|
+
- Totalmente tipado con TypeScript.
|
|
42
|
+
|
|
43
|
+
## Instalación
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
npm install @arielgonzaguer/michi-router
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Uso básico
|
|
50
|
+
|
|
51
|
+
```jsx
|
|
52
|
+
// src/App.jsx //
|
|
53
|
+
import { RouterProvider} from "@arielgonzaguer/michi-router";
|
|
54
|
+
|
|
55
|
+
// puede crear rutas en un archivo separado o en el mismo archivo
|
|
56
|
+
const routes = [
|
|
57
|
+
{ path: "/", component: <Home /> },
|
|
58
|
+
{ path: "/about", component: <About /> },
|
|
59
|
+
];
|
|
60
|
+
|
|
61
|
+
function App() {
|
|
62
|
+
return (
|
|
63
|
+
<RouterProvider routes={routes}>
|
|
64
|
+
{/* Esto se muestra si no se encuentra una ruta */}
|
|
65
|
+
<div>404: Page not found</div>
|
|
66
|
+
</RouterProvider>
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
### Usando Link
|
|
72
|
+
|
|
73
|
+
#### Usando el componente Link solo con prop to
|
|
74
|
+
```jsx
|
|
75
|
+
import { Link} from "@arielgonzaguer/michi-router";
|
|
76
|
+
|
|
77
|
+
function NavigationComponent() {
|
|
78
|
+
return (
|
|
79
|
+
<nav>
|
|
80
|
+
<Link to="/">Home</Link>
|
|
81
|
+
<Link to="/about">About</Link>
|
|
82
|
+
</nav>
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
#### Usando el componente Link con todas las props
|
|
88
|
+
```jsx
|
|
89
|
+
import { Link} from "@arielgonzaguer/michi-router";
|
|
90
|
+
|
|
91
|
+
<Link
|
|
92
|
+
to="/contact"
|
|
93
|
+
className="nav-link"
|
|
94
|
+
target="_blank"
|
|
95
|
+
rel="noopener noreferrer"
|
|
96
|
+
aria-label="Contacto"
|
|
97
|
+
>
|
|
98
|
+
Contacto
|
|
99
|
+
</Link>
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
### Navegación programática (Hook useNavigate)
|
|
105
|
+
|
|
106
|
+
```jsx
|
|
107
|
+
import { useNavigate } from "@arielgonzaguer/michi-router";
|
|
108
|
+
|
|
109
|
+
function NavigateButton() {
|
|
110
|
+
const navigate = useNavigate();
|
|
111
|
+
return <button onClick={() => navigate("/about")}>Ir a About</button>;
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Organización de rutas en archivos separados
|
|
116
|
+
|
|
117
|
+
Para aplicaciones más grandes, puedes organizar tus rutas en archivos separados:
|
|
118
|
+
|
|
119
|
+
```jsx
|
|
120
|
+
// routes.js
|
|
121
|
+
import Home from "./pages/Home";
|
|
122
|
+
import About from "./pages/About";
|
|
123
|
+
import Products from "./pages/Products";
|
|
124
|
+
import NotFound from "./pages/NotFound";
|
|
125
|
+
|
|
126
|
+
export const routes = [
|
|
127
|
+
{ path: "/", component: <Home /> },
|
|
128
|
+
{ path: "/about", component: <About /> },
|
|
129
|
+
{ path: "/products", component: <Products /> },
|
|
130
|
+
];
|
|
131
|
+
|
|
132
|
+
export const notFoundPage = <NotFound />;
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
```jsx
|
|
136
|
+
// App.jsx
|
|
137
|
+
import { RouterProvider } from "@arielgonzaguer/michi-router";
|
|
138
|
+
import { routes, notFoundPage } from "./routes";
|
|
139
|
+
|
|
140
|
+
function App() {
|
|
141
|
+
return <RouterProvider routes={routes}>{notFoundPage}</RouterProvider>;
|
|
142
|
+
}
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### Integración con sistemas de autenticación
|
|
146
|
+
|
|
147
|
+
```jsx
|
|
148
|
+
import { RouterProvider } from "@arielgonzaguer/michi-router";
|
|
149
|
+
import { useAuth } from "./auth-context";
|
|
150
|
+
|
|
151
|
+
function App() {
|
|
152
|
+
const { isAuthenticated } = useAuth();
|
|
153
|
+
|
|
154
|
+
// Definimos rutas condicionalmente basadas en autenticación
|
|
155
|
+
const routes = [
|
|
156
|
+
{ path: "/", component: <Home /> },
|
|
157
|
+
{ path: "/login", component: <Login /> },
|
|
158
|
+
// Solo agregamos estas rutas si el usuario está autenticado
|
|
159
|
+
...(isAuthenticated
|
|
160
|
+
? [
|
|
161
|
+
{ path: "/profile", component: <Profile /> },
|
|
162
|
+
{ path: "/admin", component: <Admin /> },
|
|
163
|
+
]
|
|
164
|
+
: []),
|
|
165
|
+
];
|
|
166
|
+
|
|
167
|
+
return (
|
|
168
|
+
<RouterProvider routes={routes}>
|
|
169
|
+
<div>404: Page not found</div>
|
|
170
|
+
</RouterProvider>
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
### Usando un Layout General
|
|
176
|
+
|
|
177
|
+
Puedes aplicar un layout común a todas tus rutas:
|
|
178
|
+
|
|
179
|
+
```jsx
|
|
180
|
+
import { RouterProvider } from "@arielgonzaguer/michi-router";
|
|
181
|
+
import MainLayout from "./layouts/MainLayout";
|
|
182
|
+
|
|
183
|
+
const routes = [
|
|
184
|
+
{ path: "/", component: <Home /> },
|
|
185
|
+
{ path: "/about", component: <About /> },
|
|
186
|
+
];
|
|
187
|
+
|
|
188
|
+
function App() {
|
|
189
|
+
return (
|
|
190
|
+
<RouterProvider routes={routes} layout={MainLayout}>
|
|
191
|
+
<div>404: Page not found</div>
|
|
192
|
+
</RouterProvider>
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// Ejemplo de un componente Layout
|
|
197
|
+
function MainLayout({ children }) {
|
|
198
|
+
return (
|
|
199
|
+
<div className="layout">
|
|
200
|
+
<header>
|
|
201
|
+
<nav>
|
|
202
|
+
<Link to="/">Home</Link>
|
|
203
|
+
<Link to="/about">About</Link>
|
|
204
|
+
</nav>
|
|
205
|
+
</header>
|
|
206
|
+
<main>{children}</main>
|
|
207
|
+
<footer>Michi-router 😸</footer>
|
|
208
|
+
</div>
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
## API
|
|
214
|
+
|
|
215
|
+
### `<RouterProvider>`
|
|
216
|
+
|
|
217
|
+
Componente principal para el enrutamiento.
|
|
218
|
+
|
|
219
|
+
**Props:**
|
|
220
|
+
|
|
221
|
+
- `routes`: Array de objetos con `path` y `component`.
|
|
222
|
+
- `children`: Elemento a mostrar cuando no hay ruta coincidente.
|
|
223
|
+
- `layout`: Componente opcional que funciona como layout general para todas las rutas.
|
|
224
|
+
|
|
225
|
+
### `<Link>`
|
|
226
|
+
|
|
227
|
+
Componente para navegación declarativa.
|
|
228
|
+
|
|
229
|
+
**Props:**
|
|
230
|
+
|
|
231
|
+
- `to`: Ruta destino.
|
|
232
|
+
- `children`: Contenido del enlace.
|
|
233
|
+
- `className`: Clases CSS opcionales.
|
|
234
|
+
- `...rest`: Cualquier otra prop válida para elementos `<a>` de HTML.
|
|
235
|
+
|
|
236
|
+
### `useNavigate()`
|
|
237
|
+
|
|
238
|
+
Hook para navegación programática.
|
|
239
|
+
|
|
240
|
+
**Retorna:**
|
|
241
|
+
|
|
242
|
+
- Función navigate que acepta la ruta como parámetro.
|
|
243
|
+
|
|
244
|
+
## Solución de problemas comunes
|
|
245
|
+
|
|
246
|
+
### El componente no se renderiza después de navegar
|
|
247
|
+
|
|
248
|
+
Asegúrate de que la ruta en el array `routes` coincida exactamente con la URL, incluyendo barras diagonales.
|
|
249
|
+
|
|
250
|
+
### Errores con TypeScript
|
|
251
|
+
|
|
252
|
+
Si estás usando TypeScript, asegúrate de importar los tipos correctamente:
|
|
253
|
+
|
|
254
|
+
```typescript
|
|
255
|
+
import {
|
|
256
|
+
RouterProvider,
|
|
257
|
+
Link,
|
|
258
|
+
useNavigate,
|
|
259
|
+
} from "@arielgonzaguer/michi-router";
|
|
260
|
+
import type { RouterProviderProps } from "@arielgonzaguer/michi-router";
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
## Contribuir
|
|
264
|
+
|
|
265
|
+
Las contribuciones son bienvenidas. Por favor, sigue estos pasos:
|
|
266
|
+
|
|
267
|
+
1. Fork el repositorio.
|
|
268
|
+
2. Crea una rama para tu feature (`git checkout -b feature/amazing-feature`).
|
|
269
|
+
3. Haz commit de tus cambios (`git commit -m 'Add some amazing feature'`).
|
|
270
|
+
4. Push a la rama (`git push origin feature/amazing-feature`).
|
|
271
|
+
5. Abre un Pull Request.
|
|
272
|
+
|
|
273
|
+
## Licencia
|
|
274
|
+
|
|
275
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,40 +1,40 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@arielgonzaguer/michi-router",
|
|
3
|
-
"private": false,
|
|
4
|
-
"version": "1.0
|
|
5
|
-
"description": "El router minimalista y simple para React.\nEl objetivo principal de esta herramienta es proporcionar la funcionalidad básica de enrutamiento.",
|
|
6
|
-
"main": "dist/index.js",
|
|
7
|
-
"types": "dist/index.d.ts",
|
|
8
|
-
"files": [
|
|
9
|
-
"dist"
|
|
10
|
-
],
|
|
11
|
-
"scripts": {
|
|
12
|
-
"build": "tsc",
|
|
13
|
-
"prepare": "npm run build",
|
|
14
|
-
"clean": "rimraf dist",
|
|
15
|
-
"prebuild": "npm run clean",
|
|
16
|
-
"prepublishOnly": "npm run build"
|
|
17
|
-
},
|
|
18
|
-
"keywords": [
|
|
19
|
-
"react",
|
|
20
|
-
"router",
|
|
21
|
-
"typescript",
|
|
22
|
-
"michi",
|
|
23
|
-
"cats",
|
|
24
|
-
"gatos"
|
|
25
|
-
],
|
|
26
|
-
"peerDependencies": {
|
|
27
|
-
"react": ">=16.8.0",
|
|
28
|
-
"react-dom": ">=16.8.0"
|
|
29
|
-
},
|
|
30
|
-
"devDependencies": {
|
|
31
|
-
"@types/react": "^18.3.18",
|
|
32
|
-
"@types/react-dom": "^18.3.5",
|
|
33
|
-
"react": "^18.3.1",
|
|
34
|
-
"react-dom": "^18.3.1",
|
|
35
|
-
"rimraf": "^6.0.1",
|
|
36
|
-
"typescript": "^4.9.5"
|
|
37
|
-
},
|
|
38
|
-
"author": "Ariel González Agüero",
|
|
39
|
-
"license": "MIT"
|
|
40
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@arielgonzaguer/michi-router",
|
|
3
|
+
"private": false,
|
|
4
|
+
"version": "1.2.0",
|
|
5
|
+
"description": "El router minimalista y simple para React.\nEl objetivo principal de esta herramienta es proporcionar la funcionalidad básica de enrutamiento.",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "tsc",
|
|
13
|
+
"prepare": "npm run build",
|
|
14
|
+
"clean": "rimraf dist",
|
|
15
|
+
"prebuild": "npm run clean",
|
|
16
|
+
"prepublishOnly": "npm run build"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"react",
|
|
20
|
+
"router",
|
|
21
|
+
"typescript",
|
|
22
|
+
"michi",
|
|
23
|
+
"cats",
|
|
24
|
+
"gatos"
|
|
25
|
+
],
|
|
26
|
+
"peerDependencies": {
|
|
27
|
+
"react": ">=16.8.0",
|
|
28
|
+
"react-dom": ">=16.8.0"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@types/react": "^18.3.18",
|
|
32
|
+
"@types/react-dom": "^18.3.5",
|
|
33
|
+
"react": "^18.3.1",
|
|
34
|
+
"react-dom": "^18.3.1",
|
|
35
|
+
"rimraf": "^6.0.1",
|
|
36
|
+
"typescript": "^4.9.5"
|
|
37
|
+
},
|
|
38
|
+
"author": "Ariel González Agüero",
|
|
39
|
+
"license": "MIT"
|
|
40
|
+
}
|