UNPKG

weather-cli-nk_sm

Version:
51 lines (50 loc) 2.29 kB
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()); }); }; import axios from 'axios'; import StorageService, { DBKeys } from './StorageService.js'; export default class APIService { static getKey(key, DBkey, keyName) { return __awaiter(this, void 0, void 0, function* () { if (key) return key; let keyValue = yield StorageService.get(DBKeys[DBkey]); if (!keyValue) throw new Error(`No ${keyName} in the database!`); return keyValue; }); } static getWeather() { return __awaiter(this, void 0, void 0, function* () { let { API_KEY: apiKey, CITY: city } = process.env; apiKey = yield this.getKey(apiKey, DBKeys.API_KEY, 'api-key'); city = yield this.getKey(city, DBKeys.CITY, 'city'); const { data } = yield axios.get(`${this.baseUrl}/geo/1.0/direct`, { params: { q: city, limit: 1, appid: apiKey, }, }); if (!data.length) throw new Error('Sorry, we do not know such a place'); const { data: weatherData } = yield axios.get(`${this.baseUrl}/data/2.5/weather`, { params: { lat: data[0].lat, lon: data[0].lon, appid: apiKey, units: 'metric', lang: 'ru', }, }); return weatherData; }); } } APIService.baseUrl = 'https://api.openweathermap.org';