hx-transform
Advanced tools
Comparing version 0.0.1 to 0.0.2
74
index.js
window.htmx.defineExtension("transform-response", { | ||
onEvent: function (name, evt) { | ||
const xhr = evt.detail.xhr; | ||
if (name === "htmx:responseError" && xhr) { | ||
const transform = evt.target.getAttribute("hx-transform-error"); | ||
const target = evt.target.getAttribute("hx-target-error"); | ||
console.log({ name, transform, xhr, target }); | ||
if (transform) { | ||
document.querySelector(target).innerHTML = handleTransform( | ||
transform, | ||
xhr.response, | ||
xhr | ||
); | ||
} | ||
} | ||
}, | ||
transformResponse: function (text, xhr, request) { | ||
// get the hx-transform attribute from the request element | ||
const transform = request.getAttribute("hx-transform"); | ||
if (transform) { | ||
const transformJs = transform.trim(); | ||
let parsed; | ||
return handleTransform(transform, text, xhr); | ||
}, | ||
}); | ||
// if the response type is JSON, parse it | ||
if ( | ||
xhr.getResponseHeader("Content-Type").startsWith("application/json") | ||
) { | ||
try { | ||
// try to parse as JSON | ||
parsed = JSON.parse(text); | ||
} catch (e) { | ||
// if not JSON, just return the text | ||
return text; | ||
} | ||
} else if ( | ||
xhr.getResponseHeader("Content-Type").startsWith("text/html") | ||
) { | ||
function handleTransform(transform, text, xhr) { | ||
if (transform) { | ||
const transformJs = transform.trim(); | ||
let parsed; | ||
// if the response type is JSON, parse it | ||
if (xhr.getResponseHeader("Content-Type").startsWith("application/json")) { | ||
try { | ||
// try to parse as JSON | ||
parsed = JSON.parse(text); | ||
} catch (e) { | ||
// if not JSON, just return the text | ||
return text; | ||
} else if ( | ||
xhr.getResponseHeader("Content-Type").startsWith("text/plain") | ||
) { | ||
return text; | ||
} | ||
} else if (xhr.getResponseHeader("Content-Type").startsWith("text/html")) { | ||
return text; | ||
} else if (xhr.getResponseHeader("Content-Type").startsWith("text/plain")) { | ||
return text; | ||
} | ||
// execute inline function | ||
if (transformJs.startsWith("function") || transformJs.startsWith("(")) { | ||
const transformFunction = eval(transformJs); | ||
return transformFunction(parsed); | ||
} | ||
// execute function by name | ||
// execute inline function | ||
if (transformJs.startsWith("function") || transformJs.startsWith("(")) { | ||
const transformFunction = eval(transformJs); | ||
@@ -41,4 +50,7 @@ return transformFunction(parsed); | ||
return text; | ||
}, | ||
}); | ||
// execute function by name | ||
const transformFunction = eval(transformJs); | ||
return transformFunction(parsed); | ||
} | ||
return text; | ||
} |
{ | ||
"version:": "0.0.1", | ||
"name": "hx-transform", | ||
@@ -23,3 +22,3 @@ "module": "index.js", | ||
"homepage": "https://github.com/rohenaz/hx-transform#readme", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"main": "index.js", | ||
@@ -26,0 +25,0 @@ "keywords": [ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
35563
51