From 33e9644e1a8d4eb385a7d1bc56865d2aae5f11dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Pluta?= Date: Tue, 12 Nov 2024 22:46:17 +0100 Subject: [PATCH] Make Tauron service supports Dayjs --- package-lock.json | 6 ++++++ package.json | 1 + src/tauron/index.ts | 32 ++++++++++++++++++++------------ 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3a4e420..37967bb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,7 @@ "dependencies": { "axios": "^1.7.7", "axios-cookiejar-support": "^5.0.3", + "dayjs": "^1.11.13", "tough-cookie": "^5.0.0", "tough-cookie-file-store": "^2.0.3", "yaml": "^2.6.0" @@ -469,6 +470,11 @@ "node": ">= 0.8" } }, + "node_modules/dayjs": { + "version": "1.11.13", + "resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.11.13.tgz", + "integrity": "sha512-oaMBel6gjolK862uaPQOVTA7q3TZhuSvuMQAAglQDOWYO9A91IrAOUJEyKVlqJlHE0vq5p5UXxzdPfMH/x6xNg==" + }, "node_modules/debug": { "version": "4.3.7", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.7.tgz", diff --git a/package.json b/package.json index 9d7740a..406b7f2 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "dependencies": { "axios": "^1.7.7", "axios-cookiejar-support": "^5.0.3", + "dayjs": "^1.11.13", "tough-cookie": "^5.0.0", "tough-cookie-file-store": "^2.0.3", "yaml": "^2.6.0" diff --git a/src/tauron/index.ts b/src/tauron/index.ts index 8b6ec86..f2f0e9b 100644 --- a/src/tauron/index.ts +++ b/src/tauron/index.ts @@ -5,6 +5,7 @@ import { CookieJar } from 'tough-cookie'; import { FileCookieStore } from 'tough-cookie-file-store'; import { EnergyDTO, EnergyRequestDTO, Payload, PowerDTO, PowerRequestDTO, ReadingDTO, ReadingRequestDTO } from "./types"; import { TauronConfig } from '../config'; +import { Dayjs } from 'dayjs'; export * from './types'; @@ -13,12 +14,14 @@ const BASE_URL = 'https://elicznik.tauron-dystrybucja.pl'; const ENERGY_API = '/energia/api'; const POWER_API = '/moc/api'; const READING_API = '/odczyty/api'; +const DATE_FORMAT = 'DD.MM.YYYY'; + /** * Utility class for Tauron API. * @param config - configuration of service */ -export const Tauron = class { +export class Tauron { #http: Axios; #config: TauronConfig; @@ -113,12 +116,14 @@ export const Tauron = class { * @param type - type of measurement, can be 'consum' (regular one) or 'average' * @returns the energy data from Tauron API */ - async getEnergyForDay(day: string): Promise { + async getEnergyForDay(day: Dayjs): Promise { + const date = day.format(DATE_FORMAT); + return await this.getEnergy({ type: 'consum', profile: "full time", - from: day, - to: day, + from: date, + to: date, }); } @@ -129,11 +134,11 @@ export const Tauron = class { * @param type - type of measurement, can be 'consum' (regular one) or 'average' * @returns the energy data from Tauron API */ - async getEnergyForRange(from: string, to: string): Promise { + async getEnergyForRange(from: Dayjs, to: Dayjs): Promise { return await this.getEnergy({ type: 'consum', - from, - to, + from: from.format(DATE_FORMAT), + to: to.format(DATE_FORMAT), profile: "range" }); } @@ -168,8 +173,11 @@ export const Tauron = class { * @param to - the measurement ending date * @returns the power data from Tauron API */ - async getPowerForRange(from: string, to: string): Promise { - return await this.getPower({ from, to }); + async getPowerForRange(from: Dayjs, to: Dayjs): Promise { + return await this.getPower({ + from: from.format(DATE_FORMAT), + to: to.format(DATE_FORMAT) + }); } /** @@ -187,10 +195,10 @@ export const Tauron = class { * @param to - the measurement ending date * @returns the readings from Tauron API */ - async getReadingForRange(from: string, to: string): Promise { + async getReadingForRange(from: Dayjs, to: Dayjs): Promise { return await this.getReading({ - from, - to, + from: from.format(DATE_FORMAT), + to: to.format(DATE_FORMAT), type: 'energia-pobrana', }); }