-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathserver-context-getter.js
88 lines (78 loc) · 2.37 KB
/
server-context-getter.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
import {
cartGetIdDefault,
cartSetIdDefault,
createCartHandler,
createCustomerAccountClient,
createStorefrontClient,
} from '@shopify/hydrogen'
import { getStorefrontHeaders } from '@shopify/remix-oxygen'
import { CART_QUERY_FRAGMENT } from '~/lib/fragments'
import { AppSession } from '~/lib/session'
import * as Hydrogen from '@shopify/hydrogen'
console.log('Hydrogen', Hydrogen)
/** The getLoadContext is used both in server.js, and by Utopia's storyboard.js */
export const getLoadContext =
(env, executionContext, mockCartID) =>
async (request) => {
const waitUntil = executionContext.waitUntil.bind(
executionContext,
)
const [cache, session] = await Promise.all([
caches.open('hydrogen'),
AppSession.init(request, [env.SESSION_SECRET]),
])
/**
* Create Hydrogen's Storefront client.
*/
const { storefront } = createStorefrontClient({
cache,
waitUntil,
i18n: { language: 'EN', country: 'US' },
publicStorefrontToken:
env.PUBLIC_STOREFRONT_API_TOKEN,
privateStorefrontToken:
env.PRIVATE_STOREFRONT_API_TOKEN,
storeDomain: env.PUBLIC_STORE_DOMAIN,
storefrontId: env.PUBLIC_STOREFRONT_ID,
storefrontHeaders: getStorefrontHeaders(request),
})
// /**
// * Create a client for Customer Account API.
// */
// const customerAccount = createCustomerAccountClient({
// waitUntil,
// request,
// session,
// customerAccountId: env.PUBLIC_CUSTOMER_ACCOUNT_API_CLIENT_ID,
// customerAccountUrl: env.PUBLIC_CUSTOMER_ACCOUNT_API_URL,
// });
/*
* Create a cart handler that will be used to
* create and update the cart in the session.
*/
const IS_TEST_ENVIRONMENT =
typeof window !== 'undefined'
const cart = createCartHandler({
storefront,
getCartId:
mockCartID != null
? () => mockCartID
: cartGetIdDefault(request.headers),
setCartId: IS_TEST_ENVIRONMENT
? () => {}
: cartSetIdDefault(),
cartQueryFragment: CART_QUERY_FRAGMENT,
})
console.log(
'Logging CartID for Utopia',
cart.getCartId(),
)
return {
session,
storefront,
// customerAccount, // TODO UTOPIA createCustomerAccountClient is unavailable in the hydrogen import in the browser
cart,
env,
waitUntil,
}
}