diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 31bfa26..6454bd3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -29,5 +29,7 @@ jobs: pull-requests: write uses: fastify/workflows/.github/workflows/plugins-ci.yml@ef591e2186785d5ab36b9fe6a79c7ce2f1d94e57 # v7.0.0 with: + node-versions: '["22", "24", "26"]' license-check: true lint: true + diff --git a/README.md b/README.md index 6e3f0a1..1278556 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,44 @@ npm i @fastify/flash Flash messages are stored in the session. First, we need to register the session plugin: [@fastify/secure-session](https://www.npmjs.com/package/@fastify/secure-session). +### ESM + +```javascript +import { readFileSync } from 'node:fs' +import { join } from 'node:path' +import Fastify from 'fastify' +import fastifySession from '@fastify/secure-session' +import fastifyFlash from '@fastify/flash' + +const fastify = Fastify() + +fastify.register(fastifySession, { + // adapt this to point to the directory where secret-key is located + key: readFileSync(join(import.meta.dirname, 'secret-key')), + cookie: { + // options from setCookie, see https://github.com/fastify/fastify-cookie + path: "/" + } +}) +fastify.register(fastifyFlash, { + prefix: "/" +}) + +fastify.get('/test', (req, reply) => { + req.flash('warning', ['username required', 'password required']) + + const warning = reply.flash('warning') + reply.send({ warning }) // {"warning":["username required","password required"]} +}) +``` + +### CommonJS + +Even though this package ships as ESM, it can still be `require()`'d from a CommonJS project (see the [Install](#install) note above): + ```javascript +const fs = require('node:fs') +const path = require('node:path') const fastify = require('fastify')() const fastifySession = require('@fastify/secure-session') const fastifyFlash = require('@fastify/flash') diff --git a/eslint.config.js b/eslint.config.cjs similarity index 100% rename from eslint.config.js rename to eslint.config.cjs diff --git a/package.json b/package.json index 6061398..c589a67 100644 --- a/package.json +++ b/package.json @@ -2,8 +2,15 @@ "name": "@fastify/flash", "version": "6.0.5", "description": "Flash message plugin for fastify.", - "main": "./lib", - "type": "commonjs", + "type": "module", + "main": "./lib/index.js", + "types": "./lib/index.d.ts", + "exports": { + ".": { + "types": "./lib/index.d.ts", + "default": "./lib/index.js" + } + }, "scripts": { "build": "npm run clean-build && npm run lint && tsc -p ./tsconfig.build.json", "clean-build": "rimraf ./lib && mkdir lib", @@ -57,7 +64,7 @@ "devDependencies": { "@fastify/secure-session": "^8.0.0", "@types/node": "^26.0.0", - "borp": "^0.21.0", + "borp": "^1.0.0", "eslint": "^9.35.0", "fastify": "^5.0.0", "neostandard": "^0.13.0", @@ -72,5 +79,8 @@ ], "publishConfig": { "access": "public" + }, + "engines": { + "node": ">=20.19.0 <21 || >=22.12.0" } } diff --git a/src/index.ts b/src/index.ts index 9d5ddf8..c936e15 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,5 @@ -import fp = require('fastify-plugin') -import { flashFactory } from './flash' +import fp from 'fastify-plugin' +import { flashFactory } from './flash.ts' declare module 'fastify' { export interface FastifyRequest { @@ -10,10 +10,9 @@ declare module 'fastify' { } } -export = fp<{ core?: string }>( +export default fp<{ core?: string }>( function (fastify, _opts, done) { const flash = flashFactory() - fastify.decorateRequest('flash', flash.request) fastify.decorateReply('flash', flash.reply) done() diff --git a/test/index.test.ts b/test/index.test.ts index d3c8766..6994191 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -1,12 +1,12 @@ -import { test, TestContext } from 'node:test' +import { test, type TestContext } from 'node:test' import { join } from 'node:path' import { readFileSync } from 'node:fs' import Fastify from 'fastify' -import fastifySession, { Session, SessionData } from '@fastify/secure-session' +import fastifySession, { type Session, type SessionData } from '@fastify/secure-session' import querystring from 'node:querystring' -import fastifyFlash from '../src' +import fastifyFlash from '../src/index.ts' -const key = readFileSync(join(__dirname, '..', '..', 'secret-key')) +const key = readFileSync(join(import.meta.dirname, '..', 'secret-key')) test('should set error message and and clear up after displaying.', async (t: TestContext) => { t.plan(5) diff --git a/tsconfig.json b/tsconfig.json index ed79f80..fde3b51 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,9 +1,13 @@ { "compilerOptions": { - "moduleResolution": "node16", + "target": "esnext", + "module": "nodenext", + "moduleResolution": "nodenext", + "allowImportingTsExtensions": true, + "rewriteRelativeImportExtensions": true, + "verbatimModuleSyntax": true, + "erasableSyntaxOnly": true, "declaration": true, - "target": "es2022", - "module": "node16", "outDir": "lib", "pretty": true, "noEmitOnError": true,