UNPKG

node-clima

Version:

Simple wrapper for OpenWeatherMap API

78 lines (77 loc) 3.47 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); // current.ts const clima_1 = require("./clima"); const clientrequest_1 = require("./clientrequest"); const querystring_1 = __importDefault(require("querystring")); class Current extends clima_1.Clima { constructor(apiKey) { super(apiKey); this.client = new clientrequest_1.ClientRequest(); } byCityName(cityName, countryCode) { return __awaiter(this, void 0, void 0, function* () { if (typeof cityName !== 'string') { throw new Error('the parameter for byCityName() must be a string.'); } else { const query = { q: cityName, appid: this.apiKey }; const response = yield this.client.makeRequest(this.url + querystring_1.default.stringify(query)); return response; } }); } byCityId(cityId) { return __awaiter(this, void 0, void 0, function* () { if (typeof cityId === 'number') { const query = { id: cityId, appid: this.apiKey }; const response = yield this.client.makeRequest(this.url + querystring_1.default.stringify(query)); return response; } else { throw new Error('the parameter for byCityId() must be a number.'); } }); } byGeographicCoordinates(lat, lon) { return __awaiter(this, void 0, void 0, function* () { if (typeof lat === 'number' && typeof lon === 'number') { const query = { lat: lat, lon: lon, appid: this.apiKey }; const response = yield this.client.makeRequest(this.url + querystring_1.default.stringify(query)); return response; } else { throw new Error('the parameters for byGeographicCoordinates() must be a number.'); } }); } byZipCode(zipCode, countryCode) { return __awaiter(this, void 0, void 0, function* () { if (typeof zipCode !== 'string' || typeof countryCode !== 'string') { throw new Error('the parameters for byZipCode() must be a string.'); } else { const zipQs = `${zipCode},${countryCode}`; const query = { zip: zipQs, appid: this.apiKey }; const response = yield this.client.makeRequest(this.url + querystring_1.default.stringify(query)); return response; } }); } } exports.Current = Current;