/*
* ENDPOINT USAGE
* - https://cal-badge-icd0onfvrxx6.runkit.sh
* - https://cal-badge-icd0onfvrxx6.runkit.sh/:timezone
*
* AVAILABLE TIMEZONES
* https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
*
* EXAMPLE
* API
* - https://cal-badge-icd0onfvrxx6.runkit.sh
* - https://cal-badge-icd0onfvrxx6.runkit.sh/Asia/Shanghai
* BADGE
* - https://badgen.net/runkit/cal-badge-icd0onfvrxx6
* - https://badgen.net/runkit/cal-badge-icd0onfvrxx6/Asia/Shanghai
*/
const send = (res, json) => res.end(JSON.stringify(json, null, 2))
const currentMDY = (timeZone) => (new Date).toLocaleDateString('en-US', { timeZone }).split('/')
exports.endpoint = async function (req, res) {
try {
const [month, day, year] = currentMDY(req.url.replace('/', '') || undefined)
send(res, {
subject: year,
status: (month + '-' + day),
color: 'blue'
})
} catch (e) {
if (e instanceof RangeError) {
send(res, {
subject: 'cal badge',
status: 'unknown timezone',
color: 'grey'
})
} else {
send(res, {
subject: 'cal badge',
status: e.toString(),
color: 'grey'
})
}
}
}