feathers-openweathermap
Version:
A OpenWeatherMap Service for feathers.js applications
160 lines (159 loc) • 5.86 kB
JavaScript
"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 });
exports.Service = void 0;
const source_1 = __importDefault(require("got/dist/source"));
const errors_1 = require("@feathersjs/errors");
const makeOptions = (options) => {
return Object.assign({ v: "2.5", mode: "json", lang: "en", units: "standard" }, options);
};
const baseUrl = "https://api.openweathermap.org/data";
class Service {
constructor(options) {
this.options = makeOptions(options);
}
find(params) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.makeRequest(params.query, params.query.endpoint);
});
}
create(data) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.makeRequest(data, data.endpoint);
});
}
composeSearchParamsFromData(data) {
data = Object.assign({}, data);
const appid = (data === null || data === void 0 ? void 0 : data.appid) || this.options.appid;
const lang = (data === null || data === void 0 ? void 0 : data.lang) || this.options.lang;
const mode = (data === null || data === void 0 ? void 0 : data.mode) || this.options.mode;
const units = (data === null || data === void 0 ? void 0 : data.units) || this.options.units;
const query = {
appid,
lang,
mode,
units
};
if (data.cityName) {
const q = [data.cityName];
if (data.stateCode) {
q.push(data.stateCode);
}
if (data.countryCode) {
q.push(data.countryCode);
}
query.q = q.join(",");
}
else if (data.cityId) {
query.id = data.cityId;
}
else if (data.lat && data.lon) {
query.lat = data.lat;
query.lon = data.lon;
}
else if (data.zipCode) {
const zip = [data.zipCode];
if (data.countryCode) {
zip.push(data.countryCode);
}
query.zip = zip.join(",");
}
const keysToIgnore = [
"cityName",
"stateCode",
"countryCode",
"cityId",
"lat",
"lon",
"zipCode",
"countryCode"
];
for (const key in data) {
if (keysToIgnore.includes(key)) {
continue;
}
query[key] = data[key];
}
return query;
}
getUrl(data, endpoint) {
const v = (data === null || data === void 0 ? void 0 : data.v) || this.options.v;
return `${baseUrl}/${v}/${endpoint}`;
}
makeRequest(data, endpoint) {
return __awaiter(this, void 0, void 0, function* () {
try {
const queryParams = this.composeSearchParamsFromData(data);
const result = yield (0, source_1.default)(this.getUrl(data, endpoint), {
searchParams: queryParams
});
if (queryParams.mode === "json") {
return JSON.parse(result.body);
}
else {
return result.body;
}
}
catch (err) {
new errors_1.BadRequest("unprocessable", err);
}
});
}
currentWeatherData(data) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.makeRequest(data, "weather");
});
}
oneCall(data) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.makeRequest(data, "onecall");
});
}
fiveDay3HourForecast(data) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.makeRequest(data, "forecast");
});
}
hourlyForecast4Days(data) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.makeRequest(data, "forecast/hourly");
});
}
dailyForecast16Days(data) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.makeRequest(data, "forecast/daily");
});
}
climaticForecast30Days(data) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.makeRequest(data, "forecast/climate");
});
}
airPollutionCurrent(data) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.makeRequest(data, "air_pollution");
});
}
airPollutionForecast(data) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.makeRequest(data, "air_pollution/forecast");
});
}
airPollutionHistorical(data) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.makeRequest(data, "air_pollution/history");
});
}
}
exports.Service = Service;