|
1
|
-
class e extends Error{constructor(e,n,t){super(e),this.functionName=n,this.statusCode=t,this.name="BackendFunctionError"}}const n=async(n,t)=>{const r={functionName:n,args:t};let o,i;try{o=await fetch("/__dd/executeAction",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(r)})}catch(t){throw new e(`Network error while executing backend function "${n}": ${t instanceof Error?t.message:String(t)}`,n)}if(!o.ok){let t=`Backend function "${n}" failed with status ${o.status}`;try{const e=await o.text();e&&(t+=`: ${e}`)}catch{}throw new e(t,n,o.status)}try{i=await o.json()}catch(t){throw new e(`Failed to parse response from backend function "${n}": ${t instanceof Error?t.message:String(t)}`,n,o.status)}if(!i.success)throw new e(`Backend function "${n}" returned an error: ${i.error}`,n,o.status);return i.result.data};function t(e){return null!==e&&"object"==typeof e}let r=0;const o=12e4;function i(e){if(!t(e))return;const n="allowedConnectionIds"in e?e.allowedConnectionIds:void 0,r={code:"code"in e?e.code:void 0,allowedConnectionIds:Array.isArray(n)?n.slice():void 0};return t(o=r)&&"code"in o&&"string"==typeof o.code&&o.code.length>0&&"allowedConnectionIds"in o&&Array.isArray(o.allowedConnectionIds)&&o.allowedConnectionIds.every((e=>"string"==typeof e))?r:void 0;var o}function c(e){try{if(!t(e))return{};const n="targetOrigin"in e?e.targetOrigin:void 0;if(!function(e){if("string"!=typeof e)return!1;try{return new URL(e).origin===e}catch{return!1}}(n))return{};const r=i("bundle"in e?e.bundle:void 0);return void 0===r?{targetOrigin:n}:{targetOrigin:n,bundle:r}}catch{return{}}}function s(n,i,{targetOrigin:c,bundle:s}){const a=function(){if("undefined"!=typeof crypto&&crypto.randomUUID)return crypto.randomUUID();r+=1;const e=Math.random().toString(36).slice(2);return`req-${Date.now()}-${r}-${e}`}();return new Promise(((r,u)=>{let d;function f(){window.removeEventListener("message",l),clearTimeout(d)}function l(o){const i=o.data;if(o.source!==window.parent||!function(e,n){return t(e)&&"type"in e&&"app-builder:run-query:response"===e.type&&"requestId"in e&&e.requestId===n}(i,a))return;if(f(),!0===i.success&&t(i.result))return void r(i.result.data);const c="error"in i?i.error:void 0,s=new e("string"==typeof c?c:`Backend function "${n}" failed`,n);u(s)}window.addEventListener("message",l),d=setTimeout((()=>{f(),u(new e(`Backend function "${n}" timed out waiting for response`,n))}),o);const w={type:"app-builder:run-query",requestId:a,queryName:n,args:i,...s&&{bundle:s}};try{window.parent.postMessage(w,c??"*")}catch(e){f(),u(e)}}))}const a=(e,n)=>{const t=function(){try{return Reflect.get(globalThis,"DD_APPS_PREPARE_CALL")}catch{return}}();if("function"!=typeof t)return s(e,n,{});let r;const i=new Promise((e=>{r=setTimeout((()=>e({})),o)})),a=Promise.resolve().then((()=>t(e))).catch((()=>({})));return Promise.race([a,i]).then((t=>{clearTimeout(r);const o=c(t);return s(e,n,o)}))};function u(){return function(){try{return"undefined"!=typeof window&&window.parent!==window}catch{return!0}}()?a:n}globalThis.DD_APPS_RUNTIME={executeBackendFunction:async function(e,n){return u()(e,n)}};
|
|
1
|
+
class e extends Error{constructor(e,t,n){super(e),this.functionName=t,this.statusCode=n,this.name="BackendFunctionError"}}const t=async(t,n)=>{const r={functionName:t,args:n};let o,s;try{o=await fetch("/__dd/executeAction",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(r)})}catch(n){throw new e(`Network error while executing backend function "${t}": ${n instanceof Error?n.message:String(n)}`,t)}if(!o.ok){let n=`Backend function "${t}" failed with status ${o.status}`;try{const e=await o.text();e&&(n+=`: ${e}`)}catch{}throw new e(n,t,o.status)}try{s=await o.json()}catch(n){throw new e(`Failed to parse response from backend function "${t}": ${n instanceof Error?n.message:String(n)}`,t,o.status)}if(!s.success)throw new e(`Backend function "${t}" returned an error: ${s.error}`,t,o.status);return s.result.data};let n=0;const r=(t,r)=>{const o="undefined"!=typeof crypto&&crypto.randomUUID?crypto.randomUUID():(n+=1,`req-${Date.now()}-${n}`);return new Promise(((n,s)=>{let a;function i(){window.removeEventListener("message",c),clearTimeout(a)}function c(r){if(!function(e,t){return null!==e&&"object"==typeof e&&"type"in e&&"app-builder:run-query:response"===e.type&&"requestId"in e&&e.requestId===t}(r.data,o))return;i();const a=r.data;a.success?n(a.result.data):s(new e(a.error??`Backend function "${t}" failed`,t))}window.addEventListener("message",c),a=setTimeout((()=>{i(),s(new e(`Backend function "${t}" timed out waiting for response`,t))}),12e4),window.parent.postMessage({type:"app-builder:run-query",requestId:o,queryName:t,args:r},"*")}))};function o(){return function(){try{return"undefined"!=typeof window&&window.parent!==window}catch{return!0}}()?r:t}globalThis.DD_APPS_RUNTIME={executeBackendFunction:async function(e,t){return o()(e,t)}};
|