@fastify/autoload
Advanced tools
| 'use strict' | ||
| module.exports = async function (app) { | ||
| app.get('/entity', async () => ({ ok: true })) | ||
| } | ||
| module.exports.autoPrefix = '/batch' |
| 'use strict' | ||
| const { describe, it } = require('node:test') | ||
| const assert = require('node:assert') | ||
| const path = require('node:path') | ||
| const Fastify = require('fastify') | ||
| const autoLoad = require('../../../') | ||
| describe('Issue 205: append autoPrefix to directory prefixes without breaking defaults', function () { | ||
| it('should keep autoPrefix overriding directory prefixes by default', async function (t) { | ||
| const app = Fastify() | ||
| t.after(() => app.close()) | ||
| app.register(autoLoad, { | ||
| dir: path.join(__dirname, 'routes'), | ||
| options: { prefix: '/hooked-plugin' } | ||
| }) | ||
| await app.ready() | ||
| const overridden = await app.inject({ method: 'GET', url: '/hooked-plugin/batch/entity' }) | ||
| assert.strictEqual(overridden.statusCode, 200) | ||
| assert.deepStrictEqual(overridden.json(), { ok: true }) | ||
| const appended = await app.inject({ method: 'GET', url: '/hooked-plugin/children/batch/entity' }) | ||
| assert.strictEqual(appended.statusCode, 404) | ||
| }) | ||
| it('should concatenate directory prefixes before plugin autoPrefix when appendAutoPrefix is true', async function (t) { | ||
| const app = Fastify() | ||
| t.after(() => app.close()) | ||
| app.register(autoLoad, { | ||
| dir: path.join(__dirname, 'routes'), | ||
| options: { prefix: '/hooked-plugin' }, | ||
| appendAutoPrefix: true | ||
| }) | ||
| await app.ready() | ||
| const appended = await app.inject({ method: 'GET', url: '/hooked-plugin/children/batch/entity' }) | ||
| assert.strictEqual(appended.statusCode, 200) | ||
| assert.deepStrictEqual(appended.json(), { ok: true }) | ||
| const overridden = await app.inject({ method: 'GET', url: '/hooked-plugin/batch/entity' }) | ||
| assert.strictEqual(overridden.statusCode, 404) | ||
| }) | ||
| }) |
@@ -37,2 +37,3 @@ version: 2 | ||
| - dependency-name: "@stylistic/*" | ||
| - dependency-name: "typescript" | ||
| open-pull-requests-limit: 10 | ||
@@ -39,0 +40,0 @@ groups: |
@@ -30,3 +30,3 @@ name: CI | ||
| steps: | ||
| - uses: actions/checkout@v7 | ||
| - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 | ||
| with: | ||
@@ -36,3 +36,3 @@ persist-credentials: false | ||
| - name: Setup Bun | ||
| uses: oven-sh/setup-bun@3d267786b128fe76c2f16a390aa2448b815359f3 | ||
| uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2.2.0 | ||
@@ -49,3 +49,3 @@ - name: Install dependencies | ||
| steps: | ||
| - uses: actions/checkout@v7 | ||
| - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 | ||
| with: | ||
@@ -55,3 +55,3 @@ persist-credentials: false | ||
| - name: Setup Deno | ||
| uses: denoland/setup-deno@e95548e56dfa95d4e1a28d6f422fafe75c4c26fb | ||
| uses: denoland/setup-deno@667a34cdef165d8d2b2e98dde39547c9daac7282 # v2.0.4 | ||
@@ -68,5 +68,5 @@ - name: Install dependencies | ||
| pull-requests: write | ||
| uses: fastify/workflows/.github/workflows/plugins-ci.yml@v6 | ||
| uses: fastify/workflows/.github/workflows/plugins-ci.yml@2073dc8e1f9e172bf42daa3843c9dbd31af1e8cb # v6.0.0 | ||
| with: | ||
| license-check: true | ||
| lint: true |
@@ -19,2 +19,2 @@ name: Lock Threads | ||
| pull-requests: write | ||
| uses: fastify/workflows/.github/workflows/lock-threads.yml@v6 | ||
| uses: fastify/workflows/.github/workflows/lock-threads.yml@2073dc8e1f9e172bf42daa3843c9dbd31af1e8cb # v6.0.0 |
+10
-7
@@ -14,3 +14,4 @@ 'use strict' | ||
| dirNameRoutePrefix: true, | ||
| encapsulate: true | ||
| encapsulate: true, | ||
| appendAutoPrefix: false | ||
| } | ||
@@ -105,3 +106,3 @@ | ||
| handlePrefixConfig({ plugin, pluginOptions, content, directoryPrefix }) | ||
| handlePrefixConfig({ plugin, pluginOptions, content, directoryPrefix, appendAutoPrefix: options.appendAutoPrefix }) | ||
@@ -263,3 +264,3 @@ return { | ||
| function handlePrefixConfig ({ plugin, pluginOptions, content, directoryPrefix }) { | ||
| function handlePrefixConfig ({ plugin, pluginOptions, content, directoryPrefix, appendAutoPrefix }) { | ||
| if (pluginOptions.prefix?.endsWith('/')) { | ||
@@ -269,7 +270,9 @@ pluginOptions.prefix = pluginOptions.prefix.slice(0, -1) | ||
| const autoPrefix = plugin.autoPrefix ?? content.autoPrefix | ||
| let prefix | ||
| if (plugin.autoPrefix !== undefined) { | ||
| prefix = plugin.autoPrefix | ||
| } else if (content.autoPrefix !== undefined) { | ||
| prefix = content.autoPrefix | ||
| if (appendAutoPrefix && autoPrefix !== undefined && directoryPrefix) { | ||
| prefix = `${directoryPrefix}/${autoPrefix}` | ||
| } else if (autoPrefix !== undefined) { | ||
| prefix = autoPrefix | ||
| } else { | ||
@@ -276,0 +279,0 @@ prefix = directoryPrefix |
+1
-1
| { | ||
| "name": "@fastify/autoload", | ||
| "version": "6.4.0", | ||
| "version": "6.5.0", | ||
| "description": "Require all plugins in a directory", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
+38
-0
@@ -130,2 +130,36 @@ # @fastify/autoload | ||
| ### `appendAutoPrefix` (optional) - Default: `false` | ||
| Controls how `autoPrefix` from a plugin file is combined with the directory-derived prefix. | ||
| - `false` (default): `autoPrefix` replaces the directory-derived prefix for that plugin. | ||
| - `true`: the directory-derived prefix is kept, and `autoPrefix` is appended after it. | ||
| Example directory: | ||
| ```text | ||
| routes/ | ||
| children/ | ||
| new-routes.js // exports autoPrefix = '/batch' | ||
| ``` | ||
| With default behavior (`appendAutoPrefix: false`), the route is loaded as: | ||
| ```text | ||
| /your-parent-prefix/batch/entity | ||
| ``` | ||
| With `appendAutoPrefix: true`, the route is loaded as: | ||
| ```text | ||
| /your-parent-prefix/children/batch/entity | ||
| ``` | ||
| ```js | ||
| fastify.register(autoLoad, { | ||
| dir: path.join(__dirname, 'routes'), | ||
| appendAutoPrefix: true | ||
| }) | ||
| ``` | ||
| ### `matchFilter` (optional) | ||
@@ -229,2 +263,4 @@ | ||
| By default, `plugin.autoPrefix` replaces directory-derived prefixes. Set `appendAutoPrefix: true` to keep directory prefixes and append `plugin.autoPrefix` after them. | ||
| ```js | ||
@@ -414,2 +450,4 @@ // index.js | ||
| `plugin.autoPrefix` overrides directory-derived prefixes by default. Use the global `appendAutoPrefix` option to append `plugin.autoPrefix` to directory prefixes instead. | ||
| ```js | ||
@@ -416,0 +454,0 @@ module.exports = function (fastify, opts, next) { |
+1
-0
@@ -12,2 +12,3 @@ import { FastifyPluginCallback, FastifyPluginOptions } from 'fastify' | ||
| dirNameRoutePrefix?: boolean | RewritePrefix | ||
| appendAutoPrefix?: boolean | ||
| ignoreFilter?: Filter | ||
@@ -14,0 +15,0 @@ matchFilter?: Filter |
Potential vulnerability
Supply chain riskInitial human review suggests the presence of a vulnerability in this package. It is pending further analysis and confirmation.
Potential vulnerability
Supply chain riskInitial human review suggests the presence of a vulnerability in this package. It is pending further analysis and confirmation.
185543
1.87%300
0.67%4860
0.93%673
5.98%