1.2.4 • Published 2 years ago

@backpackjs/transforms v1.2.4

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

@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-end
  • products: 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.

1.2.4

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.0.0

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.1.2

2 years ago

0.2.0

2 years ago

0.1.19

2 years ago

0.1.12

2 years ago

0.1.13

2 years ago

0.1.14

2 years ago

0.1.15

2 years ago

0.1.16

2 years ago

0.1.17

2 years ago

0.1.18

2 years ago

0.1.10

2 years ago

0.1.11

2 years ago

0.1.8

2 years ago

0.1.7

2 years ago

0.1.9

2 years ago

0.1.6

2 years ago

0.1.5

3 years ago

0.1.4

3 years ago

0.0.120

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.3

3 years ago

0.0.119

3 years ago

0.0.118

3 years ago

0.0.117

3 years ago

0.0.116

3 years ago

0.0.115

3 years ago

0.0.114

3 years ago

0.0.113

3 years ago

0.0.112

3 years ago

0.0.111

3 years ago

0.0.110

3 years ago

0.0.109

3 years ago

0.0.108

3 years ago

0.0.107

3 years ago

0.0.106

3 years ago

0.0.105

3 years ago

0.0.104

3 years ago

0.0.103

3 years ago

0.0.102

3 years ago

0.0.101

3 years ago

0.0.100

3 years ago

0.0.23

3 years ago

0.0.22

3 years ago

0.0.16

3 years ago

0.0.15

3 years ago

0.0.14

3 years ago

0.0.13

3 years ago

0.0.12

3 years ago

0.0.11

3 years ago

0.0.10

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago