UNPKG

commerce-vuex

Version:
69 lines (58 loc) 2.66 kB
import { auth, nav } from 'oak-vuex' const { getHubAuthHeader } = auth const { wrapRequest, wrapRequestWithRange } = nav const hubName = process.env.VUE_APP_RETAINHUB_NAME const retainhubCommerceApiUrl = process.env.VUE_APP_RETAINHUB_COMMERCE_API_URL const doFetchList = wrapRequestWithRange(({ nameLike,serviceId, appId, businessEntityId, limit, offset }) => { const requestOptions = { method: 'GET', headers: { 'Content-type': 'application/json', 'Prefer': 'count=exact', ...getHubAuthHeader(hubName) }, }; const params = { app_id:`eq.${appId}`, business_entity_id:`eq.${businessEntityId}`,limit: limit || 50, offset: offset || 0} if (nameLike) params['name'] = `ilike.*${nameLike}*` if (serviceId) params['id'] = `eq.${serviceId}` const qs = Object.keys(params).map(key => key + '=' + params[key]).join('&'); return fetch(`${retainhubCommerceApiUrl}/service_contract?${qs}&select=*,business_entity!business_entity_id(name)`, requestOptions); }) const doPostOne = wrapRequest(({serviceContract}) => { const requestOptions = { method: 'POST', headers: { 'Content-Type': 'application/json', 'Accept': 'application/vnd.pgrst.object+json', 'Prefer': 'return=representation', ...getHubAuthHeader(hubName) }, body: JSON.stringify(serviceContract) }; return fetch(`${retainhubCommerceApiUrl}/serviceContract`, requestOptions) }) const doPatchOne = wrapRequest((serviceContract) => { const requestOptions = { method: 'PATCH', headers: { 'Content-Type': 'application/json', 'Accept': 'application/vnd.pgrst.object+json', 'Prefer': 'return=representation', ...getHubAuthHeader(hubName) }, body: JSON.stringify(serviceContract) }; return fetch(`${retainhubCommerceApiUrl}/serviceContract?id=eq.${serviceContract.id}`, requestOptions) }) const doFetchOne = wrapRequest(({serviceId}) => { const requestOptions = { method: 'GET', headers: { 'Content-type': 'application/json', 'Accept': 'application/vnd.pgrst.object+json', ...getHubAuthHeader(hubName) }, }; return fetch(`${retainhubCommerceApiUrl}/serviceContract?id=eq.${serviceId}`, requestOptions); }) const doDeleteSelected = wrapRequest((serviceId) => { const requestOptions = { method: 'DELETE', headers: { 'Content-Type': 'application/json' }, }; return fetch(`${retainhubCommerceApiUrl}/serviceContract?id=in.(${serviceId})`, requestOptions) }) export default { doFetchOne, doFetchList, doPostOne, doPatchOne, doDeleteSelected }