/
index.server.ts
125 lines (111 loc) · 3.69 KB
/
index.server.ts
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
/* istanbul ignore file */
import { ApiClientExtension, apiClientFactory } from "@vue-storefront/middleware";
import * as api from "./api";
import { ClientInstance, Config } from "./types/setup";
import { createMagentoConnection } from "./helpers/magentoLink";
import { defaultSettings } from "./helpers/apiClient/defaultSettings";
import { apolloClientFactory } from "./helpers/magentoLink/graphQl";
const buildConfig = (settings: Config) =>
({
...defaultSettings,
...settings,
state: settings.state || defaultSettings.state,
} as unknown as Config);
const init = (settings: Config) => {
const config = buildConfig(settings);
if (settings.client) {
return {
client: settings.client,
config,
};
}
if (settings.customOptions && settings.customOptions.link) {
return {
client: apolloClientFactory(settings.customOptions),
config,
};
}
const { apolloLink } = createMagentoConnection(config);
const client = apolloClientFactory({
link: apolloLink,
defaultOptions: {
query: {
errorPolicy: "all",
fetchPolicy: "no-cache",
},
mutate: {
errorPolicy: "all",
},
},
...settings.customOptions,
});
return {
config,
client,
};
};
const onCreate = (settings: Config): { config: Config; client: ClientInstance } => {
if (!settings?.client) {
return init(settings);
}
const config = buildConfig(settings);
return { config, client: settings.client };
};
const tokenExtension: ApiClientExtension = {
name: "tokenExtension",
hooks: (req, res) => ({
beforeCreate: ({ configuration }) => {
const cartCookieName: string = configuration.cookies?.cartCookieName || defaultSettings.cookies.cartCookieName;
const customerCookieName: string = configuration.cookies?.customerCookieName || defaultSettings.cookies.customerCookieName;
const storeCookieName: string = configuration.cookies?.storeCookieName || defaultSettings.cookies.storeCookieName;
const currencyCookieName: string = configuration.cookies?.currencyCookieName || defaultSettings.cookies.currencyCookieName;
return {
...configuration,
state: {
getCartId: () => req.cookies[cartCookieName],
setCartId: (id) => {
if (!id) {
// eslint-disable-next-line no-param-reassign
delete req.cookies[cartCookieName];
return;
}
res.cookie(cartCookieName, JSON.stringify(id));
},
getCustomerToken: () => req.cookies[customerCookieName],
setCustomerToken: (token) => {
if (!token) {
// eslint-disable-next-line no-param-reassign
delete req.cookies[customerCookieName];
return;
}
res.cookie(customerCookieName, JSON.stringify(token));
},
getStore: () => configuration?.storeViewCode ?? req.cookies[storeCookieName],
setStore: (id) => {
if (!id) {
// eslint-disable-next-line no-param-reassign
delete req.cookies[storeCookieName];
return;
}
res.cookie(storeCookieName, JSON.stringify(id));
},
getCurrency: () => req.cookies[currencyCookieName],
setCurrency: (id) => {
if (!id) {
// eslint-disable-next-line no-param-reassign
delete req.cookies[currencyCookieName];
return;
}
res.cookie(currencyCookieName, JSON.stringify(id));
},
},
};
},
}),
};
const { createApiClient } = apiClientFactory({
onCreate,
api,
extensions: [tokenExtension],
});
export { createApiClient, init };