diff --git a/index.js b/index.js index 9c6b458..63af0e4 100644 --- a/index.js +++ b/index.js @@ -1,15 +1,28 @@ -const { fastifySchedulePlugin } = require("./lib/schedulePlugin"); +"use strict"; + +const { ToadScheduler } = require("toad-scheduler"); +const fp = require("fastify-plugin"); + +function plugin(fastify, opts, next) { + const scheduler = new ToadScheduler(); + fastify.decorate("scheduler", scheduler); + + fastify.addHook("onClose", (fastify, done) => { + scheduler.stop(); + done(); + }); + + next(); +} + +const fastifySchedulePlugin = fp(plugin, { + fastify: "4.x", + name: "@fastify/schedule", +}); + +module.exports = fastifySchedulePlugin; /** - * These export configurations enable JS and TS developers - * to consume fastify-schedule in whatever way best suits their needs. - * Some examples of supported import syntax includes: - * - `const fastifySchedulePlugin = require('fastify-schedule')` - * - `const { fastifySchedulePlugin } = require('fastify-schedule')` - * - `import * as fastifySchedulePlugin from 'fastify-schedule'` - * - `import { fastifySchedulePlugin } from 'fastify-schedule'` - * - `import fastifySchedulePlugin from 'fastify-schedule'` + * TODO: Should be removed in the next major release */ -fastifySchedulePlugin.fastifySchedulePlugin = fastifySchedulePlugin; -fastifySchedulePlugin.default = fastifySchedulePlugin; -module.exports = fastifySchedulePlugin; +module.exports.fastifySchedulePlugin = fastifySchedulePlugin; diff --git a/lib/schedulePlugin.js b/lib/schedulePlugin.js deleted file mode 100644 index 3c6559f..0000000 --- a/lib/schedulePlugin.js +++ /dev/null @@ -1,23 +0,0 @@ -const { ToadScheduler } = require("toad-scheduler"); -const fp = require("fastify-plugin"); - -function plugin(fastify, opts, next) { - const scheduler = new ToadScheduler(); - fastify.decorate("scheduler", scheduler); - - fastify.addHook("onClose", (fastify, done) => { - scheduler.stop(); - done(); - }); - - next(); -} - -const fastifySchedulePlugin = fp(plugin, { - fastify: "4.x", - name: "@fastify/schedule", -}); - -module.exports = { - fastifySchedulePlugin, -}; diff --git a/package.json b/package.json index c822fc1..523fb62 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "test": "jest --config=jest.config.json", "test:coverage": "jest --config=jest.config.json --coverage", "test:typescript": "tsd", - "lint": "eslint \"lib/**/*.js\" \"test/**/*.js\" index.js", + "lint": "eslint \"test/**/*.js\" index.js", "lint:everything": "npm run lint && npm run test:typescript", "prettier": "prettier --write \"{lib,test}/**/*.js\" index.js index.d.ts", "postinstall": "husky install",