From 4a9066f13612faaac6fc0c7e192b124fe0cf46d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20V?= Date: Mon, 23 Dec 2024 02:02:49 +0100 Subject: [PATCH] Revert "Refactoring" This reverts commit 98aa382f6dcb97e3f8c72e4d5ffea36782bc82e3. --- biome.json | 6 ------ src/auth.ts | 8 ++++---- src/charger/charger.ts | 11 ----------- src/charger/chargerRouter.ts | 2 +- src/main.ts | 21 +++++++++++---------- 5 files changed, 16 insertions(+), 32 deletions(-) delete mode 100644 src/charger/charger.ts diff --git a/biome.json b/biome.json index a7a72cc..700e56d 100644 --- a/biome.json +++ b/biome.json @@ -15,11 +15,5 @@ "formatter": { "enabled": true, "indentStyle": "space" - }, - "files": { - "ignore": [ - "node_modules/**", - "dist/**" - ] } } \ No newline at end of file diff --git a/src/auth.ts b/src/auth.ts index 3191629..1bed2e7 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -101,7 +101,7 @@ async function exchangeCodeForIdAndAuthToken(code: string): Promise { +async function getElawayToken(accessToken: string, idToken: string): Promise { try { console.info("Requesting Elaway token with access token and ID token."); const response = await axios.post(ampecoApiUrl, { @@ -128,11 +128,11 @@ async function getBearerToken(accessToken: string, idToken: string): Promise { accessIdResponse = await exchangeCodeForIdAndAuthToken(code); - tokenResponse = await getBearerToken(accessIdResponse.access_token, accessIdResponse.id_token); + tokenResponse = await getElawayToken(accessIdResponse.access_token, accessIdResponse.id_token); } } catch (error) { console.error(error.message); diff --git a/src/charger/charger.ts b/src/charger/charger.ts deleted file mode 100644 index 1e23228..0000000 --- a/src/charger/charger.ts +++ /dev/null @@ -1,11 +0,0 @@ -import config from '../config.js'; -import axios from 'axios'; - -const chargerData = (await axios.get(`${config.ampecoApiUrl}/personal/charge-points`)).data.data[0]; - -const chargerInfo = { - chargerId: chargerData.id, - evseId: chargerData.evses[0].id -} - -export { chargerInfo }; diff --git a/src/charger/chargerRouter.ts b/src/charger/chargerRouter.ts index 025478b..83679ec 100644 --- a/src/charger/chargerRouter.ts +++ b/src/charger/chargerRouter.ts @@ -1,7 +1,7 @@ import express from "express"; import axios from "axios"; import config from "../config.js"; -import { chargerInfo } from "./charger.js"; +import { chargerInfo } from "../main.js"; const router = express.Router(); diff --git a/src/main.ts b/src/main.ts index ff68993..04753df 100644 --- a/src/main.ts +++ b/src/main.ts @@ -6,25 +6,24 @@ import { getValidCredentials } from './auth.js'; const port = config.port; const app = express(); - -let token = await getValidCredentials(); +const token = await getValidCredentials(); if (!token) { throw new Error('Could not get valid credentials'); } axios.interceptors.request.use(async (config) => { - // Check if the token is still valid, if not, get a new one - if (token.expires_at < Date.now()) { - token = await getValidCredentials(); - if (!token) { - throw new Error('Could not get valid credentials'); - } - } - config.headers.Authorization = `Bearer ${token.access_token}`; + config.headers.Authorization = `Bearer ${token?.access_token}`; return config; }); +const chargerData = (await axios.get(`${config.ampecoApiUrl}/personal/charge-points`)).data.data[0]; + +const chargerInfo = { + chargerId: chargerData.id, + evseId: chargerData.evses[0].id +} + app.use(express.json()); app.use("/charger", chargerRouter); @@ -32,3 +31,5 @@ app.use("/charger", chargerRouter); app.listen(port, () => { console.info(`Server is running on http://localhost:${port}`); }); + +export { chargerInfo };