Skip to content

Commit

Permalink
Updated to Fastify v4 (#154)
Browse files Browse the repository at this point in the history
* Updated to Fastify v4

* fixed ts

Signed-off-by: Matteo Collina <hello@matteocollina.com>
  • Loading branch information
mcollina committed May 12, 2022
1 parent 75e195e commit 513fdcb
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 24 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ jobs:
strategy:
matrix:
node-version:
- 10
- 12
- 14
- 16
- 18
os:
- macOS-latest
- windows-latest
Expand Down
4 changes: 2 additions & 2 deletions auth.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FastifyPlugin, FastifyRequest, FastifyReply, preHandlerHookHandler, FastifyInstance } from 'fastify';
import { FastifyPluginCallback, FastifyRequest, FastifyReply, preHandlerHookHandler, FastifyInstance } from 'fastify';

export type FastifyAuthFunction = (
this: FastifyInstance,
Expand All @@ -19,5 +19,5 @@ declare module 'fastify' {
}
}

declare const fastifyAuth: FastifyPlugin;
declare const fastifyAuth: FastifyPluginCallback;
export default fastifyAuth;
12 changes: 8 additions & 4 deletions auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,14 @@ function auth (functions, opts) {
return
}

const maybePromise = func(that.request, that.reply, that.onAuth)
try {
const maybePromise = func(that.request, that.reply, that.onAuth)

if (maybePromise && typeof maybePromise.then === 'function') {
maybePromise.then(results => that.onAuth(null, results), that.onAuth)
if (maybePromise && typeof maybePromise.then === 'function') {
maybePromise.then(results => that.onAuth(null, results), that.onAuth)
}
} catch (err) {
this.onAuth(err)
}
}

Expand Down Expand Up @@ -118,4 +122,4 @@ function auth (functions, opts) {
}
}

module.exports = fp(checkAuth, '3.x')
module.exports = fp(checkAuth, '4.x')
11 changes: 5 additions & 6 deletions example-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ const Fastify = require('fastify')
function build (opts) {
const fastify = Fastify(opts)

fastify.register(require('fastify-jwt'), { secret: 'supersecret' })
fastify.register(require('fastify-leveldb'), { name: 'authdb-async' })
fastify.register(require('@fastify/jwt'), { secret: 'supersecret' })
fastify.register(require('@fastify/leveldb'), { name: 'authdb-async' })
fastify.register(require('./auth'))
fastify.after(routes)
fastify.register(routes)

fastify.decorate('verifyJWTandLevel', verifyJWTandLevel)
fastify.decorate('verifyUserAndPassword', verifyUserAndPassword)
Expand Down Expand Up @@ -65,7 +65,7 @@ function build (opts) {
}
}

function routes () {
async function routes (fastify) {
fastify.route({
method: 'POST',
url: '/register',
Expand Down Expand Up @@ -138,9 +138,8 @@ if (require.main === module) {
level: 'info'
}
})
fastify.listen(3000, err => {
fastify.listen({ port: 3000, host: '0.0.0.0' }, err => {
if (err) throw err
console.log(`Server listenting at http://localhost:${fastify.server.address().port}`)
})
}

Expand Down
3 changes: 1 addition & 2 deletions example-composited.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,8 @@ if (require.main === module) {
level: 'info'
}
})
fastify.listen(3000, '0.0.0.0', err => {
fastify.listen({ port: 3000, host: '0.0.0.0' }, err => {
if (err) throw err
console.log(`Server listenting at http://${JSON.stringify(fastify.server.address())}`)
})
}

Expand Down
7 changes: 3 additions & 4 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ const Fastify = require('fastify')
function build (opts) {
const fastify = Fastify(opts)

fastify.register(require('fastify-jwt'), { secret: 'supersecret' })
fastify.register(require('fastify-leveldb'), { name: 'authdb' })
fastify.register(require('@fastify/jwt'), { secret: 'supersecret' })
fastify.register(require('@fastify/leveldb'), { name: 'authdb' })
fastify.register(require('./auth')) // just 'fastify-auth' IRL
fastify.after(routes)

Expand Down Expand Up @@ -170,9 +170,8 @@ if (require.main === module) {
level: 'info'
}
})
fastify.listen(3000, err => {
fastify.listen({ port: 3000 }, err => {
if (err) throw err
console.log(`Server listening at http://localhost:${fastify.server.address().port}`)
})
}

Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@
],
"license": "MIT",
"devDependencies": {
"fastify": "^3.0.0",
"fastify-jwt": "^3.0.0",
"fastify-leveldb": "^3.0.0",
"@fastify/jwt": "^6.0.0",
"@fastify/leveldb": "^5.0.0",
"@types/node": "^17.0.32",
"fastify": "^4.0.0-rc.2",
"pre-commit": "^1.2.2",
"rimraf": "^3.0.2",
"standard": "^17.0.0",
Expand Down
3 changes: 2 additions & 1 deletion test/example.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,9 @@ test('Auth not successful', t => {
t.error(err)
const payload = JSON.parse(res.payload)
t.same(payload, {
code: 'FAST_JWT_MALFORMED',
error: 'Unauthorized',
message: 'Token not valid',
message: 'The token is malformed.',
statusCode: 401
})
})
Expand Down

0 comments on commit 513fdcb

Please sign in to comment.