datocms-plugin-asset-optimization 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1 @@
|
|
|
1
|
+
async function U(t,a,n,s=60,c=2e3){const r="https://site-api.datocms.com",d={Authorization:`Bearer ${a}`,"Content-Type":"application/json",Accept:"application/json","X-Api-Version":"3","X-Environment":n};let i=0;for(;i<s;){i++;try{console.log(`Checking job status (attempt ${i}/${s}): ${t}`);const e=await fetch(`${r}/job-results/${t}`,{method:"GET",headers:d});if(!e.ok){const w=await e.text();throw new Error(`API error: ${e.status} ${w}`)}const p=await e.json();if(p.data.attributes&&p.data.attributes.status===200)return console.log(`Job completed successfully: ${t}`),p;console.log(`Job in progress (${i}/${s}), waiting ${c}ms...`),await new Promise(w=>setTimeout(w,c))}catch(e){console.error(`Error checking job status: ${e instanceof Error?e.message:String(e)}`),await new Promise(p=>setTimeout(p,c))}}throw new Error(`Job timed out after ${s} attempts: ${t}`)}const A=t=>new Promise(a=>setTimeout(a,t)),j=(t,a,n,s)=>{const c=a*s**t;return Math.min(c,n)};async function T(t,a,n,s,c,r=0,d=1e3,i=6e4,e=2){var p,w;if(console.log(`Replacing DatoCMS asset ID ${t} with image from URL: ${a}${r>0?` (retry ${r})`:""}`),!t||!n)throw new Error("Missing required parameters: assetId and apiToken are required");try{const l="https://site-api.datocms.com",f={Authorization:`Bearer ${n}`,"Content-Type":"application/json",Accept:"application/json","X-Api-Version":"3","X-Environment":s},h=await fetch(`${l}/upload-requests`,{method:"POST",headers:f,body:JSON.stringify({data:{type:"upload_request",attributes:{filename:c||"optimized-image.jpg"}}})});if(h.status===429){const o=h.headers.get("Retry-After"),u=o?Number.parseInt(o,10):0,b=u>0?u*1e3:j(r,d,i,e);return console.log(`Rate limit exceeded. Retrying after ${b}ms`),await A(b),T(t,a,n,s,c,r+1,d,i,e)}if(!h.ok){const o=await h.text();throw new Error(`Failed to create upload request: ${h.status} ${o}`)}const S=await h.json(),{id:q,attributes:{url:R,request_headers:J}}=S.data,m=await fetch(a);if(!m.ok)throw new Error(`Failed to fetch image from URL: ${m.status} ${m.statusText}`);const M=await(await m.blob()).arrayBuffer(),P=new Uint8Array(M),E=await fetch(R,{method:"PUT",headers:{...J,"Content-Length":P.length.toString()},body:P});if(!E.ok)throw new Error(`Failed to upload file to S3: ${E.status} ${E.statusText}`);const $=await fetch(`${l}/uploads/${t}`,{method:"PUT",headers:f,body:JSON.stringify({data:{id:t,type:"upload",attributes:{path:q}}})});if($.status===429){const o=$.headers.get("Retry-After"),u=o?Number.parseInt(o,10):0,b=u>0?u*1e3:j(r,d,i,e);return console.log(`Rate limit exceeded during asset update. Retrying after ${b}ms`),await A(b),T(t,a,n,s,c,r+1,d,i,e)}if(!$.ok){const o=await $.text();throw new Error(`Failed to update asset metadata: ${$.status} ${o}`)}const g=await $.json();if(g.data&&g.data.type==="job"){const o=g.data.id;console.log(`Asset update initiated as job ${o}, waiting for completion...`);const u=await U(o,n,s);if(((p=u.data.attributes)==null?void 0:p.status)!==200)throw new Error(`Job completed with error status: ${(w=u.data.attributes)==null?void 0:w.status}`);return u.data.attributes.payload}return console.log("Asset replaced successfully:",g),g}catch(l){if(l instanceof Error&&l.message.includes("API error")&&r<5){const f=j(r,d,i,e);return console.error(`Error replacing asset: ${l.message}. Retrying in ${f}ms...`),await A(f),T(t,a,n,s,c,r+1,d,i,e)}throw console.error("Error replacing asset:",l),l}}export{T as default};
|