diff --git a/lib/fastifyAwilixPlugin.js b/lib/fastifyAwilixPlugin.js index 005b3f0..4037e89 100644 --- a/lib/fastifyAwilixPlugin.js +++ b/lib/fastifyAwilixPlugin.js @@ -5,19 +5,19 @@ const { AwilixManager } = require('awilix-manager') const fp = require('fastify-plugin') const diContainerProxy = awilix.createContainer({ - injectionMode: 'PROXY', + injectionMode: 'PROXY' }) const diContainerClassic = awilix.createContainer({ - injectionMode: 'CLASSIC', + injectionMode: 'CLASSIC' }) -function plugin(fastify, opts, next) { +function plugin (fastify, opts, next) { if (opts.container && opts.injectionMode) { return next( new Error( - 'If you are passing pre-created container explicitly, you cannot specify injection mode', - ), + 'If you are passing pre-created container explicitly, you cannot specify injection mode' + ) ) } @@ -32,7 +32,7 @@ function plugin(fastify, opts, next) { asyncDispose: opts.asyncDispose, asyncInit: opts.asyncInit, eagerInject: opts.eagerInject, - strictBooleanEnforced: opts.strictBooleanEnforced, + strictBooleanEnforced: opts.strictBooleanEnforced }) const disposeOnResponse = opts.disposeOnResponse === true || opts.disposeOnResponse === undefined const disposeOnClose = opts.disposeOnClose === true || opts.disposeOnClose === undefined @@ -80,12 +80,12 @@ function plugin(fastify, opts, next) { const fastifyAwilixPlugin = fp(plugin, { fastify: '4.x', - name: '@fastify/awilix', + name: '@fastify/awilix' }) module.exports = { diContainer: diContainerProxy, diContainerProxy, diContainerClassic, - fastifyAwilixPlugin, + fastifyAwilixPlugin } diff --git a/lib/index.js b/lib/index.js index c18c2f5..f79e816 100644 --- a/lib/index.js +++ b/lib/index.js @@ -4,12 +4,12 @@ const { diContainer, fastifyAwilixPlugin, diContainerProxy, - diContainerClassic, + diContainerClassic } = require('./fastifyAwilixPlugin') module.exports = { diContainer, diContainerClassic, diContainerProxy, - fastifyAwilixPlugin, + fastifyAwilixPlugin } diff --git a/package.json b/package.json index a06c7cd..7eb2ad1 100644 --- a/package.json +++ b/package.json @@ -16,9 +16,8 @@ "test": "c8 --100 node --test", "test:ci": "npm run lint && npm run test:typescript && npm run test", "test:typescript": "tsd", - "lint": "eslint \"lib/**/*.js\" lib/index.js", - "lint:fix": "eslint --fix \"lib/**/*.js\" lib/index.js", - "prettier": "prettier --write \"{lib,test}/**/*.js\" lib/index.js lib/index.d.ts" + "lint": "standard | snazzy", + "lint:fix": "standard --fix | snazzy" }, "dependencies": { "awilix-manager": "^5.1.0", @@ -33,11 +32,9 @@ "@types/node": "^22.0.0", "awilix": "^10.0.2", "c8": "^10.1.2", - "eslint": "^9.5.0", - "eslint-config-prettier": "^9.1.0", - "eslint-plugin-prettier": "^5.1.3", "fastify": "^4.26.2", - "prettier": "^3.2.5", + "snazzy": "^9.0.0", + "standard": "^17.1.0", "tsd": "^0.31.0" }, "homepage": "http://github.com/fastify/fastify-awilix", diff --git a/test/awilixManager.test.js b/test/awilixManager.test.js index e66a61b..baeb884 100644 --- a/test/awilixManager.test.js +++ b/test/awilixManager.test.js @@ -11,19 +11,17 @@ let isInittedGlobal = false let isDisposedGlobal = false class InitSetClass { - constructor() { + constructor () { isInittedGlobal = true } } class AsyncInitSetClass { - constructor() {} - - async init() { + async init () { isInittedGlobal = true } - async dispose() { + async dispose () { isDisposedGlobal = true } } @@ -31,12 +29,12 @@ class AsyncInitSetClass { const variations = [ { injectionMode: 'PROXY', - container: diContainer, + container: diContainer }, { injectionMode: 'CLASSIC', - container: diContainerClassic, - }, + container: diContainerClassic + } ] describe('awilixManager', () => { @@ -63,13 +61,13 @@ describe('awilixManager', () => { 'dependency1', asClass(InitSetClass, { lifetime: 'SINGLETON', - eagerInject: true, - }), + eagerInject: true + }) ) app = fastify({ logger: false }) await app.register(fastifyAwilixPlugin, { eagerInject: true, - injectionMode: variation.injectionMode, + injectionMode: variation.injectionMode }) await app.ready() @@ -81,13 +79,13 @@ describe('awilixManager', () => { 'dependency1', asClass(AsyncInitSetClass, { lifetime: 'SINGLETON', - asyncInit: 'init', - }), + asyncInit: 'init' + }) ) app = fastify({ logger: false }) await app.register(fastifyAwilixPlugin, { asyncInit: true, - injectionMode: variation.injectionMode, + injectionMode: variation.injectionMode }) await app.ready() @@ -99,13 +97,13 @@ describe('awilixManager', () => { 'dependency1', asClass(AsyncInitSetClass, { lifetime: 'SINGLETON', - asyncDispose: 'dispose', - }), + asyncDispose: 'dispose' + }) ) app = fastify({ logger: false }) await app.register(fastifyAwilixPlugin, { asyncDispose: true, - injectionMode: variation.injectionMode, + injectionMode: variation.injectionMode }) await app.ready() await app.close() diff --git a/test/fastifyAwilixPlugin.dispose.test.js b/test/fastifyAwilixPlugin.dispose.test.js index 3c83306..11793da 100644 --- a/test/fastifyAwilixPlugin.dispose.test.js +++ b/test/fastifyAwilixPlugin.dispose.test.js @@ -8,11 +8,11 @@ const assert = require('node:assert') const { fastifyAwilixPlugin, diContainer, diContainerClassic } = require('../lib') class UserRepository { - constructor() { + constructor () { this.disposeCounter = 0 } - dispose() { + dispose () { this.disposeCounter++ } } @@ -23,15 +23,15 @@ let storedUserRepositoryScoped const variations = [ { injectionMode: 'PROXY', - container: diContainer, + container: diContainer }, { injectionMode: 'CLASSIC', - container: diContainerClassic, - }, + container: diContainerClassic + } ] -function getCompletedRequests(output) { +function getCompletedRequests (output) { return output.filter((line) => { try { return JSON.parse(line).msg === 'request completed' @@ -43,7 +43,7 @@ function getCompletedRequests(output) { describe('fastifyAwilixPlugin', () => { let app, output - let write = process.stdout.write + const write = process.stdout.write beforeEach(() => { storedUserRepository = undefined @@ -65,7 +65,7 @@ describe('fastifyAwilixPlugin', () => { assert.equal(userRepository.disposeCounter, 0) res.send({ - status: 'OK', + status: 'OK' }) } @@ -80,7 +80,7 @@ describe('fastifyAwilixPlugin', () => { assert.equal(userRepositoryScoped.disposeCounter, 0) res.send({ - status: 'OK', + status: 'OK' }) } @@ -89,13 +89,13 @@ describe('fastifyAwilixPlugin', () => { app.register(fastifyAwilixPlugin, { disposeOnClose: true, disposeOnResponse: false, - injectionMode: variation.injectionMode, + injectionMode: variation.injectionMode }) variation.container.register({ userRepository: asClass(UserRepository, { lifetime: Lifetime.SINGLETON, - dispose: (service) => service.dispose(), - }), + dispose: (service) => service.dispose() + }) }) app.post('/', endpoint) @@ -116,13 +116,13 @@ describe('fastifyAwilixPlugin', () => { app.register(fastifyAwilixPlugin, { disposeOnClose: false, disposeOnResponse: true, - injectionMode: variation.injectionMode, + injectionMode: variation.injectionMode }) variation.container.register({ userRepository: asClass(UserRepository, { lifetime: Lifetime.SINGLETON, - dispose: (service) => service.dispose(), - }), + dispose: (service) => service.dispose() + }) }) app.post('/', endpoint) @@ -147,13 +147,13 @@ describe('fastifyAwilixPlugin', () => { await app.register(fastifyAwilixPlugin, { disposeOnClose: false, disposeOnResponse: true, - injectionMode: variation.injectionMode, + injectionMode: variation.injectionMode }) variation.container.register({ userRepository: asClass(UserRepository, { lifetime: Lifetime.SINGLETON, - dispose: (service) => service.dispose(), - }), + dispose: (service) => service.dispose() + }) }) let storedError = null @@ -181,21 +181,21 @@ describe('fastifyAwilixPlugin', () => { app.register(fastifyAwilixPlugin, { disposeOnClose: false, disposeOnResponse: true, - injectionMode: variation.injectionMode, + injectionMode: variation.injectionMode }) variation.container.register({ userRepository: asClass(UserRepository, { lifetime: Lifetime.SINGLETON, - dispose: (service) => service.dispose(), - }), + dispose: (service) => service.dispose() + }) }) app.addHook('onRequest', (request, reply, done) => { request.diScope.register({ userRepositoryScoped: asClass(UserRepository, { lifetime: Lifetime.SCOPED, - dispose: (service) => service.dispose(), - }), + dispose: (service) => service.dispose() + }) }) done() }) @@ -220,21 +220,21 @@ describe('fastifyAwilixPlugin', () => { app.register(fastifyAwilixPlugin, { disposeOnClose: true, disposeOnResponse: true, - injectionMode: variation.injectionMode, + injectionMode: variation.injectionMode }) variation.container.register({ userRepository: asClass(UserRepository, { lifetime: Lifetime.SINGLETON, - dispose: (service) => service.dispose(), - }), + dispose: (service) => service.dispose() + }) }) app.addHook('onRequest', (request, reply, done) => { request.diScope.register({ userRepositoryScoped: asClass(UserRepository, { lifetime: Lifetime.SCOPED, - dispose: (service) => service.dispose(), - }), + dispose: (service) => service.dispose() + }) }) done() }) @@ -261,13 +261,13 @@ describe('fastifyAwilixPlugin', () => { app.register(fastifyAwilixPlugin, { disposeOnClose: true, disposeOnResponse: true, - injectionMode: variation.injectionMode, + injectionMode: variation.injectionMode }) variation.container.register({ userRepository: asClass(UserRepository, { lifetime: Lifetime.SINGLETON, - dispose: (service) => service.dispose(), - }), + dispose: (service) => service.dispose() + }) }) app.post('/', endpoint) @@ -287,13 +287,13 @@ describe('fastifyAwilixPlugin', () => { app.register(fastifyAwilixPlugin, { disposeOnClose: false, disposeOnResponse: false, - injectionMode: variation.injectionMode, + injectionMode: variation.injectionMode }) variation.container.register({ userRepository: asClass(UserRepository, { lifetime: Lifetime.SINGLETON, - dispose: (service) => service.dispose(), - }), + dispose: (service) => service.dispose() + }) }) app.post('/', endpoint) diff --git a/test/fastifyAwilixPlugin.test.js b/test/fastifyAwilixPlugin.test.js index e9a9662..d437d7f 100644 --- a/test/fastifyAwilixPlugin.test.js +++ b/test/fastifyAwilixPlugin.test.js @@ -8,7 +8,7 @@ const assert = require('node:assert') const { diContainer, diContainerClassic, fastifyAwilixPlugin } = require('../lib') class UserServiceClassic { - constructor(userRepository, maxUserName, maxEmail) { + constructor (userRepository, maxUserName, maxEmail) { this.userRepository = userRepository this.maxUserName = maxUserName this.maxEmail = maxEmail @@ -16,7 +16,7 @@ class UserServiceClassic { } class UserServiceProxy { - constructor({ userRepository, maxUserName, maxEmail }) { + constructor ({ userRepository, maxUserName, maxEmail }) { this.userRepository = userRepository this.maxUserName = maxUserName this.maxEmail = maxEmail @@ -29,21 +29,21 @@ const variations = [ container: diContainer, optsContainer: undefined, optsInjectionMode: undefined, - UserService: UserServiceProxy, + UserService: UserServiceProxy }, { injectionMode: 'PROXY', container: diContainer, optsContainer: undefined, optsInjectionMode: 'PROXY', - UserService: UserServiceProxy, + UserService: UserServiceProxy }, { injectionMode: 'CLASSIC', container: diContainerClassic, optsContainer: undefined, optsInjectionMode: 'CLASSIC', - UserService: UserServiceClassic, + UserService: UserServiceClassic }, { @@ -51,15 +51,15 @@ const variations = [ container: diContainer, optsContainer: diContainer, optsInjectionMode: undefined, - UserService: UserServiceProxy, + UserService: UserServiceProxy }, { injectionMode: 'CLASSIC', container: diContainerClassic, optsContainer: diContainerClassic, optsInjectionMode: undefined, - UserService: UserServiceClassic, - }, + UserService: UserServiceClassic + } ] describe('fastifyAwilixPlugin', () => { @@ -74,7 +74,7 @@ describe('fastifyAwilixPlugin', () => { describe('inject singleton', () => { it('injects correctly', async () => { class UserRepository { - constructor() { + constructor () { this.id = 'userRepository' } } @@ -100,22 +100,22 @@ describe('fastifyAwilixPlugin', () => { assert.equal(maxUserPassword, 20) res.send({ - status: 'OK', + status: 'OK' }) } app.register(fastifyAwilixPlugin, { injectionMode: variation.optsInjectionMode, - container: variation.optsContainer, + container: variation.optsContainer }) variation.container.register({ userService: asClass(variation.UserService), userRepository: asClass(UserRepository, { lifetime: Lifetime.SINGLETON }), maxUserName: asFunction(maxUserNameVariableFactory, { lifetime: Lifetime.SINGLETON }), maxUserPassword: asFunction(maxUserPasswordVariableFactory, { - lifetime: Lifetime.SINGLETON, + lifetime: Lifetime.SINGLETON }), - maxEmail: asValue(40), + maxEmail: asValue(40) }) app.post('/', endpoint) @@ -136,7 +136,7 @@ describe('fastifyAwilixPlugin', () => { await assert.rejects(async () => { await app.register(fastifyAwilixPlugin, { injectionMode: 'PROXY', - container: diContainer, + container: diContainer }) }, /If you are passing pre-created container explicitly, you cannot specify injection mode/) })