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

chore(gatsby-plugin-netlify): add pluginOptionsSchema export #27420

Merged
merged 8 commits into from
Oct 13, 2020
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
3 changes: 2 additions & 1 deletion packages/gatsby-plugin-netlify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"@babel/cli": "^7.11.6",
"@babel/core": "^7.11.6",
"babel-preset-gatsby-package": "^0.5.3",
"cross-env": "^7.0.2"
"cross-env": "^7.0.2",
"gatsby-plugin-utils": "^0.2.27"
},
"homepage": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-netlify#readme",
"keywords": [
Expand Down
45 changes: 45 additions & 0 deletions packages/gatsby-plugin-netlify/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { testPluginOptionsSchema } from "gatsby-plugin-utils"
import { pluginOptionsSchema } from "../gatsby-node"

describe(`gatsby-node.js`, () => {
it(`should provide meaningful errors when fields are invalid`, () => {
const expectedErrors = [
`"headers" must be of type object`,
`"allPageHeaders" must be an array`,
`"mergeSecurityHeaders" must be a boolean`,
`"mergeLinkHeaders" must be a boolean`,
`"mergeCachingHeaders" must be a boolean`,
`"transformHeaders" must have an arity lesser or equal to 2`,
`"generateMatchPathRewrites" must be a boolean`,
]

const { errors } = testPluginOptionsSchema(pluginOptionsSchema, {
headers: `this should be an object`,
allPageHeaders: `this should be an array`,
mergeSecurityHeaders: `this should be a boolean`,
mergeLinkHeaders: `this should be a boolean`,
mergeCachingHeaders: `this should be a boolean`,
transformHeaders: (too, many, args) => ``,
generateMatchPathRewrites: `this should be a boolean`,
})

expect(errors).toEqual(expectedErrors)
})

it(`should validate the schema`, () => {
const { isValid } = testPluginOptionsSchema(pluginOptionsSchema, {
headers: {
"/some-page": [`Bearer: Some-Magic-Token`],
"/some-other-page": [`some`, `great`, `headers`],
},
allPageHeaders: [`First header`, `Second header`],
mergeSecurityHeaders: true,
mergeLinkHeaders: false,
mergeCachingHeaders: true,
transformHeaders: () => null,
generateMatchPathRewrites: false,
})

expect(isValid).toBe(true)
})
})
36 changes: 36 additions & 0 deletions packages/gatsby-plugin-netlify/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,39 @@ exports.onPostBuild = async (
createRedirects(pluginData, redirects, rewrites),
])
}

const MATH_ALL_KEYS = /^/
const pluginOptionsSchema = function ({ Joi }) {
// headers is a specific type used by Netlify: https://www.gatsbyjs.com/plugins/gatsby-plugin-netlify/#headers
const headersSchema = Joi.object()
.pattern(MATH_ALL_KEYS, Joi.array().items(Joi.string()))
.description(`Add more Netlify headers to specific pages`)

return Joi.object({
headers: headersSchema,
allPageHeaders: Joi.array()
.items(Joi.string())
.description(`Add more headers to all the pages`),
mergeSecurityHeaders: Joi.boolean().description(
`When set to true, turns off the default security headers`
),
mergeLinkHeaders: Joi.boolean().description(
`When set to true, turns off the default gatsby js headers`
),
mergeCachingHeaders: Joi.boolean().description(
`When set to true, turns off the default caching headers`
),
transformHeaders: Joi.function()
.maxArity(2)
mfrachet marked this conversation as resolved.
Show resolved Hide resolved
.description(
`Transform function for manipulating headers under each path (e.g.sorting), etc. This should return an object of type: { key: Array<string> }`
),
generateMatchPathRewrites: Joi.boolean().description(
`When set to true, turns off automatic creation of redirect rules for client only paths`
),
})
}

if (process.env.GATSBY_EXPERIMENTAL_PLUGIN_OPTION_VALIDATION) {
exports.pluginOptionsSchema = pluginOptionsSchema
}