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

types: export FastifyHelmetOptions to TypeScript users #165

Merged
merged 2 commits into from
Jan 9, 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
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);