@akinon/next 2.0.0-beta.12 → 2.0.0-beta.13
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 +282 -29
- package/api/auth.ts +99 -77
- package/api/cache.ts +41 -5
- package/api/client.ts +3 -3
- package/api/form.ts +85 -0
- package/api/image-proxy.ts +75 -0
- package/api/product-categories.ts +53 -0
- package/api/similar-product-list.ts +63 -0
- package/api/similar-products.ts +111 -0
- package/api/virtual-try-on.ts +382 -0
- package/bin/pz-generate-routes.js +105 -0
- package/bin/pz-prebuild.js +1 -1
- package/bin/pz-predev.js +1 -0
- package/components/accordion.tsx +21 -6
- package/components/button.tsx +1 -1
- package/components/file-input.tsx +65 -3
- package/components/input.tsx +2 -2
- package/components/modal.tsx +32 -16
- package/components/plugin-module.tsx +61 -3
- package/components/select.tsx +2 -2
- package/components/selected-payment-option-view.tsx +21 -0
- package/data/client/checkout.ts +130 -74
- package/data/server/category.ts +11 -9
- package/data/server/flatpage.ts +4 -1
- package/data/server/form.ts +4 -1
- package/data/server/landingpage.ts +4 -1
- package/data/server/list.ts +5 -4
- package/data/server/menu.ts +4 -1
- package/data/server/product.ts +97 -52
- package/data/server/seo.ts +4 -1
- package/data/server/special-page.ts +5 -4
- package/data/server/widget.ts +4 -1
- package/data/urls.ts +3 -2
- package/hocs/client/with-segment-defaults.tsx +2 -2
- package/hocs/server/with-segment-defaults.tsx +65 -20
- package/hooks/index.ts +1 -0
- package/hooks/use-loyalty-availability.ts +21 -0
- package/hooks/use-payment-options.ts +2 -1
- package/hooks/use-pz-params.ts +37 -0
- package/instrumentation/index.ts +0 -1
- package/instrumentation/node.ts +2 -20
- package/jest.config.js +7 -1
- package/lib/cache-handler.mjs +527 -15
- package/lib/cache.ts +260 -31
- package/localization/provider.tsx +2 -5
- package/middlewares/checkout-provider.ts +1 -1
- package/middlewares/complete-gpay.ts +33 -26
- package/middlewares/complete-masterpass.ts +34 -26
- package/middlewares/complete-wallet.ts +183 -0
- package/middlewares/default.ts +346 -235
- package/middlewares/index.ts +8 -2
- package/middlewares/locale.ts +0 -1
- package/middlewares/masterpass-rest-callback.ts +220 -0
- package/middlewares/pretty-url.ts +21 -8
- package/middlewares/redirection-payment.ts +33 -26
- package/middlewares/saved-card-redirection.ts +34 -26
- package/middlewares/three-d-redirection.ts +33 -26
- package/middlewares/url-redirection.ts +9 -15
- package/middlewares/wallet-complete-redirection.ts +207 -0
- package/package.json +20 -11
- package/plugins.d.ts +19 -4
- package/plugins.js +9 -1
- package/redux/actions.ts +47 -0
- package/redux/middlewares/checkout.ts +20 -8
- package/redux/middlewares/index.ts +12 -10
- package/redux/middlewares/pre-order/address.ts +1 -1
- package/redux/middlewares/pre-order/attribute-based-shipping-option.ts +1 -1
- package/redux/middlewares/pre-order/data-source-shipping-option.ts +1 -1
- package/redux/middlewares/pre-order/delivery-option.ts +1 -1
- package/redux/middlewares/pre-order/index.ts +3 -1
- package/redux/middlewares/pre-order/installment-option.ts +2 -1
- package/redux/middlewares/pre-order/payment-option-reset.ts +37 -0
- package/redux/middlewares/pre-order/payment-option.ts +1 -1
- package/redux/middlewares/pre-order/pre-order-validation.ts +4 -3
- package/redux/middlewares/pre-order/redirection.ts +2 -2
- package/redux/middlewares/pre-order/set-pre-order.ts +2 -2
- package/redux/middlewares/pre-order/shipping-option.ts +1 -1
- package/redux/middlewares/pre-order/shipping-step.ts +1 -1
- package/redux/reducers/checkout.ts +9 -1
- package/redux/reducers/index.ts +5 -1
- package/sentry/index.ts +54 -17
- package/types/commerce/checkout.ts +11 -1
- package/types/index.ts +96 -6
- package/types/next-auth.d.ts +2 -2
- package/utils/app-fetch.ts +2 -2
- package/utils/generate-commerce-search-params.ts +3 -2
- package/utils/get-checkout-path.ts +3 -0
- package/utils/index.ts +38 -11
- package/utils/override-middleware.ts +1 -0
- package/utils/pz-segments.ts +92 -0
- package/utils/redirect-ignore.ts +35 -0
- package/utils/redirect.ts +9 -3
- package/with-pz-config.js +10 -4
|
@@ -4,6 +4,8 @@ import { Buffer } from 'buffer';
|
|
|
4
4
|
import logger from '../utils/log';
|
|
5
5
|
import { getUrlPathWithLocale } from '../utils/localization';
|
|
6
6
|
import { PzNextRequest } from '.';
|
|
7
|
+
import { ServerVariables } from '../utils/server-variables';
|
|
8
|
+
import { getCheckoutPath } from '../utils';
|
|
7
9
|
|
|
8
10
|
const streamToString = async (stream: ReadableStream<Uint8Array> | null) => {
|
|
9
11
|
if (stream) {
|
|
@@ -34,18 +36,22 @@ const withCompleteMasterpass =
|
|
|
34
36
|
const url = req.nextUrl.clone();
|
|
35
37
|
const ip = req.headers.get('x-forwarded-for') ?? '';
|
|
36
38
|
const sessionId = req.cookies.get('osessionid');
|
|
39
|
+
const currentLocale = req.middlewareParams?.rewrites?.locale;
|
|
37
40
|
|
|
38
41
|
if (url.search.indexOf('MasterpassCompletePage') === -1) {
|
|
39
42
|
return middleware(req, event);
|
|
40
43
|
}
|
|
41
44
|
|
|
42
|
-
const
|
|
45
|
+
const isPostCheckout = req.cookies.get('pz-post-checkout-flow')?.value === 'true';
|
|
46
|
+
const requestUrl = `${Settings.commerceUrl}${getCheckoutPath(isPostCheckout)}${url.search}`;
|
|
43
47
|
const requestHeaders = {
|
|
44
48
|
'X-Requested-With': 'XMLHttpRequest',
|
|
45
49
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
46
50
|
Cookie: req.headers.get('cookie') ?? '',
|
|
47
51
|
'x-currency': req.cookies.get('pz-currency')?.value ?? '',
|
|
48
|
-
'x-forwarded-for': ip
|
|
52
|
+
'x-forwarded-for': ip,
|
|
53
|
+
'Accept-Language':
|
|
54
|
+
currentLocale ?? req.cookies.get('pz-locale')?.value ?? ''
|
|
49
55
|
};
|
|
50
56
|
|
|
51
57
|
try {
|
|
@@ -59,17 +65,9 @@ const withCompleteMasterpass =
|
|
|
59
65
|
ip
|
|
60
66
|
}
|
|
61
67
|
);
|
|
62
|
-
|
|
63
|
-
return NextResponse.redirect(
|
|
64
|
-
`${url.origin}${getUrlPathWithLocale(
|
|
65
|
-
'/orders/checkout/',
|
|
66
|
-
req.cookies.get('pz-locale')?.value
|
|
67
|
-
)}`,
|
|
68
|
-
303
|
|
69
|
-
);
|
|
70
68
|
}
|
|
71
69
|
|
|
72
|
-
const
|
|
70
|
+
const fetchResponse = await fetch(requestUrl, {
|
|
73
71
|
method: 'POST',
|
|
74
72
|
headers: requestHeaders,
|
|
75
73
|
body
|
|
@@ -77,14 +75,14 @@ const withCompleteMasterpass =
|
|
|
77
75
|
|
|
78
76
|
logger.info('Complete Masterpass payment request', {
|
|
79
77
|
requestUrl,
|
|
80
|
-
status:
|
|
78
|
+
status: fetchResponse.status,
|
|
81
79
|
requestHeaders,
|
|
82
80
|
ip
|
|
83
81
|
});
|
|
84
82
|
|
|
85
|
-
const
|
|
83
|
+
const responseData = await fetchResponse.json();
|
|
86
84
|
|
|
87
|
-
const { context_list: contextList, errors } =
|
|
85
|
+
const { context_list: contextList, errors } = responseData;
|
|
88
86
|
const redirectionContext = contextList?.find(
|
|
89
87
|
(context) => context.page_context?.redirect_url
|
|
90
88
|
);
|
|
@@ -98,18 +96,27 @@ const withCompleteMasterpass =
|
|
|
98
96
|
ip
|
|
99
97
|
});
|
|
100
98
|
|
|
101
|
-
|
|
99
|
+
const errorResponse = NextResponse.redirect(
|
|
102
100
|
`${url.origin}${getUrlPathWithLocale(
|
|
103
101
|
'/orders/checkout/',
|
|
104
102
|
req.cookies.get('pz-locale')?.value
|
|
105
103
|
)}`,
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
104
|
+
303
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
// Forward set-cookie headers from the upstream response
|
|
108
|
+
const setCookies = fetchResponse.headers.getSetCookie?.() ?? [];
|
|
109
|
+
setCookies.forEach((cookie) => {
|
|
110
|
+
errorResponse.headers.append('Set-Cookie', cookie);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
// Add error cookie
|
|
114
|
+
errorResponse.headers.append(
|
|
115
|
+
'Set-Cookie',
|
|
116
|
+
`pz-pos-error=${JSON.stringify(errors)}; path=/;`
|
|
112
117
|
);
|
|
118
|
+
|
|
119
|
+
return errorResponse;
|
|
113
120
|
}
|
|
114
121
|
|
|
115
122
|
logger.info('Order success page context list', {
|
|
@@ -124,7 +131,7 @@ const withCompleteMasterpass =
|
|
|
124
131
|
{
|
|
125
132
|
middleware: 'complete-masterpass',
|
|
126
133
|
requestHeaders,
|
|
127
|
-
response: JSON.stringify(
|
|
134
|
+
response: JSON.stringify(responseData),
|
|
128
135
|
ip
|
|
129
136
|
}
|
|
130
137
|
);
|
|
@@ -152,10 +159,11 @@ const withCompleteMasterpass =
|
|
|
152
159
|
// So we use 303 status code to change the method to GET
|
|
153
160
|
const nextResponse = NextResponse.redirect(redirectUrlWithLocale, 303);
|
|
154
161
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
162
|
+
// Forward all set-cookie headers from the upstream response
|
|
163
|
+
const setCookies = fetchResponse.headers.getSetCookie?.() ?? [];
|
|
164
|
+
setCookies.forEach((cookie) => {
|
|
165
|
+
nextResponse.headers.append('Set-Cookie', cookie);
|
|
166
|
+
});
|
|
159
167
|
|
|
160
168
|
return nextResponse;
|
|
161
169
|
} catch (error) {
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { NextFetchEvent, NextMiddleware, NextResponse } from 'next/server';
|
|
2
|
+
import Settings from 'settings';
|
|
3
|
+
import { Buffer } from 'buffer';
|
|
4
|
+
import logger from '../utils/log';
|
|
5
|
+
import { getUrlPathWithLocale } from '../utils/localization';
|
|
6
|
+
import { PzNextRequest } from '.';
|
|
7
|
+
import { getCheckoutPath } from '../utils';
|
|
8
|
+
|
|
9
|
+
const streamToString = async (stream: ReadableStream<Uint8Array> | null) => {
|
|
10
|
+
if (stream) {
|
|
11
|
+
const chunks = [];
|
|
12
|
+
let result = '';
|
|
13
|
+
|
|
14
|
+
try {
|
|
15
|
+
for await (const chunk of stream as any) {
|
|
16
|
+
chunks.push(Buffer.from(chunk));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
result = Buffer.concat(chunks).toString('utf-8');
|
|
20
|
+
} catch (error) {
|
|
21
|
+
logger.error('Error while reading body stream', {
|
|
22
|
+
middleware: 'complete-wallet',
|
|
23
|
+
error
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return result;
|
|
28
|
+
}
|
|
29
|
+
return null;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const withCompleteWallet =
|
|
33
|
+
(middleware: NextMiddleware) =>
|
|
34
|
+
async (req: PzNextRequest, event: NextFetchEvent) => {
|
|
35
|
+
const url = req.nextUrl.clone();
|
|
36
|
+
const ip = req.headers.get('x-forwarded-for') ?? '';
|
|
37
|
+
const sessionId = req.cookies.get('osessionid');
|
|
38
|
+
|
|
39
|
+
if (url.search.indexOf('WalletRedirectCompletePage') === -1) {
|
|
40
|
+
return middleware(req, event);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const isPostCheckout = req.cookies.get('pz-post-checkout-flow')?.value === 'true';
|
|
44
|
+
const requestUrl = `${Settings.commerceUrl}${getCheckoutPath(isPostCheckout)}${url.search}`;
|
|
45
|
+
const requestHeaders = {
|
|
46
|
+
'X-Requested-With': 'XMLHttpRequest',
|
|
47
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
48
|
+
Cookie: req.headers.get('cookie') ?? '',
|
|
49
|
+
'x-currency': req.cookies.get('pz-currency')?.value ?? '',
|
|
50
|
+
'x-forwarded-for': ip
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
try {
|
|
54
|
+
const body = await streamToString(req.body);
|
|
55
|
+
|
|
56
|
+
if (!sessionId) {
|
|
57
|
+
logger.warn(
|
|
58
|
+
'Make sure that the SESSION_COOKIE_SAMESITE environment variable is set to None in Commerce.',
|
|
59
|
+
{
|
|
60
|
+
middleware: 'complete-wallet',
|
|
61
|
+
ip
|
|
62
|
+
}
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const fetchResponse = await fetch(requestUrl, {
|
|
67
|
+
method: 'POST',
|
|
68
|
+
headers: requestHeaders,
|
|
69
|
+
body
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
logger.info('Complete Wallet payment request', {
|
|
73
|
+
requestUrl,
|
|
74
|
+
status: fetchResponse.status,
|
|
75
|
+
requestHeaders,
|
|
76
|
+
ip
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
const responseData = await fetchResponse.json();
|
|
80
|
+
|
|
81
|
+
const { context_list: contextList, errors } = responseData;
|
|
82
|
+
const redirectionContext = contextList?.find(
|
|
83
|
+
(context) => context.page_context?.redirect_url
|
|
84
|
+
);
|
|
85
|
+
const redirectUrl = redirectionContext?.page_context?.redirect_url;
|
|
86
|
+
|
|
87
|
+
if (errors && Object.keys(errors).length) {
|
|
88
|
+
logger.error('Error while completing Wallet payment', {
|
|
89
|
+
middleware: 'complete-wallet',
|
|
90
|
+
errors,
|
|
91
|
+
requestHeaders,
|
|
92
|
+
ip
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
const errorResponse = NextResponse.redirect(
|
|
96
|
+
`${url.origin}${getUrlPathWithLocale(
|
|
97
|
+
'/orders/checkout/',
|
|
98
|
+
req.cookies.get('pz-locale')?.value
|
|
99
|
+
)}`,
|
|
100
|
+
303
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
// Forward set-cookie headers from the upstream response
|
|
104
|
+
const setCookies = fetchResponse.headers.getSetCookie?.() ?? [];
|
|
105
|
+
setCookies.forEach((cookie) => {
|
|
106
|
+
errorResponse.headers.append('Set-Cookie', cookie);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
// Add error cookie
|
|
110
|
+
errorResponse.headers.append(
|
|
111
|
+
'Set-Cookie',
|
|
112
|
+
`pz-pos-error=${JSON.stringify(errors)}; path=/;`
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
return errorResponse;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
logger.info('Order success page context list', {
|
|
119
|
+
middleware: 'complete-wallet',
|
|
120
|
+
contextList,
|
|
121
|
+
ip
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
if (!redirectUrl) {
|
|
125
|
+
logger.warn(
|
|
126
|
+
'No redirection url for order success page found in page_context. Redirecting to checkout page.',
|
|
127
|
+
{
|
|
128
|
+
middleware: 'complete-wallet',
|
|
129
|
+
requestHeaders,
|
|
130
|
+
response: JSON.stringify(responseData),
|
|
131
|
+
ip
|
|
132
|
+
}
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
const redirectUrlWithLocale = `${url.origin}${getUrlPathWithLocale(
|
|
136
|
+
'/orders/checkout/',
|
|
137
|
+
req.cookies.get('pz-locale')?.value
|
|
138
|
+
)}`;
|
|
139
|
+
|
|
140
|
+
return NextResponse.redirect(redirectUrlWithLocale, 303);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const redirectUrlWithLocale = `${url.origin}${getUrlPathWithLocale(
|
|
144
|
+
redirectUrl,
|
|
145
|
+
req.cookies.get('pz-locale')?.value
|
|
146
|
+
)}`;
|
|
147
|
+
|
|
148
|
+
logger.info('Redirecting to order success page', {
|
|
149
|
+
middleware: 'complete-wallet',
|
|
150
|
+
redirectUrlWithLocale,
|
|
151
|
+
ip
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
// Using POST method while redirecting causes an error,
|
|
155
|
+
// So we use 303 status code to change the method to GET
|
|
156
|
+
const nextResponse = NextResponse.redirect(redirectUrlWithLocale, 303);
|
|
157
|
+
|
|
158
|
+
// Forward all set-cookie headers from the upstream response
|
|
159
|
+
const setCookies = fetchResponse.headers.getSetCookie?.() ?? [];
|
|
160
|
+
setCookies.forEach((cookie) => {
|
|
161
|
+
nextResponse.headers.append('Set-Cookie', cookie);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
return nextResponse;
|
|
165
|
+
} catch (error) {
|
|
166
|
+
logger.error('Error while completing Wallet payment', {
|
|
167
|
+
middleware: 'complete-wallet',
|
|
168
|
+
error,
|
|
169
|
+
requestHeaders,
|
|
170
|
+
ip
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
return NextResponse.redirect(
|
|
174
|
+
`${url.origin}${getUrlPathWithLocale(
|
|
175
|
+
'/orders/checkout/',
|
|
176
|
+
req.cookies.get('pz-locale')?.value
|
|
177
|
+
)}`,
|
|
178
|
+
303
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
export default withCompleteWallet;
|