@logismix/mydata-client
Version:
Greek myDATA (AADE) API client library
76 lines (75 loc) • 2.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatDate = formatDate;
exports.formatSearchParamsDate = formatSearchParamsDate;
exports.formatTime = formatTime;
exports.formatDatesInObject = formatDatesInObject;
exports.requestParamsToUrlParams = requestParamsToUrlParams;
// Date/Time Formatting Helpers
function formatDate(date) {
const year = date.getFullYear();
const month = (date.getMonth() + 1).toString().padStart(2, '0');
const day = date.getDate().toString().padStart(2, '0');
return `${year}-${month}-${day}`;
}
function formatSearchParamsDate(date) {
const year = date.getFullYear();
const month = (date.getMonth() + 1).toString().padStart(2, '0');
const day = date.getDate().toString().padStart(2, '0');
return `${day}/${month}/${year}`;
}
function formatTime(date) {
const hours = date.getHours().toString().padStart(2, '0');
const minutes = date.getMinutes().toString().padStart(2, '0');
const seconds = date.getSeconds().toString().padStart(2, '0');
return `${hours}:${minutes}:${seconds}`;
}
function formatDatesInObject(obj) {
if (obj === null || typeof obj !== 'object')
return obj;
if (obj instanceof Date)
return obj;
if (Array.isArray(obj))
return obj.map(formatDatesInObject);
const newObj = {};
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
const value = obj[key];
if (value instanceof Date) {
if (key === 'issueDate' ||
key === 'dispatchDate' ||
key === 'applicationDate' ||
key === 'cancellationDate' ||
key === 'IssueDate') {
newObj[key] = formatDate(value);
}
else if (key === 'dispatchTime') {
newObj[key] = formatTime(value);
}
else {
newObj[key] = formatDate(value);
console.warn(`Unhandled Date object for key: ${key}. Formatting as YYYY-MM-DD.`);
}
}
else {
newObj[key] = formatDatesInObject(value);
}
}
}
return newObj;
}
function requestParamsToUrlParams(params) {
const queryParams = new URLSearchParams();
Object.entries(params).forEach(([key, value]) => {
if (value !== undefined && value !== null) {
if (key === 'dateFrom' || key === 'dateTo') {
if (value instanceof Date) {
value = formatSearchParamsDate(value);
console.log(`Formatted ${key}: ${value}`);
}
}
queryParams.set(key, String(value));
}
});
return queryParams;
}