-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.js
219 lines (206 loc) · 6.04 KB
/
utils.js
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
const { createRemoteFileNode } = require("gatsby-source-filesystem");
const fetch = require("node-fetch");
const apiHost = "https://api.mercadolibre.com";
// Get product main data
// Documentation: https://api.mercadolibre.com/items/:id#options
exports.getCompleteProductData = itemID => {
return fetch(`${apiHost}/items/${itemID}`)
.then(response => response.json())
.then(allProductData => allProductData)
.catch(err =>
console.log(
"An error ocurred while fetching a product from Mercado Libre.",
err.code
)
);
};
// Query the /pictures/:id endpoint
// and import the largest image from
// the variations provided,
// so they can be processed by transformer plugins
exports.importAndCreateImageNodes = async data => {
if (!data.productPictures || data.productPictures.length === 0) {
return;
}
let picturesToProcess = data.productPictures;
if (data.totalProducts > 300) {
// Note: We are currently limiting to 3 images per product on their lowest size
// due to a bug that happens when importing
// several images and the build hangs on "souce and transform nodes..."
// It usually fixes the problem but large stores
// might still fail
// https://github.com/gatsbyjs/gatsby/issues/6654
picturesToProcess = data.productPictures.filter((p, i) => i < 3);
}
const largestPictures = data.productPictures.map(async pic => {
const image = await fetch(`${apiHost}/pictures/${pic.id}`)
.then(res => {
if (res.status === 200) {
return res.json();
} else {
console.log("Response not 200");
}
})
.catch(() => {
console.log("Fetching image for product failed =>", data.productName);
});
if (image) {
const largest = image.variations && image.variations[0];
image.variations && data.totalProducts > 300
? image.variations[image.variations.length - 1]
: image.variations.find(i => i.size === image.max_size);
if (largest) {
return {
url: largest.secure_url,
id: image.id
};
}
}
});
try {
async function createNodes() {
let nodes = [];
const pictures = await Promise.all(largestPictures);
for (const pic of pictures) {
if (pic) {
const node = await createImageNode(pic, data);
if (node) {
nodes.push(node);
}
}
}
return nodes;
}
return createNodes();
} catch (error) {
return [];
}
};
exports.importAndCreateThumbnailNode = async data => {
if (!data.thumbnail) {
return;
}
const thumbnail = await fetch(`${apiHost}/pictures/${data.thumbnail.id}`)
.then(res => {
if (res.status === 200) {
return res.json();
}
})
.then(image => {
if (image) {
const largest =
image.variations &&
image.variations.find(i => i.size === image.max_size);
if (largest) {
return {
url: largest.secure_url,
id: image.id
};
}
}
})
.catch(error => {
console.log("thumbnail image failed", error.code);
});
if (thumbnail) {
return createImageNode(thumbnail, data);
}
return false;
};
async function createImageNode(pic, data) {
let fileNode = null;
if (pic && pic.url && data.productID) {
try {
fileNode = await createRemoteFileNode({
url: pic.url,
parent: data.productID,
store: data.store,
cache: data.cache,
createNode: data.createNode,
createNodeId: () => `ml-image-${data.productID}-${pic.id}`
});
} catch (e) {
console.log(`Error importing image from MercadoLibre - ${e}`);
}
if (fileNode) {
return { image___NODE: fileNode.id };
}
return;
}
}
// Helper function that processes a product to match Gatsby's node structure
exports.processProduct = (product, createNodeId, createContentDigest) => {
const nodeId = createNodeId(`ML-Product-${product.id}`);
const nodeContent = JSON.stringify(product);
const nodeData = Object.assign({}, product, {
id: nodeId,
parent: null,
children: [],
internal: {
type: `MercadoLibreProduct`,
content: nodeContent,
contentDigest: createContentDigest(product)
}
});
return nodeData;
};
// Seller data
exports.createSellerNode = (data, createNodeId, createContentDigest) => {
const nodeId = createNodeId(`ML-Seller`);
const nodeContent = JSON.stringify(data);
const nodeData = {
id: nodeId,
seller: data,
parent: null,
children: [],
internal: {
type: `MercadoLibreSeller`,
content: nodeContent,
contentDigest: createContentDigest(data)
}
};
return nodeData;
};
// Store wide available filters
exports.createStoreFiltersNode = (data, createNodeId, createContentDigest) => {
const nodeId = createNodeId(`ML-StoreFilters`);
const nodeContent = JSON.stringify(data);
const nodeData = {
id: nodeId,
filters: data,
parent: null,
children: [],
internal: {
type: `MercadoLibreFilters`,
content: nodeContent,
contentDigest: createContentDigest(data)
}
};
return nodeData;
};
// Get the product's description
// Documentation: https://api.mercadolibre.com/items/#options
exports.getItemDescription = itemID => {
return fetch(`${apiHost}/items/${itemID}/description`)
.then(description => description.json())
.catch(() => {
console.log(
`\n ℹ️ Product with id ${itemID} didn't provide a description from the API.`
);
});
};
// Get the product's category information
// Documentation: https://developers.mercadolibre.com.ar/en_us/categories-attributes
exports.getCategoryData = categoryID => {
return fetch(`${apiHost}/categories/${categoryID}`)
.then(category => category.json())
.then(category => ({
category_id: category.id || "",
category_name: category.name || "",
children_categories: category.children_categories || [],
path_from_root: category.path_from_root || []
}))
.catch(e => {
return "";
});
};