Skip to content

Commit c58d2d8

Browse files
authored
feat!: update plugin for fastify v5 and fix types (#15)
1 parent 0d13c03 commit c58d2d8

File tree

9 files changed

+42
-39
lines changed

9 files changed

+42
-39
lines changed

.eslintignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
fail-fast: false
3131
matrix:
3232
os: [ubuntu-latest, macos-latest, windows-latest]
33-
node-version: [16.x, 18.x, 20.x]
33+
node-version: [20.x, 22.x, 24.x]
3434
runs-on: ${{matrix.os}}
3535
steps:
3636
- uses: actions/checkout@v3

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,4 @@ profile-*
7474
# lock files
7575
package-lock.json
7676
yarn.lock
77+
pnpm-lock.yaml

esm-wrapper.mjs

Lines changed: 0 additions & 4 deletions
This file was deleted.

example/esm/server.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { fastify } from 'fastify';
2-
import * as fastifyPiscina from '../../esm-wrapper.mjs';
2+
import * as fastifyPiscina from '../../plugin.js';
33

44
const app = fastify({ logger: true });
55

package.json

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
"version": "5.0.0",
44
"description": "A Fastify Piscina Plugin",
55
"main": "./plugin.js",
6+
"types": "./plugin.d.ts",
67
"exports": {
7-
"import": "./esm-wrapper.mjs",
8+
"types": "./plugin.d.ts",
9+
"import": "./plugin.js",
810
"require": "./plugin.js"
911
},
10-
"types": "./plugin.d.ts",
1112
"scripts": {
12-
"prepack": "npm run build",
13-
"build": "gen-esm-wrapper . esm-wrapper.mjs",
1413
"lint": "standardx \"**/*.{mjs,js,cjs}\" | snazzy",
1514
"lint:fix": "standardx --fix",
1615
"test": "tap -J test/*.test.{js,mjs}",
@@ -35,17 +34,16 @@
3534
],
3635
"license": "MIT",
3736
"devDependencies": {
38-
"@types/node": "^16.11.11",
39-
"fastify": "^4.24.3",
40-
"gen-esm-wrapper": "^1.1.3",
37+
"@types/node": "^24.3.0",
38+
"fastify": "^5.0.0",
4139
"snazzy": "^9.0.0",
4240
"standardx": "^7.0.0",
4341
"tap": "^15.1.5",
44-
"tsd": "^0.19.0"
42+
"tsd": "^0.33.0"
4543
},
4644
"dependencies": {
47-
"fastify-plugin": "^4.5.1",
48-
"piscina": "^4.2.0"
45+
"fastify-plugin": "^5.0.0",
46+
"piscina": "^5.1.3"
4947
},
5048
"eslintConfig": {
5149
"rules": {
@@ -63,5 +61,8 @@
6361
"standardx": {},
6462
"tsd": {
6563
"directory": "test/types"
64+
},
65+
"engines": {
66+
"node": ">=20.x"
6667
}
6768
}

plugin.d.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
import { FastifyPluginCallback } from 'fastify';
2-
import Piscina from 'piscina';
2+
import { Piscina } from 'piscina';
33

4-
type PiscinaOptions = typeof Piscina extends {
5-
new (options?: infer T): Piscina;
6-
}
7-
? T
8-
: never;
4+
type PiscinaOptions = NonNullable<ConstructorParameters<typeof Piscina>[0]>;
95

10-
export interface FastifyPiscinaPool extends Piscina {}
6+
type FastifyPiscinaPluginType = FastifyPluginCallback<PiscinaOptions>;
117

12-
// Most importantly, use declaration merging to add the custom property to the Fastify type system
138
declare module 'fastify' {
149
interface FastifyInstance {
15-
piscina: FastifyPiscinaPool;
16-
runTask: FastifyPiscinaPool['run'];
10+
piscina: fastifyPiscina.FastifyPiscinaPool;
11+
runTask: fastifyPiscina.FastifyPiscinaRunTask;
1712
}
1813
}
1914

20-
declare const fastifyPiscina: FastifyPluginCallback<PiscinaOptions>;
21-
export default fastifyPiscina;
15+
declare namespace fastifyPiscina {
16+
export type FastifyPiscinaPool = Piscina;
17+
export type FastifyPiscinaRunTask = Piscina['run'];
18+
19+
export type FastifyPiscinaPluginOptions = PiscinaOptions;
20+
21+
export const fastifyPiscina: FastifyPiscinaPluginType;
22+
export { fastifyPiscina as default };
23+
}
24+
25+
declare function fastifyPiscina(...params: Parameters<FastifyPiscinaPluginType>): ReturnType<FastifyPiscinaPluginType>;
26+
export = fastifyPiscina;

plugin.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
const fp = require('fastify-plugin');
44
const Piscina = require('piscina');
5-
const { name, version } = require('./package.json');
5+
const { name } = require('./package.json');
66

7-
function piscinaPlugin (fastify, options, next) {
7+
function fastifyPiscina (fastify, options, next) {
88
if (fastify.piscina || fastify.runTask) {
99
return next(new Error('fastify-piscina has already been registered'));
1010
}
@@ -17,8 +17,11 @@ function piscinaPlugin (fastify, options, next) {
1717
next();
1818
}
1919

20-
module.exports = fp(piscinaPlugin, {
21-
fastify: '>=4.0.0',
22-
name,
23-
version
20+
const plugin = fp(fastifyPiscina, {
21+
fastify: '5.x',
22+
name
2423
});
24+
25+
module.exports = plugin;
26+
module.exports.default = plugin;
27+
module.exports.fastifyPiscina = plugin;

test/plugin-esm.test.mjs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { test } from 'tap';
44
import Fastify from 'fastify';
5-
import fastifyPiscina from '../esm-wrapper.mjs';
5+
import fastifyPiscina from '../plugin.js';
66

77
test('It should add decorators - ESM', async (t) => {
88
t.plan(3);
@@ -29,9 +29,7 @@ test('It should throw when trying to register the plugin more than once - ESM',
2929
t.plan(1);
3030

3131
const fastify = Fastify();
32-
fastify
33-
.register(fastifyPiscina)
34-
.register(fastifyPiscina);
32+
fastify.register(fastifyPiscina).register(fastifyPiscina);
3533

3634
fastify.ready((err) => {
3735
t.equal(err.message, 'fastify-piscina has already been registered');

0 commit comments

Comments
 (0)