Skip to content

Commit 52989fe

Browse files
authored
Merge pull request #14 from storefront-foundation/fixing-endpoints
Not sending response for home and session endpoints
2 parents 705f42e + 97fbe88 commit 52989fe

File tree

3 files changed

+49
-35
lines changed

3 files changed

+49
-35
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-storefront-magento2-connector",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "Magento 2 Connector",
55
"module": "./index.js",
66
"watch": {

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: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,39 @@
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'
6-
import Session from 'react-storefront-connector/Session'
711

8-
export default async function session(req, res): Promise<Session> {
9-
const cookiesToSet = [];
12+
export default async function session(req, res): Promise<any> {
13+
const cookiesToSet = []
1014

1115
// ### 1 - LOGGED IN SESSION
1216
const tokenCookieValue = getCookieValue(req, COOKIES.M2_CUSTOMER_TOKEN)
1317
if (tokenCookieValue) {
1418
const customerCartData = await customerCart(tokenCookieValue)
1519
if (customerCartData.error) {
16-
return res.status(400).json({
20+
return {
1721
error: customerCartData.error,
18-
})
22+
}
1923
}
2024
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
25+
cookiesToSet.push(
26+
prepareSetCookie(COOKIES.M2_CUSTOMER_TOKEN, tokenCookieValue, { maxAge: 3600 * 24 * 30 })
27+
) // renew customer token cookie for 30 more days
28+
cookiesToSet.push(
29+
prepareSetCookie(COOKIES.M2_CUSTOMER_CART_ID, customerCartId, { maxAge: 3600 * 24 * 30 })
30+
) // set/renew customer cart ID cookie for 30 days
2331
cookiesToSet.push(prepareKillCookie(COOKIES.M2_GUEST_CART_ID)) // kill guest cart ID cookie just in case (prevents possible cart merges issues)
2432
setCookies(res, cookiesToSet)
25-
return res.status(200).json({
33+
return {
2634
signedIn: true,
2735
cart,
28-
})
36+
}
2937
}
3038

3139
// ### 2 - GUEST SESSION
@@ -36,34 +44,38 @@ export default async function session(req, res): Promise<Session> {
3644
const guestCartData = await guestCart(guestCartIdCookieValue)
3745
if (guestCartData.error) {
3846
setCookies(res, cookiesToSet)
39-
return res.status(400).json({
47+
return {
4048
error: guestCartData.error,
41-
})
49+
}
4250
}
4351
const { cart } = guestCartData
44-
cookiesToSet.push(prepareSetCookie(COOKIES.M2_GUEST_CART_ID, guestCartIdCookieValue, { maxAge: 3600 * 24 * 7 })) // renew cookie for 7 more days
52+
cookiesToSet.push(
53+
prepareSetCookie(COOKIES.M2_GUEST_CART_ID, guestCartIdCookieValue, { maxAge: 3600 * 24 * 7 })
54+
) // renew cookie for 7 more days
4555
setCookies(res, cookiesToSet)
46-
return res.status(200).json({
56+
return {
4757
signedIn: false,
4858
cart,
49-
})
59+
}
5060
}
5161

5262
// # 2.2 - Obtain new guest session
5363
const obtainSessionData = await obtainSession()
5464
if (obtainSessionData.error) {
5565
setCookies(res, cookiesToSet)
56-
return res.status(400).json({
66+
return {
5767
error: obtainSessionData.error,
58-
})
68+
}
5969
}
6070
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
71+
cookiesToSet.push(
72+
prepareSetCookie(COOKIES.M2_GUEST_CART_ID, guestCartId, { maxAge: 3600 * 24 * 7 })
73+
) // set guest cart id cookie for 7 days
6274
setCookies(res, cookiesToSet)
63-
return res.status(200).json({
75+
return {
6476
signedIn: false,
6577
cart: {
6678
items: [],
6779
},
68-
})
80+
}
6981
}

0 commit comments

Comments
 (0)