@akinon/next 2.0.33-rc.0 → 2.0.33
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 +1 -24
- package/bin/pz-generate-routes.js +1 -4
- package/components/plugin-module.tsx +1 -0
- package/data/client/checkout.ts +1 -0
- package/data/server/category.ts +1 -13
- package/data/server/list.ts +0 -12
- package/data/server/product.ts +0 -10
- package/data/server/special-page.ts +0 -13
- package/data/server/widget.ts +1 -14
- package/data/urls.ts +4 -9
- package/hooks/use-captcha.tsx +1 -1
- package/middlewares/default.ts +205 -252
- package/package.json +2 -2
- package/plugins.d.ts +0 -10
- package/plugins.js +0 -1
- package/redux/middlewares/checkout.ts +3 -45
- package/redux/middlewares/pre-order/installment-option.ts +1 -9
- package/types/index.ts +9 -20
- package/utils/csrf.ts +1 -1
- package/with-pz-config.js +2 -3
- package/utils/payload-optimizer.ts +0 -481
|
@@ -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/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
|
/**
|
|
95
89
|
* CSRF cookie hardening settings.
|
|
96
90
|
*
|
|
@@ -242,7 +236,6 @@ export interface Settings {
|
|
|
242
236
|
separator?: string;
|
|
243
237
|
segments: PzSegmentDefinition[];
|
|
244
238
|
};
|
|
245
|
-
payloadOptimization?: import('../utils/payload-optimizer').PayloadOptimizationConfig;
|
|
246
239
|
}
|
|
247
240
|
|
|
248
241
|
export interface CacheOptions {
|
|
@@ -302,9 +295,7 @@ export interface PzSegmentsConfig {
|
|
|
302
295
|
}
|
|
303
296
|
|
|
304
297
|
// Search params type compatible with both Next.js resolved searchParams and URLSearchParams
|
|
305
|
-
export type SearchParams =
|
|
306
|
-
| Record<string, string | string[] | undefined>
|
|
307
|
-
| URLSearchParams;
|
|
298
|
+
export type SearchParams = Record<string, string | string[] | undefined> | URLSearchParams;
|
|
308
299
|
|
|
309
300
|
// Raw Next 16 server prop shape, used at the middleware/HOC boundary before normalization
|
|
310
301
|
export type RawSearchParams = Record<string, string | string[] | undefined>;
|
|
@@ -337,16 +328,14 @@ export interface RootLayoutProps<T = any> extends LayoutProps<T> {
|
|
|
337
328
|
|
|
338
329
|
// Async versions for Next.js 16 generateMetadata and internal use
|
|
339
330
|
export interface AsyncPageProps<T = any> {
|
|
340
|
-
params: Promise<
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
}
|
|
349
|
-
>;
|
|
331
|
+
params: Promise<T & {
|
|
332
|
+
pz?: string;
|
|
333
|
+
commerce?: string;
|
|
334
|
+
locale?: string;
|
|
335
|
+
currency?: string;
|
|
336
|
+
url?: string;
|
|
337
|
+
[key: string]: any;
|
|
338
|
+
}>;
|
|
350
339
|
searchParams: Promise<{ [key: string]: string | string[] | undefined }>;
|
|
351
340
|
}
|
|
352
341
|
|
package/utils/csrf.ts
CHANGED
|
@@ -31,7 +31,7 @@ export function getCsrfCookieFlags(): CsrfCookieFlags {
|
|
|
31
31
|
const httpOnly = isCsrfHttpOnly();
|
|
32
32
|
return {
|
|
33
33
|
httpOnly,
|
|
34
|
-
secure: process.env.NODE_ENV === 'production',
|
|
34
|
+
secure: httpOnly && process.env.NODE_ENV === 'production',
|
|
35
35
|
sameSite: 'lax'
|
|
36
36
|
};
|
|
37
37
|
}
|
package/with-pz-config.js
CHANGED
|
@@ -50,7 +50,7 @@ const defaultConfig = {
|
|
|
50
50
|
{
|
|
51
51
|
key: 'Content-Security-Policy',
|
|
52
52
|
value:
|
|
53
|
-
"frame-ancestors 'self' https://*.akifast.com akifast.com https://*.akinoncloud.com akinoncloud.com
|
|
53
|
+
"frame-ancestors 'self' https://*.akifast.com akifast.com https://*.akinoncloud.com akinoncloud.com http://localhost:5174 localhost:5174"
|
|
54
54
|
}
|
|
55
55
|
]
|
|
56
56
|
}
|
|
@@ -69,8 +69,7 @@ const defaultConfig = {
|
|
|
69
69
|
acc[`@akinon/${plugin}`] = false;
|
|
70
70
|
return acc;
|
|
71
71
|
}, {}),
|
|
72
|
-
translations: false
|
|
73
|
-
'@opentelemetry/exporter-jaeger': false
|
|
72
|
+
translations: false
|
|
74
73
|
};
|
|
75
74
|
// Ensure webpack can resolve deps from the app's node_modules when
|
|
76
75
|
// compiling transpiled packages (e.g. @akinon/next) whose imports
|
|
@@ -1,481 +0,0 @@
|
|
|
1
|
-
import { GetCategoryResponse } from '../types/commerce/category'
|
|
2
|
-
import { Product, ProductResult } from '../types/commerce/product'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Payload Optimization Config
|
|
6
|
-
*
|
|
7
|
-
* - Optimizer returns minimum fields needed for the base project.
|
|
8
|
-
* - Projects extend via "Extra" arrays in settings.js.
|
|
9
|
-
* - Each config array works as "base + extra".
|
|
10
|
-
*
|
|
11
|
-
* Example:
|
|
12
|
-
* ```js
|
|
13
|
-
* payloadOptimization: {
|
|
14
|
-
* enabled: true,
|
|
15
|
-
* listProductImageLimit: 1,
|
|
16
|
-
* listProductExtraFields: ['stock', 'tax_rate'],
|
|
17
|
-
* listProductExtraAttributes: ['my_custom_attr'],
|
|
18
|
-
* listProductExtraAttributesKwargs: ['integration_renk'],
|
|
19
|
-
* }
|
|
20
|
-
* ```
|
|
21
|
-
*/
|
|
22
|
-
export interface PayloadOptimizationConfig {
|
|
23
|
-
enabled: boolean
|
|
24
|
-
|
|
25
|
-
// ── Image Limits ───────────────────────────────────────────────────────────
|
|
26
|
-
/** Max images per product on list pages (default: 3) */
|
|
27
|
-
listProductImageLimit?: number
|
|
28
|
-
/** Extra hover image count after the limit (default: 2) */
|
|
29
|
-
listHoverImageCount?: number
|
|
30
|
-
/** Max images per product in widgets (default: listProductImageLimit) */
|
|
31
|
-
widgetProductImageLimit?: number
|
|
32
|
-
/** Widget hover image count (default: listHoverImageCount) */
|
|
33
|
-
widgetHoverImageCount?: number
|
|
34
|
-
/** Max images per variant product on PDP (default: 1) */
|
|
35
|
-
pdpVariantImageLimit?: number
|
|
36
|
-
|
|
37
|
-
// ── List / Category / Special Page ─────────────────────────────────────────
|
|
38
|
-
/** Extra top-level product fields on top of base */
|
|
39
|
-
listProductExtraFields?: string[]
|
|
40
|
-
/** Extra keys from product.attributes on top of base */
|
|
41
|
-
listProductExtraAttributes?: string[]
|
|
42
|
-
/** Extra keys from product.attributes_kwargs on top of base */
|
|
43
|
-
listProductExtraAttributesKwargs?: string[]
|
|
44
|
-
/** Extra keys from product.extra_attributes on top of base */
|
|
45
|
-
listProductExtraExtraAttributes?: string[]
|
|
46
|
-
/** Extra keys from product.data_source on top of base */
|
|
47
|
-
listDataSourceExtraFields?: string[]
|
|
48
|
-
|
|
49
|
-
// ── Product Detail Page ────────────────────────────────────────────────────
|
|
50
|
-
/** Extra keys from product.attributes on top of base */
|
|
51
|
-
pdpProductExtraAttributes?: string[]
|
|
52
|
-
/** Extra keys from product.attributes_kwargs on top of base */
|
|
53
|
-
pdpProductExtraAttributesKwargs?: string[]
|
|
54
|
-
/** Extra keys from product.extra_attributes on top of base */
|
|
55
|
-
pdpProductExtraExtraAttributes?: string[]
|
|
56
|
-
/** Extra keys from variant product.attributes on top of base */
|
|
57
|
-
pdpVariantExtraAttributes?: string[]
|
|
58
|
-
/** Extra keys from variant product.attributes_kwargs on top of base */
|
|
59
|
-
pdpVariantExtraAttributesKwargs?: string[]
|
|
60
|
-
/** Extra keys from product.data_source on top of base */
|
|
61
|
-
pdpDataSourceExtraFields?: string[]
|
|
62
|
-
/** Extra top-level product fields on top of base */
|
|
63
|
-
pdpProductExtraFields?: string[]
|
|
64
|
-
|
|
65
|
-
// ── Category ───────────────────────────────────────────────────────────────
|
|
66
|
-
/** Extra top-level category fields on top of base */
|
|
67
|
-
categoryExtraFields?: string[]
|
|
68
|
-
/** Extra keys from category.attributes on top of base */
|
|
69
|
-
categoryExtraAttributes?: string[]
|
|
70
|
-
|
|
71
|
-
// ── Special Page ───────────────────────────────────────────────────────────
|
|
72
|
-
/** Extra fields from special_page on top of base */
|
|
73
|
-
specialPageExtraFields?: string[]
|
|
74
|
-
|
|
75
|
-
// ── Offers ─────────────────────────────────────────────────────────────────
|
|
76
|
-
/** Extra keys from offer.listing_kwargs on top of base */
|
|
77
|
-
offerExtraListingKwargsFields?: string[]
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// ─── Base Field Sets ─────────────────────────────────────────────────────────
|
|
81
|
-
// Minimum fields required by the base project (projectzeronext).
|
|
82
|
-
// Projects extend these via "Extra" arrays in settings.js.
|
|
83
|
-
|
|
84
|
-
const BASE_LIST_PRODUCT_FIELDS = [
|
|
85
|
-
'pk', 'name', 'base_code', 'absolute_url',
|
|
86
|
-
'price', 'retail_price', 'in_stock'
|
|
87
|
-
]
|
|
88
|
-
|
|
89
|
-
const BASE_LIST_PRODUCT_ATTRIBUTES: string[] = []
|
|
90
|
-
const BASE_LIST_PRODUCT_ATTRIBUTES_KWARGS: string[] = []
|
|
91
|
-
const BASE_LIST_PRODUCT_EXTRA_ATTRIBUTES: string[] = []
|
|
92
|
-
const BASE_LIST_DATASOURCE_FIELDS = ['pk', 'name']
|
|
93
|
-
|
|
94
|
-
const BASE_PDP_PRODUCT_FIELDS = [
|
|
95
|
-
'pk', 'name', 'sku', 'base_code', 'price', 'retail_price',
|
|
96
|
-
'in_stock', 'product_type', 'currency_type', 'absolute_url',
|
|
97
|
-
'productimage_set', 'attribute_set', 'unit_type', 'tax_rate',
|
|
98
|
-
'stock', 'price_type', 'form_schema', 'is_ready_to_basket',
|
|
99
|
-
'data_source', 'basket_offers', 'data_source_offers',
|
|
100
|
-
'is_listable', 'listing_code', 'price_extra_field'
|
|
101
|
-
]
|
|
102
|
-
|
|
103
|
-
const BASE_PDP_PRODUCT_ATTRIBUTES = [
|
|
104
|
-
'integration_ProductAtt03Desc', 'integration_kumas_icerik',
|
|
105
|
-
'model_olculeri', 'aciklama'
|
|
106
|
-
]
|
|
107
|
-
|
|
108
|
-
const BASE_PDP_PRODUCT_ATTRIBUTES_KWARGS: string[] = []
|
|
109
|
-
const BASE_PDP_PRODUCT_EXTRA_ATTRIBUTES: string[] = []
|
|
110
|
-
|
|
111
|
-
const BASE_PDP_DATASOURCE_FIELDS = [
|
|
112
|
-
'pk', 'name', 'title', 'phone_number', 'address',
|
|
113
|
-
'mersis_number', 'kep_address'
|
|
114
|
-
]
|
|
115
|
-
|
|
116
|
-
const BASE_PDP_VARIANT_ATTRIBUTES: string[] = []
|
|
117
|
-
const BASE_PDP_VARIANT_ATTRIBUTES_KWARGS: string[] = []
|
|
118
|
-
|
|
119
|
-
const BASE_CATEGORY_FIELDS = [
|
|
120
|
-
'pk', 'name', 'menuitemmodel', 'absolute_url', 'sort_option'
|
|
121
|
-
]
|
|
122
|
-
|
|
123
|
-
const BASE_CATEGORY_ATTRIBUTES: string[] = []
|
|
124
|
-
|
|
125
|
-
const BASE_SPECIAL_PAGE_FIELDS = ['pk', 'name', 'url', 'banner_url']
|
|
126
|
-
|
|
127
|
-
const DEFAULT_LIST_IMAGE_LIMIT = 3
|
|
128
|
-
const DEFAULT_PDP_VARIANT_IMAGE_LIMIT = 1
|
|
129
|
-
|
|
130
|
-
// ─── Helpers ─────────────────────────────────────────────────────────────────
|
|
131
|
-
|
|
132
|
-
function merge(base: string[], extra?: string[]): string[] {
|
|
133
|
-
if (!extra?.length) return base
|
|
134
|
-
return [...base, ...extra]
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
function pickKeys(obj: any, keys: string[]): any {
|
|
138
|
-
if (!obj) return undefined
|
|
139
|
-
const result: any = {}
|
|
140
|
-
for (const key of keys) {
|
|
141
|
-
if (obj[key] != null) {
|
|
142
|
-
result[key] = obj[key]
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
return result
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
function pickAttributesKwargs(kwargs: any, keys: string[]): any {
|
|
149
|
-
if (!kwargs) return undefined
|
|
150
|
-
const result: any = {}
|
|
151
|
-
for (const key of keys) {
|
|
152
|
-
if (kwargs[key] != null) {
|
|
153
|
-
result[key] = kwargs[key]?.label != null
|
|
154
|
-
? { label: kwargs[key].label }
|
|
155
|
-
: kwargs[key]
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
return result
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
function stripImages(images: any[] | undefined, limit: number) {
|
|
162
|
-
if (!images?.length) return undefined
|
|
163
|
-
return images.slice(0, limit).map((img: any) => ({ image: img.image }))
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
function stripOffer(offer: any, extraFields?: string[]) {
|
|
167
|
-
if (!offer) return offer
|
|
168
|
-
const lk: any = offer.listing_kwargs
|
|
169
|
-
? { discounted_total_price: offer.listing_kwargs.discounted_total_price, quantity: offer.listing_kwargs.quantity }
|
|
170
|
-
: undefined
|
|
171
|
-
|
|
172
|
-
if (lk && extraFields && offer.listing_kwargs) {
|
|
173
|
-
for (const f of extraFields) {
|
|
174
|
-
if (offer.listing_kwargs[f] !== undefined) lk[f] = offer.listing_kwargs[f]
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
return {
|
|
179
|
-
pk: offer.pk,
|
|
180
|
-
label: offer.label,
|
|
181
|
-
listing_kwargs: lk,
|
|
182
|
-
kwargs: offer.kwargs,
|
|
183
|
-
data_source: offer.data_source ? { pk: offer.data_source.pk } : undefined
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
function applyOffers(target: any, source: any, config: PayloadOptimizationConfig) {
|
|
188
|
-
const extra = config.offerExtraListingKwargsFields
|
|
189
|
-
if (source.basket_offers) {
|
|
190
|
-
target.basket_offers = source.basket_offers.map((o: any) => stripOffer(o, extra))
|
|
191
|
-
}
|
|
192
|
-
if (source.data_source_offers) {
|
|
193
|
-
target.data_source_offers = source.data_source_offers.map(
|
|
194
|
-
(g: any) => Array.isArray(g) ? g.map((o: any) => stripOffer(o, extra)) : stripOffer(g, extra)
|
|
195
|
-
)
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
function stripFacet(facet: any) {
|
|
200
|
-
if (!facet) return facet
|
|
201
|
-
return {
|
|
202
|
-
key: facet.key,
|
|
203
|
-
name: facet.name,
|
|
204
|
-
search_key: facet.search_key,
|
|
205
|
-
widget_type: facet.widget_type,
|
|
206
|
-
data: facet.data ? {
|
|
207
|
-
choices: facet.data.choices?.map((c: any) => ({
|
|
208
|
-
label: c.label, value: c.value, quantity: c.quantity,
|
|
209
|
-
selected: c.selected, is_selected: c.is_selected,
|
|
210
|
-
children: c.children
|
|
211
|
-
})),
|
|
212
|
-
key: facet.data.key, name: facet.data.name, order: facet.data.order,
|
|
213
|
-
search_key: facet.data.search_key, widget_type: facet.data.widget_type
|
|
214
|
-
} : undefined
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
// ─── Precomputed Key Sets ────────────────────────────────────────────────────
|
|
219
|
-
// Computed once per config to avoid repeated merge() calls per product.
|
|
220
|
-
|
|
221
|
-
interface ListKeySet {
|
|
222
|
-
fields: string[]
|
|
223
|
-
attrKeys: string[]
|
|
224
|
-
kwargsKeys: string[]
|
|
225
|
-
extraAttrKeys: string[]
|
|
226
|
-
dsFields: string[]
|
|
227
|
-
imageLimit: number
|
|
228
|
-
hoverCount: number
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
function buildListKeySet(config: PayloadOptimizationConfig): ListKeySet {
|
|
232
|
-
return {
|
|
233
|
-
fields: merge(BASE_LIST_PRODUCT_FIELDS, config.listProductExtraFields),
|
|
234
|
-
attrKeys: merge(BASE_LIST_PRODUCT_ATTRIBUTES, config.listProductExtraAttributes),
|
|
235
|
-
kwargsKeys: merge(BASE_LIST_PRODUCT_ATTRIBUTES_KWARGS, config.listProductExtraAttributesKwargs),
|
|
236
|
-
extraAttrKeys: merge(BASE_LIST_PRODUCT_EXTRA_ATTRIBUTES, config.listProductExtraExtraAttributes),
|
|
237
|
-
dsFields: merge(BASE_LIST_DATASOURCE_FIELDS, config.listDataSourceExtraFields),
|
|
238
|
-
imageLimit: config.listProductImageLimit ?? DEFAULT_LIST_IMAGE_LIMIT,
|
|
239
|
-
hoverCount: config.listHoverImageCount ?? 2
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
function buildWidgetKeySet(config: PayloadOptimizationConfig): ListKeySet {
|
|
244
|
-
const listKeys = buildListKeySet(config)
|
|
245
|
-
return {
|
|
246
|
-
...listKeys,
|
|
247
|
-
imageLimit: config.widgetProductImageLimit ?? listKeys.imageLimit,
|
|
248
|
-
hoverCount: config.widgetHoverImageCount ?? listKeys.hoverCount
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
// ─── Shared Product Stripping ────────────────────────────────────────────────
|
|
253
|
-
|
|
254
|
-
function stripProduct(
|
|
255
|
-
product: any,
|
|
256
|
-
keys: ListKeySet,
|
|
257
|
-
config: PayloadOptimizationConfig,
|
|
258
|
-
opts?: { includeHoverImages?: boolean, includeWidgetVariants?: boolean }
|
|
259
|
-
): any {
|
|
260
|
-
if (!product) return product
|
|
261
|
-
|
|
262
|
-
const allImages = product.productimage_set ?? []
|
|
263
|
-
const stripped: any = pickKeys(product, keys.fields)
|
|
264
|
-
|
|
265
|
-
stripped.productimage_set = stripImages(allImages, keys.imageLimit)
|
|
266
|
-
|
|
267
|
-
if (opts?.includeHoverImages && keys.hoverCount > 0 && allImages.length > keys.imageLimit) {
|
|
268
|
-
stripped._hoverImageUrls = allImages
|
|
269
|
-
.slice(keys.imageLimit, keys.imageLimit + keys.hoverCount)
|
|
270
|
-
.map((img: any) => img.image)
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
stripped.attributes = pickKeys(product.attributes, keys.attrKeys)
|
|
274
|
-
stripped.attributes_kwargs = pickAttributesKwargs(product.attributes_kwargs, keys.kwargsKeys)
|
|
275
|
-
stripped.extra_attributes = pickKeys(product.extra_attributes, keys.extraAttrKeys)
|
|
276
|
-
|
|
277
|
-
if (product.data_source) {
|
|
278
|
-
stripped.data_source = pickKeys(product.data_source, keys.dsFields)
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
applyOffers(stripped, product, config)
|
|
282
|
-
|
|
283
|
-
// Widget variants: strip to minimal
|
|
284
|
-
if (opts?.includeWidgetVariants && product.extra_data?.variants) {
|
|
285
|
-
stripped.extra_data = {
|
|
286
|
-
variants: product.extra_data.variants.map((variant: any) => ({
|
|
287
|
-
attribute_key: variant.attribute_key,
|
|
288
|
-
options: variant.options?.map((option: any) => {
|
|
289
|
-
if (!option.product) return option
|
|
290
|
-
const op = option.product
|
|
291
|
-
return {
|
|
292
|
-
...option,
|
|
293
|
-
product: {
|
|
294
|
-
pk: op.pk,
|
|
295
|
-
absolute_url: op.absolute_url,
|
|
296
|
-
price: op.price,
|
|
297
|
-
retail_price: op.retail_price,
|
|
298
|
-
stock: op.stock,
|
|
299
|
-
in_stock: op.in_stock != null ? op.in_stock : (op.stock ?? 0) > 0,
|
|
300
|
-
productimage_set: stripImages(op.productimage_set, 1),
|
|
301
|
-
attributes: pickKeys(op.attributes, keys.attrKeys),
|
|
302
|
-
price_extra_field: op.price_extra_field
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
})
|
|
306
|
-
}))
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
return stripped
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
// ─── List / Category / Special Page ──────────────────────────────────────────
|
|
314
|
-
|
|
315
|
-
export function optimizeCategoryResponse(data: GetCategoryResponse, config: PayloadOptimizationConfig): GetCategoryResponse {
|
|
316
|
-
if (!data) return data
|
|
317
|
-
|
|
318
|
-
// Shallow copy to avoid mutating cached data
|
|
319
|
-
const result = { ...data }
|
|
320
|
-
const keys = buildListKeySet(config)
|
|
321
|
-
|
|
322
|
-
if (result.products?.length) {
|
|
323
|
-
result.products = result.products.map(
|
|
324
|
-
(p) => stripProduct(p, keys, config, { includeHoverImages: true }) as Product
|
|
325
|
-
)
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
if (result.facets?.length) {
|
|
329
|
-
result.facets = result.facets.map((f) => stripFacet(f) as typeof f)
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
if (result.category) {
|
|
333
|
-
const cat = result.category as any
|
|
334
|
-
const catFields = merge(BASE_CATEGORY_FIELDS, config.categoryExtraFields)
|
|
335
|
-
const strippedCat: any = pickKeys(cat, catFields)
|
|
336
|
-
strippedCat.attributes = config.categoryExtraAttributes?.length
|
|
337
|
-
? pickKeys(cat.attributes, merge(BASE_CATEGORY_ATTRIBUTES, config.categoryExtraAttributes))
|
|
338
|
-
: cat.attributes
|
|
339
|
-
result.category = strippedCat
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
if ((result as any).special_page) {
|
|
343
|
-
const spFields = merge(BASE_SPECIAL_PAGE_FIELDS, config.specialPageExtraFields)
|
|
344
|
-
;(result as any).special_page = pickKeys((result as any).special_page, spFields)
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
return result
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
// ─── Product Detail Page ─────────────────────────────────────────────────────
|
|
351
|
-
|
|
352
|
-
export function optimizeProductResponse(data: ProductResult, config: PayloadOptimizationConfig): ProductResult {
|
|
353
|
-
if (!data) return data
|
|
354
|
-
|
|
355
|
-
// Shallow copy to avoid mutating cached data
|
|
356
|
-
const result = { ...data } as any
|
|
357
|
-
const variantImageLimit = config.pdpVariantImageLimit ?? DEFAULT_PDP_VARIANT_IMAGE_LIMIT
|
|
358
|
-
|
|
359
|
-
// Precompute key sets once
|
|
360
|
-
const pdpFields = merge(BASE_PDP_PRODUCT_FIELDS, config.pdpProductExtraFields)
|
|
361
|
-
const attrKeys = merge(BASE_PDP_PRODUCT_ATTRIBUTES, config.pdpProductExtraAttributes)
|
|
362
|
-
const kwargsKeys = merge(BASE_PDP_PRODUCT_ATTRIBUTES_KWARGS, config.pdpProductExtraAttributesKwargs)
|
|
363
|
-
const extraAttrKeys = merge(BASE_PDP_PRODUCT_EXTRA_ATTRIBUTES, config.pdpProductExtraExtraAttributes)
|
|
364
|
-
const dsFields = merge(BASE_PDP_DATASOURCE_FIELDS, config.pdpDataSourceExtraFields)
|
|
365
|
-
const varAttrKeys = merge(BASE_PDP_VARIANT_ATTRIBUTES, config.pdpVariantExtraAttributes)
|
|
366
|
-
const varKwargsKeys = config.pdpVariantExtraAttributesKwargs?.length
|
|
367
|
-
? merge(BASE_PDP_VARIANT_ATTRIBUTES_KWARGS, config.pdpVariantExtraAttributesKwargs)
|
|
368
|
-
: undefined
|
|
369
|
-
|
|
370
|
-
if (data.product) {
|
|
371
|
-
const p = data.product as any
|
|
372
|
-
const stripped: any = pickKeys(p, pdpFields)
|
|
373
|
-
|
|
374
|
-
// Strip specialimage_set from images
|
|
375
|
-
if (stripped.productimage_set) {
|
|
376
|
-
stripped.productimage_set = stripped.productimage_set.map((img: any) => ({
|
|
377
|
-
pk: img.pk, image: img.image, order: img.order, status: img.status
|
|
378
|
-
}))
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
stripped.attributes = pickKeys(p.attributes, attrKeys)
|
|
382
|
-
stripped.attributes_kwargs = pickKeys(p.attributes_kwargs, kwargsKeys)
|
|
383
|
-
stripped.extra_attributes = pickKeys(p.extra_attributes, extraAttrKeys)
|
|
384
|
-
|
|
385
|
-
if (p.data_source) {
|
|
386
|
-
stripped.data_source = pickKeys(p.data_source, dsFields)
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
applyOffers(stripped, p, config)
|
|
390
|
-
result.product = stripped
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
if (result.offers) {
|
|
394
|
-
result.offers = result.offers.map((offer: any) => ({
|
|
395
|
-
pk: offer.pk,
|
|
396
|
-
name: offer.name,
|
|
397
|
-
in_stock: offer.in_stock,
|
|
398
|
-
price: offer.price,
|
|
399
|
-
retail_price: offer.retail_price,
|
|
400
|
-
absolute_url: offer.absolute_url,
|
|
401
|
-
attributes: pickKeys(offer.attributes, attrKeys),
|
|
402
|
-
attributes_kwargs: pickKeys(offer.attributes_kwargs, kwargsKeys),
|
|
403
|
-
extra_attributes: pickKeys(offer.extra_attributes, extraAttrKeys),
|
|
404
|
-
data_source: offer.data_source ? pickKeys(offer.data_source, dsFields) : undefined
|
|
405
|
-
}))
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
if (data.variants) {
|
|
409
|
-
result.variants = data.variants.map((variant: any) => ({
|
|
410
|
-
...variant,
|
|
411
|
-
options: variant.options?.map((option: any) => {
|
|
412
|
-
if (!option.product) return option
|
|
413
|
-
const op = option.product
|
|
414
|
-
return {
|
|
415
|
-
...option,
|
|
416
|
-
product: {
|
|
417
|
-
pk: op.pk,
|
|
418
|
-
sku: op.sku,
|
|
419
|
-
name: op.name,
|
|
420
|
-
in_stock: op.in_stock,
|
|
421
|
-
stock: op.stock,
|
|
422
|
-
price: op.price,
|
|
423
|
-
retail_price: op.retail_price,
|
|
424
|
-
absolute_url: op.absolute_url,
|
|
425
|
-
productimage_set: op.productimage_set?.slice(0, variantImageLimit),
|
|
426
|
-
price_extra_field: op.price_extra_field,
|
|
427
|
-
attributes: pickKeys(op.attributes, varAttrKeys),
|
|
428
|
-
attributes_kwargs: varKwargsKeys ? pickKeys(op.attributes_kwargs, varKwargsKeys) : op.attributes_kwargs
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
})
|
|
432
|
-
}))
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
if (data.group_products) {
|
|
436
|
-
result.group_products = data.group_products.map((gp: any) => ({
|
|
437
|
-
pk: gp.pk, name: gp.name, sku: gp.sku,
|
|
438
|
-
in_stock: gp.in_stock, price: gp.price, retail_price: gp.retail_price,
|
|
439
|
-
productimage_set: gp.productimage_set
|
|
440
|
-
}))
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
return result as ProductResult
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
// ─── Widget ──────────────────────────────────────────────────────────────────
|
|
447
|
-
|
|
448
|
-
export function optimizeWidgetResponse(data: any, config: PayloadOptimizationConfig): any {
|
|
449
|
-
if (!data) return data
|
|
450
|
-
|
|
451
|
-
const result = { ...data }
|
|
452
|
-
const keys = buildWidgetKeySet(config)
|
|
453
|
-
|
|
454
|
-
if (result.products?.length && result.products[0]?.pk) {
|
|
455
|
-
result.products = result.products.map(
|
|
456
|
-
(p: any) => stripProduct(p, keys, config, { includeHoverImages: true, includeWidgetVariants: true })
|
|
457
|
-
)
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
if (result.attributes) {
|
|
461
|
-
result.attributes = { ...result.attributes }
|
|
462
|
-
for (const key of Object.keys(result.attributes)) {
|
|
463
|
-
const val = result.attributes[key]
|
|
464
|
-
if (Array.isArray(val) && val.length > 0 && val[0]?.pk && val[0]?.productimage_set) {
|
|
465
|
-
result.attributes[key] = val.map(
|
|
466
|
-
(p: any) => stripProduct(p, keys, config, { includeHoverImages: true, includeWidgetVariants: true })
|
|
467
|
-
)
|
|
468
|
-
}
|
|
469
|
-
if (val?.value && Array.isArray(val.value) && val.value.length > 0 && val.value[0]?.pk && val.value[0]?.productimage_set) {
|
|
470
|
-
result.attributes[key] = {
|
|
471
|
-
...val,
|
|
472
|
-
value: val.value.map(
|
|
473
|
-
(p: any) => stripProduct(p, keys, config, { includeHoverImages: true, includeWidgetVariants: true })
|
|
474
|
-
)
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
|
-
}
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
return result
|
|
481
|
-
}
|