esios-api-client
Version:
Another ESIOS api client
115 lines (114 loc) • 4.7 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.ESIOSApiClient = void 0;
const enums_1 = require("../enums");
const indicator_id_enum_1 = require("../enums/indicator-id.enum");
const date_formatter_function_1 = require("../functions/date-formatter.function");
const indicator_class_1 = require("./indicator/indicator.class");
const pvpc_day_class_1 = require("./pvpc/pvpc-day.class");
class ESIOSApiClient {
constructor() {
this.baseUrl = 'https://api.esios.ree.es';
this.auth = {
set: (key) => {
this.authentication = key;
},
get: () => {
return this.authentication;
}
};
this.archives = {
pvpc: async (date, locale = 'es') => {
// Min date is 2021/06/01, before have another format
const minDate = new Date('2021/06/01');
// Check it
if (date < minDate) {
throw new Error('Date is too early. Only supported from 2021-06-01');
}
// Archive ID to get
const action = 'download_json';
const [formatted] = date.toISOString().split("T");
const params = new URLSearchParams({
date: formatted,
locale
});
// Format url
const url = `${this.baseUrl}/archives/${enums_1.ArchiveID.PVPC}/${action}?${params.toString()}`;
try {
const response = await fetch(url);
if (!response.ok) {
throw 'Something went wrong';
}
const json = await response.json();
if ('message' in json) {
throw json.message;
}
return new pvpc_day_class_1.PVPCDay(json);
}
catch (error) {
throw new Error(typeof error === 'string' ? error : 'Error fetching archives');
}
}
};
this.indicators = {
it: async (indicator, date, geo, locale = 'es') => {
const authentication = await this.loadAuthentication();
if (!authentication) {
throw new Error('Could not load authentication');
}
const start = new Date(date);
start.setHours(0, 0, 0, 0);
const start_date = (0, date_formatter_function_1.formatDate)(start);
const end = new Date(date);
end.setHours(23, 0, 0, 0);
const end_date = (0, date_formatter_function_1.formatDate)(end);
const params = new URLSearchParams({
start_date,
end_date,
"geo_ids[]": `${geo}`,
locale
});
const url = `${this.baseUrl}/indicators/${indicator}?${params.toString()}`;
try {
const response = await fetch(url, {
headers: {
'x-api-key': `${authentication}`
}
});
if (!response.ok) {
throw 'Something went wrong';
}
const json = await response.json();
if ('message' in json) {
throw json.message;
}
return new indicator_class_1.Indicator(json.indicator);
}
catch (error) {
console.log(error);
throw new Error(typeof error === 'string' ? error : 'Error fetching indicator');
}
},
pvpc: async (date, geo, locale = 'es') => {
return this.indicators.it(indicator_id_enum_1.IndicatorID.PVPC, date, geo, locale);
},
spot: async (date, geo, locale = 'es') => {
return this.indicators.it(indicator_id_enum_1.IndicatorID.SPOT, date, geo, locale);
}
};
}
async loadAuthentication() {
if (this.authentication) {
return this.authentication;
}
try {
const url = 'https://www.esios.ree.es/environment.json';
const response = await fetch(url);
const json = await response.json();
this.authentication = json.API_X_KEY;
}
catch { }
return this.authentication;
}
}
exports.ESIOSApiClient = ESIOSApiClient;
;