feat: API wrapper for timestamps

This commit is contained in:
buzzcode2007 2025-03-19 14:26:16 +00:00
parent a9d67eb0bf
commit cfdda0402f

45
scripts/timestampAPI.js Normal file
View file

@ -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}