-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfetch.js
390 lines (344 loc) · 11.4 KB
/
fetch.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
"use strict";
var _extends2 = require("babel-runtime/helpers/extends");
var _extends3 = _interopRequireDefault(_extends2);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const querystring = require(`querystring`);
const axios = require(`axios`);
const _ = require(`lodash`);
const colorized = require(`./output-color`);
const httpExceptionHandler = require(`./http-exception-handler`);
const minimatch = require(`minimatch`);
/**
* High-level function to coordinate fetching data from a WordPress
* site.
*/
async function fetch({
baseUrl,
_verbose,
_siteURL,
_useACF,
_hostingWPCOM,
_auth,
_perPage,
_excludedRoutes,
typePrefix,
refactoredEntityTypes
}) {
// If the site is hosted on wordpress.com, the API Route differs.
// Same entity types are exposed (excepted for medias and users which need auth)
// but the data model contain slights variations.
let url;
let _accessToken;
if (_hostingWPCOM) {
url = `https://public-api.wordpress.com/wp/v2/sites/${baseUrl}`;
_accessToken = await getWPCOMAccessToken(_auth);
} else {
url = `${_siteURL}/wp-json`;
}
if (_verbose) console.log();
if (_verbose) console.log(colorized.out(`=START PLUGIN=====================================`, colorized.color.Font.FgBlue));
if (_verbose) console.time(`=END PLUGIN=====================================`);
if (_verbose) console.log(``);
if (_verbose) console.log(colorized.out(`Site URL: ${_siteURL}`, colorized.color.Font.FgBlue));
if (_verbose) console.log(colorized.out(`Site hosted on Wordpress.com: ${_hostingWPCOM}`, colorized.color.Font.FgBlue));
if (_verbose) console.log(colorized.out(`Using ACF: ${_useACF}`, colorized.color.Font.FgBlue));
if (_verbose) console.log(colorized.out(`Using Auth: ${_auth.htaccess_user} ${_auth.htaccess_pass}`, colorized.color.Font.FgBlue));
if (_verbose) console.log(colorized.out(`Verbose output: ${_verbose}`, colorized.color.Font.FgBlue));
if (_verbose) console.log(``);
if (_verbose) console.log(colorized.out(`Mama Route URL: ${url}`, colorized.color.Font.FgBlue));
if (_verbose) console.log(``);
// Call the main API Route to discover the all the routes exposed on this API.
let allRoutes;
try {
let options = {
method: `get`,
url: url
};
if (_auth) {
options.auth = {
username: _auth.htaccess_user,
password: _auth.htaccess_pass
};
}
allRoutes = await axios(options);
} catch (e) {
httpExceptionHandler(e);
}
let entities = [];
if (allRoutes) {
let validRoutes = getValidRoutes({
allRoutes,
url,
baseUrl,
_verbose,
_useACF,
_hostingWPCOM,
_excludedRoutes,
typePrefix,
refactoredEntityTypes
});
if (_verbose) console.log(``);
if (_verbose) console.log(colorized.out(`Fetching the JSON data from ${validRoutes.length} valid API Routes...`, colorized.color.Font.FgBlue));
if (_verbose) console.log(``);
for (let route of validRoutes) {
entities = entities.concat((await fetchData({
route,
_verbose,
_perPage,
_hostingWPCOM,
_auth,
_accessToken
})));
if (_verbose) console.log(``);
}
if (_verbose) console.timeEnd(`=END PLUGIN=====================================`);
} else {
console.log(colorized.out(`No routes to fetch. Ending.`, colorized.color.Font.FgRed));
}
return entities;
}
/**
* Gets wordpress.com access token so it can fetch private data like medias :/
*
* @returns
*/
async function getWPCOMAccessToken(_auth) {
let result;
const oauthUrl = `https://public-api.wordpress.com/oauth2/token`;
try {
let options = {
url: oauthUrl,
method: `post`,
data: querystring.stringify({
client_secret: _auth.wpcom_app_clientSecret,
client_id: _auth.wpcom_app_clientId,
username: _auth.wpcom_user,
password: _auth.wpcom_pass,
grant_type: `password`
})
};
result = await axios(options);
result = result.data.access_token;
} catch (e) {
httpExceptionHandler(e);
}
return result;
}
/**
* Fetch the data from specified route url, using the auth provided.
*
* @param {any} route
* @param {any} createNode
*/
async function fetchData({
route,
_verbose,
_perPage,
_hostingWPCOM,
_auth,
_accessToken
}) {
const type = route.type;
const url = route.url;
if (_verbose) console.log(colorized.out(`=== [ Fetching ${type} ] ===`, colorized.color.Font.FgBlue), url);
if (_verbose) console.time(`Fetching the ${type} took`);
let routeResponse = await getPages({ url, _perPage, _hostingWPCOM, _auth, _accessToken }, 1);
let entities = [];
if (routeResponse) {
// Process entities to creating GraphQL Nodes.
if (Array.isArray(routeResponse)) {
routeResponse = routeResponse.map(r => {
return (0, _extends3.default)({}, r, { __type: type });
});
entities = entities.concat(routeResponse);
} else {
routeResponse.__type = type;
entities.push(routeResponse);
}
// WordPress exposes the menu items in meta links.
if (type == `wordpress__wp_api_menus_menus`) {
for (let menu of routeResponse) {
if (menu.meta && menu.meta.links && menu.meta.links.self) {
entities = entities.concat((await fetchData({
route: { url: menu.meta.links.self, type: `${type}_items` },
_verbose,
_perPage,
_hostingWPCOM,
_auth,
_accessToken
})));
}
}
}
// TODO : Get the number of created nodes using the nodes in state.
let length;
if (routeResponse && Array.isArray(routeResponse)) {
length = routeResponse.length;
} else if (routeResponse && !Array.isArray(routeResponse)) {
length = Object.keys(routeResponse).length;
}
console.log(colorized.out(` -> ${type} fetched : ${length}`, colorized.color.Font.FgGreen));
}
if (_verbose) {
console.timeEnd(`Fetching the ${type} took`);
}
return entities;
}
/**
* Get the pages of data
*
* @param {any} url
* @param {number} [page=1]
* @returns
*/
async function getPages({ url, _perPage, _hostingWPCOM, _auth, _accessToken, _verbose }, page = 1) {
try {
let result = [];
const getOptions = page => {
let o = {
method: `get`,
url: `${url}?${querystring.stringify({
per_page: _perPage,
page: page
})}`
};
if (_hostingWPCOM) {
o.headers = {
Authorization: `Bearer ${_accessToken}`
};
} else {
o.auth = _auth ? { username: _auth.htaccess_user, password: _auth.htaccess_pass } : null;
}
return o;
};
// Initial request gets the first page of data
// but also the total count of objects, used for
// multiple concurrent requests (rather than waterfall)
const options = getOptions(page);
const { headers, data } = await axios(options);
result = result.concat(data);
// Some resources have no paging, e.g. `/types`
const wpTotal = headers[`x-wp-total`];
const total = parseInt(wpTotal);
const totalPages = parseInt(headers[`x-wp-totalpages`]);
if (!wpTotal || totalPages <= 1) {
return result;
}
if (_verbose) {
console.log(`\nTotal entities :`, total);
console.log(`Pages to be requested :`, totalPages);
}
// We got page 1, now we want pages 2 through totalPages
const requests = _.range(2, totalPages + 1).map(getPage => {
const options = getOptions(getPage);
return axios(options);
});
return Promise.all(requests).then(pages => {
const data = pages.map(page => page.data);
data.forEach(list => {
result = result.concat(list);
});
return result;
});
} catch (e) {
return httpExceptionHandler(e);
}
}
/**
* Extract valid routes and format its data.
*
* @param {any} allRoutes
* @param {any} url
* @param {any} baseUrl
* @returns
*/
function getValidRoutes({
allRoutes,
url,
baseUrl,
_verbose,
_useACF,
_hostingWPCOM,
_excludedRoutes,
typePrefix,
refactoredEntityTypes
}) {
let validRoutes = [];
for (let key of Object.keys(allRoutes.data.routes)) {
if (_verbose) console.log(`Route discovered :`, key);
let route = allRoutes.data.routes[key];
// A valid route exposes its _links (for now)
if (route._links) {
const entityType = getRawEntityType(route);
// Excluding the "technical" API Routes
const excludedTypes = [undefined, `v2`, `v3`, `1.0`, `2.0`, `embed`, `proxy`, ``, baseUrl];
const routePath = getRoutePath(url, route._links.self);
if (excludedTypes.includes(entityType)) {
if (_verbose) console.log(colorized.out(`Invalid route.`, colorized.color.Font.FgRed));
} else if (_excludedRoutes.some(excludedRoute => minimatch(routePath, excludedRoute))) {
if (_verbose) console.log(colorized.out(`Excluded route from excludedRoutes pattern.`, colorized.color.Font.FgYellow));
} else {
if (_verbose) console.log(colorized.out(`Valid route found. Will try to fetch.`, colorized.color.Font.FgGreen));
const manufacturer = getManufacturer(route);
let rawType = ``;
if (manufacturer === `wp`) {
rawType = `${typePrefix}${entityType}`;
}
let validType;
switch (rawType) {
case `${typePrefix}posts`:
validType = refactoredEntityTypes.post;
break;
case `${typePrefix}pages`:
validType = refactoredEntityTypes.page;
break;
case `${typePrefix}tags`:
validType = refactoredEntityTypes.tag;
break;
case `${typePrefix}categories`:
validType = refactoredEntityTypes.category;
break;
default:
validType = `${typePrefix}${manufacturer.replace(/-/g, `_`)}_${entityType.replace(/-/g, `_`)}`;
break;
}
validRoutes.push({ url: route._links.self, type: validType });
}
} else {
if (_verbose) console.log(colorized.out(`Invalid route.`, colorized.color.Font.FgRed));
}
}
if (_useACF) {
// The OPTIONS ACF API Route is not giving a valid _link so let`s add it manually.
validRoutes.push({
url: `${url}/acf/v2/options`,
type: `${typePrefix}acf_options`
});
if (_verbose) console.log(colorized.out(`Added ACF Options route.`, colorized.color.Font.FgGreen));
if (_hostingWPCOM) {
// TODO : Need to test that out with ACF on Wordpress.com hosted site. Need a premium account on wp.com to install extensions.
if (_verbose) console.log(colorized.out(`The ACF options pages is untested under wordpress.com hosting. Please let me know if it works.`, colorized.color.Effect.Blink));
}
}
return validRoutes;
}
/**
* Extract the raw entity type from route
*
* @param {any} route
*/
const getRawEntityType = route => route._links.self.substring(route._links.self.lastIndexOf(`/`) + 1, route._links.self.length);
/**
* Extract the route path for an endpoint
*
* @param {any} baseUrl The base site URL that should be removed
* @param {any} fullUrl The full URL to retrieve the route path from
*/
const getRoutePath = (baseUrl, fullUrl) => fullUrl.replace(baseUrl, ``);
/**
* Extract the route manufacturer
*
* @param {any} route
*/
const getManufacturer = route => route.namespace.substring(0, route.namespace.lastIndexOf(`/`));
module.exports = fetch;