feat: API wrapper for timestamps
This commit is contained in:
parent
a9d67eb0bf
commit
cfdda0402f
1 changed files with 45 additions and 0 deletions
45
scripts/timestampAPI.js
Normal file
45
scripts/timestampAPI.js
Normal 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}
|
Loading…
Add table
Add a link
Reference in a new issue