@fastify/autoload
Advanced tools
Comparing version 5.6.0 to 5.7.0
32
index.js
@@ -132,3 +132,3 @@ 'use strict' | ||
async function findPlugins (dir, options, hookedAccumulator = {}, prefix, depth = 0, hooks = []) { | ||
const { indexPattern, ignorePattern, scriptPattern, dirNameRoutePrefix, maxDepth, autoHooksPattern } = options | ||
const { indexPattern, ignorePattern, ignoreFilter, matchFilter, scriptPattern, dirNameRoutePrefix, maxDepth, autoHooksPattern } = options | ||
const list = await readdir(dir, { withFileTypes: true }) | ||
@@ -175,3 +175,3 @@ let currentHooks = [] | ||
hookedAccumulator[prefix || '/'].plugins.push({ file, type, prefix }) | ||
accumulatePlugin({ file, type }, indexDirent.name) | ||
const hasDirectory = list.find((dirent) => dirent.isDirectory()) | ||
@@ -231,3 +231,3 @@ | ||
if (!autoHooksPattern.test(dirent.name)) { | ||
hookedAccumulator[prefix || '/'].plugins.push({ file, type, prefix }) | ||
accumulatePlugin({ file, type }, dirent.name) | ||
} | ||
@@ -239,2 +239,16 @@ } | ||
return hookedAccumulator | ||
function accumulatePlugin ({ file, type }, direntName = 'index.ts') { | ||
const routePath = `${prefix ?? ''}/${direntName}` | ||
if (matchFilter && !filterPath(routePath, matchFilter)) { | ||
return | ||
} | ||
if (ignoreFilter && filterPath(routePath, ignoreFilter)) { | ||
return | ||
} | ||
hookedAccumulator[prefix || '/'].plugins.push({ file, type, prefix }) | ||
} | ||
} | ||
@@ -322,2 +336,14 @@ | ||
function filterPath (path, filter) { | ||
if (typeof filter === 'string') { | ||
return path.includes(filter) | ||
} | ||
if (filter instanceof RegExp) { | ||
return filter.test(path) | ||
} | ||
return filter(path) | ||
} | ||
/** | ||
@@ -324,0 +350,0 @@ * Used to determine if the contents of a required autoloaded file matches |
{ | ||
"name": "@fastify/autoload", | ||
"version": "5.6.0", | ||
"version": "5.7.0", | ||
"description": "Require all plugins in a directory", | ||
@@ -20,2 +20,3 @@ "main": "index.js", | ||
"typescript:vitest": "vitest run", | ||
"typescript:vitest:dev": "vitest", | ||
"unit": "node scripts/unit.js", | ||
@@ -63,10 +64,10 @@ "unit:with-modules": "tap test/commonjs/*.js test/module/*.js test/typescript/*.ts" | ||
"ts-node-dev": "^2.0.0", | ||
"tsd": "^0.24.1", | ||
"tsd": "^0.25.0", | ||
"tsm": "^2.2.1", | ||
"tsx": "^3.7.1", | ||
"typescript": "^4.5.4", | ||
"esbuild": "^0.15.14", | ||
"esbuild": "^0.16.4", | ||
"esbuild-register": "^3.4.1", | ||
"vite": "^3.1.7", | ||
"vitest": "^0.25.2" | ||
"vite": "^4.0.0", | ||
"vitest": "^0.26.2" | ||
}, | ||
@@ -73,0 +74,0 @@ "dependencies": { |
@@ -112,8 +112,27 @@ # @fastify/autoload | ||
- `matchFilter` (optional) - Filter matching any path that should be loaded. Can be a RegExp, a string or a function returning a boolean. | ||
- `ignorePattern` (optional) - Regex matching any file that should not be loaded | ||
```js | ||
fastify.register(autoLoad, { | ||
dir: path.join(__dirname, 'plugins'), | ||
matchFilter: (path) => path.split("/").at(-2) === "handlers" | ||
}) | ||
``` | ||
- `ignoreFilter` (optional) - Filter matching any path that should not be loaded. Can be a RegExp, a string or a function returning a boolean. | ||
```js | ||
fastify.register(autoLoad, { | ||
dir: path.join(__dirname, 'plugins'), | ||
ignoreFilter: (path) => path.endsWith('.spec.js') | ||
}) | ||
``` | ||
- `ignorePattern` (optional) - RegExp matching any file or folder that should not be loaded. | ||
```js | ||
fastify.register(autoLoad, { | ||
dir: path.join(__dirname, 'plugins'), | ||
ignorePattern: /.*(test|spec).js/ | ||
@@ -123,2 +142,3 @@ }) | ||
- `indexPattern` (optional) - Regex to override the `index.js` naming convention | ||
@@ -125,0 +145,0 @@ |
@@ -7,2 +7,3 @@ import { FastifyPluginCallback, FastifyPluginOptions } from 'fastify' | ||
type RewritePrefix = (folderParent: string, folderName: string) => string | boolean | ||
type Filter = string | RegExp | ((value: {file: string; path: string}) => boolean) | ||
@@ -12,2 +13,4 @@ export interface AutoloadPluginOptions { | ||
dirNameRoutePrefix?: boolean | RewritePrefix | ||
ignoreFilter?: Filter | ||
matchFilter?: Filter | ||
ignorePattern?: RegExp | ||
@@ -14,0 +17,0 @@ scriptPattern?: RegExp |
@@ -64,2 +64,17 @@ import fastify, { FastifyInstance, FastifyPluginCallback } from 'fastify' | ||
} | ||
const opt9: AutoloadPluginOptions = { | ||
dir: 'test', | ||
ignoreFilter: /test/, | ||
matchFilter: /handler/ | ||
} | ||
const opt10: AutoloadPluginOptions = { | ||
dir: 'test', | ||
ignoreFilter: 'test', | ||
matchFilter: 'handler' | ||
} | ||
const opt11: AutoloadPluginOptions = { | ||
dir: 'test', | ||
ignoreFilter: ({file}) => file.endsWith('.spec.ts'), | ||
matchFilter: ({path}) => path.split("/").at(-2) === 'handlers' | ||
} | ||
app.register(fastifyAutoloadDefault, opt1) | ||
@@ -72,2 +87,5 @@ app.register(fastifyAutoloadDefault, opt2) | ||
app.register(fastifyAutoloadDefault, opt7) | ||
app.register(fastifyAutoloadDefault, opt8) | ||
app.register(fastifyAutoloadDefault, opt8) | ||
app.register(fastifyAutoloadDefault, opt9) | ||
app.register(fastifyAutoloadDefault, opt10) | ||
app.register(fastifyAutoloadDefault, opt11) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
120481
239
3524
541