diff options
| author | Raphael Kabo <raphaelkabo@hey.com> | 2023-10-06 12:34:36 +0100 | 
|---|---|---|
| committer | Raphael Kabo <raphaelkabo@hey.com> | 2023-10-06 12:34:36 +0100 | 
| commit | f80e509895b7b2c1d716ac276977b7165a13c192 (patch) | |
| tree | 58220155d7ccdd8e47fca8ab9df087af5b80cc2b /src | |
| parent | bd9fb237fa5bcc076d4c6f3bf8ff748df63515e4 (diff) | |
Typescript migration
Diffstat (limited to 'src')
| -rwxr-xr-x | src/app.ts (renamed from src/app.js) | 16 | ||||
| -rw-r--r-- | src/helpers.ts (renamed from src/helpers.js) | 32 | ||||
| -rwxr-xr-x | src/start.ts (renamed from src/start.js) | 8 | 
3 files changed, 22 insertions, 34 deletions
@@ -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 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 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}`    );  });  | 
