fruit-company
Version:
Apple services library
117 lines • 5.04 kB
JavaScript
;
/*
* MIT No Attribution
*
* Copyright 2024 Peter "Kevin" Contreras
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
* software and associated documentation files (the "Software"), to deal in the Software
* without restriction, including without limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
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.WeatherAttribution = exports.WeatherQuery = exports.allWeatherDataSets = void 0;
const serene_front_1 = require("serene-front");
const models_1 = require("./models");
const weather_1 = require("./models/weather");
exports.allWeatherDataSets = Object.freeze([
'currentWeather',
'forecastDaily',
'forecastHourly',
'forecastNextHour',
'weatherAlerts',
]);
class WeatherQuery {
constructor(options) {
this.options = options;
}
prepare({}) {
const url = new URL(`${models_1.weatherKitUrl}/api/v1/weather/${this.options.language}/${this.options.location.latitude}/${this.options.location.longitude}`);
for (const [key, value] of Object.entries(this.options)) {
if (key === "location" || key === "language") {
// Skip, part of the path.
continue;
}
else if (Array.isArray(value)) {
url.searchParams.append(key, value.join(","));
}
else if (value instanceof Date) {
url.searchParams.append(key, value.toISOString());
}
else if (typeof value === "number") {
url.searchParams.append(key, String(value));
}
else if (typeof value === "string") {
url.searchParams.append(key, value);
}
else {
throw new Error(`GetWeatherOptions.${key} invalid <${value}>`);
}
}
if (this.options.countryCode !== undefined) {
// The documentation for how to specify a country code is likely wrong.
// See <https://forums.developer.apple.com/forums/thread/723800> for discussion.
// Including both parameters here just in case.
url.searchParams.append("country", this.options.countryCode);
}
return new Request(url);
}
parse(_a) {
return __awaiter(this, arguments, void 0, function* ({ fetchResponse }) {
if (!fetchResponse.ok) {
throw new serene_front_1.RESTError(fetchResponse.status, fetchResponse.statusText, `<${fetchResponse.url}>`);
}
const raw = yield fetchResponse.text();
return (0, weather_1.parseWeather)(raw);
});
}
toString() {
return `WeatherQuery(${JSON.stringify(this.options)})`;
}
}
exports.WeatherQuery = WeatherQuery;
class WeatherAttribution {
constructor(options) {
this.options = options;
}
prepare({}) {
const url = new URL(`${models_1.weatherKitUrl}/attribution/${this.options.language}`);
return new Request(url);
}
parse(_a) {
return __awaiter(this, arguments, void 0, function* ({ fetchResponse }) {
const raw = yield fetchResponse.text();
const object = JSON.parse(raw, (key, value) => {
if (key.startsWith("logo")) {
return `${models_1.weatherKitUrl}${value}`;
}
else {
return value;
}
});
return object;
});
}
toString() {
return `WeatherAttribution(${JSON.stringify(this.options)})`;
}
}
exports.WeatherAttribution = WeatherAttribution;
//# sourceMappingURL=requests.js.map