@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.
@@ -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
- }