Skip to content

Commit

Permalink
major: migrate to fastify v5 (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilteoood authored Dec 27, 2024
1 parent 0e36c1b commit 873b5e9
Show file tree
Hide file tree
Showing 15 changed files with 253 additions and 513 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ on:

jobs:
test:
uses: fastify/workflows/.github/workflows/plugins-ci.yml@v3
uses: fastify/workflows/.github/workflows/plugins-ci.yml@v5
with:
lint: true
5 changes: 1 addition & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,6 @@ function fastifyOverview (fastify, options, next) {
instance[kTrackerMe] = trackingToken

const trackStructure = getPluginNode(trackingToken, instance.pluginName)
if (opts.addSource && this) {
trackStructure.source = this._current.find(loadPipe => loadPipe.func[kSourceRegister] !== undefined).func[kSourceRegister]
}
contextMap.set(trackingToken, trackStructure)
instance[kStructure] = trackStructure

Expand Down Expand Up @@ -192,7 +189,7 @@ function getJsonOverview (request, reply) {

const plugin = fp(fastifyOverview, {
name: 'fastify-overview',
fastify: '^4.23.x'
fastify: '5.x'
})

module.exports = plugin
Expand Down
23 changes: 11 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"lint": "standard && npm run lint:typescript",
"lint:fix": "standard --fix && npm run lint:typescript -- --fix",
"lint:typescript": "standard --parser @typescript-eslint/parser --plugin @typescript-eslint/eslint-plugin *.ts test/**/*.ts",
"test": "tap test/**/*.test.js && npm run test:typescript",
"test": "node --test && npm run test:typescript",
"test:typescript": "tsd"
},
"repository": {
Expand All @@ -28,22 +28,21 @@
"url": "https://github.com/Eomm/fastify-overview/issues"
},
"engines": {
"node": ">=14"
"node": ">=20"
},
"homepage": "https://github.com/Eomm/fastify-overview#readme",
"devDependencies": {
"@fastify/autoload": "^5.0.0",
"@types/node": "^20.1.0",
"@typescript-eslint/eslint-plugin": "^6.2.1",
"@typescript-eslint/parser": "^6.0.0",
"fastify": "^4.23.0",
"standard": "^17.0.0",
"tap": "^16.0.1",
"tsd": "^0.31.0"
"@fastify/autoload": "^6.0.2",
"@types/node": "^22.10.1",
"@typescript-eslint/eslint-plugin": "^8.17.0",
"@typescript-eslint/parser": "^8.17.0",
"fastify": "^5.1.0",
"standard": "^17.1.2",
"tsd": "^0.31.2"
},
"dependencies": {
"fastify-plugin": "^4.0.0",
"object-hash": "^2.2.0"
"fastify-plugin": "^5.0.1",
"object-hash": "^3.0.0"
},
"tsd": {
"directory": "test/types"
Expand Down
4 changes: 2 additions & 2 deletions test/application-hooks.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { test } = require('tap')
const { test } = require('node:test')
const fastify = require('fastify')
const plugin = require('../index')

Expand Down Expand Up @@ -29,5 +29,5 @@ test('should track all the application hooks', {
await app.ready()
const structure = app.overview({ hideEmpty: true })

t.same(structure.hooks, require('./fixture/app-hooks.json').hooks)
t.assert.deepStrictEqual(structure.hooks, require('./fixture/app-hooks.json').hooks)
})
64 changes: 32 additions & 32 deletions test/decorator.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { test } = require('tap')
const { test } = require('node:test')
const fastify = require('fastify')
const plugin = require('../index')

Expand All @@ -14,7 +14,7 @@ test('decorator', async t => {
app.decorateReply('root-reply', function () {})

app.decorate('root-symbol', Symbol('testSymbol'))
app.decorateReply('root-reply-array', [])
app.decorateReply('root-reply-array', () => ({ getter: [] }))
app.decorateRequest('root-req-boolean', true)

app.register(function register1 (instance, opts, next) {
Expand All @@ -28,7 +28,7 @@ test('decorator', async t => {
instance.register(function register3 (sub, opts, next) {
sub.decorate('sub-instance', 50)
sub.decorateReply('sub', 50)
sub.decorateRequest('sub-object', {})
sub.decorateRequest('sub-object', () => ({ getter: {} }))
next()
})
next()
Expand All @@ -40,28 +40,28 @@ test('decorator', async t => {
await app.ready()
const root = app.overview()

t.equal(root.children.length, 2)
t.equal(root.children[0].name, 'register1')
t.equal(root.children[1].name, 'sibling')
t.same(root.decorators.decorate, [{ name: 'root-func', type: 'function' }, { name: 'root-symbol', type: 'symbol' }])
t.same(root.decorators.decorateRequest, [{ name: 'root-req', type: 'function' }, { name: 'root-req-two', type: 'function' }, { name: 'root-req-boolean', type: 'boolean' }])
t.same(root.decorators.decorateReply, [{ name: 'root-reply', type: 'function' }, { name: 'root-reply-array', type: 'array' }])
t.assert.deepEqual(root.children.length, 2)
t.assert.deepEqual(root.children[0].name, 'register1')
t.assert.deepEqual(root.children[1].name, 'sibling')
t.assert.deepStrictEqual(root.decorators.decorate, [{ name: 'root-func', type: 'function' }, { name: 'root-symbol', type: 'symbol' }])
t.assert.deepStrictEqual(root.decorators.decorateRequest, [{ name: 'root-req', type: 'function' }, { name: 'root-req-two', type: 'function' }, { name: 'root-req-boolean', type: 'boolean' }])
t.assert.deepStrictEqual(root.decorators.decorateReply, [{ name: 'root-reply', type: 'function' }, { name: 'root-reply-array', type: 'function' }])

const reg1 = root.children[0]
t.same(reg1.decorators.decorate, [{ name: 'child-1-bigint', type: 'bigint' }, { name: 'child-1-undefined', type: 'undefined' }])
t.same(reg1.decorators.decorateRequest, [{ name: 'child-1', type: 'number' }])
t.same(reg1.decorators.decorateReply, [])
t.equal(reg1.children.length, 2)
t.assert.deepStrictEqual(reg1.decorators.decorate, [{ name: 'child-1-bigint', type: 'bigint' }, { name: 'child-1-undefined', type: 'undefined' }])
t.assert.deepStrictEqual(reg1.decorators.decorateRequest, [{ name: 'child-1', type: 'number' }])
t.assert.deepStrictEqual(reg1.decorators.decorateReply, [])
t.assert.deepEqual(reg1.children.length, 2)

const reg2 = reg1.children[0]
t.same(reg2.decorators.decorate, [])
t.same(reg2.decorators.decorateRequest, [])
t.same(reg2.decorators.decorateReply, [{ name: 'sub', type: 'number' }])
t.assert.deepStrictEqual(reg2.decorators.decorate, [])
t.assert.deepStrictEqual(reg2.decorators.decorateRequest, [])
t.assert.deepStrictEqual(reg2.decorators.decorateReply, [{ name: 'sub', type: 'number' }])

const reg3 = reg1.children[1]
t.same(reg3.decorators.decorate, [{ name: 'sub-instance', type: 'number' }])
t.same(reg3.decorators.decorateRequest, [{ name: 'sub-object', type: 'object' }])
t.same(reg3.decorators.decorateReply, [{ name: 'sub', type: 'number' }])
t.assert.deepStrictEqual(reg3.decorators.decorate, [{ name: 'sub-instance', type: 'number' }])
t.assert.deepStrictEqual(reg3.decorators.decorateRequest, [{ name: 'sub-object', type: 'function' }])
t.assert.deepStrictEqual(reg3.decorators.decorateReply, [{ name: 'sub', type: 'number' }])
})

test('onDecorateDefinition', async t => {
Expand All @@ -83,14 +83,14 @@ test('onDecorateDefinition', async t => {
}
})

app.decorate('emptyObj', {})
app.decorate('emptyObj', () => ({ getter: {} }))
app.decorate('obj1', {
run: () => {}
})
app.decorateRequest('emptyObj', {})
app.decorateReply('obj2', {
test: 'str'
})
app.decorateRequest('emptyObj', () => ({ getter: {} }))
app.decorateReply('obj2', () => ({
getter: { test: 'str' }
}))

app.register(async function child1 (instance) {
instance.decorate('encapsulatedObj', {
Expand All @@ -105,14 +105,14 @@ test('onDecorateDefinition', async t => {

const root = app.overview()

t.equal(root.children.length, 1)
t.same(root.decorators.decorate, [{ name: 'emptyObj', type: 'object', recursive: [] }, { name: 'obj1', type: 'object', recursive: [{ name: 'run', type: 'function' }] }])
t.same(root.decorators.decorateReply, [{ name: 'obj2', type: 'object', recursive: [{ name: 'test', type: 'string' }] }])
t.same(root.decorators.decorateRequest, [{ name: 'emptyObj', type: 'object', recursive: [] }])
t.assert.deepEqual(root.children.length, 1)
t.assert.deepStrictEqual(root.decorators.decorate, [{ name: 'emptyObj', type: 'function' }, { name: 'obj1', type: 'object', recursive: [{ name: 'run', type: 'function' }] }])
t.assert.deepStrictEqual(root.decorators.decorateReply, [{ name: 'obj2', type: 'function' }])
t.assert.deepStrictEqual(root.decorators.decorateRequest, [{ name: 'emptyObj', type: 'function' }])

t.equal(root.children[0].name, 'child1')
t.assert.deepEqual(root.children[0].name, 'child1')
const child1 = root.children[0]
t.same(child1.decorators.decorate, [{ name: 'encapsulatedObj', type: 'object', recursive: [{ name: 'a', type: 'function' }, { name: 'b', type: 'string' }, { name: 'c', type: 'boolean' }, { name: 'd', type: 'number' }] }])
t.equal(child1.decorators.decorateRequest.length, 0)
t.equal(child1.decorators.decorateReply.length, 0)
t.assert.deepStrictEqual(child1.decorators.decorate, [{ name: 'encapsulatedObj', type: 'object', recursive: [{ name: 'a', type: 'function' }, { name: 'b', type: 'string' }, { name: 'c', type: 'boolean' }, { name: 'd', type: 'number' }] }])
t.assert.deepEqual(child1.decorators.decorateRequest.length, 0)
t.assert.deepEqual(child1.decorators.decorateReply.length, 0)
})
164 changes: 86 additions & 78 deletions test/expose-routes.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { test } = require('tap')
const { test } = require('node:test')
const fastify = require('fastify')
const plugin = require('../index')

Expand All @@ -9,106 +9,114 @@ test('expose the route to the the json', async t => {
await app.register(plugin, { exposeRoute: true })

const response = await app.inject('/json-overview')
t.hasStrict(response.json(), {
name: 'fastify -> fastify-overview',
children: [],
routes: [
{
method: 'GET',
url: '/json-overview',
prefix: '',
hooks: {
onRequest: [],
preParsing: [],
preValidation: [],
preHandler: [],
preSerialization: [],
onError: [],
onSend: [],
onResponse: [],
onTimeout: []
}
const responseContent = response.json()

t.assert.deepStrictEqual(responseContent.name, 'fastify -> fastify-overview')
t.assert.deepStrictEqual(responseContent.children, [])
t.assert.deepEqual(responseContent.routes, [
{
method: 'GET',
url: '/json-overview',
prefix: '',
hooks: {
onRequest: [],
onRequestAbort: [],
preParsing: [],
preValidation: [],
preHandler: [],
preSerialization: [],
onError: [],
onSend: [],
onResponse: [],
onTimeout: []
}
],
decorators: {
decorate: [],
decorateRequest: [],
decorateReply: []
},
hooks: {
onRequest: [],
preParsing: [],
preValidation: [],
preHandler: [],
preSerialization: [],
onError: [],
onSend: [],
onResponse: [],
onTimeout: [],
onReady: [],
onClose: [],
onRoute: [],
onRegister: []
}
])
t.assert.deepStrictEqual(responseContent.decorators, {
decorate: [],
decorateRequest: [],
decorateReply: []
})
t.assert.deepEqual(responseContent.hooks, {
onClose: [],
onError: [],
onListen: [],
onReady: [],
onRegister: [],
onRequest: [],
onRequestAbort: [],
onResponse: [],
onRoute: [],
onSend: [],
onTimeout: [],
preClose: [],
preHandler: [],
preParsing: [],
preSerialization: [],
preValidation: []
})
})

test('expose the route within more options', async t => {
t.plan(2)
t.plan(7)
const app = fastify()
await app.register(plugin,
{
exposeRoute: true,
exposeRouteOptions: {
url: '/foo',
preHandler: function hook (request, reply, done) {
t.pass('preHandler hook called')
t.assert.ok(true, 'preHandler hook registered')
done(null)
}
}
})

const response = await app.inject('/foo')
t.hasStrict(response.json(), {
name: 'fastify -> fastify-overview',
children: [],
routes: [
{
method: 'GET',
url: '/foo',
prefix: '',
hooks: {
onRequest: [],
preParsing: [],
preValidation: [],
preHandler: [],
preSerialization: [],
onError: [],
onSend: [],
onResponse: [],
onTimeout: []
}
}
],
decorators: {
decorate: [],
decorateRequest: [],
decorateReply: []
},
const responseContent = response.json()

t.assert.deepStrictEqual(responseContent.name, 'fastify -> fastify-overview')
t.assert.deepStrictEqual(responseContent.children, [])
t.assert.strictEqual(responseContent.routes.length, 1)
delete responseContent.routes[0].hooks.preHandler[0].hash
t.assert.deepStrictEqual(responseContent.routes[0], {
method: 'GET',
url: '/foo',
prefix: '',
hooks: {
onRequest: [],
preParsing: [],
preValidation: [],
preHandler: [],
preSerialization: [],
onError: [],
onSend: [],
onRequest: [],
onRequestAbort: [],
onResponse: [],
onSend: [],
onTimeout: [],
onReady: [],
onClose: [],
onRoute: [],
onRegister: []
preHandler: [{ name: 'hook' }],
preParsing: [],
preSerialization: [],
preValidation: []
}
})
t.assert.deepStrictEqual(responseContent.decorators, {
decorate: [],
decorateRequest: [],
decorateReply: []
})
t.assert.deepStrictEqual(responseContent.hooks, {
onRequest: [],
preParsing: [],
preValidation: [],
preHandler: [],
preSerialization: [],
onError: [],
onSend: [],
onResponse: [],
onTimeout: [],
onReady: [],
onClose: [],
onRoute: [],
onRegister: [],
onListen: [],
onRequestAbort: [],
preClose: []
})
})
Loading

0 comments on commit 873b5e9

Please sign in to comment.