1
0
mirror of https://github.com/trambarhq/relaks-wordpress-example.git synced 2025-09-25 14:59:09 +02:00
Files
relaks-wordpress-example/server/json-retriever.js
Chung Leong 36a9d0c619 Removing unneeded props (issue #12).
Moved code into separate modules.
2019-01-23 13:59:56 +01:00

33 lines
819 B
JavaScript

const CrossFetch = require('cross-fetch');
const WORDPRESS_HOST = process.env.WORDPRESS_HOST;
async function fetch(path) {
let url = `http://${WORDPRESS_HOST}${path}`;
let res = await CrossFetch(url);
let object = await res.json();
removeSuperfluousProps(path, object);
let text = JSON.stringify(object);
return { path, text };
}
function removeSuperfluousProps(path, object) {
if (object instanceof Array) {
let objects = object;
for (let object of objects) {
return removeSuperfluousProps(path, object);
}
} else if (object instanceof Object) {
delete object._links;
if (path === '/wp-json/') {
delete object.routes;
} else {
delete object.guid;
}
}
}
module.exports = {
fetch,
};