Skip to content

Commit

Permalink
Typescript migration
Browse files Browse the repository at this point in the history
  • Loading branch information
lowercasename committed Oct 6, 2023
1 parent bd9fb23 commit f80e509
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 34 deletions.
16 changes: 5 additions & 11 deletions src/app.js → src/app.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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;
32 changes: 14 additions & 18 deletions src/helpers.js → src/helpers.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down
8 changes: 3 additions & 5 deletions src/start.js → src/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
);
});

0 comments on commit f80e509

Please sign in to comment.