gatsby-source-ghibli
Version:
Gatsby source plugin for fetching data from Studio Ghibli API.
129 lines (120 loc) • 3.67 kB
JavaScript
const fetch = require('node-fetch');
exports.sourceNodes = (
{ actions, createNodeId, createContentDigest },
configOptions
) => {
const { createNode } = actions;
delete configOptions.plugins;
const processFilm = film => {
const nodeData = Object.assign({}, film, {
id: createNodeId(`ghibli-film-${film.id}`),
parent: null,
children: [],
internal: {
type: 'GhibliFilm',
content: JSON.stringify(film),
contentDigest: createContentDigest(film),
},
});
return nodeData;
};
const processPerson = person => {
const nodeData = Object.assign({}, person, {
id: createNodeId(`ghibli-person-${person.id}`),
parent: null,
children: [],
internal: {
type: 'GhibliPerson',
content: JSON.stringify(person),
contentDigest: createContentDigest(person),
},
});
return nodeData;
};
const processLocation = location => {
const nodeData = Object.assign({}, location, {
id: createNodeId(`ghibli-location-${location.id}`),
parent: null,
children: [],
internal: {
type: 'GhibliLocation',
content: JSON.stringify(location),
contentDigest: createContentDigest(location),
},
});
return nodeData;
};
const processSpecies = species => {
const nodeData = Object.assign({}, species, {
id: createNodeId(`ghibli-species-${species.id}`),
parent: null,
children: [],
internal: {
type: 'GhibliSpecies',
content: JSON.stringify(species),
contentDigest: createContentDigest(species),
},
});
return nodeData;
};
const processVehicle = vehicle => {
const nodeData = Object.assign({}, vehicle, {
id: createNodeId(`ghibli-vehicle-${vehicle.id}`),
parent: null,
children: [],
internal: {
type: 'GhibliVehicle',
content: JSON.stringify(vehicle),
contentDigest: createContentDigest(vehicle),
},
});
return nodeData;
};
const filmsUrl = 'https://ghibliapi.herokuapp.com/films';
const peopleUrl = 'https://ghibliapi.herokuapp.com/people';
const locationsUrl = 'https://ghibliapi.herokuapp.com/locations';
const speciesUrl = 'https://ghibliapi.herokuapp.com/species';
const vehiclesUrl = 'https://ghibliapi.herokuapp.com/vehicles';
return Promise.all([
fetch(filmsUrl)
.then(response => response.json())
.then(data => {
data.forEach(film => {
const nodeData = processFilm(film);
createNode(nodeData);
});
}),
fetch(peopleUrl)
.then(response => response.json())
.then(data => {
data.forEach(person => {
const nodeData = processPerson(person);
createNode(nodeData);
});
}),
fetch(locationsUrl)
.then(response => response.json())
.then(data => {
data.forEach(location => {
const nodeData = processLocation(location);
createNode(nodeData);
});
}),
fetch(speciesUrl)
.then(response => response.json())
.then(data => {
data.forEach(species => {
const nodeData = processSpecies(species);
createNode(nodeData);
});
}),
fetch(vehiclesUrl)
.then(response => response.json())
.then(data => {
data.forEach(vehicle => {
const nodeData = processVehicle(vehicle);
createNode(nodeData);
});
}),
]).catch(err => console.error(err));
};