EcomRouter

EcomRouter

new EcomRouter(storeIdopt, locationopt)

Source:
Construct a new storefront router object.
Examples
const router = new EcomRouter()
// Defining Store ID and using custom location interface
const storeId = 2000
const router = new EcomRouter(storeId, DOM.location)
// P.S.: You may want to use custom location when using jsdom on Node.js for example
Parameters:
Name Type Attributes Default Description
storeId number <optional>
_config.get('store_id') Preset Store ID number
location object <optional>
window.location Location interface

Members

location :object

Source:
Type:
  • object

storeId :number

Source:
Respective Store ID number
Type:
  • number

Methods

list() → {Promise.<(Array.<route>|error)>}

Source:
List all storefront routes for product, categories, brands and collections.
Example
// Listing all routes, then resolving to get each document body
router.list()
  .then(routes => {
    routes.forEach(route => {
      console.log(route.path)
      console.log(route.resource)
      console.log(route._id)
      if (route.name) {
        console.log('Not a product', route.name)
      }
      router.resolve(route)
        .then(context => {
          console.log(context.body)
        })
        .catch(error => { throw error })
    })
  })
  .catch(error => {
    console.error(error)
    if (error.response) {
      console.log(error.response)
    }
  })
Returns:
Type
Promise.<(Array.<route>|error)>

map(pathopt) → {Promise.<(route|error)>}

Source:
Get page resource and object ID based on URL pathname.
Examples
// Mapping resource and object ID from current location pathname
router.map()
  .then(route => {
    console.log(route.path)
    console.log(route.resource)
    console.log(route._id)
  })
  .catch(error => {
    console.error(error)
    if (error.response) {
      console.log(error.response)
    }
  })
// Specifying some URL pathname
router.map('/monitores')
  .then(route => console.log(route))
  .catch(error => console.error(error))
Parameters:
Name Type Attributes Default Description
path string <optional>
location.pathname URL pathname
Returns:
Type
Promise.<(route|error)>

resolve(route) → {Promise.<(context|error)>}

Source:
Get Store API document body based on route resource and Object ID.

Tip: Although resolve can be used standalone, you may want to use map method first.
Example
// Get document body for current URL pathname
router.map()
  .then(route => {
    router.resolve(route)
      .then(context => {
        console.log(context)
        console.log(context.resource)
        console.log(context.body)
        console.log(context.body._id)
        console.log(context.body.name)
      })
      .catch(error => { throw error })
  })
  .catch(error => {
    console.error(error)
    if (error.response) {
      console.log(error.response)
    }
  })
Parameters:
Name Type Description
route route Route object to get respective document body
Returns:
Type
Promise.<(context|error)>

setupStore(domainopt, updateConfigopt) → {Promise.<(channel|error)>}

Source:
Get Store IDs and default lang (and set on `_config`) based on domain name.
Examples
// Using current location URL
router.setupStore()
  .then(channel => {
    console.log(channel.store_id)
    console.log(channel.store_object_id)
    console.log(channel)
  })
  .catch(error => {
    console.error(error)
    if (error.response) {
      console.log(error.response)
    }
  })
// Specifying domain name and disabling `ecomUtils._config` update
router.setupStore('shop-plus.e-com.plus', false)
  .then(channel => console.log(channel))
  .catch(error => console.error(error))
Parameters:
Name Type Attributes Default Description
domain string <optional>
location.hostname Sales channel domain name
updateConfig boolean <optional>
true Update global configs from `ecomUtils._config`
Returns:
Type
Promise.<(channel|error)>