export default async function main(args) {
const { originalDate, timezone } = args.inputVars;
let timezoneOffset = timezone;
// Validate our input values
if (!originalDate) {
return {
outputVars: {
error: `No original date was passed`
},
next: {
path: 'error'
},
trace: [
{
type: 'debug',
payload: {
message: `No original date was passed`
}
}
],
}
}
if (timezoneOffset === undefined || isNaN(parseFloat(timezoneOffset))) { timezoneOffset = 0; }
try {
// Send the original date and timezone offset to the server, which will perform the date conversion
const result = await fetch("https://api-date.voiceflow.fr/date", {
method: 'POST',
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
date: originalDate,
tz: Number(timezoneOffset)
})
});
// SUCCESS - Output the parsed dates
const responseBody = result?.json;
if (responseBody.date) {
let date = new Date(responseBody.date);
return {
outputVars: {
ISODate: responseBody.date,
humanReadableDate: date.toLocaleString()
},
next: {
path: 'success'
},
trace: [
{
type: 'debug',
payload: {
message: `Converting string "${originalDate}" to ${responseBody.date}`
}
}
],
}
}
// FAILURE - The server gave an error response, so report that back to the designer
return {
outputVars: {
error: `Unable to convert the given date`
},
next: {
path: 'error'
},
trace: [
{
type: 'debug',
payload: {
message: `API response error while trying to convert the given date`
}
}
],
}
} catch (error) {
return {
outputVars: {
error
},
next: {
path: 'error'
},
trace: [
{
type: 'debug',
payload: {
message: error
}
}
],
}
}
}