@scayle/storefront-core 7.22.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (268) hide show
  1. package/CHANGELOG.md +480 -0
  2. package/LICENSE +21 -0
  3. package/README.md +29 -0
  4. package/dist/bapi/init.cjs +30 -0
  5. package/dist/bapi/init.d.ts +14 -0
  6. package/dist/bapi/init.mjs +23 -0
  7. package/dist/cache/cache.cjs +1 -0
  8. package/dist/cache/cache.d.ts +8 -0
  9. package/dist/cache/cache.mjs +0 -0
  10. package/dist/cache/cached.cjs +89 -0
  11. package/dist/cache/cached.d.ts +24 -0
  12. package/dist/cache/cached.mjs +92 -0
  13. package/dist/cache/index.cjs +38 -0
  14. package/dist/cache/index.d.ts +3 -0
  15. package/dist/cache/index.mjs +3 -0
  16. package/dist/cache/providers/redis.cjs +94 -0
  17. package/dist/cache/providers/redis.d.ts +28 -0
  18. package/dist/cache/providers/redis.mjs +86 -0
  19. package/dist/cache/providers/unstorage.cjs +54 -0
  20. package/dist/cache/providers/unstorage.d.ts +16 -0
  21. package/dist/cache/providers/unstorage.mjs +49 -0
  22. package/dist/cache/types.cjs +11 -0
  23. package/dist/cache/types.d.ts +6 -0
  24. package/dist/cache/types.mjs +4 -0
  25. package/dist/constants/basket.cjs +13 -0
  26. package/dist/constants/basket.d.ts +8 -0
  27. package/dist/constants/basket.mjs +6 -0
  28. package/dist/constants/cache.cjs +8 -0
  29. package/dist/constants/cache.d.ts +1 -0
  30. package/dist/constants/cache.mjs +1 -0
  31. package/dist/constants/hash.cjs +11 -0
  32. package/dist/constants/hash.d.ts +6 -0
  33. package/dist/constants/hash.mjs +4 -0
  34. package/dist/constants/httpStatus.cjs +636 -0
  35. package/dist/constants/httpStatus.d.ts +632 -0
  36. package/dist/constants/httpStatus.mjs +629 -0
  37. package/dist/constants/index.cjs +93 -0
  38. package/dist/constants/index.d.ts +8 -0
  39. package/dist/constants/index.mjs +8 -0
  40. package/dist/constants/product.cjs +11 -0
  41. package/dist/constants/product.d.ts +6 -0
  42. package/dist/constants/product.mjs +4 -0
  43. package/dist/constants/promotion.cjs +10 -0
  44. package/dist/constants/promotion.d.ts +2 -0
  45. package/dist/constants/promotion.mjs +2 -0
  46. package/dist/constants/sorting.cjs +24 -0
  47. package/dist/constants/sorting.d.ts +20 -0
  48. package/dist/constants/sorting.mjs +17 -0
  49. package/dist/constants/withParameters.cjs +138 -0
  50. package/dist/constants/withParameters.d.ts +30 -0
  51. package/dist/constants/withParameters.mjs +132 -0
  52. package/dist/errors/BAPIError.cjs +16 -0
  53. package/dist/errors/BAPIError.d.ts +6 -0
  54. package/dist/errors/BAPIError.mjs +9 -0
  55. package/dist/errors/baseError.cjs +17 -0
  56. package/dist/errors/baseError.d.ts +5 -0
  57. package/dist/errors/baseError.mjs +10 -0
  58. package/dist/errors/index.cjs +27 -0
  59. package/dist/errors/index.d.ts +2 -0
  60. package/dist/errors/index.mjs +2 -0
  61. package/dist/helpers/advancedAttributeHelpers.cjs +26 -0
  62. package/dist/helpers/advancedAttributeHelpers.d.ts +12 -0
  63. package/dist/helpers/advancedAttributeHelpers.mjs +21 -0
  64. package/dist/helpers/arrayHelpers.cjs +10 -0
  65. package/dist/helpers/arrayHelpers.d.ts +1 -0
  66. package/dist/helpers/arrayHelpers.mjs +3 -0
  67. package/dist/helpers/attributeHelpers.cjs +24 -0
  68. package/dist/helpers/attributeHelpers.d.ts +5 -0
  69. package/dist/helpers/attributeHelpers.mjs +17 -0
  70. package/dist/helpers/autobind.cjs +44 -0
  71. package/dist/helpers/autobind.d.ts +39 -0
  72. package/dist/helpers/autobind.mjs +35 -0
  73. package/dist/helpers/basketHelpers.cjs +12 -0
  74. package/dist/helpers/basketHelpers.d.ts +2 -0
  75. package/dist/helpers/basketHelpers.mjs +5 -0
  76. package/dist/helpers/categoryHelper.cjs +61 -0
  77. package/dist/helpers/categoryHelper.d.ts +7 -0
  78. package/dist/helpers/categoryHelper.mjs +44 -0
  79. package/dist/helpers/filterHelper.cjs +156 -0
  80. package/dist/helpers/filterHelper.d.ts +82 -0
  81. package/dist/helpers/filterHelper.mjs +147 -0
  82. package/dist/helpers/formHelpers.cjs +114 -0
  83. package/dist/helpers/formHelpers.d.ts +48 -0
  84. package/dist/helpers/formHelpers.mjs +106 -0
  85. package/dist/helpers/imageHelpers.cjs +20 -0
  86. package/dist/helpers/imageHelpers.d.ts +4 -0
  87. package/dist/helpers/imageHelpers.mjs +14 -0
  88. package/dist/helpers/index.cjs +192 -0
  89. package/dist/helpers/index.d.ts +17 -0
  90. package/dist/helpers/index.mjs +17 -0
  91. package/dist/helpers/localization.cjs +13 -0
  92. package/dist/helpers/localization.d.ts +7 -0
  93. package/dist/helpers/localization.mjs +6 -0
  94. package/dist/helpers/objectHelpers.cjs +19 -0
  95. package/dist/helpers/objectHelpers.d.ts +1 -0
  96. package/dist/helpers/objectHelpers.mjs +6 -0
  97. package/dist/helpers/orderHelpers.cjs +10 -0
  98. package/dist/helpers/orderHelpers.d.ts +67 -0
  99. package/dist/helpers/orderHelpers.mjs +4 -0
  100. package/dist/helpers/product.fixture.cjs +770 -0
  101. package/dist/helpers/product.fixture.d.ts +415 -0
  102. package/dist/helpers/product.fixture.mjs +838 -0
  103. package/dist/helpers/productDisruptorHelper.cjs +37 -0
  104. package/dist/helpers/productDisruptorHelper.d.ts +10 -0
  105. package/dist/helpers/productDisruptorHelper.mjs +23 -0
  106. package/dist/helpers/productHelpers.cjs +188 -0
  107. package/dist/helpers/productHelpers.d.ts +41 -0
  108. package/dist/helpers/productHelpers.mjs +170 -0
  109. package/dist/helpers/sanitizationHelpers.cjs +15 -0
  110. package/dist/helpers/sanitizationHelpers.d.ts +2 -0
  111. package/dist/helpers/sanitizationHelpers.mjs +6 -0
  112. package/dist/helpers/sortingHelper.cjs +67 -0
  113. package/dist/helpers/sortingHelper.d.ts +69 -0
  114. package/dist/helpers/sortingHelper.mjs +66 -0
  115. package/dist/helpers/stringHelpers.cjs +8 -0
  116. package/dist/helpers/stringHelpers.d.ts +2 -0
  117. package/dist/helpers/stringHelpers.mjs +2 -0
  118. package/dist/index.cjs +85 -0
  119. package/dist/index.d.ts +19 -0
  120. package/dist/index.mjs +8 -0
  121. package/dist/rpc/methods/basket/basket.cjs +183 -0
  122. package/dist/rpc/methods/basket/basket.d.ts +23 -0
  123. package/dist/rpc/methods/basket/basket.mjs +181 -0
  124. package/dist/rpc/methods/brands.cjs +34 -0
  125. package/dist/rpc/methods/brands.d.ts +5 -0
  126. package/dist/rpc/methods/brands.mjs +16 -0
  127. package/dist/rpc/methods/categories.cjs +102 -0
  128. package/dist/rpc/methods/categories.d.ts +21 -0
  129. package/dist/rpc/methods/categories.mjs +64 -0
  130. package/dist/rpc/methods/cbd.cjs +45 -0
  131. package/dist/rpc/methods/cbd.d.ts +4 -0
  132. package/dist/rpc/methods/cbd.mjs +36 -0
  133. package/dist/rpc/methods/checkout/index.cjs +38 -0
  134. package/dist/rpc/methods/checkout/index.d.ts +3 -0
  135. package/dist/rpc/methods/checkout/index.mjs +3 -0
  136. package/dist/rpc/methods/checkout/order.cjs +55 -0
  137. package/dist/rpc/methods/checkout/order.d.ts +4 -0
  138. package/dist/rpc/methods/checkout/order.mjs +43 -0
  139. package/dist/rpc/methods/checkout/shopUser.cjs +126 -0
  140. package/dist/rpc/methods/checkout/shopUser.d.ts +7 -0
  141. package/dist/rpc/methods/checkout/shopUser.mjs +109 -0
  142. package/dist/rpc/methods/checkout/shopUserAddresses.cjs +25 -0
  143. package/dist/rpc/methods/checkout/shopUserAddresses.d.ts +3 -0
  144. package/dist/rpc/methods/checkout/shopUserAddresses.mjs +19 -0
  145. package/dist/rpc/methods/index.cjs +159 -0
  146. package/dist/rpc/methods/index.d.ts +14 -0
  147. package/dist/rpc/methods/index.mjs +14 -0
  148. package/dist/rpc/methods/navigationTrees.cjs +33 -0
  149. package/dist/rpc/methods/navigationTrees.d.ts +5 -0
  150. package/dist/rpc/methods/navigationTrees.mjs +10 -0
  151. package/dist/rpc/methods/products.cjs +261 -0
  152. package/dist/rpc/methods/products.d.ts +13 -0
  153. package/dist/rpc/methods/products.mjs +217 -0
  154. package/dist/rpc/methods/promotion.cjs +53 -0
  155. package/dist/rpc/methods/promotion.d.ts +4 -0
  156. package/dist/rpc/methods/promotion.mjs +38 -0
  157. package/dist/rpc/methods/search.cjs +38 -0
  158. package/dist/rpc/methods/search.d.ts +10 -0
  159. package/dist/rpc/methods/search.mjs +22 -0
  160. package/dist/rpc/methods/session.cjs +274 -0
  161. package/dist/rpc/methods/session.d.ts +20 -0
  162. package/dist/rpc/methods/session.mjs +309 -0
  163. package/dist/rpc/methods/shopConfiguration.cjs +15 -0
  164. package/dist/rpc/methods/shopConfiguration.d.ts +4 -0
  165. package/dist/rpc/methods/shopConfiguration.mjs +6 -0
  166. package/dist/rpc/methods/user.cjs +89 -0
  167. package/dist/rpc/methods/user.d.ts +20 -0
  168. package/dist/rpc/methods/user.mjs +56 -0
  169. package/dist/rpc/methods/variants.cjs +29 -0
  170. package/dist/rpc/methods/variants.d.ts +8 -0
  171. package/dist/rpc/methods/variants.mjs +12 -0
  172. package/dist/rpc/methods/wishlist.cjs +85 -0
  173. package/dist/rpc/methods/wishlist.d.ts +13 -0
  174. package/dist/rpc/methods/wishlist.mjs +61 -0
  175. package/dist/server.cjs +39 -0
  176. package/dist/server.d.ts +5 -0
  177. package/dist/server.mjs +4 -0
  178. package/dist/types/api/auth.cjs +1 -0
  179. package/dist/types/api/auth.d.ts +63 -0
  180. package/dist/types/api/auth.mjs +0 -0
  181. package/dist/types/api/context.cjs +1 -0
  182. package/dist/types/api/context.d.ts +61 -0
  183. package/dist/types/api/context.mjs +0 -0
  184. package/dist/types/api/rpc.cjs +1 -0
  185. package/dist/types/api/rpc.d.ts +4 -0
  186. package/dist/types/api/rpc.mjs +0 -0
  187. package/dist/types/bapi/basket.cjs +1 -0
  188. package/dist/types/bapi/basket.d.ts +25 -0
  189. package/dist/types/bapi/basket.mjs +0 -0
  190. package/dist/types/bapi/brand.cjs +1 -0
  191. package/dist/types/bapi/brand.d.ts +2 -0
  192. package/dist/types/bapi/brand.mjs +0 -0
  193. package/dist/types/bapi/category.cjs +1 -0
  194. package/dist/types/bapi/category.d.ts +6 -0
  195. package/dist/types/bapi/category.mjs +0 -0
  196. package/dist/types/bapi/filter.cjs +1 -0
  197. package/dist/types/bapi/filter.d.ts +49 -0
  198. package/dist/types/bapi/filter.mjs +0 -0
  199. package/dist/types/bapi/navigation.cjs +1 -0
  200. package/dist/types/bapi/navigation.d.ts +10 -0
  201. package/dist/types/bapi/navigation.mjs +0 -0
  202. package/dist/types/bapi/order.cjs +1 -0
  203. package/dist/types/bapi/order.d.ts +554 -0
  204. package/dist/types/bapi/order.mjs +0 -0
  205. package/dist/types/bapi/pagination.cjs +1 -0
  206. package/dist/types/bapi/pagination.d.ts +13 -0
  207. package/dist/types/bapi/pagination.mjs +0 -0
  208. package/dist/types/bapi/product.cjs +1 -0
  209. package/dist/types/bapi/product.d.ts +84 -0
  210. package/dist/types/bapi/product.mjs +0 -0
  211. package/dist/types/bapi/productFilter.cjs +1 -0
  212. package/dist/types/bapi/productFilter.d.ts +7 -0
  213. package/dist/types/bapi/productFilter.mjs +0 -0
  214. package/dist/types/bapi/promotion.cjs +1 -0
  215. package/dist/types/bapi/promotion.d.ts +2 -0
  216. package/dist/types/bapi/promotion.mjs +0 -0
  217. package/dist/types/bapi/redirects.cjs +1 -0
  218. package/dist/types/bapi/redirects.d.ts +1 -0
  219. package/dist/types/bapi/redirects.mjs +0 -0
  220. package/dist/types/bapi/router.cjs +1 -0
  221. package/dist/types/bapi/router.d.ts +1 -0
  222. package/dist/types/bapi/router.mjs +0 -0
  223. package/dist/types/bapi/search.cjs +1 -0
  224. package/dist/types/bapi/search.d.ts +21 -0
  225. package/dist/types/bapi/search.mjs +0 -0
  226. package/dist/types/bapi/sorting.cjs +5 -0
  227. package/dist/types/bapi/sorting.d.ts +10 -0
  228. package/dist/types/bapi/sorting.mjs +1 -0
  229. package/dist/types/bapi/wishlist.cjs +1 -0
  230. package/dist/types/bapi/wishlist.d.ts +9 -0
  231. package/dist/types/bapi/wishlist.mjs +0 -0
  232. package/dist/types/breadcrumb.cjs +1 -0
  233. package/dist/types/breadcrumb.d.ts +4 -0
  234. package/dist/types/breadcrumb.mjs +0 -0
  235. package/dist/types/index.cjs +236 -0
  236. package/dist/types/index.d.ts +22 -0
  237. package/dist/types/index.mjs +21 -0
  238. package/dist/types/promises.cjs +1 -0
  239. package/dist/types/promises.d.ts +2 -0
  240. package/dist/types/promises.mjs +0 -0
  241. package/dist/types/user.cjs +1 -0
  242. package/dist/types/user.d.ts +100 -0
  243. package/dist/types/user.mjs +0 -0
  244. package/dist/utils/compression.cjs +24 -0
  245. package/dist/utils/compression.d.ts +2 -0
  246. package/dist/utils/compression.mjs +16 -0
  247. package/dist/utils/hash.cjs +68 -0
  248. package/dist/utils/hash.d.ts +8 -0
  249. package/dist/utils/hash.mjs +61 -0
  250. package/dist/utils/index.cjs +27 -0
  251. package/dist/utils/index.d.ts +2 -0
  252. package/dist/utils/index.mjs +2 -0
  253. package/dist/utils/keys.cjs +67 -0
  254. package/dist/utils/keys.d.ts +24 -0
  255. package/dist/utils/keys.mjs +56 -0
  256. package/dist/utils/log.cjs +93 -0
  257. package/dist/utils/log.d.ts +43 -0
  258. package/dist/utils/log.mjs +75 -0
  259. package/dist/utils/rpc.cjs +41 -0
  260. package/dist/utils/rpc.d.ts +6 -0
  261. package/dist/utils/rpc.mjs +35 -0
  262. package/dist/utils/timeout.cjs +17 -0
  263. package/dist/utils/timeout.d.ts +2 -0
  264. package/dist/utils/timeout.mjs +9 -0
  265. package/dist/utils/user.cjs +75 -0
  266. package/dist/utils/user.d.ts +14 -0
  267. package/dist/utils/user.mjs +65 -0
  268. package/package.json +97 -0
@@ -0,0 +1,309 @@
1
+ import axios from "axios";
2
+ import { decodeJwt } from "jose";
3
+ import { DEFAULT_WITH_LISTING, HttpStatusCode } from "../../constants/index.mjs";
4
+ import { convertErrorForRpcCall } from "../../utils/rpc.mjs";
5
+ import { encodeBase64 } from "../../utils/hash.mjs";
6
+ import { mergeBaskets, mergeWishlists } from "../../utils/user.mjs";
7
+ import { fetchUser } from "../../rpc/methods/user.mjs";
8
+ class MissingCredentialsError extends Error {
9
+ constructor() {
10
+ super("[Token based Authentication] No credentials configured");
11
+ this.name = "MissingCredentialsError";
12
+ }
13
+ }
14
+ function getOAuthSettings(context) {
15
+ const clientId = context.oauth?.clientId ?? process.env.OAUTH_CLIENT_ID;
16
+ const clientSecret = context.oauth?.clientSecret ?? process.env.OAUTH_CLIENT_SECRET;
17
+ const apiHost = context.oauth?.apiHost ?? process.env.OAUTH_API_HOST;
18
+ if (!clientId || !clientSecret) {
19
+ throw new MissingCredentialsError();
20
+ }
21
+ const BASIC_AUTH_HASH = encodeBase64(`${clientId}:${clientSecret}`);
22
+ const axiosInstance2 = axios.create({
23
+ baseURL: `${apiHost}/v1`,
24
+ headers: {
25
+ Authorization: `Basic ${BASIC_AUTH_HASH}`
26
+ }
27
+ });
28
+ return {
29
+ clientId,
30
+ clientSecret,
31
+ apiHost,
32
+ axiosInstance: axiosInstance2
33
+ };
34
+ }
35
+ const saveUserOnSession = async (accessToken, context) => {
36
+ const checkoutUrl = context.checkout.url;
37
+ const shopId = context.shopId;
38
+ const user = await fetchUser(
39
+ { accessToken, callback: "" },
40
+ {
41
+ checkoutUrl,
42
+ shopId
43
+ }
44
+ );
45
+ context.updateUser(user);
46
+ };
47
+ export const oauthLogin = async (login, context) => {
48
+ const shopId = context.shopId;
49
+ const { axiosInstance: axiosInstance2 } = getOAuthSettings(context);
50
+ if (!login.email || !login.password) {
51
+ throw new Error(
52
+ "Login or password are missing, seems like validation has failed"
53
+ );
54
+ }
55
+ try {
56
+ const response = await axiosInstance2.post(
57
+ "/auth/login",
58
+ {
59
+ ...login,
60
+ shop_id: shopId
61
+ }
62
+ );
63
+ context.updateTokens({
64
+ accessToken: response.data.access_token,
65
+ refreshToken: response.data.refresh_token
66
+ });
67
+ await saveUserOnSession(response.data.access_token, context);
68
+ const { customerId } = decodeJwt(response.data.access_token);
69
+ await Promise.all([
70
+ mergeBaskets(
71
+ context.sessionId,
72
+ await context.generateBasketKeyForUserId(customerId),
73
+ DEFAULT_WITH_LISTING,
74
+ context
75
+ ),
76
+ mergeWishlists(
77
+ context.sessionId,
78
+ await context.generateWishlistKeyForUserId(customerId),
79
+ DEFAULT_WITH_LISTING,
80
+ context
81
+ )
82
+ ]);
83
+ await context.createUserBoundSession();
84
+ return {
85
+ oauth: response.data
86
+ };
87
+ } catch (error) {
88
+ const err = convertErrorForRpcCall(error, [
89
+ HttpStatusCode.INTERNAL_SERVER_ERROR,
90
+ HttpStatusCode.BAD_REQUEST,
91
+ HttpStatusCode.UNAUTHORIZED,
92
+ HttpStatusCode.NOT_FOUND
93
+ ]);
94
+ if (err) {
95
+ throw err;
96
+ }
97
+ return { oauth: null };
98
+ }
99
+ };
100
+ export const oauthRegister = async (register, context) => {
101
+ const shopId = context.shopId;
102
+ const { axiosInstance: axiosInstance2 } = getOAuthSettings(context);
103
+ try {
104
+ const response = await axiosInstance2.post(
105
+ "/auth/register",
106
+ {
107
+ ...register,
108
+ shop_id: shopId
109
+ }
110
+ );
111
+ context.updateTokens({
112
+ accessToken: response.data.access_token,
113
+ refreshToken: response.data.refresh_token
114
+ });
115
+ await saveUserOnSession(response.data.access_token, context);
116
+ const { customerId } = decodeJwt(response.data.access_token);
117
+ await Promise.all([
118
+ mergeBaskets(
119
+ context.sessionId,
120
+ await context.generateBasketKeyForUserId(customerId),
121
+ DEFAULT_WITH_LISTING,
122
+ context
123
+ ),
124
+ mergeWishlists(
125
+ context.sessionId,
126
+ await context.generateWishlistKeyForUserId(customerId),
127
+ DEFAULT_WITH_LISTING,
128
+ context
129
+ )
130
+ ]);
131
+ await context.createUserBoundSession();
132
+ return {
133
+ oauth: response.data
134
+ };
135
+ } catch (error) {
136
+ const err = convertErrorForRpcCall(error, [
137
+ HttpStatusCode.BAD_REQUEST,
138
+ HttpStatusCode.UNAUTHORIZED,
139
+ HttpStatusCode.CONFLICT
140
+ ]);
141
+ if (err) {
142
+ throw err;
143
+ }
144
+ return { oauth: null };
145
+ }
146
+ };
147
+ export const oauthGuestLogin = async (guest, context) => {
148
+ const shopId = context.shopId;
149
+ const { axiosInstance: axiosInstance2 } = getOAuthSettings(context);
150
+ try {
151
+ const response = await axiosInstance2.post(
152
+ "/auth/login/guest",
153
+ {
154
+ ...guest,
155
+ shop_id: shopId
156
+ }
157
+ );
158
+ context.updateTokens({
159
+ accessToken: response.data.access_token,
160
+ refreshToken: response.data.refresh_token
161
+ });
162
+ await saveUserOnSession(response.data.access_token, context);
163
+ const { customerId } = decodeJwt(response.data.access_token);
164
+ await Promise.all([
165
+ mergeBaskets(
166
+ context.sessionId,
167
+ await context.generateBasketKeyForUserId(customerId),
168
+ DEFAULT_WITH_LISTING,
169
+ context
170
+ ),
171
+ mergeWishlists(
172
+ context.sessionId,
173
+ await context.generateWishlistKeyForUserId(customerId),
174
+ DEFAULT_WITH_LISTING,
175
+ context
176
+ )
177
+ ]);
178
+ await context.createUserBoundSession();
179
+ return {
180
+ oauth: response.data
181
+ };
182
+ } catch (error) {
183
+ const err = convertErrorForRpcCall(error, [
184
+ HttpStatusCode.BAD_REQUEST,
185
+ HttpStatusCode.UNAUTHORIZED,
186
+ HttpStatusCode.CONFLICT
187
+ ]);
188
+ if (err) {
189
+ throw err;
190
+ }
191
+ return { oauth: null };
192
+ }
193
+ };
194
+ export const refreshAccessToken = async (context) => {
195
+ const refreshToken = context.refreshToken;
196
+ const { axiosInstance: axiosInstance2 } = getOAuthSettings(context);
197
+ if (!refreshToken) {
198
+ throw new Error("No app refresh token provided");
199
+ }
200
+ try {
201
+ const response = await axiosInstance2.post(
202
+ "/oauth/token",
203
+ {
204
+ grant_type: "refresh_token",
205
+ refresh_token: refreshToken
206
+ }
207
+ );
208
+ context.updateTokens({
209
+ accessToken: response.data.access_token,
210
+ refreshToken: response.data.refresh_token
211
+ });
212
+ return response.data;
213
+ } catch (error) {
214
+ const err = convertErrorForRpcCall(error, [
215
+ HttpStatusCode.BAD_REQUEST,
216
+ HttpStatusCode.UNAUTHORIZED
217
+ ]);
218
+ if (err) {
219
+ throw err;
220
+ }
221
+ return null;
222
+ }
223
+ };
224
+ export const oauthRevokeToken = async (context) => {
225
+ const accessToken = context.accessToken;
226
+ if (!accessToken) {
227
+ throw new Error("No app oauth authentication credentials");
228
+ }
229
+ const decodedAccessToken = decodeJwt(accessToken);
230
+ await context.destroySession();
231
+ try {
232
+ await axiosInstance.delete(`/oauth/tokens/${decodedAccessToken.jti}`, {
233
+ headers: {
234
+ Authorization: `Bearer ${accessToken}`
235
+ }
236
+ });
237
+ return { result: true };
238
+ } catch (error) {
239
+ const err = convertErrorForRpcCall(error, [
240
+ HttpStatusCode.BAD_REQUEST,
241
+ HttpStatusCode.UNAUTHORIZED,
242
+ HttpStatusCode.NOT_FOUND
243
+ ]);
244
+ if (err) {
245
+ throw err;
246
+ }
247
+ return { result: false };
248
+ }
249
+ };
250
+ export const oauthForgetPassword = async ({ email }, context) => {
251
+ const shopId = context.shopId;
252
+ const { axiosInstance: axiosInstance2 } = getOAuthSettings(context);
253
+ try {
254
+ const resetUrl = new URL(context.auth.resetPasswordUrl);
255
+ if (!resetUrl.searchParams.has("hash")) {
256
+ resetUrl.searchParams.append("hash", "{hash}");
257
+ } else {
258
+ resetUrl.searchParams.set("hash", "{hash}");
259
+ }
260
+ await axiosInstance2.post("/auth/password/send-reset-email", {
261
+ email,
262
+ reset_url: decodeURI(resetUrl.toString()),
263
+ shop_id: shopId
264
+ });
265
+ return { success: true };
266
+ } catch (error) {
267
+ const err = convertErrorForRpcCall(error, [
268
+ HttpStatusCode.BAD_REQUEST,
269
+ HttpStatusCode.NOT_FOUND,
270
+ HttpStatusCode.UNAUTHORIZED,
271
+ HttpStatusCode.FORBIDDEN
272
+ ]);
273
+ if (err) {
274
+ throw err;
275
+ }
276
+ return { success: false };
277
+ }
278
+ };
279
+ export const updatePasswordByHash = async (passwordHash, context) => {
280
+ const shopId = context.shopId;
281
+ const { axiosInstance: axiosInstance2 } = getOAuthSettings(context);
282
+ try {
283
+ const response = await axiosInstance2.put(
284
+ "/auth/password/update-by-hash",
285
+ {
286
+ ...passwordHash,
287
+ shop_id: shopId
288
+ }
289
+ );
290
+ context.updateTokens({
291
+ accessToken: response.data.access_token,
292
+ refreshToken: response.data.refresh_token
293
+ });
294
+ await context.createUserBoundSession();
295
+ return {
296
+ oauth: response.data
297
+ };
298
+ } catch (error) {
299
+ const err = convertErrorForRpcCall(error, [
300
+ HttpStatusCode.BAD_REQUEST,
301
+ HttpStatusCode.UNAUTHORIZED,
302
+ HttpStatusCode.NOT_ACCEPTABLE
303
+ ]);
304
+ if (err) {
305
+ throw err;
306
+ }
307
+ return { oauth: null };
308
+ }
309
+ };
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getShopConfiguration = void 0;
7
+ const getShopConfiguration = async function getShopConfiguration2({
8
+ cached,
9
+ bapiClient
10
+ }) {
11
+ return await cached(bapiClient.shopConfiguration.get, {
12
+ cacheKeyPrefix: "get-shopConfigurations"
13
+ })();
14
+ };
15
+ exports.getShopConfiguration = getShopConfiguration;
@@ -0,0 +1,4 @@
1
+ import { ShopConfigurationResponseData } from '@aboutyou/backbone/endpoints/shopconfiguration/shopconfiguration';
2
+ import { RpcHandler } from '../../types';
3
+ declare const getShopConfiguration: RpcHandler<ShopConfigurationResponseData>;
4
+ export { getShopConfiguration };
@@ -0,0 +1,6 @@
1
+ const getShopConfiguration = async function getShopConfiguration2({ cached, bapiClient }) {
2
+ return await cached(bapiClient.shopConfiguration.get, {
3
+ cacheKeyPrefix: "get-shopConfigurations"
4
+ })();
5
+ };
6
+ export { getShopConfiguration };
@@ -0,0 +1,89 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.refreshUser = exports.getUser = exports.fetchUser = void 0;
7
+ var _axios = _interopRequireDefault(require("axios"));
8
+ var _httpStatus = require("../../constants/httpStatus.cjs");
9
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
+ const getUser = function getUser2(context) {
11
+ return {
12
+ user: context.user
13
+ };
14
+ };
15
+ exports.getUser = getUser;
16
+ const fetchUser = async function fetchUser2(payload, options) {
17
+ const {
18
+ accessToken
19
+ } = payload;
20
+ const {
21
+ checkoutUrl,
22
+ shopId,
23
+ accessHeader
24
+ } = options;
25
+ const user = await _axios.default.get(`${checkoutUrl}/api/oauth/me`, {
26
+ headers: {
27
+ Authorization: `Bearer ${accessToken}`,
28
+ "X-Shop-Id": shopId,
29
+ "Content-Type": "application/json",
30
+ ...(accessHeader && {
31
+ "X-Access-Header": accessHeader
32
+ })
33
+ }
34
+ });
35
+ return {
36
+ ...user.data,
37
+ authentication: {
38
+ ...user.data?.authentication,
39
+ storefrontAccessToken: accessToken
40
+ },
41
+ ...{
42
+ loginShopId: shopId
43
+ }
44
+ };
45
+ };
46
+ exports.fetchUser = fetchUser;
47
+ const refreshUser = async function refreshUser2(context) {
48
+ const {
49
+ accessToken,
50
+ checkout,
51
+ shopId
52
+ } = context;
53
+ const validateStatus = status => {
54
+ const {
55
+ OK,
56
+ MULTIPLE_CHOICES,
57
+ FORBIDDEN
58
+ } = _httpStatus.HttpStatusCode;
59
+ return status >= OK && status < MULTIPLE_CHOICES || status === FORBIDDEN;
60
+ };
61
+ const user = await _axios.default.get(`${checkout.url}/api/oauth/me`, {
62
+ headers: {
63
+ Authorization: `Bearer ${accessToken}`,
64
+ "X-Shop-Id": shopId,
65
+ "Content-Type": "application/json"
66
+ },
67
+ validateStatus
68
+ });
69
+ if (user.data.id) {
70
+ context.updateUser({
71
+ ...user.data,
72
+ authentication: {
73
+ ...user.data?.authentication,
74
+ storefrontAccessToken: accessToken
75
+ },
76
+ ...{
77
+ loginShopId: shopId
78
+ }
79
+ });
80
+ return {
81
+ user: user.data
82
+ };
83
+ }
84
+ await context.destroySession();
85
+ return {
86
+ user: void 0
87
+ };
88
+ };
89
+ exports.refreshUser = refreshUser;
@@ -0,0 +1,20 @@
1
+ import { AuthConfig, RpcContext, RpcHandler, ShopUser } from '../../types';
2
+ declare const getUser: RpcHandler<{
3
+ user: ShopUser | undefined;
4
+ }>;
5
+ declare const fetchUser: (payload: AuthConfig, options: {
6
+ checkoutUrl: string;
7
+ shopId: number;
8
+ accessHeader?: string | undefined;
9
+ }) => Promise<ShopUser>;
10
+ /**
11
+ * This function adds a way to force fetch the user from checkout. In case the user is not logged in any longer the session will be destroyed and the returned user will be undefined.
12
+ * @param context
13
+ * @returns ShopUser
14
+ */
15
+ declare const refreshUser: (context: RpcContext) => Promise<{
16
+ user: ShopUser;
17
+ } | {
18
+ user: undefined;
19
+ }>;
20
+ export { getUser, fetchUser, refreshUser };
@@ -0,0 +1,56 @@
1
+ import axios from "axios";
2
+ import { HttpStatusCode } from "../../constants/httpStatus.mjs";
3
+ const getUser = function getUser2(context) {
4
+ return {
5
+ user: context.user
6
+ };
7
+ };
8
+ const fetchUser = async function fetchUser2(payload, options) {
9
+ const { accessToken } = payload;
10
+ const { checkoutUrl, shopId, accessHeader } = options;
11
+ const user = await axios.get(`${checkoutUrl}/api/oauth/me`, {
12
+ headers: {
13
+ Authorization: `Bearer ${accessToken}`,
14
+ "X-Shop-Id": shopId,
15
+ "Content-Type": "application/json",
16
+ ...accessHeader && { "X-Access-Header": accessHeader }
17
+ }
18
+ });
19
+ return {
20
+ ...user.data,
21
+ authentication: {
22
+ ...user.data?.authentication,
23
+ storefrontAccessToken: accessToken
24
+ },
25
+ ...{ loginShopId: shopId }
26
+ };
27
+ };
28
+ const refreshUser = async function refreshUser2(context) {
29
+ const { accessToken, checkout, shopId } = context;
30
+ const validateStatus = (status) => {
31
+ const { OK, MULTIPLE_CHOICES, FORBIDDEN } = HttpStatusCode;
32
+ return status >= OK && status < MULTIPLE_CHOICES || status === FORBIDDEN;
33
+ };
34
+ const user = await axios.get(`${checkout.url}/api/oauth/me`, {
35
+ headers: {
36
+ Authorization: `Bearer ${accessToken}`,
37
+ "X-Shop-Id": shopId,
38
+ "Content-Type": "application/json"
39
+ },
40
+ validateStatus
41
+ });
42
+ if (user.data.id) {
43
+ context.updateUser({
44
+ ...user.data,
45
+ authentication: {
46
+ ...user.data?.authentication,
47
+ storefrontAccessToken: accessToken
48
+ },
49
+ ...{ loginShopId: shopId }
50
+ });
51
+ return { user: user.data };
52
+ }
53
+ await context.destroySession();
54
+ return { user: void 0 };
55
+ };
56
+ export { getUser, fetchUser, refreshUser };
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getVariantById = void 0;
7
+ const defaultWith = {
8
+ attributes: "all"
9
+ };
10
+ const getVariantById = async function getVariantById2({
11
+ ids,
12
+ include = defaultWith,
13
+ pricePromotionKey = "default"
14
+ }, {
15
+ bapiClient,
16
+ cached,
17
+ campaignKey,
18
+ withParams
19
+ }) {
20
+ const resolvedWith = include ?? withParams?.variant ?? defaultWith;
21
+ return await cached(bapiClient.variants.getByIds, {
22
+ cacheKeyPrefix: "getByIds-variants"
23
+ })(ids, include ? {
24
+ with: resolvedWith,
25
+ campaignKey,
26
+ pricePromotionKey
27
+ } : void 0);
28
+ };
29
+ exports.getVariantById = getVariantById;
@@ -0,0 +1,8 @@
1
+ import type { ProductWith, VariantDetail } from '@aboutyou/backbone';
2
+ import { RpcHandler } from '../../types';
3
+ export interface FetchVariantsParams {
4
+ ids: number[];
5
+ include?: ProductWith;
6
+ pricePromotionKey?: string;
7
+ }
8
+ export declare const getVariantById: RpcHandler<FetchVariantsParams, VariantDetail[]>;
@@ -0,0 +1,12 @@
1
+ const defaultWith = {
2
+ attributes: "all"
3
+ };
4
+ export const getVariantById = async function getVariantById2({ ids, include = defaultWith, pricePromotionKey = "default" }, { bapiClient, cached, campaignKey, withParams }) {
5
+ const resolvedWith = include ?? withParams?.variant ?? defaultWith;
6
+ return await cached(bapiClient.variants.getByIds, {
7
+ cacheKeyPrefix: "getByIds-variants"
8
+ })(
9
+ ids,
10
+ include ? { with: resolvedWith, campaignKey, pricePromotionKey } : void 0
11
+ );
12
+ };
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.removeItemFromWishlist = exports.getWishlist = exports.clearWishlist = exports.addItemToWishlist = void 0;
7
+ var _errors = require("../../errors/index.cjs");
8
+ var _constants = require("../../constants/index.cjs");
9
+ function getWithParams(params, context) {
10
+ return params.with ?? context.withParams?.basket ?? _constants.MIN_WITH_PARAMS_WISHLIST;
11
+ }
12
+ const addItemToWishlist = async function addItemToWishlist2(options, context) {
13
+ const {
14
+ bapiClient,
15
+ campaignKey,
16
+ wishlistKey
17
+ } = context;
18
+ const {
19
+ productId,
20
+ variantId
21
+ } = options;
22
+ const resolvedWith = getWithParams(options, context);
23
+ const pricePromotionKey = resolvedWith?.pricePromotionKey ?? "";
24
+ if (!productId && !variantId) {
25
+ throw new Error("No productId or variantId given");
26
+ }
27
+ const result = await bapiClient.wishlist.addItem(wishlistKey, variantId ? {
28
+ variantId
29
+ } : {
30
+ productId
31
+ }, {
32
+ with: resolvedWith,
33
+ campaignKey,
34
+ pricePromotionKey
35
+ });
36
+ if (result.type === "success") {
37
+ return await getWishlist(resolvedWith, context);
38
+ } else {
39
+ const {
40
+ code,
41
+ message
42
+ } = result.wishlist;
43
+ context.log.error("Adding to wishlist failed", result.wishlist);
44
+ throw new _errors.BAPIError("BAPI ERROR", code, message, "bapiClient.wishlist.addItem");
45
+ }
46
+ };
47
+ exports.addItemToWishlist = addItemToWishlist;
48
+ const getWishlist = async function getWishlist2(options, context) {
49
+ const {
50
+ bapiClient,
51
+ campaignKey
52
+ } = context;
53
+ const resolvedWith = getWithParams({
54
+ with: options
55
+ }, context);
56
+ return await bapiClient.wishlist.get(context.wishlistKey, {
57
+ with: resolvedWith,
58
+ campaignKey,
59
+ pricePromotionKey: resolvedWith?.pricePromotionKey ?? ""
60
+ });
61
+ };
62
+ exports.getWishlist = getWishlist;
63
+ const removeItemFromWishlist = async function removeItemFromWishlist2(options, context) {
64
+ const {
65
+ bapiClient,
66
+ campaignKey,
67
+ wishlistKey
68
+ } = context;
69
+ const resolvedWith = getWithParams(options, context);
70
+ return await bapiClient.wishlist.deleteItem(wishlistKey, options.itemKey, {
71
+ with: resolvedWith,
72
+ campaignKey
73
+ });
74
+ };
75
+ exports.removeItemFromWishlist = removeItemFromWishlist;
76
+ const clearWishlist = async function clearWishlist2(context) {
77
+ const wishlist = await getWishlist({}, context);
78
+ await Promise.all(wishlist.items.map(async item => {
79
+ await removeItemFromWishlist({
80
+ itemKey: item.key
81
+ }, context);
82
+ }));
83
+ return await getWishlist({}, context);
84
+ };
85
+ exports.clearWishlist = clearWishlist;
@@ -0,0 +1,13 @@
1
+ import type { WishlistResponseData } from '@aboutyou/backbone/endpoints/wishlist/getWishlist';
2
+ import { RpcHandler, WishlistWithOptions } from '../../types';
3
+ export declare const addItemToWishlist: RpcHandler<{
4
+ variantId?: number;
5
+ productId?: number;
6
+ with?: WishlistWithOptions;
7
+ }, WishlistResponseData>;
8
+ export declare const getWishlist: RpcHandler<WishlistWithOptions | undefined, WishlistResponseData>;
9
+ export declare const removeItemFromWishlist: RpcHandler<{
10
+ itemKey: string;
11
+ with?: WishlistWithOptions;
12
+ }, ReturnType<typeof getWishlist>>;
13
+ export declare const clearWishlist: RpcHandler<ReturnType<typeof getWishlist>>;