From 0f305731805f3793a7c896afd6347d5749a71f43 Mon Sep 17 00:00:00 2001 From: Maciej Ziarkowski Date: Mon, 9 Sep 2019 15:17:44 +0100 Subject: [PATCH] Add comments to non-obvious code --- app/src/api/routes/asyncController.ts | 7 +++++++ app/src/helpers.ts | 9 +++++++++ 2 files changed, 16 insertions(+) diff --git a/app/src/api/routes/asyncController.ts b/app/src/api/routes/asyncController.ts index 48ba24f2..333f664b 100644 --- a/app/src/api/routes/asyncController.ts +++ b/app/src/api/routes/asyncController.ts @@ -1,5 +1,12 @@ import { Request, Response, NextFunction } from 'express'; +/** + * A wrapper for controller functions that return a Promise, enabling them to be used with Express + * Without this wrapper, Promise rejections caused by an error in the controller will not be passed properly + * to subsequent middleware layers. + * @param fn the async controller function to be wrapped + * @returns controller function which handles async errors correctly + */ function asyncController(fn: (req: Request, res: Response, next: NextFunction) => Promise) { return (req: Request, res: Response, next: NextFunction) => { Promise.resolve(fn(req, res, next)) diff --git a/app/src/helpers.ts b/app/src/helpers.ts index 1c4af17d..9e58d179 100644 --- a/app/src/helpers.ts +++ b/app/src/helpers.ts @@ -1,3 +1,12 @@ +/** + * A function to be passed to JSON.parse as a JSON reviver, in order to transform date values + * (which don't have a native JSON representation and therefore are serialized as strings) + * back to a JavaScript Date object. + * This works by first checking if a string value complies with a date format + * and then converting to a Date if and only if that's the case + * @param name name of the JSON field to revive + * @param value value of the JSON field to revive + */ export function dateReviver(name, value) { if (typeof value === "string" && /^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d.\d\d\dZ$/.test(value)) { return new Date(value);