Skip to content

Commit bbec61f

Browse files
committed
Not sending response for home and session endpoints
The data should be sent by the app.
1 parent 705f42e commit bbec61f

File tree

2 files changed

+47
-32
lines changed

2 files changed

+47
-32
lines changed

src/home/home.ts

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@ import withAppData from '../app/withAppData'
22
import Result from 'react-storefront-connector/Result'
33

44
export default async function home(req, res): Promise<Result<any>> {
5-
const data = await withAppData(req, () => Promise.resolve({
6-
title: `Home`,
7-
slots: {
8-
heading: 'Home',
9-
description: 'Welcome!',
10-
},
11-
breadcrumbs: [
12-
{
13-
text: 'Home',
14-
href: '/',
5+
const data = await withAppData(req, () =>
6+
Promise.resolve({
7+
title: `Home`,
8+
slots: {
9+
heading: 'Home',
10+
description: 'Welcome!',
1511
},
16-
],
17-
}))
18-
return res.status(200).send({ ...data })
19-
}
12+
breadcrumbs: [
13+
{
14+
text: 'Home',
15+
href: '/',
16+
},
17+
],
18+
})
19+
)
20+
return { ...data }
21+
}

src/session/session.ts

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,40 @@
1-
import { getCookieValue, setCookies, prepareSetCookie, prepareKillCookie } from '../helpers/nodeCookieHelpers'
1+
import {
2+
getCookieValue,
3+
setCookies,
4+
prepareSetCookie,
5+
prepareKillCookie,
6+
} from '../helpers/nodeCookieHelpers'
27
import obtainSession from './guest/obtainSession'
38
import guestCart from './guest/cart'
49
import customerCart from './customer/cart'
510
import { COOKIES } from '../constants'
611
import Session from 'react-storefront-connector/Session'
712

813
export default async function session(req, res): Promise<Session> {
9-
const cookiesToSet = [];
14+
const cookiesToSet = []
1015

1116
// ### 1 - LOGGED IN SESSION
1217
const tokenCookieValue = getCookieValue(req, COOKIES.M2_CUSTOMER_TOKEN)
1318
if (tokenCookieValue) {
1419
const customerCartData = await customerCart(tokenCookieValue)
1520
if (customerCartData.error) {
16-
return res.status(400).json({
21+
return {
1722
error: customerCartData.error,
18-
})
23+
}
1924
}
2025
const { cart, customerCartId } = customerCartData
21-
cookiesToSet.push(prepareSetCookie(COOKIES.M2_CUSTOMER_TOKEN, tokenCookieValue, { maxAge: 3600 * 24 * 30 })) // renew customer token cookie for 30 more days
22-
cookiesToSet.push(prepareSetCookie(COOKIES.M2_CUSTOMER_CART_ID, customerCartId, { maxAge: 3600 * 24 * 30 })) // set/renew customer cart ID cookie for 30 days
26+
cookiesToSet.push(
27+
prepareSetCookie(COOKIES.M2_CUSTOMER_TOKEN, tokenCookieValue, { maxAge: 3600 * 24 * 30 })
28+
) // renew customer token cookie for 30 more days
29+
cookiesToSet.push(
30+
prepareSetCookie(COOKIES.M2_CUSTOMER_CART_ID, customerCartId, { maxAge: 3600 * 24 * 30 })
31+
) // set/renew customer cart ID cookie for 30 days
2332
cookiesToSet.push(prepareKillCookie(COOKIES.M2_GUEST_CART_ID)) // kill guest cart ID cookie just in case (prevents possible cart merges issues)
2433
setCookies(res, cookiesToSet)
25-
return res.status(200).json({
34+
return {
2635
signedIn: true,
2736
cart,
28-
})
37+
}
2938
}
3039

3140
// ### 2 - GUEST SESSION
@@ -36,34 +45,38 @@ export default async function session(req, res): Promise<Session> {
3645
const guestCartData = await guestCart(guestCartIdCookieValue)
3746
if (guestCartData.error) {
3847
setCookies(res, cookiesToSet)
39-
return res.status(400).json({
48+
return {
4049
error: guestCartData.error,
41-
})
50+
}
4251
}
4352
const { cart } = guestCartData
44-
cookiesToSet.push(prepareSetCookie(COOKIES.M2_GUEST_CART_ID, guestCartIdCookieValue, { maxAge: 3600 * 24 * 7 })) // renew cookie for 7 more days
53+
cookiesToSet.push(
54+
prepareSetCookie(COOKIES.M2_GUEST_CART_ID, guestCartIdCookieValue, { maxAge: 3600 * 24 * 7 })
55+
) // renew cookie for 7 more days
4556
setCookies(res, cookiesToSet)
46-
return res.status(200).json({
57+
return {
4758
signedIn: false,
4859
cart,
49-
})
60+
}
5061
}
5162

5263
// # 2.2 - Obtain new guest session
5364
const obtainSessionData = await obtainSession()
5465
if (obtainSessionData.error) {
5566
setCookies(res, cookiesToSet)
56-
return res.status(400).json({
67+
return {
5768
error: obtainSessionData.error,
58-
})
69+
}
5970
}
6071
const { guestCartId } = obtainSessionData
61-
cookiesToSet.push(prepareSetCookie(COOKIES.M2_GUEST_CART_ID, guestCartId, { maxAge: 3600 * 24 * 7 })) // set guest cart id cookie for 7 days
72+
cookiesToSet.push(
73+
prepareSetCookie(COOKIES.M2_GUEST_CART_ID, guestCartId, { maxAge: 3600 * 24 * 7 })
74+
) // set guest cart id cookie for 7 days
6275
setCookies(res, cookiesToSet)
63-
return res.status(200).json({
76+
return {
6477
signedIn: false,
6578
cart: {
6679
items: [],
6780
},
68-
})
81+
}
6982
}

0 commit comments

Comments
 (0)