commerce-vuex
Version:
Commerce vuex module
70 lines (61 loc) • 2.75 kB
JavaScript
import { auth, nav } from 'oak-vuex'
const { getHubAuthHeader } = auth
const { wrapRequest, wrapRequestWithRange } = nav
const hubName = process.env.VUE_APP_RETAINHUB_NAME
const retainhubComApiUrl = process.env.VUE_APP_RETAINHUB_COMMERCE_API_URL
const doFetchList = wrapRequestWithRange(({ appId, nameLike,skuLike,brandCatalogId,brandProductId, brandId, limit, offset }) => {
const requestOptions = {
method: 'GET',
headers: { 'Content-type': 'application/json', 'Prefer': 'count=exact', ...getHubAuthHeader(hubName) },
};
const params = { app_id:`eq.${appId}`, limit: limit || 50, offset: offset || 0, brand_id: `eq.${brandId}` }
if (nameLike) params['name'] = `ilike.*${nameLike}*`
if (skuLike) params['sku'] = `eq.${skuLike}`
if (brandCatalogId) params['brand_catalog_id'] = `eq.${brandCatalogId}`
if (brandProductId) params['id'] = `eq.${brandProductId}`
const qs = Object.keys(params).map(key => key + '=' + params[key]).join('&');
return fetch(`${retainhubComApiUrl}/brand_product?${qs}`, requestOptions)
})
const doPostOne = wrapRequest(({brandProduct}) => {
const requestOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json', 'Accept': 'application/vnd.pgrst.object+json',
'Prefer': 'return=representation', ...getHubAuthHeader(hubName)
},
body: JSON.stringify(brandProduct)
};
return fetch(`${retainhubComApiUrl}/brand_product`, requestOptions)
})
const doPatchOne = wrapRequest((brand) => {
const requestOptions = {
method: 'PATCH',
headers: {
'Content-Type': 'application/json', 'Accept': 'application/vnd.pgrst.object+json',
'Prefer': 'return=representation', ...getHubAuthHeader(hubName)
},
body: JSON.stringify(brand)
};
return fetch(`${retainhubComApiUrl}/brand_product?id=eq.${brand.id}`, requestOptions)
})
const doFetchOne = wrapRequest(({brandProductId, appId}) => {
const requestOptions = {
method: 'GET',
headers: { 'Content-type': 'application/json', 'Accept': 'application/vnd.pgrst.object+json', ...getHubAuthHeader(hubName) },
};
return fetch(`${retainhubComApiUrl}/brand_product?id=eq.${brandProductId}&app_id=eq.${appId}`, requestOptions);
})
const doDeleteSelected = wrapRequest(({brandStyleId}) => {
const requestOptions = {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
...getHubAuthHeader(hubName)
},
};
return fetch(`${retainhubComApiUrl}/brand_product?id=in.(${brandStyleId})`, requestOptions)
})
export default {
doFetchOne, doFetchList,
doPostOne, doPatchOne,doDeleteSelected
}