Skip to content

Commit

Permalink
fix validate types issues
Browse files Browse the repository at this point in the history
  • Loading branch information
tsdexter committed Mar 8, 2023
1 parent dfe706f commit 568e8ad
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
22 changes: 13 additions & 9 deletions packages/gatsby-plugin-fastify/src/gatsby/header-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import typeOf from "just-typeof";

import { SECURITY_HEADERS, CACHING_HEADERS, IMMUTABLE_CACHING_HEADER } from "../utils/constants";
import type { PluginData } from "../utils/plugin-data";
import type { GatsbyNodeServerConfig } from "../utils/config";
import type { GatsbyFastifyPluginOptions } from "../utils/config";
import { HeadersOption } from "../utils/headers";

function deepMerge(...headers) {
function deepMerge(...headers: HeadersOption[]) {
// eslint-disable-next-line unicorn/no-array-reduce
return headers.reduce((accumulator, header: { [key: string]: object }) => {
return headers.reduce((accumulator, header) => {
for (let [key, value] of Object.entries(header)) {
// console.log(typeOf(value));
//merge needs empty object to prevent overwriting of references use across multiple routes
Expand All @@ -23,8 +24,8 @@ function deepMerge(...headers) {

// program methods
const applySecurityHeaders =
({ mergeSecurityHeaders }: GatsbyNodeServerConfig) =>
(headers) => {
({ mergeSecurityHeaders }: GatsbyFastifyPluginOptions) =>
(headers: HeadersOption) => {
if (!mergeSecurityHeaders) {
return headers;
}
Expand All @@ -33,8 +34,8 @@ const applySecurityHeaders =
};

const applyCachingHeaders =
(pluginData: PluginData, { mergeCacheHeaders }: GatsbyNodeServerConfig) =>
(headers) => {
(pluginData: PluginData, { mergeCacheHeaders }: GatsbyFastifyPluginOptions) =>
(headers: HeadersOption) => {
if (!mergeCacheHeaders) {
return headers;
}
Expand All @@ -53,7 +54,7 @@ const applyCachingHeaders =

const files = chunks.flatMap((chunk) => pluginData.manifest[chunk]);

const cachingHeaders = {};
const cachingHeaders: HeadersOption = {};

for (const file of files) {
if (typeof file === `string`) {
Expand All @@ -63,7 +64,10 @@ const applyCachingHeaders =
return deepMerge(cachingHeaders, CACHING_HEADERS, headers);
};

export function buildHeadersProgram(pluginData, pluginOptions) {
export function buildHeadersProgram(
pluginData: PluginData,
pluginOptions: GatsbyFastifyPluginOptions
) {
return compose(
applySecurityHeaders(pluginOptions),
applyCachingHeaders(pluginData, pluginOptions)
Expand Down
8 changes: 3 additions & 5 deletions packages/gatsby-plugin-fastify/src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { FastifyServerOptions } from "fastify";
import { PathConfig } from "../plugins/client-routes";
import { CONFIG_FILE_NAME, CONFIG_FILE_PATH } from "./constants";
import { buildPrefixer } from "./plugin-data";
import { HeadersOption } from "./headers";

let config: Partial<GfConfig> = {};

Expand All @@ -22,15 +23,12 @@ export interface GatsbyFastifyPluginOptions extends PluginOptions {
imageCdn: boolean;
};
fastify: FastifyServerOptions;
headers: HeadersOption;
}
export interface GatsbyNodeServerConfig extends GatsbyFastifyPluginOptions {
clientSideRoutes: NoUndefinedField<PathConfig>[];
serverSideRoutes: ServerSideRoute[];
headers: {
[path: string]: {
[name: string]: string;
};
};
headers: HeadersOption;
mergeCacheHeaders?: boolean;
mergeSecurityHeaders?: boolean;
redirects: IRedirect[];
Expand Down
6 changes: 6 additions & 0 deletions packages/gatsby-plugin-fastify/src/utils/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ function appendHeader({ name, value }: { name: string; value: string }, reply: F
}
}

export type HeadersOption = {
[key: string]: {
[key: string]: string;
};
};

export type Modules =
| "DSG"
| "SSR"
Expand Down

0 comments on commit 568e8ad

Please sign in to comment.