Skip to content

Commit

Permalink
feat!: v5 (#12) (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
metcoder95 authored Nov 26, 2023
1 parent e048210 commit 2b93bcc
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 38 deletions.
64 changes: 46 additions & 18 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,28 +1,56 @@
name: CI
on:
push:
branches:
- current
- next
- 'v*'
pull_request:

on: [push, pull_request]
name: CI

jobs:
test:
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install Node.js
uses: actions/setup-node@v3
with:
node-version: v20.x
- name: Install dependencies
run: npm install
- name: Check linting
run: npm run lint

tests:
needs: [lint]
name: Tests
strategy:
fail-fast: false
matrix:
node-version: [12, 14, 16, 17]
os: [ubuntu-latest, windows-latest, macos-latest]

os: [ubuntu-latest, macos-latest, windows-latest]
node-version: [16.x, 18.x, 20.x]
runs-on: ${{matrix.os}}
steps:
- uses: actions/checkout@v2

- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- uses: actions/checkout@v3
with:
persist-credentials: false

- name: Install dependencies
run: |
npm install --ignore-scripts
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}

- name: Run tests
run: |
npm run test
- uses: actions/cache@v3
id: check-cache
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ matrix.node-version }}-${{ hashFiles('**/package.json') }}
restore-keys: |
${{ runner.os }}-node-${{ matrix.node-version }}-
- name: Install Dependencies
run: npm install
- name: Run Tests
run: npm run test:ci
20 changes: 11 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,24 @@
},
"types": "./plugin.d.ts",
"scripts": {
"prepack": "npm run build",
"build": "gen-esm-wrapper . esm-wrapper.mjs",
"lint": "standardx \"**/*.{mjs,js,cjs}\" | snazzy",
"lint:fix": "standardx --fix",
"prepack": "npm run build",
"test": "npm run lint && npm run unit && npm run test:typescript",
"test:typescript": "tsd",
"unit": "tap -J test/*.test.{js,mjs}",
"unit:verbose": "npm run unit -- -Rspec"
"test": "tap -J test/*.test.{js,mjs}",
"test:coverage": "tap -J test/*.test.{js,mjs} --cov --coverage-report=html --no-browser --no-check-coverage",
"test:verbose": "npm run unit -- -Rspec",
"test:ci": "npm run test:coverage && npm run test:typescript",
"test:typescript": "tsd"
},
"repository": {
"type": "git",
"url": "git+https://github.com/piscinajs/fastify-piscina"
},
"keywords": [
"piscina",
"fastify"
"fastify",
"fastify-plugin"
],
"author": "James M Snell <jasnell@gmail.com>",
"contributors": [
Expand All @@ -34,16 +36,16 @@
"license": "MIT",
"devDependencies": {
"@types/node": "^16.11.11",
"fastify": "^3.24.1",
"fastify": "^4.24.3",
"gen-esm-wrapper": "^1.1.3",
"snazzy": "^9.0.0",
"standardx": "^7.0.0",
"tap": "^15.1.5",
"tsd": "^0.19.0"
},
"dependencies": {
"fastify-plugin": "^3.0.0",
"piscina": "^3.1.0"
"fastify-plugin": "^4.5.1",
"piscina": "^4.2.0"
},
"eslintConfig": {
"rules": {
Expand Down
6 changes: 3 additions & 3 deletions plugin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { FastifyPluginCallback } from 'fastify';
import Piscina from 'piscina';

type PiscinaOptions = typeof Piscina extends {
new (options?: infer T): Piscina;
}
new (options?: infer T): Piscina;
}
? T
: never;

export interface FastifyPiscinaPool extends Piscina {}

// Most importantly, use declaration merging to add the custom property to the Fastify type system
declare module "fastify" {
declare module 'fastify' {
interface FastifyInstance {
piscina: FastifyPiscinaPool;
runTask: FastifyPiscinaPool['run'];
Expand Down
2 changes: 1 addition & 1 deletion plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function piscinaPlugin (fastify, options, next) {
}

module.exports = fp(piscinaPlugin, {
fastify: '>=1.0.0',
fastify: '>=4.0.0',
name,
version
});
16 changes: 9 additions & 7 deletions test/types/plugin.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import path from 'path';
import Fastify from 'fastify';
import Fastify, { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';
import { expectError, expectType } from 'tsd';
import piscinaPlugin, { FastifyPiscinaPool } from '../../plugin';

const app = Fastify();
const app: FastifyInstance = Fastify();

app.register(piscinaPlugin, {
filename: path.join(__dirname, 'worker.js')
filename: path.join(__dirname, 'worker.js'),
});

app.get('/', async (request, reply) => {
// Tsd complains for no reason
// @ts-ignore
app.get('/', async (_request: FastifyRequest, _reply: FastifyReply) => {
return { hello: `world [${await app.runTask({ a: 1, b: 2 })}]` };
});

app.after(() => {
app.ready(() => {
expectType<FastifyPiscinaPool>(app.piscina);
expectType<FastifyPiscinaPool['run']>(app.runTask);
});
Expand All @@ -22,6 +24,6 @@ const appThatTriggersTypescriptErrors = Fastify();

expectError(
appThatTriggersTypescriptErrors.register(piscinaPlugin, {
unknownOption: 'I will trigger a typescript error'
unknownOption: 'I will trigger a typescript error',
})
)
);

0 comments on commit 2b93bcc

Please sign in to comment.