Skip to content

Commit

Permalink
feat: update deps & fix fastify warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
meghein committed Jan 12, 2024
1 parent 02d6e30 commit 63da5c4
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 93 deletions.
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2
quote_type = single
15 changes: 15 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"root": true,
"extends": [
"eslint:recommended",
"@autotelic/eslint-config-js"
],
"settings": {
"node": {
"version": "^18.x"
}
},
"rules": {
"multiline-comment-style": "off"
}
}
21 changes: 21 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Validate & Release
on:
push:
tags:
- v*
jobs:
validate:
uses: ./.github/workflows/validate.yaml

publish-npm:
runs-on: ubuntu-latest
needs: validate
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version-file: .node-version
registry-url: https://registry.npmjs.org/
- run: npm publish --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH_TOKEN }}
34 changes: 0 additions & 34 deletions .github/workflows/release.yml

This file was deleted.

19 changes: 19 additions & 0 deletions .github/workflows/validate.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Lint & Test

on:
workflow_call:
pull_request:
push:
branches: [main]

jobs:
validate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version-file: .node-version
registry-url: 'https://registry.npmjs.org'
- run: npm install
- run: npm run validate
18 changes: 0 additions & 18 deletions .github/workflows/validate.yml

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ logs
*.log
npm-debug.log*
.npmrc
.tap
1 change: 1 addition & 0 deletions .node-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
18.19.0
28 changes: 12 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,13 @@ function fastifyInjector (injectorOpts = {}, fastify = require('fastify')()) {
return targetPlugin
}

const wrapPlugin = (plugin) => {
return copyPluginEncapsulation(
async function wrappedPlugin (instance, opts, done) {
// Proxy encapsulated instance.
const proxy = fastifyInjector(injectorOpts, instance)
return plugin(proxy, opts, done)
}
)
}
const wrapPlugin = (plugin) => copyPluginEncapsulation(
(instance, opts, done) => {
// Proxy encapsulated instance.
const proxy = fastifyInjector(injectorOpts, instance)
return plugin(proxy, opts, done)
}
)

const pluginName = originalPlugin[Symbol.for('fastify.display-name')] || originalPlugin.name
const injectPlugin = injectors.plugins[pluginName]
Expand All @@ -75,7 +73,7 @@ function fastifyInjector (injectorOpts = {}, fastify = require('fastify')()) {
revoke
} = Proxy.revocable(
fastify, {
get: function (target, prop, receiver) {
get (target, prop, receiver) {
if (prop === 'decorate' || prop === 'decorateReply' || prop === 'decorateRequest') {
return wrapDecorateMethod(target, prop)
}
Expand All @@ -88,12 +86,10 @@ function fastifyInjector (injectorOpts = {}, fastify = require('fastify')()) {
)

if (!proxy.loadFixtures) {
proxy.decorate('loadFixtures', (fixtures) => {
return Promise.all((Array.isArray(fixtures) ? fixtures : [fixtures]).map((fixture) => {
const opts = typeof fixture !== 'string' ? fixture : { dir: fixture, dirNameRoutePrefix: false, maxDepth: 1 }
return proxy.register(autoload, opts)
}))
})
proxy.decorate('loadFixtures', (fixtures) => Promise.all((Array.isArray(fixtures) ? fixtures : [fixtures]).map((fixture) => {
const opts = typeof fixture !== 'string' ? fixture : { dir: fixture, dirNameRoutePrefix: false, maxDepth: 1 }
return proxy.register(autoload, opts)
})))
}

proxy.addHook('onClose', () => {
Expand Down
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
"main": "index.js",
"files": [],
"scripts": {
"fix": "npm run lint -- --fix",
"lint": "standard",
"test": "tap --100 -j1 -R classic",
"lint": "eslint .",
"test": "tap",
"validate": "npm run lint && npm run test"
},
"keywords": [
Expand All @@ -33,15 +32,16 @@
},
"homepage": "https://github.com/autotelic/fastify-injector#readme",
"engines": {
"node": ">=14"
"node": ">=18"
},
"devDependencies": {
"fastify": "^4.0.3",
"standard": "^17.0.0",
"tap": "^16.3.0"
"@autotelic/eslint-config-js": "^0.2.1",
"eslint": "^8.56.0",
"fastify": "^4.25.2",
"tap": "^18.6.1"
},
"dependencies": {
"@fastify/autoload": "^5.0.0",
"fastify-plugin": "^3.0.1"
"@fastify/autoload": "^5.8.0",
"fastify-plugin": "^4.5.1"
}
}
33 changes: 17 additions & 16 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
'use strict '

const { test } = require('tap')
const fp = require('fastify-plugin')
const { join } = require('path')

const fp = require('fastify-plugin')
const { test } = require('tap')

const fastifyInjector = require('..')

test('fastifyInjector: injecting decorators', async ({ is, teardown }) => {
test('fastifyInjector: injecting decorators', async ({ equal, teardown }) => {
teardown(async () => app.close())

const injectOpts = {
Expand All @@ -31,10 +32,10 @@ test('fastifyInjector: injecting decorators', async ({ is, teardown }) => {
url: '/'
})

is(result.json().payload, 'decorate injected, decorateRequest injected, decorateReply injected')
equal(result.json().payload, 'decorate injected, decorateRequest injected, decorateReply injected')
})

test('fastifyInjector: injecting decorators with passthrough', async ({ is, teardown }) => {
test('fastifyInjector: injecting decorators with passthrough', async ({ equal, teardown }) => {
teardown(async () => app.close())

function injectFoo () {
Expand Down Expand Up @@ -66,14 +67,14 @@ test('fastifyInjector: injecting decorators with passthrough', async ({ is, tear
url: '/'
})

is(result.json().payload, 'bar, bar, bar -> passthrough')
equal(result.json().payload, 'bar, bar, bar -> passthrough')
})

test('fastifyInjector: plugins and encapsulation', async ({ is, same, teardown }) => {
test('fastifyInjector: plugins and encapsulation', async ({ equal, same, teardown }) => {
teardown(async () => app.close())

async function fooRoute (instance, opts) {
instance.decorateReply('getData', async function () {
instance.decorateReply('getData', async function getData () {
const reply = this
return reply.bar()
})
Expand Down Expand Up @@ -125,14 +126,14 @@ test('fastifyInjector: plugins and encapsulation', async ({ is, same, teardown }
await app.ready()

same(app.barOpts, { bar: 'foo' })
is(app.fooOpts, undefined, 'Should maintain original plugin\'s encapsulation')
equal(app.fooOpts, undefined, 'Should maintain original plugin\'s encapsulation')

const result = await app.inject({
method: 'GET',
url: '/foo'
})

is(result.json().payload, 'foobar -> passthrough', 'should inject decorators applied by encapsulated plugin.')
equal(result.json().payload, 'foobar -> passthrough', 'should inject decorators applied by encapsulated plugin.')
})

const fixtureDir = {
Expand All @@ -150,7 +151,7 @@ const fixtureDir = {
}
}

test('fastifyInjector: loadFixtures - single dir', async ({ error, is, testdir, teardown }) => {
test('fastifyInjector: loadFixtures - single dir', async ({ error, equal, testdir, teardown }) => {
teardown(async () => app.close())

const fixtures = testdir(fixtureDir)
Expand All @@ -173,11 +174,11 @@ test('fastifyInjector: loadFixtures - single dir', async ({ error, is, testdir,
url: '/ping'
})

is(resultFoo.json().payload, 'bar')
is(resultPing.json().payload, 'pong')
equal(resultFoo.json().payload, 'bar')
equal(resultPing.json().payload, 'pong')
})

test('fastifyInjector: loadFixtures - multi dir with custom autoload config.', async ({ error, is, testdir, teardown }) => {
test('fastifyInjector: loadFixtures - multi dir with custom autoload config.', async ({ error, equal, testdir, teardown }) => {
teardown(async () => app.close())

const fixtures = testdir(fixtureDir)
Expand All @@ -203,6 +204,6 @@ test('fastifyInjector: loadFixtures - multi dir with custom autoload config.', a
url: '/test/ping'
})

is(resultFoo.json().payload, 'bar')
is(resultPing.json().payload, 'pong')
equal(resultFoo.json().payload, 'bar')
equal(resultPing.json().payload, 'pong')
})

0 comments on commit 63da5c4

Please sign in to comment.