gatsby-source-mta
Version:
Gatsby source plugin for fetching all MTA (NYC Subway) stations.
42 lines (41 loc) • 1.52 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.sourceNodes = void 0;
const axios_1 = __importDefault(require("axios"));
const sourceNodes = async (gatsbyApi) => {
const { actions, reporter, createNodeId, createContentDigest } = gatsbyApi;
const { createNode } = actions;
const sourcingTimer = reporter.activityTimer(`Sourcing from MTA API`);
sourcingTimer.start();
const { data, status } = await (0, axios_1.default)(`https://data.cityofnewyork.us/resource/kk4q-3rt2.json`);
if (status !== 200) {
sourcingTimer.panic({
id: 'API_DOWN',
context: {
sourceMessage: `Sourcing from the MTA API failed!`
}
});
return;
}
sourcingTimer.setStatus(`Creating nodes for ${data.length} stations...`);
data.forEach((station) => createNode({
// Station details
name: station.name,
coordinates: {
lat: station.the_geom.coordinates[0],
lng: station.the_geom.coordinates[1],
},
lines: station.line.split('-'),
// Gatsby internal
id: createNodeId(`MTAStation-${station.objectid}`),
internal: {
type: 'MTAStations',
contentDigest: createContentDigest(station)
}
}));
sourcingTimer.end();
};
exports.sourceNodes = sourceNodes;