From f80e509895b7b2c1d716ac276977b7165a13c192 Mon Sep 17 00:00:00 2001 From: Raphael Kabo Date: Fri, 6 Oct 2023 12:34:36 +0100 Subject: [PATCH] Typescript migration --- src/{app.js => app.ts} | 16 +++++----------- src/{helpers.js => helpers.ts} | 32 ++++++++++++++------------------ src/{start.js => start.ts} | 8 +++----- 3 files changed, 22 insertions(+), 34 deletions(-) rename src/{app.js => app.ts} (74%) rename src/{helpers.js => helpers.ts} (69%) rename src/{start.js => start.ts} (82%) diff --git a/src/app.js b/src/app.ts similarity index 74% rename from src/app.js rename to src/app.ts index 9feb239..32e89b6 100755 --- a/src/app.js +++ b/src/app.ts @@ -1,23 +1,16 @@ import express from "express"; import routes from "./routes.js"; import hbs from "express-handlebars"; -import bodyParser from "body-parser"; const app = express(); -// Configuration // - -//app.use(cors()); -//app.use(bodyParser.json()); -//app.use(session({ secret: 'slartibartfast', cookie: { maxAge: 60000 }, resave: false, saveUninitialized: false })); - // View engine // const hbsInstance = hbs.create({ defaultLayout: "main", partialsDir: ["views/partials/"], layoutsDir: "views/layouts/", helpers: { - plural: function (number, text) { + plural: function (number: number, text: string) { var singular = number === 1; // If no text parameter was given, just return a conditional s. if (typeof text !== "string") return singular ? "" : "s"; @@ -39,12 +32,13 @@ app.set("view engine", "handlebars"); app.set("hbsInstance", hbsInstance); // Static files // - app.use(express.static("public")); +// Body parser // +app.use(express.json({ type: "application/activity+json" })); // support json encoded bodies +app.use(express.urlencoded({ extended: true })); + // Router // -app.use(bodyParser.json({ type: "application/activity+json" })); // support json encoded bodies -app.use(bodyParser.urlencoded({ extended: true })); app.use("/", routes); export default app; diff --git a/src/helpers.js b/src/helpers.ts similarity index 69% rename from src/helpers.js rename to src/helpers.ts index 305187f..4528042 100644 --- a/src/helpers.js +++ b/src/helpers.ts @@ -1,43 +1,39 @@ import moment from "moment-timezone"; import icalGenerator from "ical-generator"; -import Log from "./models/Log.js"; +import Log, { ILog } from "./models/Log.js"; import { getConfig } from "./lib/config.js"; +import { IEvent } from "./models/Event.js"; + const config = getConfig(); const domain = config.general.domain; const siteName = config.general.site_name; // LOGGING - -export function addToLog(process, status, message) { - let logEntry = new Log({ - status: status, - process: process, - message: message, - timestamp: moment(), - }); - logEntry.save().catch(() => { +export function addToLog(process: string, status: string, message: string) { + const logEntry = { + status, + process, + message, + timestamp: new Date(), + }; + new Log(logEntry).save().catch(() => { console.log("Error saving log entry!"); }); } -export function exportIcal(events, calendarName) { +export function exportIcal(events: IEvent[], calendarName: string) { + if (!events || events.length < 1) return; + // Create a new icalGenerator... generator const cal = icalGenerator({ name: calendarName || siteName, - x: { - "X-WR-CALNAME": calendarName || siteName, - }, }); - if (events instanceof Array === false) { - events = [events]; - } events.forEach((event) => { // Add the event to the generator cal.createEvent({ start: moment.tz(event.start, event.timezone), end: moment.tz(event.end, event.timezone), timezone: event.timezone, - timestamp: moment(), summary: event.name, description: event.description, organizer: { diff --git a/src/start.js b/src/start.ts similarity index 82% rename from src/start.js rename to src/start.ts index ca17862..fcdfaea 100755 --- a/src/start.js +++ b/src/start.ts @@ -14,14 +14,12 @@ mongoose.connection .on("connected", () => { console.log("Mongoose connection open!"); }) - .on("error", (err) => { - console.log("Connection error: ${err.message}"); + .on("error", (err: any) => { + console.log(`Connection error: ${err.message}`); }); const server = app.listen(config.general.port, () => { console.log( - `Welcome to gathio! The app is now running on http://localhost:${ - server.address().port - }` + `Welcome to gathio! The app is now running on http://localhost:${config.general.port}` ); });