Skip to content
This repository has been archived by the owner on Oct 9, 2024. It is now read-only.

Commit

Permalink
refactor: simplify gatsby-node (#52)
Browse files Browse the repository at this point in the history
* refactor: simplify setup

* chore: update demo deps
  • Loading branch information
notrab authored May 15, 2021
1 parent 77f3aa0 commit 9425c68
Show file tree
Hide file tree
Showing 9 changed files with 2,325 additions and 2,923 deletions.
10 changes: 5 additions & 5 deletions demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
"dev": "gatsby develop"
},
"devDependencies": {
"dotenv": "8.2.0"
"dotenv": "9.0.2"
},
"dependencies": {
"gatsby": "2.32.12",
"@chec/gatsby-source-chec": "*",
"gatsby-image": "2.11.0",
"gatsby-plugin-sharp": "2.14.3",
"gatsby-transformer-sharp": "2.12.1",
"gatsby": "3.5.0",
"gatsby-image": "3.5.0",
"gatsby-plugin-sharp": "3.5.0",
"gatsby-transformer-sharp": "3.5.0",
"react": "17.0.2",
"react-dom": "17.0.2"
}
Expand Down
162 changes: 158 additions & 4 deletions gatsby-source-chec/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,158 @@
exports.onPreBootstrap = require('./gatsby/node/onPreBootstrap');
exports.sourceNodes = require('./gatsby/node/sourceNodes');
exports.createSchemaCustomization = require('./gatsby/node/createSchemaCustomization');
exports.onCreateNode = require('./gatsby/node/onCreateNode');
const { Chec } = require('chec-request');
const { createRemoteFileNode } = require('gatsby-source-filesystem');

exports.pluginOptionsSchema = ({ Joi }) => {
return Joi.object({
publicKey: Joi.string()
.description(
'Chec Public API Key - You can be found inside Developer > API Keys'
)
.required(),
downloadImageAssets: Joi.boolean()
.description(
'Download and cache Chec image assets in your Gatsby project'
)
.default(false),
});
};

exports.sourceNodes = async (
{ actions, createContentDigest, reporter },
{ publicKey }
) => {
const { createNode } = actions;

const commerce = new Chec(publicKey);

const fetchAllPages = async (endpoint, collection = [], page = undefined) => {
try {
const { data = [], meta } = await commerce.get(endpoint, {
...(page && { page }),
});

if (data.length === 0) return data;

const {
pagination: {
current_page,
links: { next },
},
} = meta;

const newCollection = [...data, ...collection];

if (next) {
await fetchAllPages(endpoint, newCollection, current_page + 1);
}

return newCollection;
} catch (err) {
reporter.panicOnBuild('@chec/gatsby-source-chec', err);
}
};

const { id: merchantId, ...merchant } = await commerce.get('merchants');
const categories = await fetchAllPages('categories');
const products = await fetchAllPages('products');

createNode({
id: merchantId.toString(),
...merchant,
internal: {
type: `ChecMerchant`,
content: JSON.stringify(merchant),
contentDigest: createContentDigest(merchant),
},
});

categories.forEach((category) => {
const productIds = products.reduce((ids, product) => {
const matchingCategory = product.categories.find(
(cat) => cat.id === category.id
);

if (!matchingCategory) return ids;

return [...ids, product.id];
}, []);

createNode({
...category,
products: productIds,
internal: {
type: `ChecCategory`,
content: JSON.stringify(category),
contentDigest: createContentDigest(category),
},
});
});

products.forEach(({ categories, ...product }) => {
const categoryIds = categories.map(({ id }) => id);

createNode({
...product,
categories: categoryIds,
internal: {
type: `ChecProduct`,
content: JSON.stringify(product),
contentDigest: createContentDigest(product),
},
});
});
};

exports.onCreateNode = async (
{ node, actions: { createNode }, createNodeId, store, cache },
{ downloadImageAssets }
) => {
node.images = [];

if (
downloadImageAssets &&
node.internal.type === 'ChecProduct' &&
node.assets
) {
const getImageAssets = async () => {
const assetIds = [];

const processImageAssets = node.assets.map(async (asset) => {
if (!asset.is_image) return;

try {
const imageNode = await createRemoteFileNode({
url: encodeURI(asset.url),
store,
cache,
createNode,
createNodeId,
parentNodeId: node.id,
});

if (imageNode) assetIds.push(imageNode.id);
} catch (e) {
console.error('gatsby-source-chec: ERROR', e);
}
});

await Promise.all(processImageAssets);

return assetIds;
};

node.images = await getImageAssets();
}
};

exports.createSchemaCustomization = ({ actions: { createTypes } }) => {
createTypes(`
type ChecProduct implements Node {
categories: [ChecCategory] @link
images: [File] @link
}
type ChecCategory implements Node {
products: [ChecProduct] @link
}
`);
};
14 changes: 0 additions & 14 deletions gatsby-source-chec/gatsby/node/createSchemaCustomization.js

This file was deleted.

49 changes: 0 additions & 49 deletions gatsby-source-chec/gatsby/node/onCreateNode.js

This file was deleted.

12 changes: 0 additions & 12 deletions gatsby-source-chec/gatsby/node/onPreBootstrap.js

This file was deleted.

89 changes: 0 additions & 89 deletions gatsby-source-chec/gatsby/node/sourceNodes.js

This file was deleted.

2 changes: 1 addition & 1 deletion gatsby-source-chec/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
},
"dependencies": {
"chec-request": "1.2.0",
"gatsby-source-filesystem": "2.11.1"
"gatsby-source-filesystem": "3.5.0"
}
}
6 changes: 0 additions & 6 deletions gatsby-source-chec/plugin-options.js

This file was deleted.

Loading

0 comments on commit 9425c68

Please sign in to comment.