Skip to content

Commit

Permalink
nodenext compatibility (#207)
Browse files Browse the repository at this point in the history
* declare namespace

* fix: use FastifyPluginAsync

* fix

Co-authored-by: Uzlopak <aras.abbasi@googlemail.com>
  • Loading branch information
cesarvspr and Uzlopak committed Nov 23, 2022
1 parent e288315 commit 9bc0d33
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { randomBytes } = require('crypto')
const fp = require('fastify-plugin')
const helmet = require('helmet')

async function helmetPlugin (fastify, options) {
async function fastifyHelmet (fastify, options) {
// helmet will throw when any option is explicitly set to "true"
// using ECMAScript destructuring is a clean workaround as we do not need to alter options
const { enableCSPNonces, global, ...globalConfiguration } = options
Expand Down Expand Up @@ -138,9 +138,11 @@ function done (error) {
if (error) throw error
}

module.exports = fp(helmetPlugin, {
module.exports = fp(fastifyHelmet, {
fastify: '4.x',
name: '@fastify/helmet'
})
module.exports.default = fastifyHelmet
module.exports.fastifyHelmet = fastifyHelmet

module.exports.contentSecurityPolicy = helmet.contentSecurityPolicy
33 changes: 20 additions & 13 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import helmet, { contentSecurityPolicy, HelmetOptions } from 'helmet';
declare module 'fastify' {
export interface RouteShorthandOptions<
RawServer extends RawServerBase = RawServerDefault
> extends FastifyHelmetRouteOptions {}
> extends fastifyHelmet.FastifyHelmetRouteOptions { }

interface FastifyReply {
cspNonce: {
Expand All @@ -14,20 +14,27 @@ declare module 'fastify' {
helmet: (opts?: HelmetOptions) => typeof helmet
}

export interface RouteOptions extends FastifyHelmetRouteOptions {}
export interface RouteOptions extends fastifyHelmet.FastifyHelmetRouteOptions { }
}

export interface FastifyHelmetRouteOptions {
helmet?: Omit<FastifyHelmetOptions, 'global'> | false;
}

export interface FastifyHelmetOptions extends NonNullable<HelmetOptions> {
enableCSPNonces?: boolean,
global?: boolean;
}

export const fastifyHelmet: FastifyPluginAsync<FastifyHelmetOptions> & {
type FastifyHelmet = FastifyPluginAsync<fastifyHelmet.FastifyHelmetOptions> & {
contentSecurityPolicy: typeof contentSecurityPolicy;
};

export default fastifyHelmet;
declare namespace fastifyHelmet {

export interface FastifyHelmetRouteOptions {
helmet?: Omit<FastifyHelmetOptions, 'global'> | false;
}

export interface FastifyHelmetOptions extends NonNullable<HelmetOptions> {
enableCSPNonces?: boolean,
global?: boolean;
}

export const fastifyHelmet: FastifyHelmet
export { fastifyHelmet as default }
}

declare function fastifyHelmet(...params: Parameters<FastifyHelmet>): ReturnType<FastifyHelmet>
export = fastifyHelmet

0 comments on commit 9bc0d33

Please sign in to comment.