Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nodenext compatibility #172

Merged
merged 1 commit into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 0 additions & 29 deletions index.d.ts

This file was deleted.

6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const Middie = require('./engine')
const kMiddlewares = Symbol('fastify-middie-middlewares')
const kMiddie = Symbol('fastify-middie-instance')

function middiePlugin (fastify, options, next) {
function fastifyMiddie (fastify, options, next) {
fastify.decorate('use', use)
fastify[kMiddlewares] = []
fastify[kMiddie] = Middie(onMiddieEnd)
Expand Down Expand Up @@ -62,7 +62,9 @@ function middiePlugin (fastify, options, next) {
next()
}

module.exports = fp(middiePlugin, {
module.exports = fp(fastifyMiddie, {
fastify: '4.x',
name: '@fastify/middie'
})
module.exports.default = fastifyMiddie
module.exports.fastifyMiddie = fastifyMiddie
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "8.0.0",
"description": "Middleware engine for Fastify",
"main": "index.js",
"types": "index.d.ts",
"types": "types/index.d.ts",
"scripts": {
"coverage": "tap --cov --coverage-report=html test.js",
"test": "standard && tap --no-coverage test/*.test.js && tsd"
Expand Down Expand Up @@ -48,9 +48,6 @@
"path-to-regexp": "^6.1.0",
"reusify": "^1.0.4"
},
"tsd": {
"directory": "test"
},
"publishConfig": {
"access": "public"
}
Expand Down
40 changes: 40 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as connect from 'connect'
import { FastifyPluginCallback } from 'fastify'
import * as http from "http";

declare module "fastify" {
interface FastifyInstance {
use(fn: fastifyMiddie.Handler): this;
use(route: string, fn: fastifyMiddie.Handler): this;
use(routes: string[], fn: fastifyMiddie.Handler): this;
}
}

type FastifyMiddie = FastifyPluginCallback<fastifyMiddie.FastifyMiddieOptions>

declare namespace fastifyMiddie {
export interface FastifyMiddieOptions {
hook?: 'onRequest' | 'preParsing' | 'preValidation' | 'preHandler' | 'preSerialization' | 'onSend' | 'onResponse' | 'onTimeout' | 'onError';
}

/**
* @deprecated Use FastifyMiddieOptions instead
*/
export type MiddiePluginOptions = FastifyMiddieOptions

export interface IncomingMessageExtended {
body?: any;
query?: any;
}
export type NextFunction = (err?: any) => void;
export type SimpleHandleFunction = (req: http.IncomingMessage & IncomingMessageExtended, res: http.ServerResponse) => void;
export type NextHandleFunction = (req: connect.IncomingMessage & IncomingMessageExtended, res: http.ServerResponse, next: NextFunction) => void;

export type Handler = SimpleHandleFunction | NextHandleFunction

export const fastifyMiddie: FastifyMiddie
export { fastifyMiddie as default }
}

declare function fastifyMiddie(...params: Parameters<FastifyMiddie>): ReturnType<FastifyMiddie>
export = fastifyMiddie
7 changes: 5 additions & 2 deletions test/index.test-d.ts → types/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fastify from "fastify";
import middiePlugin, {MiddiePluginOptions, IncomingMessageExtended} from "..";
import { expectAssignable, expectType } from "tsd";
import middiePlugin, {MiddiePluginOptions, IncomingMessageExtended, FastifyMiddieOptions} from "..";
import { expectAssignable, expectType, expectDeprecated } from "tsd";

const app = fastify();
app.register(middiePlugin);
Expand All @@ -16,3 +16,6 @@ app.use('/', (_req, _res, next) => {

next()
})

expectDeprecated({} as MiddiePluginOptions)
expectType<FastifyMiddieOptions>({} as MiddiePluginOptions)