-
Notifications
You must be signed in to change notification settings - Fork 9
/
gatsby-node.js
52 lines (39 loc) · 1.33 KB
/
gatsby-node.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
const getPlaceDetails = require('./getPlaceDetails');
const nodeFactory = require('./nodeFactory');
const resolvePlace = async ({ apiKey, placeId, language, createNode }) => {
const response = (await getPlaceDetails(apiKey, placeId, language));
if (response.data.status !== "OK") {
throw Error("Request to Google API failed. " + response.data.error_message)
}
const place = response.data.result
place.id = placeId;
const placeNode = nodeFactory.placeNode(place);
delete placeNode.reviews
createNode(placeNode);
place.reviews.forEach(review => {
review.id = review.time;
const placeReviewNode = nodeFactory.reviewNode(review, {
parent: placeNode.id
});
createNode(placeReviewNode);
})
return;
}
exports.sourceNodes = async ({ actions }, { apiKey, placeIds, placeId, language = 'en-US' }) => {
try {
// Backwards compatibility
if (!placeIds) {
console.warn('gatsby-source-google-places has deprecated placeId. Use placeIds instead.')
placeIds = placeId
}
const { createNode } = actions;
// Single Place Id
if (typeof placeIds === "string") {
placeIds = [placeIds]
}
await Promise.all(placeIds.map((pid) => resolvePlace({ apiKey, placeId: pid, language, createNode })))
} catch (error) {
console.error(error)
process.exit(1)
}
}