Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/error/codes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ const SKYFLOW_ERROR_CODE = {
EMPTY_RUN_ID:{ http_code: 400, message: errorMessages.EMPTY_RUN_ID },
INVALID_RUN_ID:{ http_code: 400, message: errorMessages.INVALID_RUN_ID },
INTERNAL_SERVER_ERROR: { http_code: 500, message: errorMessages.INTERNAL_SERVER_ERROR },
INVALID_XML_FORMAT: { http_code: 400, message: errorMessages.INVALID_XML_FORMAT },
};

export default SKYFLOW_ERROR_CODE;
1 change: 1 addition & 0 deletions src/error/messages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ const errorMessages = {
EMPTY_RUN_ID: `${errorPrefix} Validation error. Run id cannot be empty. Specify a valid run id.`,
INVALID_RUN_ID: `${errorPrefix} Validation error. Invalid run id. Specify a valid run id as string.`,
INTERNAL_SERVER_ERROR: `${errorPrefix}. Internal server error. %s1.`,
INVALID_XML_FORMAT: `${errorPrefix} Validation error. Invalid XML format. Specify a valid XML format as string.`,
};

export default errorMessages;
42 changes: 42 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ export const CONTENT_TYPE = {
APPLICATION_JSON: 'application/json',
APPLICATION_X_WWW_FORM_URLENCODED: 'application/x-www-form-urlencoded',
TEXT_PLAIN: 'text/plain',
MULTIPART_FORM_DATA: 'multipart/form-data',
TEXT_XML: 'text/xml',
APPLICATION_XML: 'application/xml',
TEXT_HTML: 'text/html',
} as const;

// HTTP Headers
Expand Down Expand Up @@ -582,3 +586,41 @@ export const isValidURL = (url: string) => {
return false;
}
};


export function objectToXML(obj: any, rootName: string = "root"): string {
function convertToXML(data: any, nodeName: string): string {
if (data === null || data === undefined) {
return `<${nodeName}/>`;
}

if (typeof data === "object" && !Array.isArray(data)) {
let xml = `<${nodeName}>`;
for (const [key, value] of Object.entries(data)) {
xml += convertToXML(value, key);
}
xml += `</${nodeName}>`;
return xml;
}

if (Array.isArray(data)) {
return data.map((item) => convertToXML(item, nodeName)).join("");
}

// Escape special XML characters
const escapedValue = String(data)
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&apos;");

return `<${nodeName}>${escapedValue}</${nodeName}>`;
}

const xmlContent = Object.entries(obj)
.map(([key, value]) => convertToXML(value, key))
.join("");

return `<?xml version="1.0" encoding="UTF-8"?><${rootName}>${xmlContent}</${rootName}>`;
}
Loading