This repository has been archived by the owner on Oct 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: simplify gatsby-node (#52)
* refactor: simplify setup * chore: update demo deps
- Loading branch information
Showing
9 changed files
with
2,325 additions
and
2,923 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
gatsby-source-chec/gatsby/node/createSchemaCustomization.js
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.