@backpackjs/transforms
v1.2.4
Published
An optional @backpackjs/sync extension that enables json/data re-shaping and enriching during the syncing lifecycle of products and collections.
Downloads
241
Maintainers
Keywords
Readme
@backpackjs/transform
An optional @backpackjs/sync extension that enables json/data re-shaping and enriching during the syncing lifecycle of products and collections.
Transforms are performed after fetching and before saving product and collections json files.
How to
Transforms live inside ./transform/products
and or ./transform/collections
Some examples..
products
: Pre-format the product and variant prices, so that we don't have have to constantly do replace('.00', '') formatting hacks in the front-endproducts
: Convert variant options to a more front-end friendly such as optionsMap ..
transforms | [] | [...fns] | A set of product transformation functions that allows you to completely reshape (add, modify and delete any fields) of the source products data fetched from Shopify.This is an alternative method to loading transforms via the /transforms/products
folder. For more information on product transforms please see @backpackjs/transforms and our open source @backpackjs/transforms-catalog.For a basic transform example please see "Example code: Product transform"
Adding swatch images to products
The transform code:
{
...,
transforms: [
// Adds a custom field "swatchImages" to each product. This field is an object that includes each color (optionName) as key and a url to an image for this color (in this case images hosted in shopify)
{
fields: ['swatchImages'],
resolver: (product) => {
const colorOption = product && product.options && product.options.find(({ name }) => name === 'Color')
const colorValues = colorOption && colorOption.values // array of colors
const colorValuesToImages = colorValues
? colorValues.reduce((colorSwatches, color) => {
colorSwatches[color] = `//cdn.shopify.com/s/files/1/122/4944/files/${color}.png`
return colorSwatches
}, {})
: {}
return colorValuesToImages
}
}
]
}
The resulting product
{
...otherProductProperties,
swatchImages: {
'blue-102': '//cdn.shopify.com/s/files/1/122/4944/files/blue-102.png',
'white-201': '//cdn.shopify.com/s/files/1/122/4944/files/white-201.png',
'red-100': '//cdn.shopify.com/s/files/1/122/4944/files/red-100.png',
...
}
}
Example transform (imagesByAlt)
Add simple transform that performs a potentially costly front-end operation of grouping images by their alt attribute
transforms/products/imagesByAlt.js
const imagesAltMapFromImagesArray = ({ product }) => new Promise((resolve) => {
let imagesByAlt = { untagged: [] }
if (!product || !product.images || !product.images.length) resolve(imagesByAlt)
// sort images with slug atlText as keys and rest as untagged
for (let i = 0; i < product.images.length; i++) {
let image = product.images[i]
const altText = image && image.altText
const altTextDashed = altText && altText.split('-')
if (altText && altTextDashed.length) {
if (Array.isArray(imagesByAlt[altText])) {
imagesByAlt[altText] = [...imagesByAlt[altText], image]
} else {
imagesByAlt[altText] = [image]
}
} else {
imagesByAlt.untagged = [...imagesByAlt.untagged, image]
}
if (i === product.images.length - 1) {
resolve(imagesByAlt)
}
}
})
module.exports = {
fields: ['imagesByAlt'],
resolver: imagesAltMapFromImagesArray
}
Example transform (in/output)
fetched product (i.e transform input)
{
"handle": "product-a",
"images" : [
{
"src": "...image-1.jpg",
"alt": "blue",
},
{
"src": "...image-2.jpg",
"alt": "red",
}
{
"src": "...image-3.jpg",
"alt": "red",
}
]
...
}
transformed product (i.e transform output)
{
"handle": "product-a",
"images" : [
{
"src": "...image-1.jpg",
"alt": "blue",
},
{
"src": "...image-2.jpg",
"alt": "red",
}
{
"src": "...image-3.jpg",
"alt": "red",
}
],
// added by transform
"imagesByAlt": {
"blue": [
{
"src": "...image-1.jpg",
"alt": "blue",
},
],
"red": [
{
"src": "...image-2.jpg",
"alt": "red",
}
{
"src": "...image-3.jpg",
"alt": "red",
}
]
}
...
}
Transforms Signature
module.exports = {
fields: ['imagesByAlt'],
resolver: imagesAltMapFromImagesArray
}
fields
: and array of field keys that this transform will act upon. It can be an existing field that you want to overwrite, a field you want to delete or simply a field that does not yet exist.resolver
: a function (sync or async) which receives every product and/or collection and returns a value (string, integer, array or object) that will be applied to field(s) defined in this transform
Resolver function
Explain all the available inputs for each endpoint, importance of order of execution
Cache invalidation on transform(s) change
Ask JP while this is fleshed out
Best practices & caveats
Migrate computing heavy front-end code to transforms after its been tested in the frontend. This will make your front-end code, leaner faster and much easier to reason.
Ask JP while this is fleshed out.