Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

hx-transform

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hx-transform - npm Package Compare versions

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc