From 8cfcb72f0c267c5491eee28b2eece3cfc3a7165d Mon Sep 17 00:00:00 2001 From: Daniel Kluge Date: Sat, 7 May 2022 16:46:03 +0200 Subject: [PATCH] Common exports --- src/common.js | 23 +++++++++++++++++++++++ src/getAlbums.js | 24 ++---------------------- src/getToken.js | 9 ++------- 3 files changed, 27 insertions(+), 29 deletions(-) create mode 100644 src/common.js diff --git a/src/common.js b/src/common.js new file mode 100644 index 0000000..f98959b --- /dev/null +++ b/src/common.js @@ -0,0 +1,23 @@ +import fetch from "node-fetch"; +import {dirname} from "path"; +import {fileURLToPath} from "url"; +import {readFileSync} from "fs"; + +// Read and parse JSON. There is no __dirname for modules +export const secretsFile = `${dirname(fileURLToPath(import.meta.url))}/../secrets.json`; +export const secrets = JSON.parse(readFileSync(secretsFile, "utf8")); + +export async function getToken() { + const response = await fetch("https://oauth2.googleapis.com/token", { + method: "POST", + body: JSON.stringify({ + client_id: secrets.clientId, + client_secret: secrets.clientSecret, + grant_type: "refresh_token", + refresh_token: secrets.refreshToken + }), + headers: {'Content-Type': 'application/json'} + }); + const data = await response.json(); + return data.access_token; +} \ No newline at end of file diff --git a/src/getAlbums.js b/src/getAlbums.js index fdfebe1..0923d8c 100644 --- a/src/getAlbums.js +++ b/src/getAlbums.js @@ -1,34 +1,14 @@ import fetch from "node-fetch"; import readline from "readline"; -import {readFileSync, writeFileSync} from "fs"; import { exit } from "process"; -import {dirname} from "path"; -import {fileURLToPath} from "url"; - -// Read and parse JSON. There is no __dirname for modules -const secretsFile = `${dirname(fileURLToPath(import.meta.url))}/../secrets.json`; -const secrets = JSON.parse(readFileSync(secretsFile, "utf8")); +import { writeFileSync } from "fs"; +import { secretsFile, secrets, getToken } from "./common.js"; if (!secrets.refreshToken) { console.error("No refresh token found. Please run 'npm run getToken' first."); exit(); } -async function getToken() { - const response = await fetch("https://oauth2.googleapis.com/token", { - method: "POST", - body: JSON.stringify({ - client_id: secrets.clientId, - client_secret: secrets.clientSecret, - grant_type: "refresh_token", - refresh_token: secrets.refreshToken - }), - headers: {'Content-Type': 'application/json'} - }); - const data = await response.json(); - return data.access_token; -} - const rl = readline.createInterface({ input: process.stdin, output: process.stdout diff --git a/src/getToken.js b/src/getToken.js index 96113bd..4bc73ab 100644 --- a/src/getToken.js +++ b/src/getToken.js @@ -2,9 +2,8 @@ import express from "express"; import bodyParser from "body-parser"; import open from "open"; import fetch from "node-fetch"; -import {readFileSync, writeFileSync} from "fs"; -import {dirname} from "path"; -import {fileURLToPath} from "url"; +import { writeFileSync } from "fs"; +import { secretsFile, secrets } from "./common.js"; const app = express() app.use(bodyParser.urlencoded({ extended: true })); @@ -13,10 +12,6 @@ const port = 8080; let state = "code"; -// Read and parse JSON. There is no __dirname for modules -const secretsFile = `${dirname(fileURLToPath(import.meta.url))}/../secrets.json`; -const secrets = JSON.parse(readFileSync(secretsFile, "utf8")); - // Express route that prints request body and query parameters app.use("/", (req, res) => { switch (state) {