-
-
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.
* route prefixing feature * route prefix tests * adding test for parameterized route with no root action * adding cjs test for route parameters * adding route parameter documentation * moving routeParamPattern out of options scope
- Loading branch information
1 parent
56dd9e5
commit ac6e74f
Showing
15 changed files
with
284 additions
and
1 deletion.
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
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
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,41 @@ | ||
'use strict' | ||
|
||
const t = require('tap') | ||
const Fastify = require('fastify') | ||
|
||
t.plan(10) | ||
|
||
const app = Fastify() | ||
|
||
app.register(require('./route-parameters/basic')) | ||
|
||
app.ready(function (err) { | ||
t.error(err) | ||
|
||
app.inject({ | ||
url: '/users' | ||
}, function (err, res) { | ||
t.error(err) | ||
|
||
t.equal(res.statusCode, 200) | ||
t.deepEqual(JSON.parse(res.payload), { users: [{ id: 7, username: 'example' }] }) | ||
}) | ||
|
||
app.inject({ | ||
url: '/users/7' | ||
}, function (err, res) { | ||
t.error(err) | ||
|
||
t.equal(res.statusCode, 200) | ||
t.deepEqual(JSON.parse(res.payload), { user: { id: 7, username: 'example' } }) | ||
}) | ||
|
||
app.inject({ | ||
url: '/users/_id' | ||
}, function (err, res) { | ||
t.error(err) | ||
|
||
t.equal(res.statusCode, 200) | ||
t.deepEqual(JSON.parse(res.payload), { user: { id: '_id', username: 'example' } }) | ||
}) | ||
}) |
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,40 @@ | ||
'use strict' | ||
|
||
const t = require('tap') | ||
const Fastify = require('fastify') | ||
|
||
t.plan(9) | ||
|
||
const app = Fastify() | ||
|
||
app.register(require('./route-parameters/disabled')) | ||
|
||
app.ready(function (err) { | ||
t.error(err) | ||
|
||
app.inject({ | ||
url: '/users' | ||
}, function (err, res) { | ||
t.error(err) | ||
|
||
t.equal(res.statusCode, 200) | ||
t.deepEqual(JSON.parse(res.payload), { users: [{ id: 7, username: 'example' }] }) | ||
}) | ||
|
||
app.inject({ | ||
url: '/users/7' | ||
}, function (err, res) { | ||
t.error(err) | ||
|
||
t.equal(res.statusCode, 404) | ||
}) | ||
|
||
app.inject({ | ||
url: '/users/_id' | ||
}, function (err, res) { | ||
t.error(err) | ||
|
||
t.equal(res.statusCode, 200) | ||
t.deepEqual(JSON.parse(res.payload), { user: { id: 'null', username: 'example' } }) | ||
}) | ||
}) |
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,13 @@ | ||
'use strict' | ||
|
||
const path = require('path') | ||
const autoLoad = require('../../../') | ||
|
||
module.exports = function (fastify, opts, next) { | ||
fastify.register(autoLoad, { | ||
dir: path.join(__dirname, 'routes'), | ||
routeParams: true | ||
}) | ||
|
||
next() | ||
} |
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,13 @@ | ||
'use strict' | ||
|
||
const path = require('path') | ||
const autoLoad = require('../../../') | ||
|
||
module.exports = function (fastify, opts, next) { | ||
fastify.register(autoLoad, { | ||
dir: path.join(__dirname, 'routes'), | ||
routeParams: false | ||
}) | ||
|
||
next() | ||
} |
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,5 @@ | ||
module.exports = async function (app, opts, next) { | ||
app.get('/', async function (req, reply) { | ||
reply.status(200).send({ user: { id: req.params.id || 'null', username: 'example' } }) // explicit null reply to preserve response body | ||
}) | ||
} |
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,5 @@ | ||
module.exports = async function (app, opts, next) { | ||
app.get('/', async function (req, reply) { | ||
reply.status(200).send({ users: [{ id: 7, username: 'example' }] }) | ||
}) | ||
} |
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,81 @@ | ||
import t from 'tap' | ||
import fastify from 'fastify' | ||
import routeParametersBasic from './route-parameters/basic.mjs' | ||
import routeParametersDisabled from './route-parameters/disabled.mjs' | ||
|
||
const { test } = t | ||
|
||
test('routeParams: Default behaviour', async () => { | ||
const app = fastify() | ||
let res | ||
|
||
app.register(routeParametersBasic) | ||
|
||
await app.ready() | ||
|
||
res = await app.inject('/') | ||
t.equal(res.statusCode, 200) | ||
t.deepEqual(res.json(), { route: '/' }) | ||
|
||
res = await app.inject('/pages') | ||
t.equal(res.statusCode, 200) | ||
t.deepEqual(res.json(), { route: '/pages' }) | ||
|
||
res = await app.inject('/pages/archived') | ||
t.equal(res.statusCode, 200) | ||
t.deepEqual(res.json(), { route: '/pages/archived' }) | ||
|
||
res = await app.inject('/pages/test_id') | ||
t.equal(res.statusCode, 200) | ||
t.deepEqual(res.json(), { route: '/pages/:id/', id: 'test_id' }) | ||
|
||
res = await app.inject('/pages/test_id/edit') | ||
t.equal(res.statusCode, 200) | ||
t.deepEqual(res.json(), { route: '/pages/:id/edit', id: 'test_id' }) | ||
|
||
res = await app.inject('/users/test_id/details') | ||
t.equal(res.statusCode, 200) | ||
t.deepEqual(res.json(), { route: '/users/:id/details', id: 'test_id' }) | ||
}) | ||
|
||
test('routeParams: off', async () => { | ||
const app = fastify() | ||
let res | ||
|
||
app.register(routeParametersDisabled) | ||
|
||
await app.ready() | ||
|
||
res = await app.inject('/') | ||
t.equal(res.statusCode, 200) | ||
t.deepEqual(res.json(), { route: '/' }) | ||
|
||
res = await app.inject('/pages') | ||
t.equal(res.statusCode, 200) | ||
t.deepEqual(res.json(), { route: '/pages' }) | ||
|
||
res = await app.inject('/pages/archived') | ||
t.equal(res.statusCode, 200) | ||
t.deepEqual(res.json(), { route: '/pages/archived' }) | ||
|
||
res = await app.inject('/pages/test_id') | ||
t.equal(res.statusCode, 404) | ||
|
||
res = await app.inject('/pages/test_id/edit') | ||
t.equal(res.statusCode, 404) | ||
|
||
res = await app.inject('/pages/_id') | ||
t.equal(res.statusCode, 200) | ||
t.deepEqual(res.json(), { route: '/pages/:id/' }) | ||
|
||
res = await app.inject('/pages/_id/edit') | ||
t.equal(res.statusCode, 200) | ||
t.deepEqual(res.json(), { route: '/pages/:id/edit' }) | ||
|
||
res = await app.inject('/users/test_id/details') | ||
t.equal(res.statusCode, 404) | ||
|
||
res = await app.inject('/users/_id/details') | ||
t.equal(res.statusCode, 200) | ||
t.deepEqual(res.json(), { route: '/users/:id/details' }) | ||
}) |
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,15 @@ | ||
import path from 'path' | ||
import { fileURLToPath } from 'url' | ||
|
||
import autoload from '../../../index.js' | ||
|
||
const { dirname } = path | ||
const __filename = fileURLToPath(import.meta.url) | ||
const __dirname = dirname(__filename) | ||
|
||
export default async function (fastify, opts) { | ||
fastify.register(autoload, { | ||
dir: path.join(__dirname, 'routes'), | ||
routeParams: true | ||
}) | ||
} |
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,15 @@ | ||
import path from 'path' | ||
import { fileURLToPath } from 'url' | ||
|
||
import autoload from '../../../index.js' | ||
|
||
const { dirname } = path | ||
const __filename = fileURLToPath(import.meta.url) | ||
const __dirname = dirname(__filename) | ||
|
||
export default async function (fastify, opts) { | ||
fastify.register(autoload, { | ||
dir: path.join(__dirname, 'routes'), | ||
routeParams: false | ||
}) | ||
} |
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,5 @@ | ||
export default async function (app, opts) { | ||
app.get('/', async function (req, reply) { | ||
return { route: '/' } | ||
}) | ||
} |
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,9 @@ | ||
export default async function (app, opts) { | ||
app.get('/', async function (req, reply) { | ||
return { route: '/pages/:id/', id: req.params.id } | ||
}) | ||
|
||
app.get('/edit', async function (req, reply) { | ||
return { route: '/pages/:id/edit', id: req.params.id } | ||
}) | ||
} |
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,9 @@ | ||
export default async function (app, opts) { | ||
app.get('/', async function (req, reply) { | ||
return { route: '/pages' } | ||
}) | ||
|
||
app.get('/archived', async function (req, reply) { | ||
return { route: '/pages/archived', id: req.params.id } // should be null | ||
}) | ||
} |
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,5 @@ | ||
export default async function (app, opts) { | ||
app.get('/details', async function (req, reply) { | ||
return { route: '/users/:id/details', id: req.params.id } | ||
}) | ||
} |