gatsby-source-feliks
Version:
Gatsby source plugin for building websites using Apricore's Feliks as a data source
76 lines (59 loc) • 3.57 kB
JavaScript
;
var _loading = require("./utils/loading");
var _formatting = require("./utils/formatting");
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
exports.sourceNodes = function () {
var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(_ref2, pluginOptions) {
var boundActionCreators = _ref2.boundActionCreators;
var createNode, _pluginOptions$apiURL, apiURL, _pluginOptions$userna, username, _pluginOptions$passwo, password, _pluginOptions$mockPa, mockPath, _pluginOptions$useMoc, useMock, _pluginOptions$exclud, excludeStatusCodes, data, includeCourse;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
createNode = boundActionCreators.createNode;
_pluginOptions$apiURL = pluginOptions.apiURL, apiURL = _pluginOptions$apiURL === undefined ? "" : _pluginOptions$apiURL, _pluginOptions$userna = pluginOptions.username, username = _pluginOptions$userna === undefined ? "" : _pluginOptions$userna, _pluginOptions$passwo = pluginOptions.password, password = _pluginOptions$passwo === undefined ? "" : _pluginOptions$passwo, _pluginOptions$mockPa = pluginOptions.mockPath, mockPath = _pluginOptions$mockPa === undefined ? "" : _pluginOptions$mockPa, _pluginOptions$useMoc = pluginOptions.useMock, useMock = _pluginOptions$useMoc === undefined ? false : _pluginOptions$useMoc, _pluginOptions$exclud = pluginOptions.excludeStatusCodes, excludeStatusCodes = _pluginOptions$exclud === undefined ? [] : _pluginOptions$exclud;
data = void 0;
if (!useMock) {
_context.next = 7;
break;
}
data = (0, _loading.loadMockingData)(mockPath).data;
_context.next = 10;
break;
case 7:
_context.next = 9;
return (0, _loading.fetch)(username, password, apiURL).catch(function (err) {
return err !== undefined && err.response !== undefined && err.response.data !== undefined && err.response.data.data;
});
case 9:
data = _context.sent;
case 10:
if (data) {
_context.next = 12;
break;
}
return _context.abrupt("return");
case 12:
includeCourse = function includeCourse(c) {
return !excludeStatusCodes.includes(c.enrollment_status.status_code) && (c.keywords || []).reduce(function (acc, curr) {
return curr.includes("Hjemmeside@ ") || acc;
}, false);
};
Object.values(data).forEach(function (course) {
if (includeCourse(course)) {
var courseNode = (0, _formatting.formatCourseNode)(course);
createNode(courseNode);
}
});
return _context.abrupt("return");
case 15:
case "end":
return _context.stop();
}
}
}, _callee, undefined);
}));
return function (_x, _x2) {
return _ref.apply(this, arguments);
};
}();