From c9e82fd001f81362f330c664852b298a99b35646 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Pluta?= Date: Thu, 14 Nov 2024 14:37:36 +0100 Subject: [PATCH] Refactor code structure --- src/config/index.ts | 4 +-- src/config/types.ts | 32 ------------------- src/consumers/abstract.ts | 11 +++---- src/consumers/index.ts | 3 +- .../{influxdb/index.ts => influxdb.ts} | 5 ++- src/consumers/influxdb/types.ts | 8 ----- src/consumers/{mqtt/index.ts => mqtt.ts} | 8 ++--- src/fetcher/index.ts | 2 +- src/tauron/index.ts | 4 +-- src/types/config.ts | 10 ++++++ src/{consumers/types.ts => types/consumer.ts} | 0 src/{fetcher/types.ts => types/fetcher.ts} | 0 src/types/index.ts | 6 ++++ src/types/influxdb.ts | 6 ++++ .../mqtt/types.ts => types/mqtt.ts} | 3 +- src/{tauron/types.ts => types/tauron.ts} | 22 +++++++++++++ tsconfig.json | 5 ++- 17 files changed, 66 insertions(+), 63 deletions(-) delete mode 100644 src/config/types.ts rename src/consumers/{influxdb/index.ts => influxdb.ts} (93%) delete mode 100644 src/consumers/influxdb/types.ts rename src/consumers/{mqtt/index.ts => mqtt.ts} (88%) create mode 100644 src/types/config.ts rename src/{consumers/types.ts => types/consumer.ts} (100%) rename src/{fetcher/types.ts => types/fetcher.ts} (100%) create mode 100644 src/types/index.ts create mode 100644 src/types/influxdb.ts rename src/{consumers/mqtt/types.ts => types/mqtt.ts} (89%) rename src/{tauron/types.ts => types/tauron.ts} (71%) diff --git a/src/config/index.ts b/src/config/index.ts index a1db7d9..a4a23e6 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -1,8 +1,8 @@ import fs from 'fs'; import yaml from 'yaml'; -import { Config } from './types'; +import { Config } from "@types"; -export * from './types'; +export * from '@types'; export const parseConfig = (path: string): Config => { const text = fs.readFileSync(path, 'utf8'); diff --git a/src/config/types.ts b/src/config/types.ts deleted file mode 100644 index 3ddb1d3..0000000 --- a/src/config/types.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { ConsumerConfig } from "../consumers/types"; - -export type Config = { - /** - * Tauron service configuration - */ - tauron: TauronConfig; - - consumers: Record; -}; - -export type TauronConfig = ConsumerConfig & { - /** - * Path to file containing a username of Tauron account - */ - usernamePath: string; - - /** - * Path to file containing a password of Tauron account - */ - passwordPath: string; - - /** - * The measure point name - should be retrieved with browser dev-tools on website - */ - point: string; - - /** - * The path to the json file with cookie jar - */ - cookiesJarPath?: string; -}; \ No newline at end of file diff --git a/src/consumers/abstract.ts b/src/consumers/abstract.ts index 63e5022..466eb0b 100644 --- a/src/consumers/abstract.ts +++ b/src/consumers/abstract.ts @@ -1,25 +1,24 @@ import { Dayjs } from "dayjs"; +import { Measurement, ConsumerConfig } from "@types"; import { Config } from "../config"; -import { Measurement } from "../fetcher/types"; -import { ConsumerConfig } from "./types"; -export abstract class Consumer { +export abstract class Consumer { public abstract readonly name: string; protected abstract requiredFields: readonly (keyof C)[]; protected abstract publish(config: C, date: Dayjs, measurement: Measurement): void; - #validate(config: Partial): C { + #validate(config: Partial): C & ConsumerConfig { for (const field of this.requiredFields) { if (config[field] === undefined) { throw new Error(`The '${String(field)}' configuration field of'${this.name}' consumer is required`) } } - return config as C; + return config as C & ConsumerConfig; } consume(config: Config, date: Dayjs, measurement: Measurement): void { - const cfg = config.consumers?.[this.name] as Partial; + const cfg = config.consumers?.[this.name] as Partial; if (cfg?.enable !== true) { return diff --git a/src/consumers/index.ts b/src/consumers/index.ts index eeb77ad..64a89d0 100644 --- a/src/consumers/index.ts +++ b/src/consumers/index.ts @@ -1,7 +1,6 @@ import { Dayjs } from "dayjs"; -import { Config } from "../config"; +import { Config, Measurement } from "@types"; import { MQTTConsumer } from "./mqtt"; -import { Measurement } from "../fetcher/types"; import { InfluxDBConsumer } from "./influxdb"; /** diff --git a/src/consumers/influxdb/index.ts b/src/consumers/influxdb.ts similarity index 93% rename from src/consumers/influxdb/index.ts rename to src/consumers/influxdb.ts index 51f521a..7a0a75a 100644 --- a/src/consumers/influxdb/index.ts +++ b/src/consumers/influxdb.ts @@ -1,9 +1,8 @@ import { readFileSync } from "fs"; import { InfluxDB, Point } from "@influxdata/influxdb-client"; -import { Consumer } from "../abstract"; -import { InfluxDBConfig } from "./types"; +import { InfluxDBConfig, Measurement } from "@types"; +import { Consumer } from "./abstract"; import { Dayjs } from "dayjs"; -import { Measurement } from "../../fetcher/types"; export class InfluxDBConsumer extends Consumer { public name = 'influxdb'; diff --git a/src/consumers/influxdb/types.ts b/src/consumers/influxdb/types.ts deleted file mode 100644 index c1aa55d..0000000 --- a/src/consumers/influxdb/types.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { ConsumerConfig } from "../types"; - -export type InfluxDBConfig = ConsumerConfig & { - databaseURL: string; - apiToken: string; - organization: string; - bucket: string; -}; \ No newline at end of file diff --git a/src/consumers/mqtt/index.ts b/src/consumers/mqtt.ts similarity index 88% rename from src/consumers/mqtt/index.ts rename to src/consumers/mqtt.ts index 355113a..94a49e8 100644 --- a/src/consumers/mqtt/index.ts +++ b/src/consumers/mqtt.ts @@ -1,11 +1,11 @@ import fs from 'fs'; import mqtt from "mqtt"; -import { Measurement } from "../../fetcher/types"; +import { Measurement } from "@types"; import { Dayjs } from 'dayjs'; -import { MQTTConfig } from './types'; -import { Consumer } from '../abstract'; +import { MQTTConfig } from '../types/mqtt'; +import { Consumer } from './abstract'; -export * from './types'; +export * from '../types/mqtt'; export class MQTTConsumer extends Consumer { public name = "mqtt"; diff --git a/src/fetcher/index.ts b/src/fetcher/index.ts index 1fef882..1c9a462 100644 --- a/src/fetcher/index.ts +++ b/src/fetcher/index.ts @@ -1,6 +1,6 @@ import dayjs, { Dayjs } from "dayjs"; +import { Measurement } from "@types"; import { type Tauron } from "../tauron"; -import { Measurement } from "./types"; import { consume } from "../consumers"; import { Config } from "../config"; diff --git a/src/tauron/index.ts b/src/tauron/index.ts index bfad66f..6807953 100644 --- a/src/tauron/index.ts +++ b/src/tauron/index.ts @@ -1,11 +1,11 @@ import fs from 'fs'; import axios, { Axios, AxiosResponse } from "axios"; -import { EnergyDTO, EnergyRequestDTO, Payload, PowerDTO, PowerRequestDTO, ReadingDTO, ReadingRequestDTO } from "./types"; +import { EnergyDTO, EnergyRequestDTO, Payload, PowerDTO, PowerRequestDTO, ReadingDTO, ReadingRequestDTO } from "@types"; import { TauronConfig } from '../config'; import { Dayjs } from 'dayjs'; import { withCookies } from './cookie'; -export * from './types'; +export * from '../types/tauron'; const LOGIN_API = `https://logowanie.tauron-dystrybucja.pl/login`; const BASE_URL = 'https://elicznik.tauron-dystrybucja.pl'; diff --git a/src/types/config.ts b/src/types/config.ts new file mode 100644 index 0000000..d78fc31 --- /dev/null +++ b/src/types/config.ts @@ -0,0 +1,10 @@ +import { TauronConfig } from "./tauron"; + +export type Config = { + /** + * Tauron service configuration + */ + tauron: TauronConfig; + + consumers: Record; +}; \ No newline at end of file diff --git a/src/consumers/types.ts b/src/types/consumer.ts similarity index 100% rename from src/consumers/types.ts rename to src/types/consumer.ts diff --git a/src/fetcher/types.ts b/src/types/fetcher.ts similarity index 100% rename from src/fetcher/types.ts rename to src/types/fetcher.ts diff --git a/src/types/index.ts b/src/types/index.ts new file mode 100644 index 0000000..b5e928b --- /dev/null +++ b/src/types/index.ts @@ -0,0 +1,6 @@ +export * from './config'; +export * from './consumer'; +export * from './fetcher'; +export * from './influxdb'; +export * from './mqtt'; +export * from './tauron'; \ No newline at end of file diff --git a/src/types/influxdb.ts b/src/types/influxdb.ts new file mode 100644 index 0000000..0547cd1 --- /dev/null +++ b/src/types/influxdb.ts @@ -0,0 +1,6 @@ +export type InfluxDBConfig = { + databaseURL: string; + apiToken: string; + organization: string; + bucket: string; +}; \ No newline at end of file diff --git a/src/consumers/mqtt/types.ts b/src/types/mqtt.ts similarity index 89% rename from src/consumers/mqtt/types.ts rename to src/types/mqtt.ts index ac4f5e5..5b066e3 100644 --- a/src/consumers/mqtt/types.ts +++ b/src/types/mqtt.ts @@ -1,7 +1,6 @@ import mqtt from "mqtt"; -import { ConsumerConfig } from "../types"; -export type MQTTConfig = ConsumerConfig & { +export type MQTTConfig = { /** * The URL to broker. diff --git a/src/tauron/types.ts b/src/types/tauron.ts similarity index 71% rename from src/tauron/types.ts rename to src/types/tauron.ts index 73d6060..284c13b 100644 --- a/src/tauron/types.ts +++ b/src/types/tauron.ts @@ -1,3 +1,25 @@ +export type TauronConfig = { + /** + * Path to file containing a username of Tauron account + */ + usernamePath: string; + + /** + * Path to file containing a password of Tauron account + */ + passwordPath: string; + + /** + * The measure point name - should be retrieved with browser dev-tools on website + */ + point: string; + + /** + * The path to the json file with cookie jar + */ + cookiesJarPath?: string; +}; + export type Payload = { success: boolean; data: T; diff --git a/tsconfig.json b/tsconfig.json index 0c5f365..d390236 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -9,7 +9,10 @@ "typeRoots": [ "./src/types", "./node_modules/@types" - ] + ], + "paths": { + "@types": ["./src/types"] + } }, "include": ["src/**/*"], "exclude": [