-
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
65c3dd1
commit 1370697
Showing
9 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
'use strict' | ||
|
||
module.exports = async function (app, opts) { | ||
app.addHook('onRequest', async (req, reply) => { | ||
req.hooked = req.hooked || [] | ||
req.hooked.push('root') | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict' | ||
|
||
module.exports = async function (app, opts, next) { | ||
app.get('/', async function (req, reply) { | ||
reply.status(200).send({ path: req.url, hooked: req.hooked, params: req.params }) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict' | ||
|
||
module.exports = async function (app, opts, next) { | ||
app.get('/', async function (req, reply) { | ||
reply.status(200).send({ path: req.url, hooked: req.hooked }) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict' | ||
|
||
module.exports = async function (app, opts, next) { | ||
app.get('/', async function (req, reply) { | ||
reply.status(200).send({ path: req.url }) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
'use strict' | ||
|
||
const t = require('tap') | ||
const path = require('node:path') | ||
const Fastify = require('fastify') | ||
const autoLoad = require('../../../') | ||
|
||
t.plan(10) | ||
|
||
const app = Fastify() | ||
|
||
app.register(autoLoad, { | ||
dir: path.join(__dirname, 'routes'), | ||
autoHooks: true, | ||
cascadeHooks: true, | ||
routeParams: true | ||
}) | ||
|
||
app.ready(function (err) { | ||
t.error(err) | ||
|
||
app.inject({ | ||
url: '/' | ||
}, function (err, res) { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
t.same(res.json(), { path: '/' }) | ||
}) | ||
|
||
app.inject({ | ||
url: '/entity' | ||
}, function (err, res) { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
t.same(res.json(), { path: '/entity', hooked: ['root'] }) | ||
}) | ||
|
||
app.inject({ | ||
url: '/entity/1' | ||
}, function (err, res) { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
t.same(res.json(), { path: '/entity/1', hooked: ['root'], params: { entity: 1 } }) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
'use strict' | ||
|
||
module.exports = async function (app, opts) { | ||
app.addHook('onRequest', async (req, reply) => { | ||
req.hooked = req.hooked || [] | ||
req.hooked.push('root') | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict' | ||
|
||
module.exports = async function (app, opts, next) { | ||
app.get('/', async function (req, reply) { | ||
reply.status(200).send({ path: req.url, hooked: req.hooked }) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
'use strict' | ||
|
||
module.exports = async function (app, opts, next) { | ||
app.get('/', async function (req, reply) { | ||
reply.status(200).send({ path: req.url }) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
'use strict' | ||
|
||
const t = require('tap') | ||
const path = require('node:path') | ||
const Fastify = require('fastify') | ||
const autoLoad = require('../../../') | ||
|
||
t.plan(13) | ||
|
||
const app = Fastify() | ||
|
||
app.register(autoLoad, { | ||
dir: path.join(__dirname, 'routes'), | ||
autoHooks: true, | ||
options: { prefix: '/api' } | ||
}) | ||
|
||
app.register(autoLoad, { | ||
dir: path.join(__dirname, 'routes'), | ||
autoHooks: true, | ||
options: { prefix: '/prefix/' } | ||
}) | ||
|
||
app.ready(function (err) { | ||
t.error(err) | ||
|
||
app.inject({ | ||
url: '/api' | ||
}, function (err, res) { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
t.same(res.json(), { path: '/api' }) | ||
}) | ||
|
||
app.inject({ | ||
url: '/api/entity' | ||
}, function (err, res) { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
t.same(res.json(), { path: '/api/entity', hooked: ['root'] }) | ||
}) | ||
|
||
app.inject({ | ||
url: '/prefix' | ||
}, function (err, res) { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
t.same(res.json(), { path: '/prefix' }) | ||
}) | ||
|
||
app.inject({ | ||
url: '/prefix/entity' | ||
}, function (err, res) { | ||
t.error(err) | ||
t.equal(res.statusCode, 200) | ||
t.same(res.json(), { path: '/prefix/entity', hooked: ['root'] }) | ||
}) | ||
}) |