@akinon/next 2.0.6-rc.1 → 2.0.6
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.
- package/CHANGELOG.md +5 -27
- package/api/auth.ts +84 -24
- package/api/client.ts +32 -0
- package/bin/pz-generate-routes.js +1 -4
- package/components/client-root.tsx +2 -0
- package/components/plugin-module.tsx +9 -3
- package/components/toast.tsx +258 -0
- package/data/client/checkout.ts +1 -0
- package/data/server/category.ts +2 -14
- package/data/server/list.ts +1 -13
- package/data/server/product.ts +0 -10
- package/data/server/special-page.ts +1 -14
- package/data/server/widget.ts +1 -14
- package/data/urls.ts +1 -5
- package/hooks/index.ts +1 -0
- package/hooks/use-captcha.tsx +1 -1
- package/hooks/use-toast.ts +56 -0
- package/instrumentation/index.ts +1 -0
- package/instrumentation/node.ts +224 -2
- package/lib/fixture-manager.ts +146 -0
- package/middlewares/default.ts +1 -2
- package/middlewares/masterpass-rest-callback.ts +147 -34
- package/package.json +6 -7
- package/plugins.d.ts +0 -10
- package/plugins.js +0 -1
- package/redux/actions.ts +1 -0
- package/redux/middlewares/checkout.ts +3 -45
- package/redux/middlewares/pre-order/installment-option.ts +1 -9
- package/redux/reducers/index.ts +2 -0
- package/redux/reducers/toast.ts +70 -0
- package/types/commerce/flatpage.ts +7 -0
- package/types/index.ts +0 -7
- package/utils/app-fetch.ts +27 -0
- package/utils/format-error-message.ts +7 -0
- package/utils/index.ts +8 -1
- package/with-pz-config.js +3 -3
- package/utils/payload-optimizer.ts +0 -481
|
@@ -10,23 +10,49 @@ const withMasterpassRestCallback =
|
|
|
10
10
|
async (req: PzNextRequest, event: NextFetchEvent) => {
|
|
11
11
|
const url = req.nextUrl.clone();
|
|
12
12
|
const ip = req.headers.get('x-forwarded-for') ?? '';
|
|
13
|
+
const sessionId = req.cookies.get('osessionid');
|
|
13
14
|
|
|
14
|
-
|
|
15
|
-
url.pathname.includes('/orders/checkout') &&
|
|
16
|
-
url.searchParams.get('page') === 'MasterpassRestCompletePage';
|
|
17
|
-
|
|
18
|
-
if (!isMasterpassCompletePage) {
|
|
15
|
+
if (!url.pathname.includes('/orders/masterpass-rest-callback')) {
|
|
19
16
|
return middleware(req, event);
|
|
20
17
|
}
|
|
21
18
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
19
|
+
if (req.method !== 'POST') {
|
|
20
|
+
logger.warn('Invalid request method for masterpass REST callback', {
|
|
21
|
+
middleware: 'masterpass-rest-callback',
|
|
22
|
+
method: req.method,
|
|
23
|
+
ip
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
return NextResponse.redirect(
|
|
27
|
+
`${url.origin}${getUrlPathWithLocale(
|
|
28
|
+
'/orders/checkout/',
|
|
29
|
+
req.cookies.get('pz-locale')?.value
|
|
30
|
+
)}`,
|
|
31
|
+
303
|
|
32
|
+
);
|
|
33
|
+
}
|
|
25
34
|
|
|
26
|
-
|
|
27
|
-
|
|
35
|
+
const responseCode = url.searchParams.get('responseCode');
|
|
36
|
+
const token = url.searchParams.get('token');
|
|
37
|
+
|
|
38
|
+
if (!responseCode || !token) {
|
|
39
|
+
logger.warn('Missing required parameters for masterpass REST callback', {
|
|
40
|
+
middleware: 'masterpass-rest-callback',
|
|
41
|
+
responseCode,
|
|
42
|
+
token,
|
|
43
|
+
ip
|
|
28
44
|
});
|
|
29
45
|
|
|
46
|
+
return NextResponse.redirect(
|
|
47
|
+
`${url.origin}${getUrlPathWithLocale(
|
|
48
|
+
'/orders/checkout/',
|
|
49
|
+
req.cookies.get('pz-locale')?.value
|
|
50
|
+
)}`,
|
|
51
|
+
303
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
try {
|
|
30
56
|
const formData = await req.formData();
|
|
31
57
|
const body: Record<string, string> = {};
|
|
32
58
|
|
|
@@ -34,6 +60,38 @@ const withMasterpassRestCallback =
|
|
|
34
60
|
body[key] = value.toString();
|
|
35
61
|
});
|
|
36
62
|
|
|
63
|
+
if (!sessionId) {
|
|
64
|
+
logger.warn(
|
|
65
|
+
'Make sure that the SESSION_COOKIE_SAMESITE environment variable is set to None in Commerce.',
|
|
66
|
+
{
|
|
67
|
+
middleware: 'masterpass-rest-callback',
|
|
68
|
+
ip
|
|
69
|
+
}
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
return NextResponse.redirect(
|
|
73
|
+
`${url.origin}${getUrlPathWithLocale(
|
|
74
|
+
'/orders/checkout/',
|
|
75
|
+
req.cookies.get('pz-locale')?.value
|
|
76
|
+
)}`,
|
|
77
|
+
303
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const isPostCheckout = req.cookies.get('pz-post-checkout-flow')?.value === 'true';
|
|
82
|
+
const requestUrl = new URL(getCheckoutPath(isPostCheckout), Settings.commerceUrl);
|
|
83
|
+
requestUrl.searchParams.set('page', 'MasterpassRestCompletePage');
|
|
84
|
+
requestUrl.searchParams.set('responseCode', responseCode);
|
|
85
|
+
requestUrl.searchParams.set('token', token);
|
|
86
|
+
requestUrl.searchParams.set(
|
|
87
|
+
'three_d_secure',
|
|
88
|
+
body.transactionType?.includes('3D') ? 'true' : 'false'
|
|
89
|
+
);
|
|
90
|
+
requestUrl.searchParams.set(
|
|
91
|
+
'transactionType',
|
|
92
|
+
body.transactionType || ''
|
|
93
|
+
);
|
|
94
|
+
|
|
37
95
|
const requestHeaders = {
|
|
38
96
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
39
97
|
'X-Requested-With': 'XMLHttpRequest',
|
|
@@ -49,13 +107,33 @@ const withMasterpassRestCallback =
|
|
|
49
107
|
body: new URLSearchParams(body)
|
|
50
108
|
});
|
|
51
109
|
|
|
110
|
+
logger.info('Masterpass REST callback request', {
|
|
111
|
+
requestUrl: requestUrl.toString(),
|
|
112
|
+
status: request.status,
|
|
113
|
+
requestHeaders,
|
|
114
|
+
ip
|
|
115
|
+
});
|
|
116
|
+
|
|
52
117
|
const response = await request.json();
|
|
53
|
-
|
|
118
|
+
|
|
119
|
+
const { context_list: contextList, errors } = response;
|
|
120
|
+
|
|
121
|
+
let redirectUrl = response.redirect_url;
|
|
122
|
+
|
|
123
|
+
if (!redirectUrl && contextList && contextList.length > 0) {
|
|
124
|
+
for (const context of contextList) {
|
|
125
|
+
if (context.page_context && context.page_context.redirect_url) {
|
|
126
|
+
redirectUrl = context.page_context.redirect_url;
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
54
131
|
|
|
55
132
|
if (errors && Object.keys(errors).length) {
|
|
56
|
-
logger.error('Error while processing
|
|
133
|
+
logger.error('Error while processing masterpass REST callback', {
|
|
57
134
|
middleware: 'masterpass-rest-callback',
|
|
58
135
|
errors,
|
|
136
|
+
requestHeaders,
|
|
59
137
|
ip
|
|
60
138
|
});
|
|
61
139
|
|
|
@@ -64,7 +142,12 @@ const withMasterpassRestCallback =
|
|
|
64
142
|
'/orders/checkout/',
|
|
65
143
|
req.cookies.get('pz-locale')?.value
|
|
66
144
|
)}`,
|
|
67
|
-
|
|
145
|
+
{
|
|
146
|
+
status: 303,
|
|
147
|
+
headers: {
|
|
148
|
+
'Set-Cookie': `pz-pos-error=${encodeURIComponent(JSON.stringify(errors))}; path=/;`
|
|
149
|
+
}
|
|
150
|
+
}
|
|
68
151
|
);
|
|
69
152
|
|
|
70
153
|
// Add error cookie
|
|
@@ -75,40 +158,70 @@ const withMasterpassRestCallback =
|
|
|
75
158
|
return errorResponse;
|
|
76
159
|
}
|
|
77
160
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
161
|
+
logger.info('Masterpass REST callback response', {
|
|
162
|
+
middleware: 'masterpass-rest-callback',
|
|
163
|
+
contextList,
|
|
164
|
+
redirectUrl,
|
|
165
|
+
ip
|
|
166
|
+
});
|
|
81
167
|
|
|
82
|
-
if (redirectUrl) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
168
|
+
if (!redirectUrl) {
|
|
169
|
+
logger.warn(
|
|
170
|
+
'No redirection url found in response. Redirecting to checkout page.',
|
|
171
|
+
{
|
|
172
|
+
middleware: 'masterpass-rest-callback',
|
|
173
|
+
requestHeaders,
|
|
174
|
+
response: JSON.stringify(response),
|
|
175
|
+
ip
|
|
176
|
+
}
|
|
86
177
|
);
|
|
87
178
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
nextResponse.headers.append('Set-Cookie', cookie.trim());
|
|
93
|
-
});
|
|
94
|
-
}
|
|
179
|
+
const redirectUrlWithLocale = `${url.origin}${getUrlPathWithLocale(
|
|
180
|
+
'/orders/checkout/',
|
|
181
|
+
req.cookies.get('pz-locale')?.value
|
|
182
|
+
)}`;
|
|
95
183
|
|
|
96
|
-
return
|
|
184
|
+
return NextResponse.redirect(redirectUrlWithLocale, 303);
|
|
97
185
|
}
|
|
98
186
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
)
|
|
187
|
+
const redirectUrlWithLocale = `${url.origin}${getUrlPathWithLocale(
|
|
188
|
+
redirectUrl,
|
|
189
|
+
req.cookies.get('pz-locale')?.value
|
|
190
|
+
)}`;
|
|
191
|
+
|
|
192
|
+
logger.info('Redirecting after masterpass REST callback', {
|
|
193
|
+
middleware: 'masterpass-rest-callback',
|
|
194
|
+
redirectUrl: redirectUrlWithLocale,
|
|
195
|
+
ip
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
const nextResponse = NextResponse.redirect(redirectUrlWithLocale, 303);
|
|
199
|
+
|
|
200
|
+
// Forward set-cookie headers from the upstream response
|
|
201
|
+
const setCookieHeader = request.headers.get('set-cookie');
|
|
202
|
+
if (setCookieHeader) {
|
|
203
|
+
setCookieHeader.split(',').forEach((cookie) => {
|
|
204
|
+
nextResponse.headers.append('Set-Cookie', cookie.trim());
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
return nextResponse;
|
|
103
209
|
} catch (error) {
|
|
104
|
-
logger.error('Error while processing
|
|
210
|
+
logger.error('Error while processing masterpass REST callback', {
|
|
105
211
|
middleware: 'masterpass-rest-callback',
|
|
106
212
|
error,
|
|
213
|
+
requestHeaders: {
|
|
214
|
+
Cookie: req.headers.get('cookie') ?? '',
|
|
215
|
+
'x-currency': req.cookies.get('pz-currency')?.value ?? ''
|
|
216
|
+
},
|
|
107
217
|
ip
|
|
108
218
|
});
|
|
109
219
|
|
|
110
220
|
return NextResponse.redirect(
|
|
111
|
-
`${url.origin}${getUrlPathWithLocale(
|
|
221
|
+
`${url.origin}${getUrlPathWithLocale(
|
|
222
|
+
'/orders/checkout/',
|
|
223
|
+
req.cookies.get('pz-locale')?.value
|
|
224
|
+
)}`,
|
|
112
225
|
303
|
|
113
226
|
);
|
|
114
227
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@akinon/next",
|
|
3
3
|
"description": "Core package for Project Zero Next",
|
|
4
|
-
"version": "2.0.6
|
|
4
|
+
"version": "2.0.6",
|
|
5
5
|
"private": false,
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"bin": {
|
|
@@ -19,11 +19,10 @@
|
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"@mongodb-js/zstd": "^2.0.1",
|
|
21
21
|
"@neshca/cache-handler": "1.9.0",
|
|
22
|
-
"@opentelemetry/
|
|
23
|
-
"@opentelemetry/
|
|
24
|
-
"@opentelemetry/
|
|
25
|
-
"@opentelemetry/sdk-trace-
|
|
26
|
-
"@opentelemetry/semantic-conventions": "1.19.0",
|
|
22
|
+
"@opentelemetry/api": "^1.9.0",
|
|
23
|
+
"@opentelemetry/context-async-hooks": "^2.5.0",
|
|
24
|
+
"@opentelemetry/core": "^2.5.0",
|
|
25
|
+
"@opentelemetry/sdk-trace-base": "^2.5.0",
|
|
27
26
|
"@reduxjs/toolkit": "1.9.7",
|
|
28
27
|
"@sentry/nextjs": "10.39.0",
|
|
29
28
|
"cross-spawn": "7.0.3",
|
|
@@ -37,7 +36,7 @@
|
|
|
37
36
|
"set-cookie-parser": "2.6.0"
|
|
38
37
|
},
|
|
39
38
|
"devDependencies": {
|
|
40
|
-
"@akinon/eslint-plugin-projectzero": "2.0.6
|
|
39
|
+
"@akinon/eslint-plugin-projectzero": "2.0.6",
|
|
41
40
|
"@babel/core": "7.26.10",
|
|
42
41
|
"@babel/preset-env": "7.26.9",
|
|
43
42
|
"@babel/preset-typescript": "7.27.0",
|
package/plugins.d.ts
CHANGED
|
@@ -37,16 +37,6 @@ declare module '@akinon/pz-cybersource-uc/src/redux/middleware' {
|
|
|
37
37
|
export default middleware as any;
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
declare module '@akinon/pz-apple-pay' {}
|
|
41
|
-
|
|
42
|
-
declare module '@akinon/pz-similar-products' {
|
|
43
|
-
export const SimilarProductsModal: any;
|
|
44
|
-
export const SimilarProductsFilterSidebar: any;
|
|
45
|
-
export const SimilarProductsResultsGrid: any;
|
|
46
|
-
export const SimilarProductsPlugin: any;
|
|
47
|
-
export const SimilarProductsButtonPlugin: any;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
40
|
declare module '@akinon/pz-cybersource-uc/src/redux/reducer' {
|
|
51
41
|
export default reducer as any;
|
|
52
42
|
}
|
package/plugins.js
CHANGED
package/redux/actions.ts
CHANGED
|
@@ -26,28 +26,13 @@ import {
|
|
|
26
26
|
} from '../../redux/reducers/checkout';
|
|
27
27
|
import { RootState, TypedDispatch } from 'redux/store';
|
|
28
28
|
import { checkoutApi } from '../../data/client/checkout';
|
|
29
|
-
import { CheckoutContext,
|
|
29
|
+
import { CheckoutContext, PreOrder } from '../../types';
|
|
30
30
|
import { getCookie } from '../../utils';
|
|
31
31
|
import settings from 'settings';
|
|
32
32
|
import { LocaleUrlStrategy } from '../../localization';
|
|
33
33
|
import { showMobile3dIframe } from '../../utils/mobile-3d-iframe';
|
|
34
34
|
import { showRedirectionIframe } from '../../utils/redirection-iframe';
|
|
35
35
|
|
|
36
|
-
const IFRAME_REDIRECTION_KEY = 'pz-iframe-redirection-active';
|
|
37
|
-
|
|
38
|
-
const isIframeRedirectionActive = () =>
|
|
39
|
-
typeof window !== 'undefined' &&
|
|
40
|
-
sessionStorage.getItem(IFRAME_REDIRECTION_KEY) === 'true';
|
|
41
|
-
|
|
42
|
-
const setIframeRedirectionActive = (active: boolean) => {
|
|
43
|
-
if (typeof window === 'undefined') return;
|
|
44
|
-
if (active) {
|
|
45
|
-
sessionStorage.setItem(IFRAME_REDIRECTION_KEY, 'true');
|
|
46
|
-
} else {
|
|
47
|
-
sessionStorage.removeItem(IFRAME_REDIRECTION_KEY);
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
|
|
51
36
|
interface CheckoutResult {
|
|
52
37
|
payload: {
|
|
53
38
|
errors?: Record<string, string[]>;
|
|
@@ -84,7 +69,7 @@ export const redirectUrlMiddleware: Middleware = () => {
|
|
|
84
69
|
const result = next(action) as CheckoutResult;
|
|
85
70
|
const redirectUrl = result?.payload?.redirect_url;
|
|
86
71
|
|
|
87
|
-
if (redirectUrl
|
|
72
|
+
if (redirectUrl) {
|
|
88
73
|
const currentLocale = getCookie('pz-locale');
|
|
89
74
|
|
|
90
75
|
let url = redirectUrl;
|
|
@@ -114,15 +99,8 @@ export const contextListMiddleware: Middleware = ({
|
|
|
114
99
|
const { isMobileApp, userPhoneNumber } = getState().root;
|
|
115
100
|
const result = next(action) as CheckoutResult;
|
|
116
101
|
const preOrder = result?.payload?.pre_order;
|
|
117
|
-
const act = action as MiddlewareAction;
|
|
118
102
|
|
|
119
103
|
if (result?.payload?.context_list) {
|
|
120
|
-
const endpointName = act.meta?.arg?.endpointName;
|
|
121
|
-
const isBinNumberResponse = endpointName === 'setBinNumber';
|
|
122
|
-
const hasCardTypeInContextList = result.payload.context_list.some(
|
|
123
|
-
(ctx) => ctx.page_context.card_type
|
|
124
|
-
);
|
|
125
|
-
|
|
126
104
|
result.payload.context_list.forEach((context) => {
|
|
127
105
|
const redirectUrl = context.page_context.redirect_url;
|
|
128
106
|
const isIframe = context.page_context.is_iframe ?? false;
|
|
@@ -156,7 +134,6 @@ export const contextListMiddleware: Middleware = ({
|
|
|
156
134
|
if (isMobileDevice && isIframePaymentOptionIncluded) {
|
|
157
135
|
showMobile3dIframe(urlObj.toString());
|
|
158
136
|
} else if (isIframe) {
|
|
159
|
-
setIframeRedirectionActive(true);
|
|
160
137
|
showRedirectionIframe(urlObj.toString());
|
|
161
138
|
} else {
|
|
162
139
|
window.location.href = urlObj.toString();
|
|
@@ -232,34 +209,15 @@ export const contextListMiddleware: Middleware = ({
|
|
|
232
209
|
(ctx) => ctx.page_name === 'DeliveryOptionSelectionPage'
|
|
233
210
|
)
|
|
234
211
|
) {
|
|
235
|
-
const isCreditCardPayment =
|
|
236
|
-
preOrder?.payment_option?.payment_type === 'credit_card' ||
|
|
237
|
-
preOrder?.payment_option?.payment_type === 'masterpass';
|
|
238
|
-
|
|
239
212
|
if (context.page_context.card_type) {
|
|
240
213
|
dispatch(setCardType(context.page_context.card_type));
|
|
241
|
-
} else if (
|
|
242
|
-
isCreditCardPayment &&
|
|
243
|
-
isBinNumberResponse &&
|
|
244
|
-
!hasCardTypeInContextList
|
|
245
|
-
) {
|
|
246
|
-
dispatch(setCardType(null));
|
|
247
|
-
dispatch(setInstallmentOptions([]));
|
|
248
214
|
}
|
|
249
215
|
|
|
250
216
|
if (
|
|
251
217
|
context.page_context.installments &&
|
|
252
218
|
preOrder?.payment_option?.payment_type !== 'masterpass_rest'
|
|
253
219
|
) {
|
|
254
|
-
|
|
255
|
-
!isCreditCardPayment ||
|
|
256
|
-
context.page_context.card_type ||
|
|
257
|
-
hasCardTypeInContextList
|
|
258
|
-
) {
|
|
259
|
-
dispatch(
|
|
260
|
-
setInstallmentOptions(context.page_context.installments)
|
|
261
|
-
);
|
|
262
|
-
}
|
|
220
|
+
dispatch(setInstallmentOptions(context.page_context.installments));
|
|
263
221
|
}
|
|
264
222
|
}
|
|
265
223
|
|
|
@@ -14,17 +14,9 @@ export const installmentOptionMiddleware: Middleware = ({
|
|
|
14
14
|
return result;
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
const { installmentOptions
|
|
17
|
+
const { installmentOptions } = getState().checkout;
|
|
18
18
|
const { endpoints: apiEndpoints } = checkoutApi;
|
|
19
19
|
|
|
20
|
-
const isCreditCardPayment =
|
|
21
|
-
preOrder?.payment_option?.payment_type === 'credit_card' ||
|
|
22
|
-
preOrder?.payment_option?.payment_type === 'masterpass';
|
|
23
|
-
|
|
24
|
-
if (isCreditCardPayment && !cardType) {
|
|
25
|
-
return result;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
20
|
if (
|
|
29
21
|
!preOrder?.installment &&
|
|
30
22
|
preOrder?.payment_option?.payment_type !== 'saved_card' &&
|
package/redux/reducers/index.ts
CHANGED
|
@@ -2,6 +2,7 @@ import rootReducer from './root';
|
|
|
2
2
|
import checkoutReducer from './checkout';
|
|
3
3
|
import configReducer from './config';
|
|
4
4
|
import headerReducer from './header';
|
|
5
|
+
import toastReducer from './toast';
|
|
5
6
|
import widgetReducer from './widget';
|
|
6
7
|
import { api } from '../../data/client/api';
|
|
7
8
|
|
|
@@ -20,6 +21,7 @@ const reducers = {
|
|
|
20
21
|
checkout: checkoutReducer,
|
|
21
22
|
config: configReducer,
|
|
22
23
|
header: headerReducer,
|
|
24
|
+
toast: toastReducer,
|
|
23
25
|
widget: widgetReducer,
|
|
24
26
|
masterpass: masterpassReducer || fallbackReducer,
|
|
25
27
|
otp: otpReducer || fallbackReducer,
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { createSlice, PayloadAction } from '@reduxjs/toolkit'
|
|
2
|
+
|
|
3
|
+
export type ToastType = 'success' | 'error' | 'warning' | 'info'
|
|
4
|
+
|
|
5
|
+
export type ToastPosition =
|
|
6
|
+
| 'top-right'
|
|
7
|
+
| 'top-left'
|
|
8
|
+
| 'top-center'
|
|
9
|
+
| 'bottom-right'
|
|
10
|
+
| 'bottom-left'
|
|
11
|
+
| 'bottom-center'
|
|
12
|
+
|
|
13
|
+
export interface Toast {
|
|
14
|
+
id: string
|
|
15
|
+
type: ToastType
|
|
16
|
+
message: string
|
|
17
|
+
duration?: number
|
|
18
|
+
position?: ToastPosition
|
|
19
|
+
icon?: string
|
|
20
|
+
className?: string
|
|
21
|
+
dismissible?: boolean
|
|
22
|
+
action?: {
|
|
23
|
+
label: string
|
|
24
|
+
actionId: string
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export type ToastInput = Omit<Toast, 'id'>
|
|
29
|
+
|
|
30
|
+
export interface ToastState {
|
|
31
|
+
toasts: Toast[]
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const MAX_TOASTS = 5
|
|
35
|
+
|
|
36
|
+
const initialState: ToastState = {
|
|
37
|
+
toasts: []
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const toastSlice = createSlice({
|
|
41
|
+
name: 'toast',
|
|
42
|
+
initialState,
|
|
43
|
+
reducers: {
|
|
44
|
+
addToast: {
|
|
45
|
+
reducer: (state, action: PayloadAction<Toast>) => {
|
|
46
|
+
state.toasts.push(action.payload)
|
|
47
|
+
if (state.toasts.length > MAX_TOASTS) {
|
|
48
|
+
state.toasts.shift()
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
prepare: (toast: ToastInput) => ({
|
|
52
|
+
payload: {
|
|
53
|
+
...toast,
|
|
54
|
+
id: `toast-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`,
|
|
55
|
+
duration: toast.duration ?? 4000,
|
|
56
|
+
position: toast.position ?? 'top-right',
|
|
57
|
+
dismissible: toast.dismissible ?? true
|
|
58
|
+
}
|
|
59
|
+
})
|
|
60
|
+
},
|
|
61
|
+
removeToast: (state, action: PayloadAction<string>) => {
|
|
62
|
+
const idx = state.toasts.findIndex((t) => t.id === action.payload)
|
|
63
|
+
if (idx !== -1) state.toasts.splice(idx, 1)
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
export const { addToast, removeToast } = toastSlice.actions
|
|
69
|
+
|
|
70
|
+
export default toastSlice.reducer
|
package/types/index.ts
CHANGED
|
@@ -85,12 +85,6 @@ export interface Settings {
|
|
|
85
85
|
};
|
|
86
86
|
usePrettyUrlRoute?: boolean;
|
|
87
87
|
commerceUrl: string;
|
|
88
|
-
/**
|
|
89
|
-
* This option allows you to track Sentry events on the client side, in addition to server and edge environments.
|
|
90
|
-
*
|
|
91
|
-
* It overrides process.env.NEXT_PUBLIC_SENTRY_DSN and process.env.SENTRY_DSN.
|
|
92
|
-
*/
|
|
93
|
-
sentryDsn?: string;
|
|
94
88
|
redis: {
|
|
95
89
|
defaultExpirationTime: number;
|
|
96
90
|
};
|
|
@@ -223,7 +217,6 @@ export interface Settings {
|
|
|
223
217
|
separator?: string;
|
|
224
218
|
segments: PzSegmentDefinition[];
|
|
225
219
|
};
|
|
226
|
-
payloadOptimization?: import('../utils/payload-optimizer').PayloadOptimizationConfig;
|
|
227
220
|
}
|
|
228
221
|
|
|
229
222
|
export interface CacheOptions {
|
package/utils/app-fetch.ts
CHANGED
|
@@ -3,6 +3,7 @@ import logger from '../utils/log';
|
|
|
3
3
|
import { headers, cookies } from 'next/headers';
|
|
4
4
|
import { ServerVariables } from './server-variables';
|
|
5
5
|
import { notFound } from 'next/navigation';
|
|
6
|
+
import { fixtureManager, MockMode } from '../lib/fixture-manager';
|
|
6
7
|
|
|
7
8
|
export enum FetchResponseType {
|
|
8
9
|
JSON = 'json',
|
|
@@ -42,6 +43,23 @@ const appFetch = async <T>({
|
|
|
42
43
|
}
|
|
43
44
|
|
|
44
45
|
const requestURL = `${decodeURIComponent(commerceUrl)}${url}`;
|
|
46
|
+
const mockMode = process.env.PZ_MOCK;
|
|
47
|
+
const method = init.method?.toUpperCase() ?? 'GET';
|
|
48
|
+
|
|
49
|
+
// Replay mode: serve from fixtures without hitting the API
|
|
50
|
+
if (mockMode === MockMode.REPLAY) {
|
|
51
|
+
const { found, fixture } = await fixtureManager.read(method, String(url), init.body);
|
|
52
|
+
|
|
53
|
+
if (found) {
|
|
54
|
+
status = fixture.response.status;
|
|
55
|
+
response = (responseType === FetchResponseType.JSON
|
|
56
|
+
? fixture.response.body
|
|
57
|
+
: JSON.stringify(fixture.response.body)) as T;
|
|
58
|
+
return response;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
45
63
|
|
|
46
64
|
init.headers = {
|
|
47
65
|
cookie: nextCookies.toString(),
|
|
@@ -67,6 +85,15 @@ const appFetch = async <T>({
|
|
|
67
85
|
response = (await req.text()) as unknown as T;
|
|
68
86
|
}
|
|
69
87
|
|
|
88
|
+
// Record mode: save response to fixtures
|
|
89
|
+
if (mockMode === MockMode.RECORD) {
|
|
90
|
+
await fixtureManager.write(method, String(url), init.body, {
|
|
91
|
+
status: req.status,
|
|
92
|
+
headers: fixtureManager.extractHeaders(req.headers),
|
|
93
|
+
body: response
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
|
|
70
97
|
logger.trace(`FETCH RESPONSE`, { url, response, ip });
|
|
71
98
|
} catch (error) {
|
|
72
99
|
const logType = status === 500 ? 'fatal' : 'error';
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export const formatErrorMessage = (errors: any): string => {
|
|
2
|
+
if (typeof errors === 'string') return errors
|
|
3
|
+
if (Array.isArray(errors)) return errors.join(', ')
|
|
4
|
+
if (typeof errors === 'object' && errors !== null)
|
|
5
|
+
return Object.values(errors).flat().join(', ')
|
|
6
|
+
return 'An error occurred'
|
|
7
|
+
}
|
package/utils/index.ts
CHANGED
|
@@ -9,6 +9,7 @@ export * from './generate-commerce-search-params';
|
|
|
9
9
|
export * from './get-currency-label';
|
|
10
10
|
export * from './pz-segments';
|
|
11
11
|
export * from './get-checkout-path';
|
|
12
|
+
export * from './format-error-message';
|
|
12
13
|
|
|
13
14
|
export function getCookie(name: string) {
|
|
14
15
|
if (typeof document === 'undefined') {
|
|
@@ -62,7 +63,13 @@ export function setCookie(
|
|
|
62
63
|
export function removeCookie(name: string) {
|
|
63
64
|
const date = 'Thu, 01 Jan 1970 00:00:00 UTC';
|
|
64
65
|
|
|
65
|
-
|
|
66
|
+
const domain =
|
|
67
|
+
settings.localization.localeUrlStrategy === LocaleUrlStrategy.Subdomain
|
|
68
|
+
? getRootHostname(document.location.href)
|
|
69
|
+
: null;
|
|
70
|
+
|
|
71
|
+
const domainStr = domain ? ` domain=${domain};` : '';
|
|
72
|
+
document.cookie = `${name}=; expires=${date}; path=/;${domainStr}`;
|
|
66
73
|
}
|
|
67
74
|
|
|
68
75
|
/**
|
package/with-pz-config.js
CHANGED
|
@@ -12,7 +12,8 @@ const defaultConfig = {
|
|
|
12
12
|
output: 'standalone',
|
|
13
13
|
compress: false,
|
|
14
14
|
env: {
|
|
15
|
-
NEXT_PUBLIC_SENTRY_DSN: process.env.SENTRY_DSN
|
|
15
|
+
NEXT_PUBLIC_SENTRY_DSN: process.env.SENTRY_DSN,
|
|
16
|
+
...(process.env.PZ_MOCK && { PZ_MOCK: process.env.PZ_MOCK })
|
|
16
17
|
},
|
|
17
18
|
images: {
|
|
18
19
|
remotePatterns: [
|
|
@@ -68,8 +69,7 @@ const defaultConfig = {
|
|
|
68
69
|
acc[`@akinon/${plugin}`] = false;
|
|
69
70
|
return acc;
|
|
70
71
|
}, {}),
|
|
71
|
-
translations: false
|
|
72
|
-
'@opentelemetry/exporter-jaeger': false
|
|
72
|
+
translations: false
|
|
73
73
|
};
|
|
74
74
|
// Ensure webpack can resolve deps from the app's node_modules when
|
|
75
75
|
// compiling transpiled packages (e.g. @akinon/next) whose imports
|