UNPKG

5.86 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4 return new (P || (P = Promise))(function (resolve, reject) {
5 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7 function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8 step((generator = generator.apply(thisArg, _arguments || [])).next());
9 });
10};
11var __importDefault = (this && this.__importDefault) || function (mod) {
12 return (mod && mod.__esModule) ? mod : { "default": mod };
13};
14Object.defineProperty(exports, "__esModule", { value: true });
15exports.Service = void 0;
16const source_1 = __importDefault(require("got/dist/source"));
17const errors_1 = require("@feathersjs/errors");
18const makeOptions = (options) => {
19 return Object.assign({ v: "2.5", mode: "json", lang: "en", units: "standard" }, options);
20};
21const baseUrl = "https://api.openweathermap.org/data";
22class Service {
23 constructor(options) {
24 this.options = makeOptions(options);
25 }
26 find(params) {
27 return __awaiter(this, void 0, void 0, function* () {
28 return yield this.makeRequest(params.query, params.query.endpoint);
29 });
30 }
31 create(data) {
32 return __awaiter(this, void 0, void 0, function* () {
33 return yield this.makeRequest(data, data.endpoint);
34 });
35 }
36 composeSearchParamsFromData(data) {
37 data = Object.assign({}, data);
38 const appid = (data === null || data === void 0 ? void 0 : data.appid) || this.options.appid;
39 const lang = (data === null || data === void 0 ? void 0 : data.lang) || this.options.lang;
40 const mode = (data === null || data === void 0 ? void 0 : data.mode) || this.options.mode;
41 const units = (data === null || data === void 0 ? void 0 : data.units) || this.options.units;
42 const query = {
43 appid,
44 lang,
45 mode,
46 units
47 };
48 if (data.cityName) {
49 const q = [data.cityName];
50 if (data.stateCode) {
51 q.push(data.stateCode);
52 }
53 if (data.countryCode) {
54 q.push(data.countryCode);
55 }
56 query.q = q.join(",");
57 }
58 else if (data.cityId) {
59 query.id = data.cityId;
60 }
61 else if (data.lat && data.lon) {
62 query.lat = data.lat;
63 query.lon = data.lon;
64 }
65 else if (data.zipCode) {
66 const zip = [data.zipCode];
67 if (data.countryCode) {
68 zip.push(data.countryCode);
69 }
70 query.zip = zip.join(",");
71 }
72 const keysToIgnore = [
73 "cityName",
74 "stateCode",
75 "countryCode",
76 "cityId",
77 "lat",
78 "lon",
79 "zipCode",
80 "countryCode"
81 ];
82 for (const key in data) {
83 if (keysToIgnore.includes(key)) {
84 continue;
85 }
86 query[key] = data[key];
87 }
88 return query;
89 }
90 getUrl(data, endpoint) {
91 const v = (data === null || data === void 0 ? void 0 : data.v) || this.options.v;
92 return `${baseUrl}/${v}/${endpoint}`;
93 }
94 makeRequest(data, endpoint) {
95 return __awaiter(this, void 0, void 0, function* () {
96 try {
97 const queryParams = this.composeSearchParamsFromData(data);
98 const result = yield (0, source_1.default)(this.getUrl(data, endpoint), {
99 searchParams: queryParams
100 });
101 if (queryParams.mode === "json") {
102 return JSON.parse(result.body);
103 }
104 else {
105 return result.body;
106 }
107 }
108 catch (err) {
109 new errors_1.BadRequest("unprocessable", err);
110 }
111 });
112 }
113 currentWeatherData(data) {
114 return __awaiter(this, void 0, void 0, function* () {
115 return yield this.makeRequest(data, "weather");
116 });
117 }
118 oneCall(data) {
119 return __awaiter(this, void 0, void 0, function* () {
120 return yield this.makeRequest(data, "onecall");
121 });
122 }
123 fiveDay3HourForecast(data) {
124 return __awaiter(this, void 0, void 0, function* () {
125 return yield this.makeRequest(data, "forecast");
126 });
127 }
128 hourlyForecast4Days(data) {
129 return __awaiter(this, void 0, void 0, function* () {
130 return yield this.makeRequest(data, "forecast/hourly");
131 });
132 }
133 dailyForecast16Days(data) {
134 return __awaiter(this, void 0, void 0, function* () {
135 return yield this.makeRequest(data, "forecast/daily");
136 });
137 }
138 climaticForecast30Days(data) {
139 return __awaiter(this, void 0, void 0, function* () {
140 return yield this.makeRequest(data, "forecast/climate");
141 });
142 }
143 airPollutionCurrent(data) {
144 return __awaiter(this, void 0, void 0, function* () {
145 return yield this.makeRequest(data, "air_pollution");
146 });
147 }
148 airPollutionForecast(data) {
149 return __awaiter(this, void 0, void 0, function* () {
150 return yield this.makeRequest(data, "air_pollution/forecast");
151 });
152 }
153 airPollutionHistorical(data) {
154 return __awaiter(this, void 0, void 0, function* () {
155 return yield this.makeRequest(data, "air_pollution/history");
156 });
157 }
158}
159exports.Service = Service;