@efebia/fastify-auto-import 0.0.3 → 1.0.3
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 +76 -0
- package/lib/cjs/globber.js +20 -11
- package/lib/cjs/index.d.ts +1 -1
- package/lib/cjs/index.js +1 -1
- package/lib/esm/globber.js +1 -1
- package/lib/esm/index.d.ts +1 -1
- package/lib/esm/index.js +1 -1
- package/package.json +4 -4
package/README.md
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# @efebia/fastify-auto-import
|
|
2
|
+
|
|
3
|
+
Fastify plugin that automatically discovers and registers route plugins from a directory.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @efebia/fastify-auto-import
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import Fastify from 'fastify';
|
|
15
|
+
import autoImport from '@efebia/fastify-auto-import';
|
|
16
|
+
|
|
17
|
+
const fastify = Fastify();
|
|
18
|
+
|
|
19
|
+
await fastify.register(autoImport, {
|
|
20
|
+
// Required: the starting directory (use import.meta.url in ESM)
|
|
21
|
+
startingDirectory: import.meta.url,
|
|
22
|
+
// Directory to scan, relative to startingDirectory (default: 'src/plugins')
|
|
23
|
+
directory: 'src/plugins',
|
|
24
|
+
// File name to import from each subdirectory (default: 'routes')
|
|
25
|
+
routeFile: 'routes',
|
|
26
|
+
// Subdirectories to skip (default: [])
|
|
27
|
+
excludedDirectories: ['_shared'],
|
|
28
|
+
});
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Given the following structure:
|
|
32
|
+
|
|
33
|
+
```
|
|
34
|
+
src/plugins/
|
|
35
|
+
users/
|
|
36
|
+
routes.ts
|
|
37
|
+
orders/
|
|
38
|
+
routes.ts
|
|
39
|
+
_shared/
|
|
40
|
+
utils.ts ← skipped
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
The plugin will register `users/routes.ts` and `orders/routes.ts` as Fastify plugins.
|
|
44
|
+
|
|
45
|
+
Each route file must export a default Fastify plugin:
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
// src/plugins/users/routes.ts
|
|
49
|
+
import type { FastifyPluginAsync } from 'fastify';
|
|
50
|
+
|
|
51
|
+
const plugin: FastifyPluginAsync = async (fastify) => {
|
|
52
|
+
fastify.get('/users', async () => ({ users: [] }));
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export default plugin;
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## API
|
|
59
|
+
|
|
60
|
+
### `autoImport` (default export)
|
|
61
|
+
|
|
62
|
+
A `fastify-plugin` compatible plugin. Register it with `fastify.register()`.
|
|
63
|
+
|
|
64
|
+
**Options** (`Globber`):
|
|
65
|
+
|
|
66
|
+
| Option | Type | Default | Description |
|
|
67
|
+
|--------|------|---------|-------------|
|
|
68
|
+
| `startingDirectory` | `string` | — | **Required.** Base directory. Pass `import.meta.url` (ESM) or `__dirname` (CJS). |
|
|
69
|
+
| `directory` | `string` | `'src/plugins'` | Directory to scan for route subdirectories, relative to `startingDirectory`. |
|
|
70
|
+
| `routeFile` | `string` | `'routes'` | File name (without extension) to import from each subdirectory. |
|
|
71
|
+
| `excludedDirectories` | `string[]` | `[]` | Subdirectory names to skip. |
|
|
72
|
+
| `log` | `pino.BaseLogger` | Fastify logger | Logger instance for import errors. |
|
|
73
|
+
|
|
74
|
+
## License
|
|
75
|
+
|
|
76
|
+
MIT
|
package/lib/cjs/globber.js
CHANGED
|
@@ -15,13 +15,23 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
|
|
|
15
15
|
}) : function(o, v) {
|
|
16
16
|
o["default"] = v;
|
|
17
17
|
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
};
|
|
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
|
+
})();
|
|
25
35
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
36
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
37
|
};
|
|
@@ -50,20 +60,19 @@ const globFiles = async (opts) => {
|
|
|
50
60
|
const filteredDirectories = routesDirectories.filter(dir => !opts.excludedDirectories.includes(dir));
|
|
51
61
|
const importedFiles = await (0, serial_js_1.serial)(filteredDirectories.map((dir) => async () => {
|
|
52
62
|
var _a;
|
|
53
|
-
var _b;
|
|
54
63
|
const osPath = node_path_1.default.join(pluginsDirectory, dir, packageType === 'module' ? `${opts.routeFile.replace(".js", "")}.js` : opts.routeFile.replace(".js", ""));
|
|
55
64
|
const href = node_url_1.default.pathToFileURL(osPath).href;
|
|
56
65
|
try {
|
|
57
66
|
if (packageType === "module") {
|
|
58
|
-
const importedPlugin = await
|
|
67
|
+
const importedPlugin = await Promise.resolve(`${href}`).then(s => __importStar(require(s)));
|
|
59
68
|
return importedPlugin.default;
|
|
60
69
|
}
|
|
61
70
|
return require(osPath);
|
|
62
71
|
}
|
|
63
72
|
catch (e) {
|
|
64
73
|
console.log(e);
|
|
65
|
-
(
|
|
66
|
-
|
|
74
|
+
(_a = opts.log) === null || _a === void 0 ? void 0 : _a.error({ error: e, path: osPath }, "@efebia/fastify-auto-import error on importing plugin");
|
|
75
|
+
throw e;
|
|
67
76
|
}
|
|
68
77
|
}));
|
|
69
78
|
return importedFiles.filter((e) => e);
|
package/lib/cjs/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { Globber } from './globber.js';
|
|
2
|
-
declare const plugin: import("fastify").
|
|
2
|
+
declare const plugin: import("fastify").FastifyPluginCallback<Globber, import("fastify").RawServerDefault, import("fastify").FastifyTypeProviderDefault, import("fastify").FastifyBaseLogger>;
|
|
3
3
|
export default plugin;
|
package/lib/cjs/index.js
CHANGED
package/lib/esm/globber.js
CHANGED
|
@@ -33,7 +33,7 @@ export const globFiles = async (opts) => {
|
|
|
33
33
|
catch (e) {
|
|
34
34
|
console.log(e);
|
|
35
35
|
(_a = opts.log) === null || _a === void 0 ? void 0 : _a.error({ error: e, path: osPath }, "@efebia/fastify-auto-import error on importing plugin");
|
|
36
|
-
|
|
36
|
+
throw e;
|
|
37
37
|
}
|
|
38
38
|
}));
|
|
39
39
|
return importedFiles.filter((e) => e);
|
package/lib/esm/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { Globber } from './globber.js';
|
|
2
|
-
declare const plugin: import("fastify").
|
|
2
|
+
declare const plugin: import("fastify").FastifyPluginCallback<Globber, import("fastify").RawServerDefault, import("fastify").FastifyTypeProviderDefault, import("fastify").FastifyBaseLogger>;
|
|
3
3
|
export default plugin;
|
package/lib/esm/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@efebia/fastify-auto-import",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"types": "./lib/index.d.ts",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"fastify": "^
|
|
10
|
-
"fastify-plugin": "^
|
|
9
|
+
"fastify": "^5.3.0",
|
|
10
|
+
"fastify-plugin": "^5.0.1",
|
|
11
11
|
"pino": "^8.8.0",
|
|
12
12
|
"pkg-up": "^3.1.0"
|
|
13
13
|
},
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"@types/node": "^18.11.18",
|
|
17
17
|
"jest": "^29.3.1",
|
|
18
18
|
"ts-jest": "^29.0.3",
|
|
19
|
-
"typescript": "
|
|
19
|
+
"typescript": "~5.8.3"
|
|
20
20
|
},
|
|
21
21
|
"files": [
|
|
22
22
|
"/lib"
|