@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
package/middlewares/default.ts
CHANGED
|
@@ -11,9 +11,13 @@ import {
|
|
|
11
11
|
withRedirectionPayment,
|
|
12
12
|
withSavedCardRedirection,
|
|
13
13
|
withThreeDRedirection,
|
|
14
|
-
withUrlRedirection
|
|
14
|
+
withUrlRedirection,
|
|
15
|
+
withCompleteWallet,
|
|
16
|
+
withWalletCompleteRedirection,
|
|
17
|
+
withMasterpassRestCallback
|
|
15
18
|
} from '.';
|
|
16
19
|
import { urlLocaleMatcherRegex } from '../utils';
|
|
20
|
+
import { getPzSegmentsConfig, encodePzValue, isLegacyMode } from '../utils/pz-segments';
|
|
17
21
|
import withCurrency from './currency';
|
|
18
22
|
import withLocale from './locale';
|
|
19
23
|
import logger from '../utils/log';
|
|
@@ -22,6 +26,8 @@ import { getUrlPathWithLocale } from '../utils/localization';
|
|
|
22
26
|
import getRootHostname from '../utils/get-root-hostname';
|
|
23
27
|
import { LocaleUrlStrategy } from '../localization';
|
|
24
28
|
|
|
29
|
+
const POST_CHECKOUT_COOKIE_MAX_AGE_MS = 1000 * 60 * 30; // 30 minutes
|
|
30
|
+
|
|
25
31
|
const withPzDefault =
|
|
26
32
|
(middleware: NextMiddleware) =>
|
|
27
33
|
async (req: PzNextRequest, event: NextFetchEvent) => {
|
|
@@ -98,6 +104,7 @@ const withPzDefault =
|
|
|
98
104
|
if (
|
|
99
105
|
req.nextUrl.pathname.includes('/orders/hooks/') ||
|
|
100
106
|
req.nextUrl.pathname.includes('/orders/checkout-with-token/') ||
|
|
107
|
+
req.nextUrl.pathname.includes('/orders/post-checkout-redirect/') ||
|
|
101
108
|
req.nextUrl.pathname.includes('/hooks/cash_register/complete/') ||
|
|
102
109
|
req.nextUrl.pathname.includes('/hooks/cash_register/pre_order/')
|
|
103
110
|
) {
|
|
@@ -144,21 +151,35 @@ const withPzDefault =
|
|
|
144
151
|
);
|
|
145
152
|
}
|
|
146
153
|
|
|
147
|
-
// If commerce redirects to /orders/checkout/ without locale
|
|
154
|
+
// If commerce redirects to /orders/checkout/ or /orders/post-checkout/ without locale
|
|
155
|
+
const isPostCheckout = !!req.nextUrl.pathname.match(
|
|
156
|
+
new RegExp('^/orders/post-checkout/$')
|
|
157
|
+
);
|
|
158
|
+
const checkoutLocalePath = getUrlPathWithLocale(
|
|
159
|
+
'/orders/checkout/',
|
|
160
|
+
req.cookies.get('pz-locale')?.value
|
|
161
|
+
);
|
|
148
162
|
if (
|
|
149
|
-
req.nextUrl.
|
|
150
|
-
req.nextUrl.
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
req.cookies.get('pz-locale')?.value
|
|
154
|
-
) !== req.nextUrl.pathname
|
|
163
|
+
(isPostCheckout && req.nextUrl.searchParams.size === 0) ||
|
|
164
|
+
(req.nextUrl.pathname.match(new RegExp('^/orders/checkout/$')) &&
|
|
165
|
+
req.nextUrl.searchParams.size === 0 &&
|
|
166
|
+
checkoutLocalePath !== req.nextUrl.pathname)
|
|
155
167
|
) {
|
|
156
|
-
const
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
)
|
|
168
|
+
const response = NextResponse.redirect(
|
|
169
|
+
`${url.origin}${checkoutLocalePath}`,
|
|
170
|
+
303
|
|
171
|
+
);
|
|
160
172
|
|
|
161
|
-
|
|
173
|
+
if (isPostCheckout) {
|
|
174
|
+
response.cookies.set('pz-post-checkout-flow', 'true', {
|
|
175
|
+
path: '/',
|
|
176
|
+
sameSite: 'none',
|
|
177
|
+
secure: true,
|
|
178
|
+
expires: new Date(Date.now() + POST_CHECKOUT_COOKIE_MAX_AGE_MS)
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return response;
|
|
162
183
|
}
|
|
163
184
|
|
|
164
185
|
// Dynamically handle any payment gateway without specifying names
|
|
@@ -213,7 +234,6 @@ const withPzDefault =
|
|
|
213
234
|
|
|
214
235
|
req.middlewareParams = {
|
|
215
236
|
commerceUrl,
|
|
216
|
-
found: true,
|
|
217
237
|
rewrites: {}
|
|
218
238
|
};
|
|
219
239
|
|
|
@@ -228,229 +248,320 @@ const withPzDefault =
|
|
|
228
248
|
withCompleteGpay(
|
|
229
249
|
withCompleteMasterpass(
|
|
230
250
|
withSavedCardRedirection(
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
const hostname =
|
|
353
|
-
process.env.NEXT_PUBLIC_URL ||
|
|
354
|
-
`https://${fallbackHost}`;
|
|
355
|
-
const rootHostname =
|
|
356
|
-
localeUrlStrategy ===
|
|
357
|
-
LocaleUrlStrategy.Subdomain
|
|
358
|
-
? getRootHostname(hostname)
|
|
359
|
-
: null;
|
|
360
|
-
|
|
361
|
-
if (
|
|
362
|
-
!url.pathname.startsWith(`/${currency}/orders`)
|
|
363
|
-
) {
|
|
364
|
-
middlewareResult.cookies.set(
|
|
365
|
-
'pz-locale',
|
|
366
|
-
locale?.length > 0
|
|
367
|
-
? locale
|
|
368
|
-
: defaultLocaleValue,
|
|
369
|
-
{
|
|
370
|
-
domain: rootHostname,
|
|
371
|
-
sameSite: 'none',
|
|
372
|
-
secure: true,
|
|
373
|
-
expires: new Date(
|
|
374
|
-
Date.now() + 1000 * 60 * 60 * 24 * 7
|
|
375
|
-
) // 7 days
|
|
376
|
-
}
|
|
377
|
-
);
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
middlewareResult.cookies.set(
|
|
381
|
-
'pz-currency',
|
|
382
|
-
currency,
|
|
383
|
-
{
|
|
384
|
-
domain: rootHostname,
|
|
385
|
-
sameSite: 'none',
|
|
386
|
-
secure: true,
|
|
387
|
-
expires: new Date(
|
|
388
|
-
Date.now() + 1000 * 60 * 60 * 24 * 7
|
|
389
|
-
) // 7 days
|
|
390
|
-
}
|
|
391
|
-
);
|
|
392
|
-
|
|
393
|
-
if (
|
|
394
|
-
req.cookies.get('pz-locale') &&
|
|
395
|
-
req.cookies.get('pz-locale').value !== locale
|
|
396
|
-
) {
|
|
397
|
-
logger.debug('Locale changed', {
|
|
398
|
-
locale,
|
|
399
|
-
oldLocale:
|
|
400
|
-
req.cookies.get('pz-locale')?.value,
|
|
401
|
-
ip
|
|
402
|
-
});
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
middlewareResult.headers.set(
|
|
406
|
-
'pz-url',
|
|
407
|
-
req.nextUrl.toString()
|
|
408
|
-
);
|
|
409
|
-
|
|
410
|
-
if (req.cookies.get('pz-set-currency')) {
|
|
411
|
-
middlewareResult.cookies.delete(
|
|
412
|
-
'pz-set-currency'
|
|
413
|
-
);
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
if (process.env.ACC_APP_VERSION) {
|
|
417
|
-
middlewareResult.headers.set(
|
|
418
|
-
'acc-app-version',
|
|
419
|
-
process.env.ACC_APP_VERSION
|
|
420
|
-
);
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
// Set CSRF token if not set
|
|
424
|
-
try {
|
|
425
|
-
const url = `${Settings.commerceUrl}${user.csrfToken}`;
|
|
426
|
-
|
|
427
|
-
if (!req.cookies.get('csrftoken')) {
|
|
428
|
-
const { csrf_token } = await (
|
|
429
|
-
await fetch(url)
|
|
430
|
-
).json();
|
|
431
|
-
middlewareResult.cookies.set(
|
|
432
|
-
'csrftoken',
|
|
433
|
-
csrf_token,
|
|
434
|
-
{
|
|
435
|
-
domain: rootHostname
|
|
251
|
+
withCompleteWallet(
|
|
252
|
+
withWalletCompleteRedirection(
|
|
253
|
+
withMasterpassRestCallback(
|
|
254
|
+
async (
|
|
255
|
+
req: PzNextRequest,
|
|
256
|
+
event: NextFetchEvent
|
|
257
|
+
) => {
|
|
258
|
+
let middlewareResult: NextResponse | void =
|
|
259
|
+
NextResponse.next();
|
|
260
|
+
|
|
261
|
+
try {
|
|
262
|
+
const { locale, prettyUrl, currency } =
|
|
263
|
+
req.middlewareParams.rewrites;
|
|
264
|
+
const { defaultLocaleValue } =
|
|
265
|
+
Settings.localization;
|
|
266
|
+
const url = req.nextUrl.clone();
|
|
267
|
+
const pathnameWithoutLocale =
|
|
268
|
+
url.pathname.replace(
|
|
269
|
+
urlLocaleMatcherRegex,
|
|
270
|
+
''
|
|
271
|
+
);
|
|
272
|
+
|
|
273
|
+
middlewareResult = (await middleware(
|
|
274
|
+
req,
|
|
275
|
+
event
|
|
276
|
+
)) as NextResponse | void;
|
|
277
|
+
|
|
278
|
+
let customRewriteUrlDiff = '';
|
|
279
|
+
|
|
280
|
+
if (
|
|
281
|
+
middlewareResult instanceof
|
|
282
|
+
NextResponse &&
|
|
283
|
+
middlewareResult.headers.get(
|
|
284
|
+
'pz-override-response'
|
|
285
|
+
) &&
|
|
286
|
+
middlewareResult.headers.get(
|
|
287
|
+
'x-middleware-rewrite'
|
|
288
|
+
)
|
|
289
|
+
) {
|
|
290
|
+
const rewriteUrl = new URL(
|
|
291
|
+
middlewareResult.headers.get(
|
|
292
|
+
'x-middleware-rewrite'
|
|
293
|
+
)
|
|
294
|
+
);
|
|
295
|
+
const originalUrl = new URL(req.url);
|
|
296
|
+
customRewriteUrlDiff =
|
|
297
|
+
rewriteUrl.pathname.replace(
|
|
298
|
+
originalUrl.pathname,
|
|
299
|
+
''
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
let ordersPrefix: string;
|
|
304
|
+
|
|
305
|
+
if (isLegacyMode(Settings)) {
|
|
306
|
+
url.basePath = `/${commerceUrl}`;
|
|
307
|
+
url.pathname =
|
|
308
|
+
`/${locale.length ? `${locale}/` : ''}${currency}/${customRewriteUrlDiff}${
|
|
309
|
+
prettyUrl ?? pathnameWithoutLocale
|
|
310
|
+
}`.replace(/\/+/g, '/');
|
|
311
|
+
ordersPrefix = `/${currency}/orders`;
|
|
312
|
+
} else {
|
|
313
|
+
const pzConfig =
|
|
314
|
+
getPzSegmentsConfig(Settings);
|
|
315
|
+
const fullUrlPath =
|
|
316
|
+
`${customRewriteUrlDiff}${
|
|
317
|
+
prettyUrl ?? pathnameWithoutLocale
|
|
318
|
+
}`.replace(/\/+/g, '/');
|
|
319
|
+
const fullUrl = `${req.nextUrl.origin}${fullUrlPath}`;
|
|
320
|
+
const resolvedLocale =
|
|
321
|
+
locale?.length > 0
|
|
322
|
+
? locale
|
|
323
|
+
: Settings.localization
|
|
324
|
+
.defaultLocaleValue;
|
|
325
|
+
const resolveContext = {
|
|
326
|
+
req,
|
|
327
|
+
event,
|
|
328
|
+
url,
|
|
329
|
+
locale: resolvedLocale,
|
|
330
|
+
currency,
|
|
331
|
+
pathname: pathnameWithoutLocale
|
|
332
|
+
};
|
|
333
|
+
const customSegments = pzConfig.segments
|
|
334
|
+
.filter(
|
|
335
|
+
(seg) =>
|
|
336
|
+
seg.name !== 'locale' &&
|
|
337
|
+
seg.name !== 'currency' &&
|
|
338
|
+
seg.name !== 'url'
|
|
339
|
+
);
|
|
340
|
+
const segmentValues: Record<
|
|
341
|
+
string,
|
|
342
|
+
string
|
|
343
|
+
> = {
|
|
344
|
+
locale: resolvedLocale,
|
|
345
|
+
currency,
|
|
346
|
+
url: encodeURIComponent(fullUrl),
|
|
347
|
+
...Object.fromEntries(
|
|
348
|
+
customSegments
|
|
349
|
+
.map((seg) => [
|
|
350
|
+
seg.name,
|
|
351
|
+
req.middlewareParams.rewrites[
|
|
352
|
+
seg.name
|
|
353
|
+
] ??
|
|
354
|
+
(seg.resolve
|
|
355
|
+
? seg.resolve(resolveContext)
|
|
356
|
+
: '')
|
|
357
|
+
])
|
|
358
|
+
)
|
|
359
|
+
};
|
|
360
|
+
|
|
361
|
+
const pzValue = encodePzValue(
|
|
362
|
+
segmentValues,
|
|
363
|
+
pzConfig
|
|
364
|
+
);
|
|
365
|
+
|
|
366
|
+
url.pathname =
|
|
367
|
+
`/${pzValue}/${fullUrlPath}`.replace(
|
|
368
|
+
/\/+/g,
|
|
369
|
+
'/'
|
|
370
|
+
);
|
|
371
|
+
ordersPrefix = `/${pzValue}/orders`;
|
|
436
372
|
}
|
|
437
|
-
|
|
373
|
+
|
|
374
|
+
if (
|
|
375
|
+
Settings.usePrettyUrlRoute &&
|
|
376
|
+
url.searchParams.toString().length > 0 &&
|
|
377
|
+
!Object.entries(ROUTES).find(
|
|
378
|
+
([, value]) =>
|
|
379
|
+
new RegExp(`^${value}/?$`).test(
|
|
380
|
+
pathnameWithoutLocale
|
|
381
|
+
)
|
|
382
|
+
)
|
|
383
|
+
) {
|
|
384
|
+
url.pathname =
|
|
385
|
+
url.pathname +
|
|
386
|
+
(/\/$/.test(url.pathname) ? '' : '/') +
|
|
387
|
+
`searchparams|${encodeURIComponent(
|
|
388
|
+
url.searchParams.toString()
|
|
389
|
+
)}`;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
Settings.rewrites.forEach((rewrite) => {
|
|
393
|
+
url.pathname = url.pathname.replace(
|
|
394
|
+
rewrite.source,
|
|
395
|
+
rewrite.destination
|
|
396
|
+
);
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
// if middleware.ts has a return value for current url
|
|
400
|
+
if (
|
|
401
|
+
middlewareResult instanceof NextResponse
|
|
402
|
+
) {
|
|
403
|
+
// pz-override-response header is used to prevent 404 page for custom responses.
|
|
404
|
+
if (
|
|
405
|
+
middlewareResult.headers.get(
|
|
406
|
+
'pz-override-response'
|
|
407
|
+
) !== 'true'
|
|
408
|
+
) {
|
|
409
|
+
middlewareResult.headers.set(
|
|
410
|
+
'x-middleware-rewrite',
|
|
411
|
+
url.href
|
|
412
|
+
);
|
|
413
|
+
} else if (
|
|
414
|
+
middlewareResult.headers.get(
|
|
415
|
+
'x-middleware-rewrite'
|
|
416
|
+
) &&
|
|
417
|
+
middlewareResult.headers.get(
|
|
418
|
+
'pz-override-response'
|
|
419
|
+
) === 'true'
|
|
420
|
+
) {
|
|
421
|
+
middlewareResult =
|
|
422
|
+
NextResponse.rewrite(url);
|
|
423
|
+
}
|
|
424
|
+
} else {
|
|
425
|
+
// if middleware.ts doesn't have a return value.
|
|
426
|
+
// e.g. NextResponse.next() doesn't exist in middleware.ts
|
|
427
|
+
|
|
428
|
+
middlewareResult =
|
|
429
|
+
NextResponse.rewrite(url);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
const { localeUrlStrategy } =
|
|
433
|
+
Settings.localization;
|
|
434
|
+
|
|
435
|
+
const fallbackHost =
|
|
436
|
+
req.headers.get('x-forwarded-host') ||
|
|
437
|
+
req.headers.get('host');
|
|
438
|
+
const hostname =
|
|
439
|
+
process.env.NEXT_PUBLIC_URL ||
|
|
440
|
+
`https://${fallbackHost}`;
|
|
441
|
+
const rootHostname =
|
|
442
|
+
localeUrlStrategy ===
|
|
443
|
+
LocaleUrlStrategy.Subdomain
|
|
444
|
+
? getRootHostname(hostname)
|
|
445
|
+
: null;
|
|
446
|
+
|
|
447
|
+
if (
|
|
448
|
+
!url.pathname.startsWith(
|
|
449
|
+
ordersPrefix
|
|
450
|
+
)
|
|
451
|
+
) {
|
|
452
|
+
middlewareResult.cookies.set(
|
|
453
|
+
'pz-locale',
|
|
454
|
+
locale?.length > 0
|
|
455
|
+
? locale
|
|
456
|
+
: defaultLocaleValue,
|
|
457
|
+
{
|
|
458
|
+
domain: rootHostname,
|
|
459
|
+
sameSite: 'none',
|
|
460
|
+
secure: true,
|
|
461
|
+
expires: new Date(
|
|
462
|
+
Date.now() + 1000 * 60 * 60 * 24 * 7
|
|
463
|
+
) // 7 days
|
|
464
|
+
}
|
|
465
|
+
);
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
middlewareResult.cookies.set(
|
|
469
|
+
'pz-currency',
|
|
470
|
+
currency,
|
|
471
|
+
{
|
|
472
|
+
domain: rootHostname,
|
|
473
|
+
sameSite: 'none',
|
|
474
|
+
secure: true,
|
|
475
|
+
expires: new Date(
|
|
476
|
+
Date.now() + 1000 * 60 * 60 * 24 * 7
|
|
477
|
+
) // 7 days
|
|
478
|
+
}
|
|
479
|
+
);
|
|
480
|
+
|
|
481
|
+
if (
|
|
482
|
+
req.cookies.get('pz-locale') &&
|
|
483
|
+
req.cookies.get('pz-locale').value !==
|
|
484
|
+
locale
|
|
485
|
+
) {
|
|
486
|
+
logger.debug('Locale changed', {
|
|
487
|
+
locale,
|
|
488
|
+
oldLocale:
|
|
489
|
+
req.cookies.get('pz-locale')?.value,
|
|
490
|
+
ip
|
|
491
|
+
});
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
middlewareResult.headers.set(
|
|
495
|
+
'pz-url',
|
|
496
|
+
req.nextUrl.toString()
|
|
497
|
+
);
|
|
498
|
+
|
|
499
|
+
if (req.cookies.get('pz-set-currency')) {
|
|
500
|
+
middlewareResult.cookies.delete(
|
|
501
|
+
'pz-set-currency'
|
|
502
|
+
);
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
if (
|
|
506
|
+
req.cookies.get(
|
|
507
|
+
'pz-post-checkout-flow'
|
|
508
|
+
)
|
|
509
|
+
) {
|
|
510
|
+
if (
|
|
511
|
+
pathnameWithoutLocale.startsWith(
|
|
512
|
+
'/orders/completed/'
|
|
513
|
+
) ||
|
|
514
|
+
pathnameWithoutLocale.startsWith(
|
|
515
|
+
'/basket'
|
|
516
|
+
)
|
|
517
|
+
) {
|
|
518
|
+
middlewareResult.cookies.delete(
|
|
519
|
+
'pz-post-checkout-flow'
|
|
520
|
+
);
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
if (process.env.ACC_APP_VERSION) {
|
|
525
|
+
middlewareResult.headers.set(
|
|
526
|
+
'acc-app-version',
|
|
527
|
+
process.env.ACC_APP_VERSION
|
|
528
|
+
);
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
// Set CSRF token if not set
|
|
532
|
+
try {
|
|
533
|
+
const url = `${Settings.commerceUrl}${user.csrfToken}`;
|
|
534
|
+
|
|
535
|
+
if (!req.cookies.get('csrftoken')) {
|
|
536
|
+
const { csrf_token } = await (
|
|
537
|
+
await fetch(url)
|
|
538
|
+
).json();
|
|
539
|
+
middlewareResult.cookies.set(
|
|
540
|
+
'csrftoken',
|
|
541
|
+
csrf_token,
|
|
542
|
+
{
|
|
543
|
+
domain: rootHostname
|
|
544
|
+
}
|
|
545
|
+
);
|
|
546
|
+
}
|
|
547
|
+
} catch (error) {
|
|
548
|
+
logger.error('CSRF Error', {
|
|
549
|
+
error,
|
|
550
|
+
ip
|
|
551
|
+
});
|
|
552
|
+
}
|
|
553
|
+
} catch (error) {
|
|
554
|
+
logger.error('withPzDefault Error', {
|
|
555
|
+
error,
|
|
556
|
+
ip
|
|
557
|
+
});
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
return middlewareResult;
|
|
438
561
|
}
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
ip
|
|
443
|
-
});
|
|
444
|
-
}
|
|
445
|
-
} catch (error) {
|
|
446
|
-
logger.error('withPzDefault Error', {
|
|
447
|
-
error,
|
|
448
|
-
ip
|
|
449
|
-
});
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
return middlewareResult;
|
|
453
|
-
}
|
|
562
|
+
)
|
|
563
|
+
)
|
|
564
|
+
)
|
|
454
565
|
)
|
|
455
566
|
)
|
|
456
567
|
)
|
package/middlewares/index.ts
CHANGED
|
@@ -9,6 +9,9 @@ import withCompleteGpay from './complete-gpay';
|
|
|
9
9
|
import withCompleteMasterpass from './complete-masterpass';
|
|
10
10
|
import withCheckoutProvider from './checkout-provider';
|
|
11
11
|
import withSavedCardRedirection from './saved-card-redirection';
|
|
12
|
+
import withCompleteWallet from './complete-wallet';
|
|
13
|
+
import withWalletCompleteRedirection from './wallet-complete-redirection';
|
|
14
|
+
import withMasterpassRestCallback from './masterpass-rest-callback';
|
|
12
15
|
import { NextRequest } from 'next/server';
|
|
13
16
|
|
|
14
17
|
export {
|
|
@@ -22,17 +25,20 @@ export {
|
|
|
22
25
|
withCompleteGpay,
|
|
23
26
|
withCompleteMasterpass,
|
|
24
27
|
withCheckoutProvider,
|
|
25
|
-
withSavedCardRedirection
|
|
28
|
+
withSavedCardRedirection,
|
|
29
|
+
withCompleteWallet,
|
|
30
|
+
withWalletCompleteRedirection,
|
|
31
|
+
withMasterpassRestCallback
|
|
26
32
|
};
|
|
27
33
|
|
|
28
34
|
export interface PzNextRequest extends NextRequest {
|
|
29
35
|
middlewareParams: {
|
|
30
36
|
commerceUrl: string;
|
|
31
|
-
found: boolean;
|
|
32
37
|
rewrites: {
|
|
33
38
|
locale?: string;
|
|
34
39
|
prettyUrl?: string;
|
|
35
40
|
currency?: string;
|
|
41
|
+
[key: string]: string | undefined;
|
|
36
42
|
};
|
|
37
43
|
};
|
|
38
44
|
}
|