Skip to content

Commit

Permalink
types: export FastifyHelmetOptions to TypeScript users (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
sniperwolf authored Jan 9, 2022
1 parent f752e6c commit 186fba4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ declare module 'fastify' {
}
}

type FastifyHelmetOptions = Parameters<typeof helmet>[0] & { enableCSPNonces?: boolean };
export type FastifyHelmetOptions = NonNullable<Parameters<typeof helmet>[0] & { enableCSPNonces?: boolean }>;

export const fastifyHelmet: FastifyPluginCallback<NonNullable<FastifyHelmetOptions>> & {
export const fastifyHelmet: FastifyPluginCallback<FastifyHelmetOptions> & {
contentSecurityPolicy: typeof helmet.contentSecurityPolicy;
};

Expand Down
23 changes: 16 additions & 7 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import fastify from "fastify";
import { expectType } from "tsd";
import fastify, { FastifyPluginCallback } from "fastify";
import { expectAssignable, expectType } from "tsd";
import helmet from "helmet";
import fastifyHelmet from ".";
import fastifyHelmet, { FastifyHelmetOptions } from ".";

const app = fastify();

app.register(fastifyHelmet);
app.register(fastifyHelmet, {});
app.register(fastifyHelmet, {

const helmetOptions = {
contentSecurityPolicy: false,
dnsPrefetchControl: false,
expectCt: false,
Expand All @@ -19,7 +20,10 @@ app.register(fastifyHelmet, {
permittedCrossDomainPolicies: false,
referrerPolicy: false,
xssFilter: false
});
};

expectAssignable<FastifyHelmetOptions>(helmetOptions);
app.register(fastifyHelmet, helmetOptions);

app.register(fastifyHelmet, {
contentSecurityPolicy: {
Expand Down Expand Up @@ -51,15 +55,15 @@ app.register(fastifyHelmet, {
policy: 'foo'
},
// these options are false or never
// hidePoweredBy: false
// hidePoweredBy: false
// ieNoOpen: false,
// noSniff: false,
// xssFilter: false
});


app.register(fastifyHelmet, { enableCSPNonces: true });
app.register(fastifyHelmet, {
app.register(fastifyHelmet, {
enableCSPNonces: true,
contentSecurityPolicy: {
directives: {
Expand All @@ -77,3 +81,8 @@ app.get('/', function(request, reply) {

const csp = fastifyHelmet.contentSecurityPolicy;
expectType<typeof helmet.contentSecurityPolicy>(csp);

// fastify-helmet instance is using the FastifyHelmetOptions options
expectType<FastifyPluginCallback<FastifyHelmetOptions> & {
contentSecurityPolicy: typeof helmet.contentSecurityPolicy;
}>(fastifyHelmet);

0 comments on commit 186fba4

Please sign in to comment.