Initial commit

This commit is contained in:
2025-11-09 10:23:23 +01:00
commit f890341ffe
34 changed files with 1058 additions and 0 deletions

View File

@@ -0,0 +1,84 @@
import path from 'path';
import {readFileSync, writeFileSync} from 'fs';
import {Logger} from '../logging/logger.js';
export default class JsonManager {
/**
* Read the current data in a file
* @param {*} filePath
* @returns deserialized content
*/
static read(filePath) {
try {
const fullPath = path.resolve(filePath);
const jsonString = readFileSync(fullPath, 'utf8');
return JSON.parse(jsonString);
} catch (err) {
Logger.error('Error reading JSON file: ', err.message);
return null;
}
}
/**
* Write the given data in a file by overriding the previous data
* @param {*} filePath
* @param {*} data
* @returns
*/
static writeUnsafe(filePath, data) {
try {
const fullPath = path.resolve(filePath);
const jsonString = JSON.stringify(data);
writeFileSync(fullPath, jsonString, 'utf8', async err => {
if(err) await Logger.error(err.message);
});
} catch (err) {
Logger.error('Error writting in JSON file: ', err.message);
return null;
}
}
static upsertToken(filePath, userData) {
const existingData = JsonManager.read(filePath);
const index = existingData.findIndex(e => e.userId === userData.userId);
if (index !== -1) {
existingData.splice(index, 1, userData);
} else {
existingData.push(userData);
}
JsonManager.writeUnsafe(filePath, existingData);
return existingData;
}
static upsertPost(filePath, { userId, permalink }) {
const data = JsonManager.read(filePath);
let modified = false;
const index = data.findIndex(e => e.userId === userId);
if (index !== -1) {
if (data[index].permalink !== permalink) {
data[index].permalink = permalink;
modified = true;
}
} else {
data.push({ userId, permalink });
modified = true;
}
if (modified) {
JsonManager.writeUnsafe(filePath, data);
}
return modified;
}
}

View File

@@ -0,0 +1,35 @@
import {Logger} from "../logging/logger.js";
export class Requester {
static async doGetRequest(url){
return fetch(url)
.then(async response => {
if (!response.ok) {
throw new Error(`HTTP ${response.status} ${response.statusText} : ${await response.text()}`);
}
return response.json();
})
.then(data => {
return data;
})
.catch(async error => {
await Logger.error(`Failed to fetch`, error);
throw error;
});
}
static async doPostRequest(url, headers, body){
return await fetch(url,{
method: 'POST',
headers: headers,
body: body
})
.then(response => response.json())
.then(data => {
return data;
})
.catch(error => {
Logger.error(`Unable to fetch`, error);
});
}
}

View File

@@ -0,0 +1,20 @@
export default class StringFormatter{
/**
* Replaces tokens in a string with their corresponding values.
*
* Each token in the string should match a key in the `values` array.
* For example, given `s = "Hi :name !"` and `values = [{ key: ":name", value: "Foo" }]`,
* the function will return "Hi Foo !".
*
* @param {string} s - The string containing tokens to replace.
* @param {Array<{key: string, value: string}>} values - An array of objects mapping each token (`key`) to its replacement (`value`).
* @returns {string} The string with tokens replaced by their corresponding values.
*/
static format(s, values) {
values.forEach(v => {
s = s.replace(`:${v.key}`, v.value);
});
return s;
}
}