Skip to content

Commit fac88ca

Browse files
authored
nodenext compatibility (#134)
1 parent e71f65a commit fac88ca

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
lines changed

formbody.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const fp = require('fastify-plugin')
44
const { parse: defaultParser } = require('fast-querystring')
55

6-
function formBodyPlugin (fastify, options, next) {
6+
function fastifyFormbody (fastify, options, next) {
77
const opts = Object.assign({ parser: defaultParser }, options)
88
if (typeof opts.parser !== 'function') {
99
next(new Error('parser must be a function'))
@@ -22,7 +22,9 @@ function formBodyPlugin (fastify, options, next) {
2222
next()
2323
}
2424

25-
module.exports = fp(formBodyPlugin, {
25+
module.exports = fp(fastifyFormbody, {
2626
fastify: '4.x',
2727
name: '@fastify/formbody'
2828
})
29+
module.exports.default = fastifyFormbody
30+
module.exports.fastifyFormbody = fastifyFormbody

types/formbody.d.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
import { FastifyPluginCallback } from 'fastify'
22

3-
export interface FormBodyPluginOptions {
4-
bodyLimit?: number
5-
parser?: (str: string) => Record<string, unknown>
6-
}
3+
type FastifyFormbody = FastifyPluginCallback<fastifyFormbody.FastifyFormbodyOptions>
4+
5+
declare namespace fastifyFormbody {
6+
export interface FastifyFormbodyOptions {
7+
bodyLimit?: number
8+
parser?: (str: string) => Record<string, unknown>
9+
}
710

8-
declare const formBodyPlugin: FastifyPluginCallback<FormBodyPluginOptions>;
11+
/**
12+
* @deprecated Use FastifyFormbodyOptions instead
13+
*/
14+
export type FormBodyPluginOptions = FastifyFormbodyOptions
15+
16+
export const fastifyFormbody: FastifyFormbody
17+
export { fastifyFormbody as default }
18+
}
919

10-
export default formBodyPlugin
20+
declare function fastifyFormbody(...params: Parameters<FastifyFormbody>): ReturnType<FastifyFormbody>
21+
export = fastifyFormbody

types/formbody.test-d.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import fastify from 'fastify'
22
import querystring from 'querystring'
3-
import formBodyPlugin, { FormBodyPluginOptions } from '..'
3+
import { expectDeprecated, expectError, expectType } from 'tsd'
4+
import formBodyPlugin, { FastifyFormbodyOptions, FormBodyPluginOptions } from '..'
45

56
const app = fastify()
67
app.register(formBodyPlugin)
78

8-
const emptyOpts: FormBodyPluginOptions = {}
9-
app.register(formBodyPlugin, emptyOpts)
10-
11-
const bodyLimitOpts: FormBodyPluginOptions = {
9+
app.register(formBodyPlugin, { })
10+
app.register(formBodyPlugin, {
1211
bodyLimit: 1000
13-
}
14-
app.register(formBodyPlugin, bodyLimitOpts)
15-
16-
const parserOpts: FormBodyPluginOptions = {
12+
})
13+
app.register(formBodyPlugin, {
1714
parser: (s) => querystring.parse(s)
18-
}
19-
app.register(formBodyPlugin, parserOpts)
15+
})
16+
17+
expectType<FormBodyPluginOptions>({} as FastifyFormbodyOptions)
18+
expectDeprecated({} as FormBodyPluginOptions)
19+
20+
expectError(app.register(formBodyPlugin, { invalid: true }))

0 commit comments

Comments
 (0)