diff options
author | Gavin Mogan <git@gavinmogan.com> | 2025-04-25 21:43:39 -0700 |
---|---|---|
committer | Gavin Mogan <git@gavinmogan.com> | 2025-04-25 21:46:07 -0700 |
commit | 14041a319cace03cfc23c0a919ed81fb141f88ce (patch) | |
tree | c87d43fe3f889bf0794caa42953781533196cb45 /src/lib/handlebars.ts | |
parent | a8a17443c2d070d2d23920ffff7e4a43c905698c (diff) |
Refactor to have email service
* Move hbsInstance back to app
* Add email and hbs to req so typescript 🎉🎉🎉
* Init Email and config once
Diffstat (limited to 'src/lib/handlebars.ts')
-rw-r--r-- | src/lib/handlebars.ts | 55 |
1 files changed, 0 insertions, 55 deletions
diff --git a/src/lib/handlebars.ts b/src/lib/handlebars.ts deleted file mode 100644 index 6d4f796..0000000 --- a/src/lib/handlebars.ts +++ /dev/null @@ -1,55 +0,0 @@ -import hbs, { ExpressHandlebars } from "express-handlebars"; -import { RenderViewOptions } from "express-handlebars/types/index.js"; - -export class HandlebarsSingleton { - static #instance: HandlebarsSingleton; - hbsInstance: hbs.ExpressHandlebars; - - private constructor() { - this.hbsInstance = hbs.create({ - defaultLayout: "main", - partialsDir: ["views/partials/"], - layoutsDir: "views/layouts/", - helpers: { - plural: function (number: number, text: string) { - const singular = number === 1; - // If no text parameter was given, just return a conditional s. - if (typeof text !== "string") return singular ? "" : "s"; - // Split with regex into group1/group2 or group1(group3) - const match = text.match(/^([^()\/]+)(?:\/(.+))?(?:\((\w+)\))?/); - // If no match, just append a conditional s. - if (!match) return text + (singular ? "" : "s"); - // We have a good match, so fire away - return ( - (singular && match[1]) || // Singular case - match[2] || // Plural case: 'bagel/bagels' --> bagels - match[1] + (match[3] || "s") - ); // Plural case: 'bagel(s)' or 'bagel' --> bagels - }, - json: function (context: object) { - return JSON.stringify(context); - }, - }, - }); - } - - public static get instance(): HandlebarsSingleton { - if (!HandlebarsSingleton.#instance) { - HandlebarsSingleton.#instance = new HandlebarsSingleton(); - } - - return HandlebarsSingleton.#instance; - } - - public get engine(): ExpressHandlebars["engine"] { - return this.hbsInstance.engine; - } - - /** - * Finally, any singleton can define some business logic, which can be - * executed on its instance. - */ - public renderView(viewPath: string, options: RenderViewOptions): Promise<string> { - return this.hbsInstance.renderView(viewPath, options); - } -} |