1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
import type { NextApiRequest, NextApiResponse } from 'next';
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const { sku } = req.query;
if (!sku) {
return res.status(400).json({ error: 'SKU is required' });
}
// Token Magento
const token = 'vxrtcjvztv1icgjzsui45de9kmwlz0lf';
const baseUrl = 'https://pimdev.1211.my.id/rest/V1/products';
try {
// 1. Pastikan SKU menjadi string dan hapus spasi kiri/kanan
const cleanSku = String(sku).trim();
// 2. Encode SKU
const encodedSku = encodeURIComponent(cleanSku);
// 3. Bentuk URL Final
const finalUrl = `${baseUrl}/${encodedSku}`;
// --- DEBUGGING LOG ---
console.log('Fetching URL:', finalUrl);
// Request ke Product Endpoint
const response = await fetch(finalUrl, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
},
});
if (!response.ok) {
console.error(`Magento Error: ${response.status} ${response.statusText}`);
return res.status(response.status).json({
error: 'Failed to fetch from Magento',
magentoStatus: response.status,
checkedUrl: finalUrl
});
}
const data = await response.json();
// =====================================================================
// TAMBAHAN: FETCH LABEL ATRIBUT (z_*)
// =====================================================================
let specsWithLabels: any[] = [];
// Cek apakah ada custom_attributes
if (data.custom_attributes) {
// Filter atribut yang kodenya dimulai dengan 'z'
const zAttributes = data.custom_attributes.filter((attr: any) =>
attr.attribute_code.startsWith('z')
);
// Fetch detail label untuk setiap atribut secara paralel
specsWithLabels = await Promise.all(
zAttributes.map(async (attr: any) => {
try {
// Endpoint untuk ambil detail atribut (Label): /V1/products/attributes/{attributeCode}
const attrUrl = `${baseUrl}/attributes/${attr.attribute_code}`;
const attrRes = await fetch(attrUrl, {
method: 'GET',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
}
});
if (attrRes.ok) {
const attrData = await attrRes.json();
// AMBIL NILAI 'default_frontend_label'
return {
label: attrData.default_frontend_label || attr.attribute_code,
value: attr.value
};
}
} catch (err) {
console.error(`Failed to fetch label for ${attr.attribute_code}`);
}
// Fallback: Jika gagal ambil label, format manual dari kode
const fallbackLabel = attr.attribute_code
.substring(1).replace(/_/g, ' ').trim(); // z_size_ml -> size ml
return {
label: fallbackLabel,
value: attr.value
};
})
);
}
// Gabungkan data asli dengan data specs yang sudah ada labelnya
const responseData = {
...data,
specs: specsWithLabels // Frontend tinggal pakai field ini
};
res.status(200).json(responseData);
} catch (error) {
console.error('Proxy Server Error:', error);
res.status(500).json({ error: 'Internal Server Error' });
}
}
|