UNPKG

hsl-city-bikes

Version:

NPM package for fetching data about city bike station in Helsinki.

99 lines (98 loc) 4 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.fetchNearestBikeRentalStations = exports.fetchBikeRentalStations = exports.graphqlApiUrl_get = exports.graphqlApiUrl_set = void 0; global.fetch = require("node-fetch"); const graphql_request_1 = require("graphql-request"); let api = "https://api.digitransit.fi/routing/v1/routers/hsl/index/graphql"; /** * Change the GraphQL API URL from the default one. * * @param {string} url The new API URL. */ exports.graphqlApiUrl_set = (url) => (api = url); /** * Get the current API URL that is being used by the package. */ exports.graphqlApiUrl_get = () => api; function fetchBikeRentalStations(stationId) { return __awaiter(this, void 0, void 0, function* () { if (stationId) { const q1 = `{ bikeRentalStation(id:"${stationId}") { stationId name bikesAvailable spacesAvailable lat lon allowDropoff } }`; return graphql_request_1.request(api, q1) .then((res) => res.bikeRentalStation || Promise.reject("No bike rental stations found.")) .catch((e) => Promise.reject(e)); } const q2 = `{ bikeRentalStations { stationId name bikesAvailable spacesAvailable lat lon allowDropoff } }`; return graphql_request_1.request(api, q2) .then((res) => res.bikeRentalStations) .catch((e) => Promise.reject(e)); }); } exports.fetchBikeRentalStations = fetchBikeRentalStations; /** * Fetch the nearest bike rental stations. * * @param {number} latitude The latitudal position from where to measure the distance. * @param {number} longitude The longitudal position from where to measure the distance. * @param {number | undefined} maxResults The maximum amount of result to get. Note that this is only a maximum, and that the API usually do not give more than around 15 results. * @param {number | undefined} maxDistance Serach for stations within a certain radius. The distance unit is meters. * * @returns An Array of nodes. A node contains a bike rental station and its distances to the specified location. The nodes are ordered according to the distance, with the closest being the first element. */ function fetchNearestBikeRentalStations(lat, lon, maxResults, maxDistance) { return __awaiter(this, void 0, void 0, function* () { const query = `{ nearest(lat: ${lat}, lon: ${lon}, ${maxResults === undefined ? "" : `maxResults: ${maxResults}, `}${maxDistance === undefined ? "" : `maxDistance: ${maxDistance}, `}filterByPlaceTypes: [BICYCLE_RENT]) { edges { node { place { lat lon ...on BikeRentalStation { name stationId spacesAvailable bikesAvailable allowDropoff } } distance } } } }`; return graphql_request_1.request(api, query) .then((res) => res.nearest.edges.map((e) => e.node)) .catch((e) => Promise.reject(e)); }); } exports.fetchNearestBikeRentalStations = fetchNearestBikeRentalStations;