gatsby-source-buzzsprout
Version:
Gatsby plugin to load podcast episodes from Buzzsprout.
107 lines (85 loc) • 4 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var path = require('path');
var createNodeHelpers = require('gatsby-node-helpers').default;
var Buzzsprout = require('./lib/Buzzsprout');
var _createNodeHelpers = createNodeHelpers({
typePrefix: "Buzzsprout"
}),
createNodeFactory = _createNodeHelpers.createNodeFactory;
var PodcastEpisodeNode = createNodeFactory('PodcastEpisode', function (node) {
if (!node.slug && node.audio_url) {
node.slug = path.basename(node.audio_url).split('.').slice(0, -1).join('.') // buzzsprout prepends an episode ID to the beginning of its slugs
.replace(/^\d+-/g, '');
}
return node;
});
var PLUGIN_NAME = 'gatsby-source-buzzsprout';
exports.sourceNodes =
/*#__PURE__*/
function () {
var _ref = (0, _asyncToGenerator2.default)(
/*#__PURE__*/
_regenerator.default.mark(function _callee(_ref2, _ref3) {
var _ref2$actions, createNode, setPluginStatus, _ref3$query, query, token, podcastId, errorAboutGatsbyPlugins, errorAboutBuzzsproutAuth, errorAboutPodcastId, buzzsprout, episodes;
return _regenerator.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_ref2$actions = _ref2.actions, createNode = _ref2$actions.createNode, setPluginStatus = _ref2$actions.setPluginStatus;
_ref3$query = _ref3.query, query = _ref3$query === void 0 ? {} : _ref3$query, token = _ref3.token, podcastId = _ref3.podcastId;
if (!(process.env.NODE_ENV !== 'production')) {
_context.next = 10;
break;
}
errorAboutGatsbyPlugins = 'To read more about configuring Gatsby plugins, read more at https://www.gatsbyjs.org/docs/using-a-plugin-in-your-site/.';
errorAboutBuzzsproutAuth = 'To read more about Buzzsprout authentication, read more at https://github.com/Buzzsprout/buzzsprout-api#authentication.';
errorAboutPodcastId = "To get your podcast ID, login to Buzzsprout, click 'My Account' and then locate the ID next to the title of your podcast.";
if (token) {
_context.next = 8;
break;
}
throw new Error("It looks like you forgot your Buzzsprout Auth token! Make sure to pass your token into '" + PLUGIN_NAME + "' options in 'gatsby-config.js'. \n" + errorAboutBuzzsproutAuth + " \n" + errorAboutGatsbyPlugins);
case 8:
if (podcastId) {
_context.next = 10;
break;
}
throw new Error("It looks like you forgot your Buzzsprout Podcast ID! Make sure to pass the ID into '" + PLUGIN_NAME + "' options in 'gatsby-config.js'. \n" + errorAboutPodcastId + " \n" + errorAboutGatsbyPlugins);
case 10:
_context.prev = 10;
buzzsprout = new Buzzsprout({
token: token,
podcastId: podcastId
});
_context.next = 14;
return buzzsprout.getEpisodes();
case 14:
episodes = _context.sent;
episodes.map(function (episode) {
return PodcastEpisodeNode(episode);
}).forEach(function (node) {
return createNode(node);
});
setPluginStatus({
lastFetched: Date.now()
});
_context.next = 22;
break;
case 19:
_context.prev = 19;
_context.t0 = _context["catch"](10);
console.error('FAIL:', _context.t0);
case 22:
case "end":
return _context.stop();
}
}
}, _callee, null, [[10, 19]]);
}));
return function (_x, _x2) {
return _ref.apply(this, arguments);
};
}();