@spiffcommerce/core 21.9.0-alpha.2 → 21.9.0-alpha.3

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.
package/dist/index.js CHANGED
@@ -409,7 +409,7 @@ class _A {
409
409
  async getOriginalFile(e) {
410
410
  if (!e.name.endsWith("_removed_bg"))
411
411
  return e;
412
- const t = e.name.slice(0, 11);
412
+ const t = e.name.slice(0, -11);
413
413
  return this.getLocalOrFromServer(t);
414
414
  }
415
415
  /**
@@ -95,7 +95,7 @@
95
95
  }
96
96
  }
97
97
  }
98
- `,ct=async c=>(await N.getShadowGraphqlClient().query({query:uA,errorPolicy:"all",fetchPolicy:"no-cache",variables:{keys:c}})).data.assets,fA=async c=>(await N.getShadowGraphqlClient().query({query:mA,errorPolicy:"all",fetchPolicy:"no-cache",variables:{ids:c}})).data.materials,DA=async(c,e,t,A,n)=>{var i;return(i=(await N.getShadowGraphqlClient().mutate({mutation:IA,errorPolicy:"all",fetchPolicy:"no-cache",variables:{name:c,type:e,mimeType:t,anonymous:A,temporary:n}})).data)==null?void 0:i.assetCreate},pA=(c,e)=>{const t=atob(c),A=[];for(let n=0;n<t.length;n++)A.push(t.charCodeAt(n));return new Blob([new Uint8Array(A)],{type:e})};class FA{constructor(){this.cache=new Map,this.bgrmCache=new Map,this.materialCache=new Map,this.loadImageAsFileInfo=async e=>{const t=await e.arrayBuffer();return{name:e.name.substring(e.name.lastIndexOf("/")+1),blob:new Blob([t],{type:e.type})}}}async getLocalOrFromServer(e){if(this.cache.has(e)){const n=this.cache.get(e);if(!n)throw new v("Failed to get asset from cache!");return n}const A=(async()=>(await ct([e]))[0])();return this.cache.set(e,A),A}async getOriginalFile(e){if(!e.name.endsWith("_removed_bg"))return e;const t=e.name.slice(0,11);return this.getLocalOrFromServer(t)}cacheAsset(e){if(!e.key)throw new v("Asset has no key!");this.cache.has(e.key)||this.cache.set(e.key,Promise.resolve(e))}cacheMaterial(e){if(!e.id)throw new v("Material has no id!");this.materialCache.has(e.id)||this.materialCache.set(e.id,Promise.resolve(e))}async getMaterialLocalOrFromServer(e){if(this.materialCache.has(e))return this.materialCache.get(e);const A=(async()=>(await fA([e]))[0])();return this.materialCache.set(e,A),A}async uploadAssetWithProgress(e,t,A,n,a){const i=await this.dispatchCreateAssetRequest(e,t,n,a);if(!i)throw new Error("Failed to create asset.");return await new Promise((o,s)=>{const r=new XMLHttpRequest;r.open("PUT",i.assetResponse.uploadUrl,!0),r.setRequestHeader("Content-Type",i.mimeType),r.setRequestHeader("Cache-Control","public,max-age=31536000,immutable"),r.upload.onprogress=l=>{l.lengthComputable&&A(l.loaded*100/l.total)},r.onload=()=>{const l=i.assetResponse.asset;He.add(i.assetResponse.asset),o(l)},r.onerror=s,r.send(e.blob)}),this.cacheAsset(i.assetResponse.asset),i.assetResponse.asset}async uploadAsset(e,t,A,n){return this.uploadAssetWithProgress(e,t,()=>{},A,n)}async uploadFile(e,t){const A=!(e.type==="image/svg+xml"||e.type==="application/pdf"),n=A?g.AssetType.Image:g.AssetType.Illustration;if(A){const a=await this.loadImageAsFileInfo(e);return await this.uploadAssetWithProgress(a,n,t,!0)}else{const a={name:e.name,blob:new Blob([e],{type:e.type})};return await this.uploadAssetWithProgress(a,n,t,!0)}}async removeBackgroundFromAsset(e){if(this.bgrmCache.has(e.key))return this.bgrmCache.get(e.key);const A=(async()=>{const n=await g.fetchAsArrayBuffer(e.fileLink),a=await this.removeBackground(new Blob([n],{type:e.mimeType})),i={name:e.key+"_removed_bg",blob:a};return this.uploadAsset(i,e.type,!0)})();return this.bgrmCache.set(e.key,A),A}removePersistedAsset(e){He.remove(e)}getPersistedAssets(){return He.list()}async dispatchCreateAssetRequest(e,t,A,n){const a=e.blob.type?e.blob.type:this.guessMIME(e.name),i=await DA(e.name,t,a,A,n);if(i)return{assetResponse:i,mimeType:a}}async removeBackground(e){const t="https://sdk.photoroom.com/v1/segment",A=new FormData;A.append("image_file",e);try{const n=await g.fetch(t,{method:"POST",headers:{Accept:"image/png, application/json","x-api-key":"30913dbd5df6e6ea74faeb1d064865ce7e727ee2"},body:A});if(n.headers.get("Content-Type")==="image/png")return await n.blob();const i=await n.json();if(i.result_b64)return pA(i.result_b64,"image/png");throw new Error("Failed to remove background from file.")}catch(n){throw console.error("Failed to remove background from file"),n}}guessMIME(e){const t=e.split(".").pop();switch(t){case"glb":return"model/gltf-binary";case"ttf":return"font/ttf";case"mkv":return"video/x-matroska";default:throw new v("Unexpected mimetype: "+t)}}}const se="persistentAssets";class He{static add(e){if(!e.fileLink){console.error("Failed to find cdn link on asset, cannot persist!");return}const t=new Map;t.set(e.key||"",e.fileLink);const A=P.getMap(se);A&&A.forEach((n,a)=>{t.set(a,n)}),P.setMap(se,t)}static remove(e){const t=P.getMap(se);if(!t)return;const A=Array.from(t.entries()).find(n=>n[0]===e);A&&(t.delete(A[0]),P.setMap(se,t))}static list(){const e=P.getMap(se);return e?Array.from(e.entries()).map(t=>({assetKey:t[0],src:t[1]})):[]}}const J=new FA;class MA{constructor(){this.cache={},this.disabled=!1}get(e){if(this.disabled)return;const t=JSON.stringify(e);return this.cache[t]}set(e,t){if(this.disabled)return t;const A=JSON.stringify(e);return this.cache[A]=t,t}disable(e){this.disabled=e}}const Z=new MA,gt=M.gql`
98
+ `,ct=async c=>(await N.getShadowGraphqlClient().query({query:uA,errorPolicy:"all",fetchPolicy:"no-cache",variables:{keys:c}})).data.assets,fA=async c=>(await N.getShadowGraphqlClient().query({query:mA,errorPolicy:"all",fetchPolicy:"no-cache",variables:{ids:c}})).data.materials,DA=async(c,e,t,A,n)=>{var i;return(i=(await N.getShadowGraphqlClient().mutate({mutation:IA,errorPolicy:"all",fetchPolicy:"no-cache",variables:{name:c,type:e,mimeType:t,anonymous:A,temporary:n}})).data)==null?void 0:i.assetCreate},pA=(c,e)=>{const t=atob(c),A=[];for(let n=0;n<t.length;n++)A.push(t.charCodeAt(n));return new Blob([new Uint8Array(A)],{type:e})};class FA{constructor(){this.cache=new Map,this.bgrmCache=new Map,this.materialCache=new Map,this.loadImageAsFileInfo=async e=>{const t=await e.arrayBuffer();return{name:e.name.substring(e.name.lastIndexOf("/")+1),blob:new Blob([t],{type:e.type})}}}async getLocalOrFromServer(e){if(this.cache.has(e)){const n=this.cache.get(e);if(!n)throw new v("Failed to get asset from cache!");return n}const A=(async()=>(await ct([e]))[0])();return this.cache.set(e,A),A}async getOriginalFile(e){if(!e.name.endsWith("_removed_bg"))return e;const t=e.name.slice(0,-11);return this.getLocalOrFromServer(t)}cacheAsset(e){if(!e.key)throw new v("Asset has no key!");this.cache.has(e.key)||this.cache.set(e.key,Promise.resolve(e))}cacheMaterial(e){if(!e.id)throw new v("Material has no id!");this.materialCache.has(e.id)||this.materialCache.set(e.id,Promise.resolve(e))}async getMaterialLocalOrFromServer(e){if(this.materialCache.has(e))return this.materialCache.get(e);const A=(async()=>(await fA([e]))[0])();return this.materialCache.set(e,A),A}async uploadAssetWithProgress(e,t,A,n,a){const i=await this.dispatchCreateAssetRequest(e,t,n,a);if(!i)throw new Error("Failed to create asset.");return await new Promise((o,s)=>{const r=new XMLHttpRequest;r.open("PUT",i.assetResponse.uploadUrl,!0),r.setRequestHeader("Content-Type",i.mimeType),r.setRequestHeader("Cache-Control","public,max-age=31536000,immutable"),r.upload.onprogress=l=>{l.lengthComputable&&A(l.loaded*100/l.total)},r.onload=()=>{const l=i.assetResponse.asset;He.add(i.assetResponse.asset),o(l)},r.onerror=s,r.send(e.blob)}),this.cacheAsset(i.assetResponse.asset),i.assetResponse.asset}async uploadAsset(e,t,A,n){return this.uploadAssetWithProgress(e,t,()=>{},A,n)}async uploadFile(e,t){const A=!(e.type==="image/svg+xml"||e.type==="application/pdf"),n=A?g.AssetType.Image:g.AssetType.Illustration;if(A){const a=await this.loadImageAsFileInfo(e);return await this.uploadAssetWithProgress(a,n,t,!0)}else{const a={name:e.name,blob:new Blob([e],{type:e.type})};return await this.uploadAssetWithProgress(a,n,t,!0)}}async removeBackgroundFromAsset(e){if(this.bgrmCache.has(e.key))return this.bgrmCache.get(e.key);const A=(async()=>{const n=await g.fetchAsArrayBuffer(e.fileLink),a=await this.removeBackground(new Blob([n],{type:e.mimeType})),i={name:e.key+"_removed_bg",blob:a};return this.uploadAsset(i,e.type,!0)})();return this.bgrmCache.set(e.key,A),A}removePersistedAsset(e){He.remove(e)}getPersistedAssets(){return He.list()}async dispatchCreateAssetRequest(e,t,A,n){const a=e.blob.type?e.blob.type:this.guessMIME(e.name),i=await DA(e.name,t,a,A,n);if(i)return{assetResponse:i,mimeType:a}}async removeBackground(e){const t="https://sdk.photoroom.com/v1/segment",A=new FormData;A.append("image_file",e);try{const n=await g.fetch(t,{method:"POST",headers:{Accept:"image/png, application/json","x-api-key":"30913dbd5df6e6ea74faeb1d064865ce7e727ee2"},body:A});if(n.headers.get("Content-Type")==="image/png")return await n.blob();const i=await n.json();if(i.result_b64)return pA(i.result_b64,"image/png");throw new Error("Failed to remove background from file.")}catch(n){throw console.error("Failed to remove background from file"),n}}guessMIME(e){const t=e.split(".").pop();switch(t){case"glb":return"model/gltf-binary";case"ttf":return"font/ttf";case"mkv":return"video/x-matroska";default:throw new v("Unexpected mimetype: "+t)}}}const se="persistentAssets";class He{static add(e){if(!e.fileLink){console.error("Failed to find cdn link on asset, cannot persist!");return}const t=new Map;t.set(e.key||"",e.fileLink);const A=P.getMap(se);A&&A.forEach((n,a)=>{t.set(a,n)}),P.setMap(se,t)}static remove(e){const t=P.getMap(se);if(!t)return;const A=Array.from(t.entries()).find(n=>n[0]===e);A&&(t.delete(A[0]),P.setMap(se,t))}static list(){const e=P.getMap(se);return e?Array.from(e.entries()).map(t=>({assetKey:t[0],src:t[1]})):[]}}const J=new FA;class MA{constructor(){this.cache={},this.disabled=!1}get(e){if(this.disabled)return;const t=JSON.stringify(e);return this.cache[t]}set(e,t){if(this.disabled)return t;const A=JSON.stringify(e);return this.cache[A]=t,t}disable(e){this.disabled=e}}const Z=new MA,gt=M.gql`
99
99
  fragment OptionFields on Option {
100
100
  id
101
101
  name
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@spiffcommerce/core",
3
- "version": "21.9.0-alpha.2",
3
+ "version": "21.9.0-alpha.3",
4
4
  "description": "Core client API for interacting with the Spiff Commerce backend.",
5
5
  "source": "src/index.ts",
6
6
  "main": "dist/index.umd.cjs",