-
Notifications
You must be signed in to change notification settings - Fork 8
/
cart.js
66 lines (63 loc) · 1.9 KB
/
cart.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
export default async ({ query: { ids }}, res) => {
const arrOfIds = ids.split(',')
const filterArrValue = arrOfIds.map(i => ({ id : { eq: i }}))
// [{id:{eq:"<idNumber>"}},{id:{eq:"<idNumber>"}},{id:{eq:"<idNumber>"}}] - for query ↓
const filterStringValue = JSON.stringify(filterArrValue).replaceAll('"id"', 'id').replaceAll('"eq"', 'eq')
// fetch items for cart nased on passed ids
await fetch(`${
process.env.NODE_ENV === "production"
? process.env.PROD_CMS_URL
: process.env.DEV_CMS_URL
}/graphql`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
query: `
query {
products(filters: { or: ${filterStringValue} }) {
data {
id
attributes {
title
company
price
available
image {
data {
id
attributes {
name
alternativeText
url
}
}
}
discount {
data {
attributes {
discountMultiplier
}
}
}
}
}
}
}
`
})
})
.then(r => {
if (r.status >= 400) {
return r.json().then(errResData => {
const err = new Error('Error in api/cart.js, .then statement, if (r.status >= 400) condition')
err.data = errResData
throw err
})
}
return r.json()
})
.then(data => res.status(200).json(data))
.catch(err => res.status(404).json({ message: `Error in api/cart.js, .catch statement, status 404, err object: ${err}` }))
}