UNPKG

gatsby-source-personio

Version:

Gatsby JS source plugin for Personio employees API

72 lines (59 loc) 1.96 kB
"use strict"; const fetch = require("node-fetch"); const log = console.log; async function getToken() { let token; try { let response = await fetch(`https://api.personio.de/v1/auth?client_id=YWVkNDkxMjBhZmM5YWU2OTdhNmZhNjQ0&client_secret=YzliMTAxZDBiNjk0OTAyYTZhMzczODQzYmViOTM5ZmE3NmQ1`, { method: "POST", headers: { accept: "application/json" } }); token = `Bearer ${(await response.json()).data.token}`; } catch (err) { log(err); } return token; } async function main() { let result; const token = await getToken(); try { let response = await fetch(`https://api.personio.de/v1/company/employees`, { method: "GET", headers: { Authorization: token } }); result = await response.json(); } catch (err) { log(err); } //log(JSON.stringify(result.data[0].attributes, null, 2)); let node = {}; await personioParser(result.data[0], node); log("Node:", JSON.stringify(node, null, 2)); } async function personioParser(object, node) { if ("attributes" in object) { let attributes = object.attributes; Object.keys(attributes).forEach(key => { const attribute = attributes[key]; if (attribute === null) { return; } else if (typeof attribute === "object") { const label = cleanLabel(attribute.label); if (attribute.value === null) return;else if (typeof attribute.value === "string") node[label] = attribute.value;else if (typeof attribute.value === "number") node[label] = attribute.value.toString();else if (typeof attribute.value === "object") { node[label] = {}; personioParser(attribute.value, node[label]); } } else { if (typeof attribute === "number") node[key] = attribute.toString();else node[key] = attribute; } }); } } function cleanLabel(label) { return label.replace(/[^\w\s]|_/g, "").replace(/\s/g, "_").toLowerCase(); } main();