@meta-cms/core
Advanced tools
Comparing version 1.0.44 to 1.0.45
@@ -211,2 +211,2 @@ "use strict";var _=Object.defineProperty;var U=Object.getOwnPropertyDescriptor;var B=Object.getOwnPropertyNames;var K=Object.prototype.hasOwnProperty;var X=(n,e)=>{for(var t in e)_(n,t,{get:e[t],enumerable:!0})},Y=(n,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of B(e))!K.call(n,i)&&i!==t&&_(n,i,{get:()=>e[i],enumerable:!(r=U(e,i))||r.enumerable});return n};var Z=n=>Y(_({},"__esModule",{value:!0}),n);var ie={};X(ie,{GraphqlClient:()=>y,MetaCms:()=>J,buildQuery:()=>P,collectionFragment:()=>E,config:()=>te,contentQuery:()=>w,getMetaCMSConfigQuery:()=>ee,getOwnerIdQuery:()=>N,imageFragment:()=>$,isId:()=>C,mediaImageFragment:()=>A,metafieldFragment:()=>M,metafieldsForNodes:()=>q,metafieldsQuery:()=>R,normalizeEdges:()=>p,productFragment:()=>S,randomId:()=>g,setInNamespace:()=>T,setMetaCMSConfigMutation:()=>F,syncConfig:()=>re});module.exports=Z(ie);var k=(n,e,t)=>{if(!n)return t?t():void 0;var r,i;if(Array.isArray(e)&&(r=e.slice(0)),typeof e=="string"&&(r=e.split(".")),typeof e=="symbol"&&(r=[e]),!Array.isArray(r))throw new Error("props arg must be an array, a string or a symbol");for(;r.length;)if(i=r.shift(),!n||(n=n[i],n===void 0))return t?t():void 0;return n};var Q=n=>{let e=[];return n.forEach(t=>{!t||(t.type==="product_reference"&&t.product.content&&t.product.content.map(r=>{e.push(r.id)}),t.type==="list.product_reference"&&t.values.edges.map(({node:r})=>{r.content&&r.content.map(i=>{e.push(i.id)})}),t.type==="collection_reference"&&t.collection.content&&t.collection.content.map(r=>{e.push(r.id)}),t.type==="list.collection_reference"&&t.values.edges.map(({node:r})=>{r.content&&r.content.map(i=>{e.push(i.id)})}))}),e};var F=` | ||
}`;var p=n=>n.edges.reduce((e,{node:t})=>(e.push(t),e),[]),te=n=>n,g=()=>{let n="meta-cms-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict",e="",t=10;for(;t--;)e+=n[Math.random()*n.length|0];return`meta-cms-${e}`},C=n=>typeof n=="string"?n.startsWith("gid://shopify/Metafield/"):!1,T=({content:n,namespace:e,key:t,id:r,value:i})=>{let o=r?`${t}@${r}`:t;n[e]||Object.assign(n,{[e]:{}}),Object.assign(n[e],{[o]:i})},P=({handles:n,ids:e})=>{let t=r=>r.replace("gid://shopify/Product/","");return(n==null?void 0:n.length)||(e==null?void 0:e.length)?`${(e||[]).map((r,i,o)=>`id:${t(r)}${o.length-1>i?" or ":n!=null&&n.length?" or":""}`).join("")} ${(n||[]).map((r,i,o)=>`handle:${r}${o.length-1>i?" or ":""}`).join("")}`.trim():void 0};var O=({field:n,acc:e,item:t,locale:r,jsonMetafields:i,nestedContent:o})=>{var m;let s={itemID:t.id,itemType:t.type,itemProp:`${t.namespace}:${t.key}`},d,c=({id:a,key:l,namespace:f,objectId:u})=>{o&&e&&r&&T({content:e,key:l,id:u,namespace:f.replace(`_${r}`,""),value:o[a]})};switch(t.type){case"collection_reference":{d={attributes:s,value:{...t.collection,products:p(t.collection.products).map(({content:a,...l})=>(a==null||a.forEach(f=>c({...f,objectId:l.id})),{...l,variants:p(l.variants)}))}};break}case"file_reference":{(!t.image.image.alt||!t.image.image.alt.length)&&(t.image.image.alt="image"),d={attributes:s,value:t.image.image};break}case"product_reference":{let{product:a}=t;(m=a.content)==null||m.forEach(l=>c({...l,objectId:a.id})),d={attributes:s,value:{...a,variants:p(t.product.variants)}};break}case"list.collection_reference":{d={attributes:s,value:p(t.values).map(a=>{var l;return(l=a.content)==null||l.forEach(f=>c({...f,objectId:a.id})),{...a,products:p(a.products).map(f=>{var u;return(u=f.content)==null||u.forEach(h=>c({...h,objectId:f.id})),{...f,variants:p(f.variants)}})}})};break}case"list.file_reference":{d={attributes:s,value:p(t.values).map(a=>a.image)};break}case"list.product_reference":{d={attributes:s,value:p(t.values).map(a=>{var l;return(l=a.content)==null||l.forEach(f=>c({...f,objectId:a.id})),{...a,variants:p(a.variants)}})};break}case"json":{let a=H(t.value,i);d={attributes:s,value:a.value||a};break}case"list.multi_line_text_field":case"list.single_line_text_field":{d={attributes:s,value:JSON.parse(t.value)};break}case"multi_line_text_field":case"single_line_text_field":{d={attributes:s,value:t.value};break}}return d},L=({client:n,id:e,initalizer:t,content:r,jsonMetafields:i,nestedContent:o})=>{let s=t||{};for(let d=0;d<r.length;d++){let c=r[d];if(c){let m=n.config[c.namespace.replace(`_${n.locale}`,"")].schema[c.key],a=O({client:n,field:m,acc:s,item:c,jsonMetafields:i,nestedContent:o});T({content:s,key:c.key,namespace:c.namespace.replace(`_${n.locale}`,""),value:n.adapter(a)})}}return s};var ne=n=>{let e=[];return n&&JSON.parse(n,(t,r)=>(Array.isArray(r)?r.forEach(i=>{C(i)&&e.push(i)}):C(r)&&e.push(r),r)),e},D=n=>{let e=[];return n.forEach(t=>{!t||t.type==="json"&&(e=e.concat(ne(t.value)))}),e},b=(n,e)=>n.reduce((t,r)=>{if(r){console.log(r.namespace,e.locale,r.key,e.config[r.namespace.replace(`_${e.locale}`,"")]);let i=e.config[r.namespace.replace(`_${e.locale}`,"")].schema[r.key],o=O({field:i,item:r,client:e});t[r.id]=e.adapter(o)}return t},{}),H=(n,e)=>e?JSON.parse(n,(t,r)=>Array.isArray(r)?r.map(i=>C(i)?e[i]:i):C(r)?e[r]:r):JSON.parse(n);var W=n=>{let t={text:"single_line_text_field",richtext:"multi_line_text_field",image:"file_reference",product:"product_reference",collection:"collection_reference",object:"json"}[n.type];return n.isList?`list.${t}`:t},v=(n=400,e=300)=>({alt:"image",src:`https://picsum.photos/${n}/${e}`,width:n,height:e}),x=()=>({id:g(),descriptionHtml:"Lorem ipsum",title:"Lorem ipsum",handle:"",featuredImage:v(),compareAtPriceRange:{maxVariantPrice:{amount:"0",currencyCode:"EUR"},minVariantPrice:{amount:"0",currencyCode:"EUR"}},priceRange:{maxVariantPrice:{amount:"0",currencyCode:"EUR"},minVariantPrice:{amount:"0",currencyCode:"EUR"}},variants:[]}),V=()=>({id:g(),title:"Lorem ipsum",descriptionHtml:"Lorem ipsum",handle:"",image:v(),products:[x(),x(),x()]}),j=(n,e=3)=>{let t=[];for(let r=0;r<4;r++)t.push(n());return t},z=(n,e)=>{switch(n.type){case"collection":return{attributes:e,value:n.isList?j(V):V()};case"image":return{value:n.isList?j(v):{id:g(),...v()},attributes:e};case"product":return{attributes:e,value:n.isList?j(x):x()};case"richtext":return{attributes:e,value:n.isList?["Lorem ipsum","Lorem ipsum","Lorem ipsum"]:`<i>Lorem ipsum dolor sit amet consectetur adipisicing elit</i>. | ||
Officia quisquam, vero eius nam, reiciendis fugiat dolore, sit aspernatur numquam a vitae quae laborum? Rem quidem, neque dignissimos delectus vel quasi!`};default:case"text":return{attributes:e,value:n.isList?["Lorem ipsum","Lorem ipsum","Lorem ipsum"]:"Lorem ipsum dolor sit amet consectetur adipisicing elit."}}};var y=class{constructor(e,t){this.request=async(e,t)=>{let{data:r}=await(await fetch(this.url,{method:"POST",body:JSON.stringify({query:e,variables:t||{}}),headers:{...this.headers||{},"Content-Type":"application/json"}})).json();return r};this.url=e,this.headers=t}},J=class{constructor(e,{storefrontName:t,storefrontAccessToken:r,defaultNameSpaces:i},o){this.getMetafieldsByIds=async e=>{var i,o;if(!e.length)return[{},[]];let t=await this.client.request(R(!1,{customProductMetafields:(o=(i=this.params)==null?void 0:i.product)==null?void 0:o.customMetafieldsIdentifiers}),{ids:e});if(console.log({res:t}),!t)return[{},[]];let{metafields:r}=t;return[b(r,this),r]};this.getMetafieldsForProductsAndCollection=async({collection:e,product:t},r="fra")=>{var u,h;if(!(e!=null&&e.ids)&&!(e!=null&&e.handles)&&!(e!=null&&e.namespaces)&&!(t!=null&&t.ids)&&!(t!=null&&t.handles)&&!(t!=null&&t.namespaces))return[{},[],[]];let i=await this.client.request(q({collectionQuery:P({ids:e==null?void 0:e.ids,handles:e==null?void 0:e.handles}),productQuery:P({ids:t==null?void 0:t.ids,handles:t==null?void 0:t.handles})},{customProductMetafields:(h=(u=this.params)==null?void 0:u.product)==null?void 0:h.customMetafieldsIdentifiers}),{productIdentifiers:this.buildIdentifiers((t==null?void 0:t.namespaces)||[],r),collectionIdentifiers:this.buildIdentifiers((e==null?void 0:e.namespaces)||[],r)});if(console.log({res:i}),!i)return[{},[],[]];let{products:o,collections:s}=i,d=o?p(o):[],c=s?p(s):[],m=d.map(I=>I.content).flat(),a=c.map(I=>I.content).flat(),l=b(m,this),f=b(a,this);return[{...l,...f},m.concat(a),d.concat(c)]};this.getContentForNamespaces=async(e,t="fra")=>{var l,f,u;if(this.locale=t,!e.global)return{content:{},params:e};this.params=e;let{shop:{content:r}}=await this.client.request(w(!0,{customProductMetafields:(l=e.product)==null?void 0:l.customMetafieldsIdentifiers}),{productIdentifiers:this.buildIdentifiers(((f=e.product)==null?void 0:f.namespaces)||[],t),collectionIdentifiers:this.buildIdentifiers(((u=e.collection)==null?void 0:u.namespaces)||[],t),identifiers:this.buildIdentifiers(e.global.concat(this.defaultNameSpaces),t)}),[[i,o],[s,d,c]]=await Promise.all([this.getMetafieldsByIds(Q(r)),this.getMetafieldsForProductsAndCollection(e)]),[m]=await this.getMetafieldsByIds(D(r.concat(o).concat(d))),a=L({client:this,content:r,jsonMetafields:m,nestedContent:{...i,...s}});return console.log("content",r,"jsonMetafields",m,"getJsonNstedIds",D(r)),c.forEach(({id:h,content:I})=>{L({client:this,initalizer:a,id:h,content:I,jsonMetafields:m,nestedContent:{...i,...s}})}),{params:{...e,global:e.global.concat(this.defaultNameSpaces)},content:a}};this.t=(e,t,{productId:r,collectionId:i})=>{let[o,s]=e.split("."),{kind:d,schema:c}=this.config[o];r&&d==="product"&&(s=`${s}@${r}`),i&&d==="collection"&&(s=`${s}@${i}`);let m=c[s],a=()=>k(this.config[o],`${s}.default`,()=>{let l=W(m),f={itemID:g(),itemType:l,itemProp:`${o}:${s}`};return z(m,f)});return k(t,`${o}.${s}`,a)};this.buildIdentifiers=(e,t="fra")=>e?e.reduce((r,i)=>{let o=this.config[i];return o&&Object.keys(o.schema).forEach(s=>{r.push({namespace:`${i}_${t}`,key:s})}),r},[]):[];this.locale="en",this.adapter=o||(s=>s),this.config=e,this.defaultNameSpaces=i||[],this.client=new y(`https://${t}.myshopify.com/api/2022-10/graphql.json`,{"X-SHOPIFY-STOREFRONT-ACCESS-TOKEN":r})}};var re=async(n,{storefrontName:e,storefrontAccessToken:t,adminAccessToken:r},i)=>{console.log(i);let o=new y(`https://${e}.myshopify.com/api/2022-10/graphql.json`,{"X-SHOPIFY-STOREFRONT-ACCESS-TOKEN":t}),s=new y(`https://${e}.myshopify.com/admin/api/2022-10/graphql.json`,{"X-Shopify-Access-Token":r}),{shop:{id:d}}=await o.request(N);return await s.request(F,{value:JSON.stringify(n),key:i,ownerId:d})};0&&(module.exports={GraphqlClient,MetaCms,buildQuery,collectionFragment,config,contentQuery,getMetaCMSConfigQuery,getOwnerIdQuery,imageFragment,isId,mediaImageFragment,metafieldFragment,metafieldsForNodes,metafieldsQuery,normalizeEdges,productFragment,randomId,setInNamespace,setMetaCMSConfigMutation,syncConfig}); | ||
Officia quisquam, vero eius nam, reiciendis fugiat dolore, sit aspernatur numquam a vitae quae laborum? Rem quidem, neque dignissimos delectus vel quasi!`};default:case"text":return{attributes:e,value:n.isList?["Lorem ipsum","Lorem ipsum","Lorem ipsum"]:"Lorem ipsum dolor sit amet consectetur adipisicing elit."}}};var y=class{constructor(e,t){this.request=async(e,t)=>{let{data:r}=await(await fetch(this.url,{method:"POST",body:JSON.stringify({query:e,variables:t||{}}),headers:{...this.headers||{},"Content-Type":"application/json"}})).json();return r};this.url=e,this.headers=t}},J=class{constructor(e,{storefrontName:t,storefrontAccessToken:r,defaultNameSpaces:i},o){this.getMetafieldsByIds=async e=>{var i,o;if(!e.length)return[{},[]];let t=await this.client.request(R(!1,{customProductMetafields:(o=(i=this.params)==null?void 0:i.product)==null?void 0:o.customMetafieldsIdentifiers}),{ids:e});if(console.log({res:t}),!t)return[{},[]];let{metafields:r}=t;return[b(r,this),r]};this.getMetafieldsForProductsAndCollection=async({collection:e,product:t},r="fra")=>{var u,h;if(!(e!=null&&e.ids)&&!(e!=null&&e.handles)&&!(e!=null&&e.namespaces)&&!(t!=null&&t.ids)&&!(t!=null&&t.handles)&&!(t!=null&&t.namespaces))return[{},[],[]];let i=await this.client.request(q({collectionQuery:P({ids:e==null?void 0:e.ids,handles:e==null?void 0:e.handles}),productQuery:P({ids:t==null?void 0:t.ids,handles:t==null?void 0:t.handles})},{customProductMetafields:(h=(u=this.params)==null?void 0:u.product)==null?void 0:h.customMetafieldsIdentifiers}),{productIdentifiers:this.buildIdentifiers((t==null?void 0:t.namespaces)||[],r),collectionIdentifiers:this.buildIdentifiers((e==null?void 0:e.namespaces)||[],r)});if(console.log({res:i}),!i)return[{},[],[]];let{products:o,collections:s}=i,d=o?p(o):[],c=s?p(s):[],m=d.map(I=>I.content).flat(),a=c.map(I=>I.content).flat(),l=b(m,this),f=b(a,this);return[{...l,...f},m.concat(a),d.concat(c)]};this.getContentForNamespaces=async(e,t="fra")=>{var l,f,u;if(this.locale=t,!e.global)return{content:{},params:e};this.params=e;let{shop:{content:r}}=await this.client.request(w(!0,{customProductMetafields:(l=e.product)==null?void 0:l.customMetafieldsIdentifiers}),{productIdentifiers:this.buildIdentifiers(((f=e.product)==null?void 0:f.namespaces)||[],t),collectionIdentifiers:this.buildIdentifiers(((u=e.collection)==null?void 0:u.namespaces)||[],t),identifiers:this.buildIdentifiers(e.global.concat(this.defaultNameSpaces),t)}),[[i,o],[s,d,c]]=await Promise.all([this.getMetafieldsByIds(Q(r)),this.getMetafieldsForProductsAndCollection(e)]),[m]=await this.getMetafieldsByIds(D(r.concat(o).concat(d))),a=L({client:this,content:r,jsonMetafields:m,nestedContent:{...i,...s}});return console.log("content",r,"jsonMetafields",m,"getJsonNstedIds",D(r)),c.forEach(({id:h,content:I})=>{L({client:this,initalizer:a,id:h,content:I,jsonMetafields:m,nestedContent:{...i,...s}})}),{params:{...e,global:e.global.concat(this.defaultNameSpaces)},content:a}};this.t=(e,t,{productId:r,collectionId:i})=>{let[o,s]=e.split("."),{kind:d,schema:c}=this.config[o];r&&d==="product"&&(s=`${s}@${r}`),i&&d==="collection"&&(s=`${s}@${i}`);let m=c[s],a=()=>k(this.config[o],`${s}.default`,()=>{let l=W(m),f={itemID:g(),itemType:l,itemProp:`${o}:${s}`};return z(m,f)});return k(t,`${o}.${s}`,a)};this.buildIdentifiers=(e,t="fra")=>e?e.reduce((r,i)=>{let o=this.config[i];return o&&Object.keys(o.schema).forEach(s=>{r.push({namespace:`${i}@${t}`,key:s})}),r},[]):[];this.locale="en",this.adapter=o||(s=>s),this.config=e,this.defaultNameSpaces=i||[],this.client=new y(`https://${t}.myshopify.com/api/2022-10/graphql.json`,{"X-SHOPIFY-STOREFRONT-ACCESS-TOKEN":r})}};var re=async(n,{storefrontName:e,storefrontAccessToken:t,adminAccessToken:r},i)=>{console.log(i);let o=new y(`https://${e}.myshopify.com/api/2022-10/graphql.json`,{"X-SHOPIFY-STOREFRONT-ACCESS-TOKEN":t}),s=new y(`https://${e}.myshopify.com/admin/api/2022-10/graphql.json`,{"X-Shopify-Access-Token":r}),{shop:{id:d}}=await o.request(N);return await s.request(F,{value:JSON.stringify(n),key:i,ownerId:d})};0&&(module.exports={GraphqlClient,MetaCms,buildQuery,collectionFragment,config,contentQuery,getMetaCMSConfigQuery,getOwnerIdQuery,imageFragment,isId,mediaImageFragment,metafieldFragment,metafieldsForNodes,metafieldsQuery,normalizeEdges,productFragment,randomId,setInNamespace,setMetaCMSConfigMutation,syncConfig}); |
{ | ||
"name": "@meta-cms/core", | ||
"version": "1.0.44", | ||
"version": "1.0.45", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |