@aleph/gatsby-source-greenhouse-job-board
Version:
Gatsby source plugin for pulling offices, departments, and jobs into Gatsby from the Greenhouse Job Board API. It creates links between offices, departments, and jobs so they can be queried in Gatsby using GraphQL.
76 lines (70 loc) • 2.69 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
const _require = require('./fetch'),
fetchJobs = _require.fetchJobs,
fetchOffices = _require.fetchOffices,
fetchDepartments = _require.fetchDepartments;
const _require2 = require(`./normalize`),
filterResponseForIds = _require2.filterResponseForIds,
buildNodesFromResponse = _require2.buildNodesFromResponse,
linkParentChildReferences = _require2.linkParentChildReferences,
linkForeignReferences = _require2.linkForeignReferences;
const chalk = require('chalk');
const log = message => console.log('\n', message);
exports.sourceNodes = /*#__PURE__*/function () {
var _ref = (0, _asyncToGenerator2.default)(function* (gatsby, pluginOptions) {
const actions = gatsby.actions;
const createNode = actions.createNode;
const boardToken = pluginOptions.boardToken;
let offices, departments, jobs;
let nodes;
try {
// Fetch data from the Greenhouse APIs
offices = yield fetchOffices(boardToken);
departments = yield fetchDepartments(boardToken);
jobs = yield fetchJobs(boardToken);
} catch (error) {
log(`
${chalk.red.bold('error')} Fetching data from Greenhouse failed
`);
process.exit(1);
}
log(`
${chalk.blue('info')} fetched from Greenhouse API:
${chalk.bold(jobs.length)} jobs,
${chalk.bold(offices.length)} offices,
${chalk.bold(departments.length)} departments
`);
// Greenhouse API will return "No Office" and "No Department" entities with an ID of 0. Let's remove these.
offices = filterResponseForIds(offices);
departments = filterResponseForIds(departments);
jobs = filterResponseForIds(jobs);
// Construct the node objects from the API responses
nodes = buildNodesFromResponse({
offices,
departments,
jobs
});
// Link Offices to their parents/children
// Link Departments to their parents/children
nodes = linkParentChildReferences(nodes);
// Link Offices to Departments and Jobs
// Link Departments to Jobs
// Link Jobs to Departments and Offices
nodes = linkForeignReferences(nodes);
// Create the nodes with Gatsby
yield Promise.all(nodes.map( /*#__PURE__*/function () {
var _ref2 = (0, _asyncToGenerator2.default)(function* (node) {
return createNode(node);
});
return function (_x3) {
return _ref2.apply(this, arguments);
};
}()));
return;
});
return function (_x, _x2) {
return _ref.apply(this, arguments);
};
}();