Skip to content

Commit

Permalink
fix: allow complete opt-in file downloading (#470)
Browse files Browse the repository at this point in the history
* chore: update @prismicio packages

* refactor(source): remove explicit default client pageSize

* chore: update dependencies

* feat(source): add support for complete file downloading opt-in

* chore: update dependencies

* refactor(source): remove unused import

* fix(previews): add empty styles.css for easier migration

* fix(source): remove auto parameter from locally downloaded files

* refactor: remove unnecessary test plugin option
  • Loading branch information
angeloashmore authored Oct 29, 2021
1 parent a8dc1fd commit c1b27b3
Show file tree
Hide file tree
Showing 19 changed files with 2,770 additions and 3,292 deletions.
3 changes: 3 additions & 0 deletions packages/gatsby-plugin-prismic-previews/.storybook/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import postcss from "postcss";
import type { StorybookConfig } from "@storybook/react/types";

export default {
core: {
builder: "webpack5",
},
typescript: {
check: false,
},
Expand Down
33 changes: 18 additions & 15 deletions packages/gatsby-plugin-prismic-previews/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@
],
"scripts": {
"build": "siroc build",
"build-storybook": "build-storybook",
"dev": "siroc build --watch",
"format": "prettier --write .",
"prepare": "yarn build",
"lint": "eslint --ext .js,.ts .",
"unit": "nyc ava",
"prepare": "yarn build",
"storybook": "start-storybook --quiet -p 6006",
"test": "yarn lint && yarn unit",
"build-storybook": "build-storybook",
"storybook": "start-storybook --quiet -p 6006"
"unit": "nyc ava"
},
"dependencies": {
"@imgix/gatsby": "^1.6.13",
Expand All @@ -65,29 +65,31 @@
"@angeloashmore/esbuild-register": "^0.0.1",
"@prismicio/mock": "^0.0.6",
"@storybook/addon-postcss": "^2.0.0",
"@storybook/cli": "^6.3.7",
"@storybook/react": "^6.3.7",
"@storybook/builder-webpack5": "^6.3.12",
"@storybook/cli": "^6.3.12",
"@storybook/manager-webpack5": "^6.3.12",
"@storybook/react": "^6.3.12",
"@testing-library/react": "^12.0.0",
"@testing-library/react-hooks": "^7.0.1",
"@themosaad/tailwindcss-capsize": "^1.0.0",
"@types/sinon": "^10.0.2",
"@typescript-eslint/eslint-plugin": "^4.29.3",
"@typescript-eslint/parser": "^4.29.3",
"@typescript-eslint/eslint-plugin": "^5.2.0",
"@typescript-eslint/parser": "^5.2.0",
"abort-controller": "^3.0.0",
"ava": "^3.15.0",
"browser-env": "^3.3.0",
"cross-fetch": "^3.1.4",
"eslint": "^7.32.0",
"eslint": "^8.1.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.1",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-tsdoc": "^0.2.14",
"gatsby": "4.0.0-alpha-9689ff.5",
"gatsby": "4.0.2",
"gatsby-image": "^3.11.0",
"gatsby-plugin-image": "^1.11.0",
"gatsby-plugin-utils": "^1.11.0",
"gatsby-plugin-image": "^2.0.0",
"gatsby-plugin-utils": "^2.0.0",
"global-jsdom": "^8.1.0",
"identity-obj-proxy": "^3.0.0",
"jsdom": "^16.7.0",
"jsdom": "^18.0.0",
"msw": "^0.35.0",
"node-fetch": "^2.6.5",
"nyc": "^15.1.0",
Expand All @@ -106,7 +108,8 @@
"tailwindcss": "^2.2.7",
"to-string-loader": "^1.1.6",
"type-fest": "^2.1.0",
"typescript": "^4.4.2"
"typescript": "^4.4.2",
"webpack": "^5.61.0"
},
"peerDependencies": {
"gatsby": "^3.0.0-next.0 || ^4.0.0-next.0",
Expand Down
17 changes: 17 additions & 0 deletions packages/gatsby-plugin-prismic-previews/siroc.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
import { defineSirocConfig } from "siroc";
import postcss from "rollup-plugin-postcss";
import * as fs from "fs";
import * as path from "path";

export default defineSirocConfig({
rollup: {
plugins: [postcss({ inject: false })],
output: { sourcemap: true },
},
hooks: {
"build:done": () => {
console.log("Copying static files from ./static into ./dist");

const staticDir = path.resolve("./static");
const files = fs.readdirSync(staticDir);

for (const file of files) {
fs.copyFileSync(
path.resolve(staticDir, file),
path.resolve("./dist", file),
);
}
},
},
});
5 changes: 5 additions & 0 deletions packages/gatsby-plugin-prismic-previews/static/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* This is a placeholder CSS file to ease the migration from gatsby-plugin-prismic-previews V4 to V5.
*
* The `gatsby-plugin-prismic-previews/dist/styles.css` import can be removed in V5.
*/
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ export const createPluginOptions = (t: ava.ExecutionContext): PluginOptions => {
apiEndpoint: prismic.getEndpoint(repositoryName),
typePrefix: "prefix",
lang: "*",
// TODO: Remove the hardcoded default once this PR to @prismicio/client is merged:
// https://github.com/prismicio/prismic-client/pull/195
pageSize: 100,
toolbar: "new",
imageImgixParams: { q: 100 },
imagePlaceholderImgixParams: { w: 10 },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import * as msw from "msw";

// TODO: Uncomment when the Authorization header can be used
// @see Related issue - {@link https://github.com/prismicio/issue-tracker-wroom/issues/351}
// import { createAuthorizationHeader } from "./createAuthorizationHeader";
import { createAuthorizationHeader } from "./createAuthorizationHeader";

export const isValidAccessToken = (
accessToken: string | undefined,
req: msw.RestRequest,
): boolean => {
// TODO: Uncomment when the Authorization header can be used
// @see Related issue - {@link https://github.com/prismicio/issue-tracker-wroom/issues/351}
// return typeof accessToken === "string"
// ? req.headers.get("Authorization") ===
// createAuthorizationHeader(accessToken)
// : true;
return typeof accessToken === "string"
? req.url.searchParams.get("access_token") === accessToken
? req.headers.get("Authorization") ===
createAuthorizationHeader(accessToken)
: true;
};
22 changes: 11 additions & 11 deletions packages/gatsby-source-prismic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
"build": "siroc build",
"dev": "siroc build --watch",
"format": "prettier --write .",
"prepare": "yarn build",
"lint": "eslint --ext .js,.ts .",
"unit": "nyc ava",
"test": "yarn lint && yarn unit"
"prepare": "yarn build",
"test": "yarn lint && yarn unit",
"unit": "nyc ava"
},
"dependencies": {
"@imgix/gatsby": "^1.6.13",
Expand All @@ -51,27 +51,27 @@
"camel-case": "^4.1.2",
"fp-ts": "^2.10.5",
"gatsby-node-helpers": "^1.2.1",
"gatsby-source-filesystem": "^3.11.0",
"gatsby-source-filesystem": "^4.0.0",
"node-fetch": "^2.6.5",
"tiny-hashes": "^1.0.1"
},
"devDependencies": {
"@prismicio/mock": "^0.0.6",
"@types/sinon": "^10.0.2",
"@typescript-eslint/eslint-plugin": "^4.29.3",
"@typescript-eslint/parser": "^4.29.3",
"@typescript-eslint/eslint-plugin": "^5.2.0",
"@typescript-eslint/parser": "^5.2.0",
"ava": "^3.15.0",
"esbuild-register": "^3.0.0",
"eslint": "^7.32.0",
"eslint": "^8.1.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.1",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-react": "^7.24.0",
"eslint-plugin-react-hooks": "^4.2.0",
"eslint-plugin-tsdoc": "^0.2.14",
"gatsby": "4.0.0-alpha-9689ff.5",
"gatsby": "4.0.2",
"gatsby-image": "^3.11.0",
"gatsby-plugin-image": "^1.11.0",
"gatsby-plugin-utils": "^1.11.0",
"gatsby-plugin-image": "^2.0.0",
"gatsby-plugin-utils": "^2.0.0",
"msw": "^0.35.0",
"nyc": "^15.1.0",
"prettier": "^2.3.2",
Expand Down
32 changes: 18 additions & 14 deletions packages/gatsby-source-prismic/src/lib/createRemoteFileNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { pipe } from "fp-ts/function";

import { Dependencies } from "../types";
import { shouldDownloadFile } from "./shouldDownloadFile";
import { getFromOrSetToCache } from "./getFromOrSetToCache";

type CreateRemoteFileNodeConfig = {
url: string;
Expand Down Expand Up @@ -34,20 +35,23 @@ export const createRemoteFileNode = (
}),
),
RTE.chain((scope) =>
RTE.fromTaskEither(
TE.tryCatch(
() =>
scope.attemptDownload
? scope.createRemoteFileNode({
url: config.url,
store: scope.store,
cache: scope.cache,
createNode: scope.createNode,
createNodeId: scope.createNodeId,
reporter: scope.reporter,
})
: Promise.resolve(null),
(e) => e as Error,
getFromOrSetToCache(
`file-node-${config.url}`,
RTE.fromTaskEither(
TE.tryCatch(
() =>
scope.attemptDownload
? scope.createRemoteFileNode({
url: config.url,
store: scope.store,
cache: scope.cache,
createNode: scope.createNode,
createNodeId: scope.createNodeId,
reporter: scope.reporter,
})
: Promise.resolve(null),
(e) => e as Error,
),
),
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { createNodeOfType } from "./createNodeOfType";
import { mapRecordIndices } from "./mapRecordIndices";
import { createRemoteFileNode } from "./createRemoteFileNode";
import { PRISMIC_API_IMAGE_FIELDS } from "../constants";
import { removeURLParameter } from "./removeAutoURLParameter";

/**
* Determines if a value is a record.
Expand Down Expand Up @@ -473,7 +474,7 @@ export const normalizeDocumentSubtree = (
R.mapWithIndex((thumbnailName, thumbnail) =>
thumbnail.url
? createRemoteFileNode({
url: thumbnail.url,
url: removeURLParameter(thumbnail.url, "auto"),
path: [...path, thumbnailName],
field: thumbnail,
})
Expand All @@ -496,7 +497,7 @@ export const normalizeDocumentSubtree = (
RTE.bind("fileNode", (scope) => {
return scope.value.url
? createRemoteFileNode({
url: scope.value.url,
url: removeURLParameter(scope.value.url, "auto"),
path,
field: scope.value,
})
Expand Down
15 changes: 15 additions & 0 deletions packages/gatsby-source-prismic/src/lib/removeAutoURLParameter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Removes a URL parameter from a given URL.
*
* @param url - URL to modify.
* @param paramKey - Key of the URL parameter to remove.
*
* @returns `url` without the `paramKey` URL parameter.
*/
export const removeURLParameter = (url: string, paramKey: string) => {
const instance = new URL(url);

instance.searchParams.delete(paramKey);

return instance.toString();
};
42 changes: 28 additions & 14 deletions packages/gatsby-source-prismic/src/lib/shouldDownloadFile.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import * as prismicT from "@prismicio/types";
import * as RTE from "fp-ts/ReaderTaskEither";
import * as R from "fp-ts/Record";
import * as O from "fp-ts/Option";
import { pipe } from "fp-ts/function";

import { Dependencies } from "../types";
Expand All @@ -18,17 +16,33 @@ export const shouldDownloadFile = (
): RTE.ReaderTaskEither<Dependencies, never, boolean> =>
pipe(
RTE.ask<Dependencies>(),
RTE.bind("path", () => RTE.right(dotPath(config.path))),
RTE.bindW("predicate", (scope) =>
pipe(
scope.pluginOptions.shouldDownloadFiles,
R.lookup(scope.path),
O.map((predicate) =>
typeof predicate === "boolean" ? () => predicate : predicate,
),
O.getOrElseW(() => () => false),
RTE.right,
),
),
RTE.bindW("predicate", (scope) => {
const { shouldDownloadFiles } = scope.pluginOptions;
const fieldPath = dotPath(config.path);

switch (typeof shouldDownloadFiles) {
case "boolean": {
return RTE.right(() => shouldDownloadFiles);
}

case "function": {
return RTE.right(shouldDownloadFiles);
}

case "object": {
const fieldPredicate = shouldDownloadFiles[fieldPath];

if (fieldPredicate) {
if (typeof fieldPredicate === "boolean") {
return RTE.right(() => fieldPredicate);
} else if (typeof fieldPredicate === "function") {
return RTE.right(fieldPredicate);
}
}
}
}

return RTE.right(() => false);
}),
RTE.map((scope) => scope.predicate(config.field)),
);
21 changes: 12 additions & 9 deletions packages/gatsby-source-prismic/src/plugin-options-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ export const pluginOptionsSchema: NonNullable<
fetchLinks: Joi.array().items(Joi.string().required()),
graphQuery: Joi.string(),
lang: Joi.string(),
// TODO: Remove the hardcoded default once this PR to @prismicio/client is merged:
// https://github.com/prismicio/prismic-client/pull/195
pageSize: Joi.number().default(100),
pageSize: Joi.number(),
linkResolver: Joi.function(),
htmlSerializer: Joi.alternatives(
Joi.object().pattern(
Expand Down Expand Up @@ -71,9 +69,13 @@ export const pluginOptionsSchema: NonNullable<
imagePlaceholderImgixParams: Joi.object(),
typePrefix: Joi.string(),
webhookSecret: Joi.string(),
shouldDownloadFiles: Joi.object().pattern(
Joi.string(),
Joi.alternatives(Joi.boolean(), Joi.function()),
shouldDownloadFiles: Joi.alternatives(
Joi.boolean(),
Joi.function(),
Joi.object().pattern(
Joi.string(),
Joi.alternatives(Joi.boolean(), Joi.function()),
),
),
createRemoteFileNode: Joi.function(),
transformFieldName: Joi.function(),
Expand Down Expand Up @@ -173,10 +175,11 @@ export const pluginOptionsSchema: NonNullable<
}
}

// Check if all Custom Type models have been provided
// only if a Custom Types API token is not proided. If
// a token is provided, we can assume we have all
// needed Custom Types.
if (!unpreparedPluginOptions.customTypesApiToken) {
// Check if all Custom Type models have been provided only if a Custom
// Types API token is not proided. If a token is provided, we can
// assume we have all needed Custom Types.
const pluginOptions = await preparePluginOptions(
unpreparedPluginOptions,
);
Expand Down
Loading

0 comments on commit c1b27b3

Please sign in to comment.