exrout 1.0.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 +27 -0
- package/README.md +122 -0
- package/dist/cjs/index.d.ts +8 -0
- package/dist/cjs/index.js +20 -0
- package/dist/cjs/loadRoutes.d.ts +2 -0
- package/dist/cjs/loadRoutes.js +69 -0
- package/dist/cjs/utils.d.ts +2 -0
- package/dist/cjs/utils.js +10 -0
- package/package.json +29 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
|
|
2
|
+
---
|
|
3
|
+
|
|
4
|
+
## 6️⃣ LICENSE (MIT)
|
|
5
|
+
|
|
6
|
+
```text
|
|
7
|
+
MIT License
|
|
8
|
+
|
|
9
|
+
Copyright (c) 2025 [Marouane Akrich]
|
|
10
|
+
|
|
11
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
12
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
13
|
+
in the Software without restriction, including without limitation the rights
|
|
14
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
15
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
16
|
+
furnished to do so, subject to the following conditions:
|
|
17
|
+
|
|
18
|
+
The above copyright notice and this permission notice shall be included in all
|
|
19
|
+
copies or substantial portions of the Software.
|
|
20
|
+
|
|
21
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
22
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
23
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
24
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER
|
|
25
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
26
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
27
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# EXROUT
|
|
2
|
+
|
|
3
|
+
Automatically load and register Express routes based on your folder structure.
|
|
4
|
+
Stop manually importing every route file.
|
|
5
|
+
|
|
6
|
+
## ✨ Features
|
|
7
|
+
|
|
8
|
+
- 📂 Auto-load routes from folders & subfolders
|
|
9
|
+
- 📄 `index.js / index.ts` maps to the folder root
|
|
10
|
+
- 🔁 Supports JavaScript **and** TypeScript
|
|
11
|
+
- 🔌 Works with **CommonJS** and **ES Modules**
|
|
12
|
+
- 🧩 Global middleware support
|
|
13
|
+
- 🧭 Optional route prefix (e.g. `/api`)
|
|
14
|
+
- 🧠 File-based route params (`[id].ts` → `:id`)
|
|
15
|
+
- ⚡ Async / dynamic route modules
|
|
16
|
+
- 🔥 Optional route logging
|
|
17
|
+
- ♻️ Hot reload in development
|
|
18
|
+
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
## 📦 Installation
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
npm install exrout
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
# 🚀 Basic Usage
|
|
28
|
+
ES Modules
|
|
29
|
+
```js
|
|
30
|
+
import express from "express";
|
|
31
|
+
import autoRoutes from "express-auto-routes";
|
|
32
|
+
|
|
33
|
+
const app = express();
|
|
34
|
+
|
|
35
|
+
autoRoutes(app, "./routes", { prefix: "/api" });
|
|
36
|
+
|
|
37
|
+
app.listen(3000);
|
|
38
|
+
```
|
|
39
|
+
CommonJS
|
|
40
|
+
```js
|
|
41
|
+
const express = require("express");
|
|
42
|
+
const autoRoutes = require("express-auto-routes");
|
|
43
|
+
|
|
44
|
+
const app = express();
|
|
45
|
+
|
|
46
|
+
autoRoutes(app, "./routes");
|
|
47
|
+
|
|
48
|
+
app.listen(3000);
|
|
49
|
+
```
|
|
50
|
+
# 📁 Folder Structure → Routes
|
|
51
|
+
```bash
|
|
52
|
+
routes/
|
|
53
|
+
├── users.ts → /users
|
|
54
|
+
├── posts.ts → /posts
|
|
55
|
+
└── auth/
|
|
56
|
+
├── index.ts → /auth
|
|
57
|
+
├── login.ts → /auth/login
|
|
58
|
+
└── [id].ts → /auth/:id
|
|
59
|
+
```
|
|
60
|
+
# 🧩 Writing a Route
|
|
61
|
+
```ts
|
|
62
|
+
import { Router } from "express";
|
|
63
|
+
|
|
64
|
+
const router = Router();
|
|
65
|
+
|
|
66
|
+
router.get("/", (req, res) => {
|
|
67
|
+
res.json({ message: "Hello users" });
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
export default router;
|
|
71
|
+
```
|
|
72
|
+
# ⚙️ Options
|
|
73
|
+
```ts
|
|
74
|
+
autoRoutes(app, "./routes", {
|
|
75
|
+
prefix: "/api",
|
|
76
|
+
middleware: [logger],
|
|
77
|
+
log: true,
|
|
78
|
+
watch: true
|
|
79
|
+
});
|
|
80
|
+
```
|
|
81
|
+
## Available Options
|
|
82
|
+
| Option | Type | Description |
|
|
83
|
+
| ------------ | ------------ | --------------------------------------- |
|
|
84
|
+
| `prefix` | `string` | Prefix all routes (e.g. `/api`) |
|
|
85
|
+
| `middleware` | `Function[]` | Global middleware applied to all routes |
|
|
86
|
+
| `log` | `boolean` | Log loaded routes to the console |
|
|
87
|
+
| `watch` | `boolean` | Hot reload routes in development |
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
# 🧠 File-based Route Params
|
|
91
|
+
```bash
|
|
92
|
+
routes/
|
|
93
|
+
└── users/
|
|
94
|
+
└── [id].ts → /users/:id
|
|
95
|
+
ts
|
|
96
|
+
Copy code
|
|
97
|
+
router.get("/", (req, res) => {
|
|
98
|
+
res.json({ id: req.params.id });
|
|
99
|
+
});
|
|
100
|
+
```
|
|
101
|
+
# ⚡ Async Route Modules
|
|
102
|
+
```ts
|
|
103
|
+
import { Router } from "express";
|
|
104
|
+
|
|
105
|
+
export default async function createRouter() {
|
|
106
|
+
const router = Router();
|
|
107
|
+
|
|
108
|
+
router.get("/", async (req, res) => {
|
|
109
|
+
res.json({ status: "ok" });
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
return router;
|
|
113
|
+
}
|
|
114
|
+
```
|
|
115
|
+
# 🔥 Hot Reload (Dev Only)
|
|
116
|
+
```ts
|
|
117
|
+
autoRoutes(app, "./routes", { watch: true });
|
|
118
|
+
```
|
|
119
|
+
Routes automatically reload when files change.
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Express, RequestHandler } from "express";
|
|
2
|
+
export interface Options {
|
|
3
|
+
prefix?: string;
|
|
4
|
+
middleware?: RequestHandler[];
|
|
5
|
+
log?: boolean;
|
|
6
|
+
watch?: boolean;
|
|
7
|
+
}
|
|
8
|
+
export default function autoRoutes(app: Express, routesDir: string, options?: Options): void;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.default = autoRoutes;
|
|
7
|
+
const loadRoutes_1 = __importDefault(require("./loadRoutes"));
|
|
8
|
+
function autoRoutes(app, routesDir, options = {}) {
|
|
9
|
+
const { prefix = "", middleware = [], log = false, watch = false } = options;
|
|
10
|
+
(0, loadRoutes_1.default)(app, routesDir, prefix, middleware, log);
|
|
11
|
+
if (watch) {
|
|
12
|
+
const chokidar = require("chokidar");
|
|
13
|
+
const watcher = chokidar.watch(routesDir);
|
|
14
|
+
watcher.on("all", async () => {
|
|
15
|
+
console.clear();
|
|
16
|
+
console.log("[auto-routes] Reloading routes...");
|
|
17
|
+
(0, loadRoutes_1.default)(app, routesDir, prefix, middleware, log);
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.default = loadRoutes;
|
|
40
|
+
const fs_1 = __importDefault(require("fs"));
|
|
41
|
+
const path_1 = __importDefault(require("path"));
|
|
42
|
+
const utils_1 = require("./utils");
|
|
43
|
+
async function loadRoutes(app, dir, baseRoute, middleware, log = false) {
|
|
44
|
+
for (const file of fs_1.default.readdirSync(dir)) {
|
|
45
|
+
const fullPath = path_1.default.join(dir, file);
|
|
46
|
+
const stat = fs_1.default.statSync(fullPath);
|
|
47
|
+
if (stat.isDirectory()) {
|
|
48
|
+
await loadRoutes(app, fullPath, (0, utils_1.cleanSlashes)(`${baseRoute}/${(0, utils_1.normalizeRouteName)(file)}`), middleware, log);
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if (!file.match(/\.(js|ts)$/))
|
|
52
|
+
continue;
|
|
53
|
+
const name = file.replace(/\.(js|ts)$/, "");
|
|
54
|
+
const isIndex = name === "index";
|
|
55
|
+
const routePath = (0, utils_1.cleanSlashes)(isIndex
|
|
56
|
+
? baseRoute
|
|
57
|
+
: `${baseRoute}/${(0, utils_1.normalizeRouteName)(name)}`);
|
|
58
|
+
let mod = await Promise.resolve(`${fullPath}`).then(s => __importStar(require(s)));
|
|
59
|
+
let router = mod.default || mod;
|
|
60
|
+
// Async router factory support
|
|
61
|
+
if (typeof router === "function" && router.length === 0) {
|
|
62
|
+
router = await router();
|
|
63
|
+
}
|
|
64
|
+
app.use(routePath, ...middleware, router);
|
|
65
|
+
if (log) {
|
|
66
|
+
console.log(`[auto-routes] ${routePath}`);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.normalizeRouteName = normalizeRouteName;
|
|
4
|
+
exports.cleanSlashes = cleanSlashes;
|
|
5
|
+
function normalizeRouteName(name) {
|
|
6
|
+
return name.replace(/\[(.+?)\]/g, ":$1");
|
|
7
|
+
}
|
|
8
|
+
function cleanSlashes(path) {
|
|
9
|
+
return path.replace(/\/+/g, "/");
|
|
10
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "exrout",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Automatically load and register Express routes from folder structure",
|
|
5
|
+
"main": "dist/index.cjs.js",
|
|
6
|
+
"module": "dist/index.esm.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "tsc && tsc-esm",
|
|
13
|
+
"test": "jest"
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"express": "^4.18.2"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@types/express": "^4.17.17",
|
|
20
|
+
"@types/jest": "^29.5.3",
|
|
21
|
+
"jest": "^29.6.1",
|
|
22
|
+
"supertest": "^6.3.3",
|
|
23
|
+
"typescript": "^5.2.2",
|
|
24
|
+
"tsc-alias": "^1.8.16"
|
|
25
|
+
},
|
|
26
|
+
"keywords": ["express", "routes", "auto", "typescript", "commonjs", "esm"],
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"type": "module"
|
|
29
|
+
}
|