-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
274 lines (252 loc) · 7.46 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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
require("dotenv").config()
const axios = require('axios').default;
const { createRemoteFileNode } = require(`gatsby-source-filesystem`)
// logs to confirmation to console that plugin loaded
exports.onPreInit = (_, pluginOptions) => console.log("Loaded guesty-source-plugin")
// basic auth for guesty
const username = process.env.GATSBY_GUESTY_API_KEY;
const password = process.env.GATSBY_GUESTY_API_SECRET;
const token = Buffer.from(`${username}:${password}`, 'utf8').toString('base64')
// axios defaults for all requests
axios.defaults.baseURL = 'https://api.guesty.com/api/v2/';
axios.defaults.headers['Content-Type'] = 'application/json';
axios.defaults.headers.common['Authorization'] = `Basic ${token}`;
// graphQL contants
const LISTINGS_NODE_TYPE = `Listing`;
const REVIEWS_NODE_TYPE = `Review`;
// create nodes for listings and reviews
exports.sourceNodes = async ({
actions,
createContentDigest,
createNodeId,
getNodesByType,
}) => {
const { createNode } = actions
// get all listings data
const getListings = async () => await axios.get('/listings', {
params: {
fields: "_id accommodates bedrooms beds bathrooms propertyType title occupancyStats customFields active prices terms amenities pictures picture address integrations isListed publicDescription"
}
})
.then(response => {
response.data.results.forEach(listing => {
createNode({
...listing,
id: listing._id,
parent: null,
children: [],
internal: {
type: LISTINGS_NODE_TYPE,
content: JSON.stringify(listing),
contentDigest: createContentDigest(listing),
},
})
})
})
.catch(function (error) {
console.log(error);
})
// get all account reviews with respective listingIds,
const getReviews = async () => await axios.get('reviews-service/api/reviews', {
// params: {
// channelId: "airbnb2"
// }
})
.then(response => {
// console.log(response.data.data);
response.data.data.forEach(review => {
createNode({
...review,
id: review._id,
parent: null,
children: [],
internal: {
type: REVIEWS_NODE_TYPE,
content: JSON.stringify(review),
contentDigest: createContentDigest(review),
},
})
})
})
.catch(function (error) {
console.log(error);
})
getListings();
getReviews();
return
}
// use onCreateNode API to assign a url and media type (file) for images everytime
// a new Listing node is created. This allows gatsby-image-plugin to optimize image
// called each time a node is created
// exports.onCreateNode = async ({
// node, // the node that was just created
// actions: {
// createNode,
// createNodeField
// },
// createNodeId,
// getCache,
// }) => {
// if (node.internal.type === LISTINGS_NODE_TYPE) {
// const fileNode = await createRemoteFileNode({
// url: node.picture.thumbnail, // the url of the remote image to generate a node for
// parentNodeId: node.id,
// createNode,
// createNodeId,
// getCache,
// })
// if (fileNode) {
// createNodeField({ node, name: 'thumbnail', value: fileNode.id })
// }
// }
// }
exports.onCreateNode = async ({
node, // the node that was just created
actions: {
createNode,
createNodeField
},
createNodeId,
getCache,
}) => {
if (node.internal.type === LISTINGS_NODE_TYPE && node.pictures !== null) {
await node.pictures.forEach(file => {
// console.log(file);
const fileNode = createRemoteFileNode({
url: file.original, // the url of the remote image to generate a node for
parentNodeId: node.id,
createNode,
createNodeId,
getCache,
})
// for some reason I can get gatsbyImageData from File node,
// but not listing.picFiles
if (fileNode) {
createNodeField({ node, name: 'picFiles', value: fileNode.id })
}
})
}
}
// Create a custom data schema for all fields imported from Guesty API
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions
createTypes(`
type Listing implements Node @infer {
_id: String!
accommodates: Int
bedrooms: Int!
beds: Int
bathrooms: Int
propertyType: String
title: String!
occupancyStats: [String]
active: Boolean
prices: prices
amenities: [String]
pictures: [pictures]
address: address
tags: [String]
isListed: Boolean
integrations: integrations
customFields: customFields
publicDescription: publicDescription
picFiles: [File] @link(from: "fields.picFiles")
}
type prices {
basePrice: Int!
currency: String!
cleaningFee: Int
securityDepositFee: Int
}
type pictures {
_id: String!
thumbnail: String
regular: String
large: String
caption: String
original: String
}
type address {
full: String
lng: Int
lat: Int
street: String
city: String
country: String
}
type integrations {
platform: String
_id: String
airbnb: airbnb
}
type airbnb {
starRating: Int
reviewsCount: Int
importCalendar: Boolean
isCalendarSynced: Boolean
}
type customFields {
fieldId: String
value: String
fullText: String
}
type publicDescription {
summary: String
space: String
access: String
neighborhood: String
transit: String
notes: String
houseRules: String
interactionWithGuests: String
}
type Review implements Node @infer {
_id: String!
channelId: String!
createdAt: Date
listingId: String!
rawReview: rawReview
listing: Listing @link(from: "listing._id" by: "listingId")
}
type rawReview {
overall_rating: Int
public_review: String!
category_ratings_cleanliness: Int
category_ratings_accuracy: Int
category_ratings_communications: Int
category_ratings_location: Int
category_ratings_checkin: Int
category_ratings_value: Int
}
`)
}
// define schema for plugin option validation during configuration of plugin by user
exports.pluginOptionsSchema = ({ Joi }) => {
return Joi.object({
GUESTY_API_KEY: Joi.string()
.required()
.description(`Used as username for basic auth token`)
.messages({
// Override the error message if the .required() call fails
"any.required": `Your API key is incorrect, try checking https://support.guesty.com/kb/en/article/generating-an-internal-api-token for help`
}),
GUESTY_API_SECRET: Joi.string()
.required()
.description(`Used as password for basic auth token`)
}).external(async PluginOptions => {
try {
let token = Buffer
.from(`${PluginOptions.GUESTY_API_KEY}:${PluginOptions.GUESTY_API_SECRET}`, 'utf8')
.toString('base64');
await axios.get("/listings", {
headers: {
"Authorization": `Basic ${token}`
}
})
} catch (err) {
throw new Error (
'Cannot access Guesty API with the provided access token. Double check they are correct and try again! '
)
}
})
};