gatsby-source-itchio
Version:
A GatsbyJS source plugin for itch.io
47 lines (40 loc) • 1.66 kB
JavaScript
;
var _axios = _interopRequireDefault(require("axios"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
exports.sourceNodes = async ({
actions,
createNodeId,
createContentDigest
}, configOptions) => {
const {
createNode
} = actions;
const {
key
} = configOptions;
const apiUrl = `https://itch.io/api/1/${key}/my-games`;
const {
data
} = await _axios.default.get(apiUrl);
const {
games
} = data;
const gameNodes = games.map(game => {
const nodeId = createNodeId(`itchio-game-${game.id}`);
const nodeContent = JSON.stringify(game);
const nodeData = _objectSpread({}, game, {
id: nodeId,
parent: null,
children: [],
internal: {
type: "ItchioGame",
content: nodeContent,
contentDigest: createContentDigest(game)
}
});
return nodeData;
});
gameNodes.forEach(node => createNode(node));
};