-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
133 lines (121 loc) · 3.5 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* eslint-disable no-undef */
import Client from 'shopify-buy';
import fetch from 'isomorphic-fetch';
const customMiddleWareUrl = `https://emrik8wwe3.execute-api.us-east-1.amazonaws.com/Prod`;
const client = Client.buildClient({
domain: `${GATSBY_SHOP_NAME}.myshopify.com`,
storefrontAccessToken: GATSBY_ACCESS_TOKEN
});
const adminAPIBaseUrl = `${customMiddleWareUrl}/inventory`;
export const fetchProductInventory = (variantId) => {
const parsedVariantId = window.atob(variantId).split('/').pop();
// remove hard code later.
return fetch(`${adminAPIBaseUrl}?variantId=${parsedVariantId}`, {
method: 'get',
mode: 'cors',
headers: {
'Content-Type': 'application/json'
}
})
.then(async (data) => {
const { body: { variant: { inventory_quantity: remainingInventory, fulfillment_service, inventory_management }} } = await data.json();
console.info('number of variants remaining', remainingInventory);
const isInfiniteInventory = (
fulfillment_service === 'printful' ||
inventory_management === null
);
if (isInfiniteInventory) return 999;
return remainingInventory;
})
.catch((e) => {
console.error('Error fetching inventory', e);
});
}
export const subscribeToEmail = (email, status = 'subscribed') => {
return fetch(`${customMiddleWareUrl}/email-subscribe`, {
method: 'POST',
mode: 'cors',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
email_address: email,
status,
merge_fields: {
MERGE0: email
}
})
})
.then(async (resp) => {
const data = await resp.json();
return data;
})
.catch((e) => {
const error = e.json();
return error;
})
}
export const verifyCaptcha = (token) => {
return fetch(`${customMiddleWareUrl}/recaptcha`, {
method: 'POST',
mode: 'cors',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ token: token })
})
.then(async (data) => {
const resp = await data.json();
return resp;
})
.catch((e) => {
return e;
})
}
export const initCheckout = () => {
// eslint-disable
return client.checkout.create().then((checkout) => {
// Do something with the checkout
return checkout;
});
};
export const fetchCart = (checkoutId) => {
return client.checkout.fetch(checkoutId).then((checkout) => {
// Do something with the checkout
return checkout;
});
};
export const addLineItemsToCart = (checkoutId, lineItemsToAdd) => {
// Add an item to the checkout
return client.checkout.addLineItems(checkoutId, lineItemsToAdd).then((checkout) => {
// Returns only updated line items
return checkout;
});
};
export const removeLineItemsFromCart = (checkoutId, lineItemIdsToRemove) => {
return client.checkout.removeLineItems(checkoutId, lineItemIdsToRemove).then((checkout) => {
// Do something with the updated checkout
return checkout; // Checkout with line item 'xyz' removed
});
};
export const updateLineItemsInCart = (checkoutId, updatedLineItems) => {
// Update the line item on the checkout (change the quantity or variant)
return client.checkout.updateLineItems(checkoutId, updatedLineItems).then((checkout) => {
return checkout;
});
};
export const requestCommission = (data) => {
return fetch(`${customMiddleWareUrl}/send-email`, {
method: 'post',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
.then((res) => {
return res;
})
.catch((e) => {
return e;
})
};