Skip to content

Commit

Permalink
refactor: changes here and there
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremybarbet committed Feb 23, 2020
1 parent 1739cc5 commit 135bf12
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 59 deletions.
27 changes: 15 additions & 12 deletions packages/gatsby-source-prismic-graphql/src/components/WrapPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,19 @@ const queryOrSource = (obj: any) => {
return null;
};

const stripSharp = (query: any) => {
return traverse(query).map(function(x) {
const stripSharp = (query: any) =>
traverse(query).map(function(x) {
if (
typeof x === 'object' &&
x.kind == 'Name' &&
x.kind === 'Name' &&
this.parent &&
this.parent.node.kind === 'Field' &&
x.value.match(/Sharp$/) &&
!x.value.match(/.+childImageSharp$/)
x.value.match(/Sharp/) &&
!x.value.match(/childImageSharp$/)
) {
this.parent.remove();
this.parent.delete();
}
});
};

interface WrapPageState {
data: any;
Expand Down Expand Up @@ -102,14 +101,17 @@ export class WrapPage extends React.PureComponent<any, WrapPageState> {

if (pageContext.rootQuery && options.previews !== false && hasCookie) {
const closeLoading = createLoadingScreen();

this.setState({ loading: true });

this.load()
.then(res => {
this.setState({
loading: false,
error: null,
data: { ...this.state.data, prismic: res.data },
});

closeLoading();
})
.catch(error => {
Expand All @@ -133,15 +135,16 @@ export class WrapPage extends React.PureComponent<any, WrapPageState> {

const keys = [...(this.props.options.passContextKeys || []), ...this.keys];
variables = { ...pick(this.params, keys), ...variables };
const strip = stripSharp(getIsolatedQuery(query, fieldName, typeName));

return getApolloClient(this.props.options).then(client => {
return client.query({
query: stripSharp(getIsolatedQuery(query, fieldName, typeName)),
return getApolloClient(this.props.options).then(client =>
client.query({
query: strip,
fetchPolicy: 'network-only',
variables,
...rest,
});
});
})
);
};

render() {
Expand Down
6 changes: 1 addition & 5 deletions packages/gatsby-source-prismic-graphql/src/gatsby-ssr.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,11 @@ interface OnRenderBodyArgs {
}

exports.onRenderBody = ({ setHeadComponents }: OnRenderBodyArgs, options: PluginOptions) => {
const accessToken = options.previews ? null : options.accessToken;

const components = [
<script
key="prismic-config"
dangerouslySetInnerHTML={{
__html: `
window.prismicGatsbyOptions = ${JSON.stringify({ ...options, accessToken })};
`,
__html: `window.prismicGatsbyOptions = ${JSON.stringify(options)};`,
}}
/>,
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ import { InMemoryCache, IntrospectionFragmentMatcher } from 'apollo-cache-inmemo
import { getIntrospectionQueryResultData } from './getIntrospectionQueryResultData';
import { PrismicLink } from './index';
import { Endpoints } from './prismic';
import { PluginOptions } from '../interfaces/PluginOptions';

let client: ApolloClient<any> | undefined = undefined;

export const getApolloClient = async ({ repositoryName }: any): Promise<ApolloClient<any>> => {
export const getApolloClient = async ({
repositoryName,
accessToken,
}: PluginOptions): Promise<ApolloClient<any>> => {
if (!client) {
const introspectionQueryResultData: any = await getIntrospectionQueryResultData({
repositoryName,
accessToken,
});
const fragmentMatcher = new IntrospectionFragmentMatcher({
introspectionQueryResultData,
});
const fragmentMatcher = new IntrospectionFragmentMatcher({ introspectionQueryResultData });

client = new ApolloClient({
cache: new InMemoryCache({ fragmentMatcher }),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { Endpoints } from './prismic';

export const getIntrospectionQueryResultData = ({ repositoryName }: any) =>
export const getIntrospectionQueryResultData = ({ repositoryName, accessToken }: any) =>
new Promise((resolve, reject) => {
fetch(Endpoints.v2(repositoryName))
.then(r => r.json())
// fetch(`https://${repositoryName}.prismic.io/api`, {
// headers: new Headers({ Authorization: `Token ${accessToken}` }),
// })
fetch(`https://${repositoryName}.prismic.io/api`)
.then(result => result.json())
.then((data: any) => {
const ref = data.refs.find((r: any) => r.id === 'master');
if (!ref) return;
const ref = (data.refs || []).find((r: any) => r.id === 'master');

if (!ref) {
return;
}

fetch(
`${Endpoints.graphql(
repositoryName
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-source-prismic-graphql/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export function PrismicLink({
const mainContext = operation.getContext().graphqlContext || {};
const pageContext = mainContext.context || {};
const pageHeaders = pageContext.headers;

return {
headers: {
...headers,
Expand Down
6 changes: 4 additions & 2 deletions packages/gatsby-source-prismic-graphql/src/utils/prismic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ export const Endpoints = {
domain === 'wroom.io';

return { isSecuredScheme, subdomain, domain: domain || this.DEFAULT_DOMAIN };
} else throw `Invalid Prismic repository name provided: ${repositoryName}`;
} else {
throw `Invalid Prismic repository name provided: ${repositoryName}`;
}
},

domain(repositoryName: string): string {
const { subdomain, domain } = this._parse(repositoryName);
return `${subdomain}.${domain}`;
},

root(repositoryName: string, withCDN: boolean = true): string {
root(repositoryName: string, withCDN: boolean = false): string {
const { isSecuredScheme, subdomain, domain } = this._parse(repositoryName);
const scheme = isSecuredScheme ? 'https' : 'http';
const cdn = isSecuredScheme && withCDN ? '.cdn' : '';
Expand Down
37 changes: 6 additions & 31 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6819,7 +6819,7 @@ detect-indent@^6.0.0:
resolved "https://registry.yarnpkg.com/detect-indent/-/detect-indent-6.0.0.tgz#0abd0f549f69fc6659a254fe96786186b6f528fd"
integrity sha512-oSyFlqaTHCItVRGK5RmrmjB+CmaMOW7IaNA/kdxqhoa6d17j/5ce9O9eWXmV/KEdRwqpQA+Vqe8a8Bsybu4YnA==

detect-libc@^1.0.2, detect-libc@^1.0.3:
detect-libc@^1.0.3:
version "1.0.3"
resolved "https://registry.yarnpkg.com/detect-libc/-/detect-libc-1.0.3.tgz#fa137c4bd698edf55cd5cd02ac559f91a4c4ba9b"
integrity sha1-+hN8S9aY7fVc1c0CrFWfkaTEups=
Expand Down Expand Up @@ -10382,7 +10382,7 @@ iconv-lite@0.4.23:
dependencies:
safer-buffer ">= 2.1.2 < 3"

iconv-lite@0.4.24, iconv-lite@^0.4.17, iconv-lite@^0.4.24, iconv-lite@^0.4.4, iconv-lite@~0.4.13:
iconv-lite@0.4.24, iconv-lite@^0.4.17, iconv-lite@^0.4.24, iconv-lite@~0.4.13:
version "0.4.24"
resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.24.tgz#2022b4b25fbddc21d2f524974a474aafe733908b"
integrity sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==
Expand Down Expand Up @@ -12761,15 +12761,6 @@ natural-compare@^1.4.0:
resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"
integrity sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=

needle@^2.2.1:
version "2.3.2"
resolved "https://registry.yarnpkg.com/needle/-/needle-2.3.2.tgz#3342dea100b7160960a450dc8c22160ac712a528"
integrity sha512-DUzITvPVDUy6vczKKYTnWc/pBZ0EnjMJnQ3y+Jo5zfKFimJs7S3HFCxCRZYB9FUZcrzUQr3WsmvZgddMEIZv6w==
dependencies:
debug "^3.2.6"
iconv-lite "^0.4.4"
sax "^1.2.4"

negotiator@0.6.2:
version "0.6.2"
resolved "https://registry.yarnpkg.com/negotiator/-/negotiator-0.6.2.tgz#feacf7ccf525a77ae9634436a64883ffeca346fb"
Expand Down Expand Up @@ -12941,22 +12932,6 @@ node-object-hash@^2.0.0:
resolved "https://registry.yarnpkg.com/node-object-hash/-/node-object-hash-2.0.0.tgz#9971fcdb7d254f05016bd9ccf508352bee11116b"
integrity sha512-VZR0zroAusy1ETZMZiGeLkdu50LGjG5U1KHZqTruqtTyQ2wfWhHG2Ow4nsUbfTFGlaREgNHcCWoM/OzEm6p+NQ==

node-pre-gyp@*:
version "0.14.0"
resolved "https://registry.yarnpkg.com/node-pre-gyp/-/node-pre-gyp-0.14.0.tgz#9a0596533b877289bcad4e143982ca3d904ddc83"
integrity sha512-+CvDC7ZttU/sSt9rFjix/P05iS43qHCOOGzcr3Ry99bXG7VX953+vFyEuph/tfqoYu8dttBkE86JSKBO2OzcxA==
dependencies:
detect-libc "^1.0.2"
mkdirp "^0.5.1"
needle "^2.2.1"
nopt "^4.0.1"
npm-packlist "^1.1.6"
npmlog "^4.0.2"
rc "^1.2.7"
rimraf "^2.6.1"
semver "^5.3.0"
tar "^4.4.2"

node-releases@^1.1.23:
version "1.1.25"
resolved "https://registry.yarnpkg.com/node-releases/-/node-releases-1.1.25.tgz#0c2d7dbc7fed30fbe02a9ee3007b8c90bf0133d3"
Expand Down Expand Up @@ -13092,7 +13067,7 @@ npm-normalize-package-bin@^1.0.1:
semver "^5.5.0"
validate-npm-package-name "^3.0.0"

npm-packlist@^1.1.6, npm-packlist@^1.4.4:
npm-packlist@^1.4.4:
version "1.4.8"
resolved "https://registry.yarnpkg.com/npm-packlist/-/npm-packlist-1.4.8.tgz#56ee6cc135b9f98ad3d51c1c95da22bbb9b2ef3e"
integrity sha512-5+AZgwru5IevF5ZdnFglB5wNlHG1AOOuw28WhUq8/8emhBmLv6jX5by4WJCh7lW0uSYZYS6DXqIsyZVIXRZU9A==
Expand Down Expand Up @@ -13124,7 +13099,7 @@ npm-run-path@^4.0.0:
dependencies:
path-key "^3.0.0"

npmlog@^4.0.1, npmlog@^4.0.2, npmlog@^4.1.2:
npmlog@^4.0.1, npmlog@^4.1.2:
version "4.1.2"
resolved "https://registry.yarnpkg.com/npmlog/-/npmlog-4.1.2.tgz#08a7f2a8bf734604779a9efa4ad5cc717abb954b"
integrity sha512-2uUqazuKlTaSI/dC8AzicUck7+IrEaOnN/e0jd3Xtt1KcGpwx30v50mL7oPyr/h9bL3E4aZccVwpwP+5W9Vjkg==
Expand Down Expand Up @@ -15776,7 +15751,7 @@ safe-regex@^1.1.0:
resolved "https://registry.yarnpkg.com/safer-buffer/-/safer-buffer-2.1.2.tgz#44fa161b0187b9549dd84bb91802f9bd8385cd6a"
integrity sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==

sax@>=0.6.0, sax@^1.2.4, sax@~1.2.4:
sax@>=0.6.0, sax@~1.2.4:
version "1.2.4"
resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.4.tgz#2816234e2378bddc4e5354fab5caa895df7100d9"
integrity sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw==
Expand Down Expand Up @@ -17082,7 +17057,7 @@ tar-stream@^1.1.2, tar-stream@^1.5.2:
to-buffer "^1.1.1"
xtend "^4.0.0"

tar@^4.4.10, tar@^4.4.12, tar@^4.4.2:
tar@^4.4.10, tar@^4.4.12:
version "4.4.13"
resolved "https://registry.yarnpkg.com/tar/-/tar-4.4.13.tgz#43b364bc52888d555298637b10d60790254ab525"
integrity sha512-w2VwSrBoHa5BsSyH+KxEqeQBAllHhccyMFVHtGtdMpF4W7IRWfZjFiQceJPChOeTsSDVUpER2T8FA93pr0L+QA==
Expand Down

0 comments on commit 135bf12

Please sign in to comment.