sync-actions

Provides an API to construct update actions. Useful for building request body for updates.

Install

Node.js

npm install --save @commercetools/sync-actions

Browser

<script src="https://unpkg.com/@commercetools/sync-actions/dist/commercetools-sync-actions.umd.min.js"></script>
<script>// global: CommercetoolsSyncActions</script>

createSyncCategories(actionGroups)

Creates a sync action that allows to build API update actions for categories.

Arguments

  1. actionGroups (Array): A list of action group in case some actions need to be blacklisted or whitelisted.

Usage example

import { createSyncCategories } from '@commercetools/sync-actions'
import { createClient } from '@commercetools/sdk-client'

const syncCategories = createSyncCategories()
const client = createClient({
  middlewares: [...],
})
const before = {
  name: { en: 'My Category' }
}
const now = {
  name: { en: 'My Category', de: 'Meine Kategorie' }
}
const actions = syncCategories.buildActions(now, before)
const categoriesRequest = {
  url: `/categories/${before.id}`,
  method: 'POST',
  body: JSON.stringify({ version: before.version, actions }),
}

client.execute(categoriesRequest)
.then(result => ...)
.catch(error => ...)

createSyncCustomers(actionGroups)

From package @commercetools/sync-actions.

Creates a sync action that allows to build API update actions for customers.

Arguments

  1. actionGroups (Array): A list of action group in case some actions need to be blacklisted or whitelisted.

Usage example

import { createSyncCustomers } from '@commercetools/sync-actions'
import { createClient } from '@commercetools/sdk-client'

const syncCustomers = createSyncCustomers()
const client = createClient({
  middlewares: [...],
})
const before = {
  firstName: 'John'
}
const now = {
  firstName: 'John',
  lastName: 'Doe'
}
const actions = syncCustomers.buildActions(now, before)
const customersRequest = {
  url: `/customers/${before.id}`,
  method: 'POST',
  body: JSON.stringify({ version: before.version, actions }),
}

client.execute(customersRequest)
.then(result => ...)
.catch(error => ...)

createSyncInventories(actionGroups)

From package @commercetools/sync-actions.

Creates a sync action that allows to build API update actions for inventories.

Arguments

  1. actionGroups (Array): A list of action group in case some actions need to be blacklisted or whitelisted.

Usage example

import { createSyncInventories } from '@commercetools/sync-actions'
import { createClient } from '@commercetools/sdk-client'

const syncInventories = createSyncInventories()
const client = createClient({
  middlewares: [...],
})
const before = {
  quantityOnStock: 10
}
const now = {
  quantityOnStock: 5
}
const actions = syncInventories.buildActions(now, before)
const inventoriesRequest = {
  url: `/inventory/${before.id}`,
  method: 'POST',
  body: JSON.stringify({ version: before.version, actions }),
}

client.execute(inventoriesRequest)
.then(result => ...)
.catch(error => ...)

createSyncOrders(actionGroups)

From package @commercetools/sync-actions.

Creates a sync action that allows to build API update actions for orders.

Arguments

  1. actionGroups (Array): A list of action group in case some actions need to be blacklisted or whitelisted.

Usage example

import { createSyncOrders } from '@commercetools/sync-actions'
import { createClient } from '@commercetools/sdk-client'

const syncOrders = createSyncOrders()
const client = createClient({
  middlewares: [...],
})
const before = {
  orderState: 'Open'
}
const now = {
  orderState: 'Complete'
}
const actions = syncOrders.buildActions(now, before)
const ordersRequest = {
  url: `/orders/${before.id}`,
  method: 'POST',
  body: JSON.stringify({ version: before.version, actions }),
}

client.execute(ordersRequest)
.then(result => ...)
.catch(error => ...)

createSyncProducts(actionGroups)

From package @commercetools/sync-actions.

Creates a sync action that allows to build API update actions for products.

Arguments

  1. actionGroups (Array): A list of action group in case some actions need to be blacklisted or whitelisted.

Usage example

import { createSyncProducts } from '@commercetools/sync-actions'
import { createClient } from '@commercetools/sdk-client'

const syncProducts = createSyncProducts()
const client = createClient({
  middlewares: [...],
})
const before = {
  name: { en: 'My Product' }
}
const now = {
  name: { en: 'My Product', de: 'Mein Produkt' }
}
const actions = syncProducts.buildActions(now, before)
const productsRequest = {
  url: `/products/${before.id}`,
  method: 'POST',
  body: JSON.stringify({ version: before.version, actions }),
}

client.execute(productsRequest)
.then(result => ...)
.catch(error => ...)

createSyncProductDiscounts(actionGroups)

From package @commercetools/sync-actions.

Creates a sync action that allows to build API update actions for product-discounts.

Arguments

  1. actionGroups (Array): A list of action group in case some actions need to be blacklisted or whitelisted.

Usage example

import { createSyncProductDiscounts } from '@commercetools/sync-actions'
import { createClient } from '@commercetools/sdk-client'

const syncProductDiscounts = createSyncProductDiscounts()
const client = createClient({
  middlewares: [...],
})
const before = {
  name: { en: 'My Product Discount' }
}
const now = {
  name: { en: 'My Product Discount', de: 'Mein Produkt Rabatt' }
}
const actions = syncProductDiscounts.buildActions(now, before)
const productDiscountsRequests = {
  url: `/product-discounts/${before.id}`,
  method: 'POST',
  body: JSON.stringify({ version: before.version, actions }),
}

client.execute(productDiscountsRequests)
.then(result => ...)
.catch(error => ...)

createSyncDiscountCodes(actionGroups)

From package @commercetools/sync-actions.

Creates a sync action that allows to build API update actions for discount-codes.

Arguments

  1. actionGroups (Array): A list of action group in case some actions need to be blacklisted or whitelisted.

Usage example

import { createSyncDiscountCodes } from '@commercetools/sync-actions'
import { createClient } from '@commercetools/sdk-client'

const syncDiscountCodes = createSyncDiscountCodes()
const client = createClient({
  middlewares: [...],
})
const before = {
  name: { en: 'My Discount Code' }
}
const now = {
  name: { en: 'My Discount Code', de: 'Mein Rabatt Code' }
}
const actions = syncDiscountCodes.buildActions(now, before)
const discountCodesRequest = {
  url: `/discount-codes/${before.id}`,
  method: 'POST',
  body: JSON.stringify({ version: before.version, actions }),
}

client.execute(discountCodesRequest)
.then(result => ...)
.catch(error => ...)

createSyncCustomerGroup(actionGroups)

From package @commercetools/sync-actions.

Creates a sync action that allows to build API update actions for customer-groups.

Arguments

  1. actionGroups (Array): A list of action group in case some actions need to be blacklisted or whitelisted.

Usage example

import { createSyncCustomerGroup } from '@commercetools/sync-actions'
import { createClient } from '@commercetools/sdk-client'

const syncCustomerGroups = createSyncCustomerGroup()
const client = createClient({
  middlewares: [...],
})
const before = {
  name: 'My customer group',
  key: 'some-old-key'
}
const now = {
  name: 'My new customer group',
  key: 'some-new-key'
}
const actions = syncCustomerGroups.buildActions(now, before)
const customerGroupRequest = {
  url: `/customer-groups/${before.id}`,
  method: 'POST',
  body: JSON.stringify({ version: before.version, actions }),
}

client.execute(customerGroupRequest)
.then(result => ...)
.catch(error => ...)

createSyncCartDiscounts(actionGroups)

From package @commercetools/sync-actions.

Creates a sync action that allows to build API update actions for cart-discounts.

Arguments

  1. actionGroups (Array): A list of action group in case some actions need to be blacklisted or whitelisted.

Usage example

import { createSyncCartDiscounts } from '@commercetools/sync-actions'
import { createClient } from '@commercetools/sdk-client'

const syncCartDiscounts = createSyncCartDiscounts()
const client = createClient({
  middlewares: [...],
})
const before = {
  name: { en: 'My Cart Discount' }
}
const now = {
  name: { en: 'My Cart Discount', de: 'Mein Warenkorbrabatt' }
}
const actions = syncCartDiscounts.buildActions(now, before)
const cartDiscountsRequests = {
  url: `/cart-discounts/${before.id}`,
  method: 'POST',
  body: JSON.stringify({ version: before.version, actions }),
}

client.execute(cartDiscountsRequests)
.then(result => ...)
.catch(error => ...)

createSyncTaxCategories(actionGroups)

From package @commercetools/sync-actions.

Creates a sync action that allows to build API update actions for tax-categories.

Arguments

  1. actionGroups (Array): A list of action group in case some actions need to be blacklisted or whitelisted.

Usage example

import { createSyncTaxCategories } from '@commercetools/sync-actions'
import { createClient } from '@commercetools/sdk-client'

const syncTaxCategories = createSyncTaxCategories()
const client = createClient({
  middlewares: [...],
})
const before = {
  name: 'My Tax Category'
}
const now = {
  name: 'My Updated Tax Category'
}
const actions = syncTaxCategories.buildActions(now, before)
const taxCategoriesRequests = {
  url: `/tax-categories/${before.id}`,
  method: 'POST',
  body: JSON.stringify({ version: before.version, actions }),
}

client.execute(taxCategoriesRequests)
.then(result => ...)
.catch(error => ...)

createSyncZones(actionGroups)

From package @commercetools/sync-actions.

Creates a sync action that allows to build API update actions for zones.

Arguments

  1. actionGroups (Array): A list of action group in case some actions need to be blacklisted or whitelisted.

Usage example

import { createSyncZones } from '@commercetools/sync-actions'
import { createClient } from '@commercetools/sdk-client'

const syncZones = createSyncZones()
const client = createClient({
  middlewares: [...],
})
const before = {
  name: 'My Zone'
}
const now = {
  name: 'My Other Zone'
}
const actions = syncZones.buildActions(now, before)
const zonesRequests = {
  url: `/zones/${before.id}`,
  method: 'POST',
  body: JSON.stringify({ version: before.version, actions }),
}

client.execute(zonesRequests)
.then(result => ...)
.catch(error => ...)

createSyncProductTypes(actionGroups, config)

From package @commercetools/sync-actions.

Creates a sync action that allows to build API update actions for productTypes.

Arguments

  1. actionGroups (Array): A list of action group in case some actions need to be blacklisted or whitelisted.
  2. config (Object): A configuration object to which has one of the following options:
Key Type Required Description
shouldOmitEmptyString Boolean - (default: false) a flag which determines whether we should treat empty strings as NOT A VALUE in addition to undefined and null

Usage example

import { createSyncProductTypes } from '@commercetools/sync-actions'
import { createClient } from '@commercetools/sdk-client'

const syncProductTypes = createSyncProductTypes()
const client = createClient({
  middlewares: [...],
})

const before = {
  id: 'product-type-id',
  name: 'Product Type',
  version: 1,
}

const now = {
  id: 'product-type-id',
  name: 'Other Product Type',
  version: 1,
}

const actions = syncProductTypes.buildActions(now, before)
const productTypesRequest = {
  url: `/product-types/${before.id}`,
  method: 'POST',
  body: JSON.stringify({ version: before.version, actions }),
}

client.execute(productTypesRequest)
.then(result => ...)
.catch(error => ...)

createSyncShippingMethods(actionGroups)

From package @commercetools/sync-actions.

Creates a sync action that allows to build API update actions for zones.

Arguments

  1. actionGroups (Array): A list of action group in case some actions need to be blacklisted or whitelisted.

Usage example

import { createSyncShippingMethods } from '@commercetools/sync-actions'
import { createClient } from '@commercetools/sdk-client'

const syncShippingMethods = createSyncShippingMethods()
const client = createClient({
  middlewares: [...],
})

const before = {
  key: 'Key 1'
}

const now = {
  name: 'Key 2'
}

const actions = syncShippingMethods.buildActions(now, before)
const shippingMethodsRequests = {
  url: `/shipping-methods/${before.id}`,


client.execute(shippingMethodsRequests)
.then(result => ...)
.catch(error => ...)

createSyncStates(actionGroups)

From package @commercetools/sync-actions.

Creates a sync action that allows to build API update actions for states.

Arguments

  1. actionGroups (Array): A list of action group in case some actions need to be blacklisted or whitelisted.

Usage example

import { createSyncShippingStates } from '@commercetools/sync-actions'
import { createClient } from '@commercetools/sdk-client'

const syncStates = createStates()
const client = createClient({
  middlewares: [...],
})

const before = {
  key: 'Key 1'
}

const now = {
  name: 'Key 2'
}

const actions = syncStates.buildActions(now, before)
const statesRequests = {
  url: `/states/${before.id}`,


client.execute(statesRequests)
.then(result => ...)
.catch(error => ...)

With shouldOmitEmptyString=true

Given that shouldOmitEmptyString is provided, we won't generate any updateAction in the following cases:

Before Now Will generate update action? Value for the action
"" null no
"" undefined no
"" "foo" yes "foo"
null "" no
null undefined no
null "foo" yes "foo"
undefined "" no
undefined null no
undefined "foo" yes "foo"
"foo" "" yes omitted from the action
"foo" null yes omitted from the action
"foo" undefined yes omitted from the action

The final value of the action as displayed above will be given, regardless if the value of the action is required or not.

See example below.

const productTypeSync = createSyncProductTypes([], {
  shouldOmitEmptyString: true,
})
const before = { key: '' }
const now = { key: null }
const actions = sync.buildActions(now, before)

// outputs:
// []

results matching ""

    No results matching ""