From cfdda0402f5f82a2d07f38ed5a0fb0dd83a56fc7 Mon Sep 17 00:00:00 2001 From: buzzcode2007 <73412182+buzz-lightsnack-2007@users.noreply.github.com> Date: Wed, 19 Mar 2025 14:26:16 +0000 Subject: [PATCH] feat: API wrapper for timestamps --- scripts/timestampAPI.js | 45 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 scripts/timestampAPI.js diff --git a/scripts/timestampAPI.js b/scripts/timestampAPI.js new file mode 100644 index 0000000..8f1eb9f --- /dev/null +++ b/scripts/timestampAPI.js @@ -0,0 +1,45 @@ +var TimeProcessor = require('./timeprocessor').TimeProcessor; +const Messaging = require('./messaging').Messaging; + +class TimestampAPI { + /* + Custom paths used for the API. + */ + #paths = { + "conversion": "/:date" + } + + /* + Create the time stamp with direct access to the Express instance. + + @param {Express} instance - The Express instance + */ + constructor(instance) { + this[`instance`] = instance; + + // your first API endpoint... + instance.get("/api/hello", function (req, res) { + res.json({greeting: 'hello API'}); + }); + + this.conversion(); + } + + /* + Handle conversion requests. + */ + conversion() { + [`/api`, `/api${this.#paths["conversion"]}`].forEach((PATH) => { + [`get`, `post`].forEach((METHOD) => { + this[`instance`][METHOD](PATH, (REQUEST, RESPONSE) => { + try { + let RESPONSE_DATA = TimeProcessor.convert((Object.keys(REQUEST.params).length ? REQUEST.params.date : false) ? REQUEST.params.date : (new Date()).toUTCString()); + RESPONSE.json(RESPONSE_DATA); + } catch(ERROR) {Messaging.exception(RESPONSE, ERROR)} + }) + }) + }) + } +} + +module.exports = {TimestampAPI} \ No newline at end of file