Skip to content

Commit

Permalink
Revert "Refactoring"
Browse files Browse the repository at this point in the history
This reverts commit 98aa382.
  • Loading branch information
jvik committed Dec 23, 2024
1 parent 98aa382 commit 4a9066f
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 32 deletions.
6 changes: 0 additions & 6 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,5 @@
"formatter": {
"enabled": true,
"indentStyle": "space"
},
"files": {
"ignore": [
"node_modules/**",
"dist/**"
]
}
}
8 changes: 4 additions & 4 deletions src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ async function exchangeCodeForIdAndAuthToken(code: string): Promise<IdTokenRespo
// return response.data;
// }

async function getBearerToken(accessToken: string, idToken: string): Promise<ElawayTokenResponse> {
async function getElawayToken(accessToken: string, idToken: string): Promise<ElawayTokenResponse> {
try {
console.info("Requesting Elaway token with access token and ID token.");
const response = await axios.post(ampecoApiUrl, {
Expand All @@ -128,11 +128,11 @@ async function getBearerToken(accessToken: string, idToken: string): Promise<Ela
throw new Error(`Failed to get Elaway token: ${response.statusText}`);
}

console.info("Successfully obtained bearer token.");
console.info("Successfully obtained Elaway token.");
return response.data;
} catch (error) {
console.error("Error in getElawayToken:", error.message);
console.error("You likely have the wrong ELAWAY_CLIENT_ID and/or ELAWAY_CLIENT_SECRET");
console.error("You likely have the wrong ELAWAY_CLIENT_ID or ELAWAY_CLIENT_SECRET");
throw error;
}
}
Expand Down Expand Up @@ -198,7 +198,7 @@ async function startOauth(): Promise<ElawayTokenResponse> {

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);
Expand Down
11 changes: 0 additions & 11 deletions src/charger/charger.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/charger/chargerRouter.ts
Original file line number Diff line number Diff line change
@@ -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();

Expand Down
21 changes: 11 additions & 10 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,30 @@ 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);

app.listen(port, () => {
console.info(`Server is running on http://localhost:${port}`);
});

export { chargerInfo };

0 comments on commit 4a9066f

Please sign in to comment.