From 684a47f4b9c3f6aec681e4f0cfa2340dd7d97d60 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Sat, 3 Dec 2022 02:14:10 +0100 Subject: [PATCH 1/2] nodenext compatibility --- test/types/types.test.ts | 30 ------------------------------ types/index.d.ts | 25 ++++++++++++++++++++----- types/index.test-d.ts | 36 ++++++++++++++++++++++++++++++++---- 3 files changed, 52 insertions(+), 39 deletions(-) delete mode 100644 test/types/types.test.ts diff --git a/test/types/types.test.ts b/test/types/types.test.ts deleted file mode 100644 index 40021c3..0000000 --- a/test/types/types.test.ts +++ /dev/null @@ -1,30 +0,0 @@ -import fastify from "fastify"; -import fasitfyEnv from "../.."; - -const app = fastify(); - -app.register(fasitfyEnv); -app.register(fasitfyEnv, {}); -app.register(fasitfyEnv, { - schema: {}, -}); -app.register(fasitfyEnv, { - data: {}, -}); -app.register(fasitfyEnv, { - data: [{}], -}); -app.register(fasitfyEnv, { - env: true, -}); -app.register(fasitfyEnv, { - dotenv: true, -}); -app.register(fasitfyEnv, { - dotenv: {}, -}); -app.register(fasitfyEnv, { - confKey: 'config' -}) - -app.ready(); diff --git a/types/index.d.ts b/types/index.d.ts index 875ba39..61614d4 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,7 +1,22 @@ -import { EnvSchemaOpt } from "env-schema"; -import { FastifyPluginCallback } from "fastify"; +import { EnvSchemaOpt } from "env-schema" +import { FastifyPluginCallback } from "fastify" -export type fastifyEnvOpt = EnvSchemaOpt & { confKey?: string }; +type FastifyEnv = FastifyPluginCallback + +declare namespace fastifyEnv { + export interface FastifyEnvOptions extends EnvSchemaOpt { + confKey?: string + } + + /** + * @deprecated Use FastifyEnvOptions instead + */ + export type fastifyEnvOpt = FastifyEnvOptions + + export const fastifyEnv: FastifyEnv + export { fastifyEnv as default } +} + +declare function fastifyEnv(...params: Parameters): ReturnType +export = fastifyEnv -export const fastifyEnv: FastifyPluginCallback; -export default fastifyEnv; diff --git a/types/index.test-d.ts b/types/index.test-d.ts index 31a3910..643f8a6 100644 --- a/types/index.test-d.ts +++ b/types/index.test-d.ts @@ -1,6 +1,34 @@ -import fastify from 'fastify'; -import fastifyEnv from '..'; +import Fastify from 'fastify' +import { expectDeprecated, expectType } from 'tsd' +import fastifyEnv, { FastifyEnvOptions, fastifyEnvOpt } from '..' -const server = fastify(); +const fastify = Fastify() -server.register(fastifyEnv, { confKey: 'test' }); +fastify.register(fastifyEnv, { confKey: 'test' }) + +fastify.register(fastifyEnv) +fastify.register(fastifyEnv, {}) +fastify.register(fastifyEnv, { + schema: {}, +}) +fastify.register(fastifyEnv, { + data: {}, +}) +fastify.register(fastifyEnv, { + data: [{}], +}) +fastify.register(fastifyEnv, { + env: true, +}) +fastify.register(fastifyEnv, { + dotenv: true, +}) +fastify.register(fastifyEnv, { + dotenv: {}, +}) +fastify.register(fastifyEnv, { + confKey: 'config' +}) + +expectDeprecated({} as fastifyEnvOpt) +expectType({} as fastifyEnvOpt) From 1bde55290c4877f39eb34e6c6df08e141b8ca2cd Mon Sep 17 00:00:00 2001 From: uzlopak Date: Sat, 3 Dec 2022 02:18:40 +0100 Subject: [PATCH 2/2] add missing exports --- index.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 78d2f0f..b7f5bc5 100644 --- a/index.js +++ b/index.js @@ -3,7 +3,7 @@ const fp = require('fastify-plugin') const envSchema = require('env-schema') -function loadAndValidateEnvironment (fastify, opts, done) { +function fastifyEnv (fastify, opts, done) { try { const config = envSchema(opts) const confKey = opts.confKey || 'config' @@ -14,7 +14,9 @@ function loadAndValidateEnvironment (fastify, opts, done) { } } -module.exports = fp(loadAndValidateEnvironment, { +module.exports = fp(fastifyEnv, { fastify: '4.x', name: '@fastify/env' }) +module.exports.default = fastifyEnv +module.exports.fastifyEnv = fastifyEnv