@meta-cms/core
Advanced tools
Comparing version 1.0.54 to 1.0.55
62
index.js
@@ -1,2 +0,2 @@ | ||
"use strict";var F=Object.defineProperty;var U=Object.getOwnPropertyDescriptor;var K=Object.getOwnPropertyNames;var X=Object.prototype.hasOwnProperty;var Y=(r,e)=>{for(var t in e)F(r,t,{get:e[t],enumerable:!0})},Z=(r,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of K(e))!X.call(r,i)&&i!==t&&F(r,i,{get:()=>e[i],enumerable:!(n=U(e,i))||n.enumerable});return r};var ee=r=>Z(F({},"__esModule",{value:!0}),r);var ae={};Y(ae,{GraphqlClient:()=>C,MetaCms:()=>H,buildQuery:()=>P,collectionFragment:()=>L,config:()=>re,contentQuery:()=>w,fieldToMetafieldType:()=>J,getLoremCollectionData:()=>v,getLoremImageData:()=>$,getLoremProductData:()=>h,getMetaCMSConfigQuery:()=>te,getOwnerIdQuery:()=>A,imageFragment:()=>M,isId:()=>I,mediaImageFragment:()=>E,metafieldFragment:()=>x,metafieldsForNodes:()=>q,metafieldsQuery:()=>R,mockValue:()=>G,normalizeEdges:()=>u,productFragment:()=>S,randomId:()=>y,setInNamespace:()=>T,setMetaCMSConfigMutation:()=>_,syncConfig:()=>se});module.exports=ee(ae);var N=(r,e,t)=>{if(!r)return t?t():void 0;var n,i;if(Array.isArray(e)&&(n=e.slice(0)),typeof e=="string"&&(n=e.split(".")),typeof e=="symbol"&&(n=[e]),!Array.isArray(n))throw new Error("props arg must be an array, a string or a symbol");for(;n.length;)if(i=n.shift(),!r||(r=r[i],r===void 0))return t?t():void 0;return r};var V=r=>{let e=[];return r.forEach(t=>{!t||(t.type==="product_reference"&&t.product.content&&t.product.content.map(n=>{e.push(n.id)}),t.type==="list.product_reference"&&t.values.edges.map(({node:n})=>{n.content&&n.content.map(i=>{e.push(i.id)})}),t.type==="collection_reference"&&t.collection.content&&t.collection.content.map(n=>{e.push(n.id)}),t.type==="list.collection_reference"&&t.values.edges.map(({node:n})=>{n.content&&n.content.map(i=>{e.push(i.id)})}))}),e};var _=` | ||
"use strict";var F=Object.defineProperty;var U=Object.getOwnPropertyDescriptor;var K=Object.getOwnPropertyNames;var X=Object.prototype.hasOwnProperty;var Y=(t,e)=>{for(var n in e)F(t,n,{get:e[n],enumerable:!0})},Z=(t,e,n,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of K(e))!X.call(t,i)&&i!==n&&F(t,i,{get:()=>e[i],enumerable:!(r=U(e,i))||r.enumerable});return t};var ee=t=>Z(F({},"__esModule",{value:!0}),t);var ae={};Y(ae,{GraphqlClient:()=>C,MetaCms:()=>V,buildQuery:()=>P,collectionFragment:()=>L,config:()=>ne,contentQuery:()=>w,fieldToMetafieldType:()=>G,getLoremCollectionData:()=>v,getLoremImageData:()=>$,getLoremProductData:()=>h,getMetaCMSConfigQuery:()=>te,getOwnerIdQuery:()=>A,imageFragment:()=>M,isId:()=>I,mediaImageFragment:()=>E,metafieldFragment:()=>x,metafieldsForNodes:()=>q,metafieldsQuery:()=>R,mockValue:()=>H,normalizeEdges:()=>m,productFragment:()=>S,randomId:()=>y,setInNamespace:()=>T,setMetaCMSConfigMutation:()=>_,syncConfig:()=>se});module.exports=ee(ae);var N=(t,e,n)=>{if(!t)return n?n():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(),!t||(t=t[i],t===void 0))return n?n():void 0;return t};var W=t=>{let e=[];return t.forEach(n=>{!n||(n.type==="product_reference"&&n.product.content&&n.product.content.map(r=>{e.push(r.id)}),n.type==="list.product_reference"&&n.values.edges.map(({node:r})=>{r.content&&r.content.map(i=>{e.push(i.id)})}),n.type==="collection_reference"&&n.collection.content&&n.collection.content.map(r=>{e.push(r.id)}),n.type==="list.collection_reference"&&n.values.edges.map(({node:r})=>{r.content&&r.content.map(i=>{e.push(i.id)})}))}),e};var _=` | ||
mutation ($ownerId: ID!, $value: String!, $key: String!) { | ||
@@ -24,8 +24,8 @@ metafieldsSet(metafields: [{ key: $key, namespace: "meta-cms", ownerId: $ownerId, value: $value, type: "json" }]) { | ||
} | ||
`;var E=r=>` | ||
`;var E=t=>` | ||
id | ||
image { | ||
${M(r)} | ||
${M(t)} | ||
} | ||
`,M=r=>` | ||
`,M=t=>` | ||
alt: altText | ||
@@ -35,3 +35,3 @@ height | ||
src: url(transform:{maxWidth:4000}) | ||
`,S=(r,e)=>` | ||
`,S=(t,e)=>` | ||
id | ||
@@ -76,3 +76,3 @@ descriptionHtml | ||
}`:""} | ||
${W(r,"product")} | ||
${z(t,"product")} | ||
@@ -102,3 +102,3 @@ featuredImage { | ||
} | ||
`,L=(r,e)=>` | ||
`,L=(t,e)=>` | ||
id | ||
@@ -109,3 +109,3 @@ title | ||
image { | ||
${M(r)} | ||
${M(t)} | ||
} | ||
@@ -115,8 +115,8 @@ products(first: 20) { | ||
node { | ||
${S(r,e)} | ||
${S(t,e)} | ||
} | ||
} | ||
} | ||
${W(r,"collection")} | ||
`,W=(r,e)=>r?` | ||
${z(t,"collection")} | ||
`,z=(t,e)=>t?` | ||
content: metafields(identifiers: ${e==="product"?"$productIdentifiers":"$collectionIdentifiers"}) { | ||
@@ -127,3 +127,3 @@ id | ||
} | ||
`:"",x=(r,e)=>` | ||
`:"",x=(t,e)=>` | ||
type | ||
@@ -142,9 +142,9 @@ value | ||
... on MediaImage { | ||
${E(r)} | ||
${E(t)} | ||
} | ||
... on Collection { | ||
${L(r,e)} | ||
${L(t,e)} | ||
} | ||
... on Product { | ||
${S(r,e)} | ||
${S(t,e)} | ||
} | ||
@@ -156,3 +156,3 @@ } | ||
... on MediaImage { | ||
${E(r)} | ||
${E(t)} | ||
} | ||
@@ -162,3 +162,3 @@ } | ||
... on Collection { | ||
${L(r,e)} | ||
${L(t,e)} | ||
} | ||
@@ -168,9 +168,9 @@ } | ||
... on Product { | ||
${S(r,e)} | ||
${S(t,e)} | ||
} | ||
} | ||
`,w=(r,e)=>` | ||
`,w=(t,e)=>` | ||
query getContent( | ||
$identifiers: [HasMetafieldsIdentifier!]! | ||
${r?`$productIdentifiers: [HasMetafieldsIdentifier!]! | ||
${t?`$productIdentifiers: [HasMetafieldsIdentifier!]! | ||
$collectionIdentifiers: [HasMetafieldsIdentifier!]!`:""} | ||
@@ -180,19 +180,19 @@ ) { | ||
content: metafields(identifiers: $identifiers) { | ||
${x(r)} | ||
${x(t)} | ||
} | ||
} | ||
} | ||
`,R=(r,e)=>` | ||
`,R=(t,e)=>` | ||
query getMetafields($ids: [ID!]!) { | ||
metafields: nodes(ids: $ids ${e!=null&&e.customProductMetafields?"$customProductMetafields: [HasMetafieldsIdentifier!]! ":""}) { | ||
... on Metafield { | ||
${x(r,e)} | ||
${x(t,e)} | ||
} | ||
} | ||
} | ||
`;var q=({collectionQuery:r,productQuery:e},t)=>` | ||
`;var q=({collectionQuery:t,productQuery:e},n)=>` | ||
query getNodeContent( | ||
${e?"$productIdentifiers: [HasMetafieldsIdentifier!]! $productQuery:String":""} | ||
${r?"$collectionIdentifiers: [HasMetafieldsIdentifier!]! $collectionQuery:String":""} | ||
${t!=null&&t.customProductMetafields?"$customProductMetafields: [HasMetafieldsIdentifier!]! ":""} | ||
${t?"$collectionIdentifiers: [HasMetafieldsIdentifier!]! $collectionQuery:String":""} | ||
${n!=null&&n.customProductMetafields?"$customProductMetafields: [HasMetafieldsIdentifier!]! ":""} | ||
){ | ||
@@ -205,3 +205,3 @@ | ||
content: metafields(identifiers: $productIdentifiers) { | ||
${x(!1,t)} | ||
${x(!1,n)} | ||
} | ||
@@ -212,3 +212,3 @@ } | ||
${r?`collections(first:50, query:$collectionQuery){ | ||
${t?`collections(first:50, query:$collectionQuery){ | ||
edges{ | ||
@@ -218,3 +218,3 @@ node{ | ||
content: metafields(identifiers: $collectionIdentifiers) { | ||
${x(!1,t)} | ||
${x(!1,n)} | ||
} | ||
@@ -225,3 +225,3 @@ } | ||
}`;var u=r=>r.edges.reduce((e,{node:t})=>(e.push(t),e),[]),re=r=>r,y=()=>{let r="meta-cms-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict",e="",t=10;for(;t--;)e+=r[Math.random()*r.length|0];return`meta-cms-${e}`},I=r=>typeof r=="string"?r.startsWith("gid://shopify/Metafield/"):!1,T=({content:r,namespace:e,key:t,id:n,value:i})=>{let c=n?`${t}@${n}`:t;r[e]||Object.assign(r,{[e]:{}}),Object.assign(r[e],{[c]:i})},P=({handles:r,ids:e})=>{let t=n=>n.replace("gid://shopify/Product/","");return(r==null?void 0:r.length)||(e==null?void 0:e.length)?`${(e||[]).map((n,i,c)=>`id:${t(n)}${c.length-1>i?" or ":r!=null&&r.length?" or":""}`).join("")} ${(r||[]).map((n,i,c)=>`handle:${n}${c.length-1>i?" or ":""}`).join("")}`.trim():void 0};var O=({field:r,acc:e,item:t,locale:n,jsonMetafields:i,nestedContent:c})=>{var m;let a={itemID:t.id,itemType:t.type,itemProp:`${t.namespace}:${t.key}`},o,f=({id:s,key:d,namespace:l,objectId:p})=>{c&&e&&n&&T({content:e,key:d,id:p,namespace:l.replace(`@${n}`,""),value:c[s]})};switch(t.type){case"collection_reference":{o={attributes:a,value:{...t.collection,products:u(t.collection.products).map(({content:s,...d})=>(s==null||s.forEach(l=>f({...l,objectId:d.id})),{...d,variants:u(d.variants)}))}};break}case"file_reference":{(!t.image.image.alt||!t.image.image.alt.length)&&(t.image.image.alt="image"),o={attributes:a,value:t.image.image};break}case"product_reference":{let{product:s}=t;(m=s.content)==null||m.forEach(d=>f({...d,objectId:s.id})),o={attributes:a,value:{...s,variants:u(t.product.variants)}};break}case"list.collection_reference":{o={attributes:a,value:u(t.values).map(s=>{var d;return(d=s.content)==null||d.forEach(l=>f({...l,objectId:s.id})),{...s,products:u(s.products).map(l=>{var p;return(p=l.content)==null||p.forEach(g=>f({...g,objectId:l.id})),{...l,variants:u(l.variants)}})}})};break}case"list.file_reference":{o={attributes:a,value:u(t.values).map(s=>s.image)};break}case"list.product_reference":{o={attributes:a,value:u(t.values).map(s=>{var d;return(d=s.content)==null||d.forEach(l=>f({...l,objectId:s.id})),{...s,variants:u(s.variants)}})};break}case"json":{let s=z(t.value,i);o={attributes:a,value:s.value||s};break}case"list.multi_line_text_field":case"list.single_line_text_field":{o={attributes:a,value:JSON.parse(t.value)};break}case"multi_line_text_field":case"single_line_text_field":{o={attributes:a,value:t.value};break}case"boolean":{o={attributes:a,value:t.value==="true"};break}case"url":{o={attributes:a,value:t.value};break}}return o},j=({client:r,id:e,initalizer:t,content:n,jsonMetafields:i,nestedContent:c})=>{var o,f;let a=t||{};for(let m=0;m<n.length;m++){let s=n[m];if(s){let d=(f=(o=r.config[s.namespace.replace(`@${r.locale}`,"")])==null?void 0:o.schema)==null?void 0:f[s.key];if(!d)return a;let l=O({client:r,field:d,acc:a,item:s,jsonMetafields:i,nestedContent:c});T({content:a,key:s.key,namespace:s.namespace.replace(`@${r.locale}`,""),value:r.adapter(l)})}}return a};var ne=r=>{let e=[];return r&&JSON.parse(r,(t,n)=>(Array.isArray(n)?n.forEach(i=>{I(i)&&e.push(i)}):I(n)&&e.push(n),n)),e},B=r=>{let e=[];return r.forEach(t=>{!t||t.type==="json"&&(e=e.concat(ne(t.value)))}),e},b=(r,e)=>r.reduce((t,n)=>{var i,c;if(n){let a=(c=(i=e.config[n.namespace.replace(`@${e.locale}`,"")])==null?void 0:i.schema)==null?void 0:c[n.key];if(!a)return t;let o=O({field:a,item:n,client:e});t[n.id]=e.adapter(o)}return t},{}),z=(r,e)=>e?JSON.parse(r,(t,n)=>Array.isArray(n)?n.map(i=>I(i)?e[i]:i):I(n)?e[n]:n):JSON.parse(r);var J=r=>{let t={text:"single_line_text_field",richtext:"multi_line_text_field",image:"file_reference",product:"product_reference",collection:"collection_reference",object:"json",boolean:"boolean",url:"url"}[r.type];return r.isList?`list.${t}`:t},$=(r=400,e=300)=>({alt:"image",src:`https://picsum.photos/${r}/${e}`,width:r,height:e}),h=()=>({id:y(),descriptionHtml:"Lorem ipsum",title:"Lorem ipsum",handle:"",featuredImage:$(),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:y(),title:"Lorem ipsum",descriptionHtml:"Lorem ipsum",handle:"",image:$(),products:[h(),h(),h()]}),D=(r,e=3)=>{let t=[];for(let n=0;n<4;n++)t.push(r());return t},ie=r=>{if(r==="text"||r==="richtext")return"Lorem ipsum";if(r==="image")return $();if(r==="product")return h();if(r==="collection")return v()},Q=(r,e=!0)=>Object.entries(r.schema).reduce((t,[n,i])=>(i.isList?i.type==="object"?t[n]=Q(i,!1):t[n]=[]:i.type==="object"?t[n]=Q(i,!1):t[n]=ie(i.type),t),r.type==="object"&&e?{}:r.isList?[]:{}),G=(r,e,t=!0)=>{switch(r.type){case"object":return r.isList?{attributes:e,value:[]}:{attributes:e,value:Q(r)};case"collection":return{attributes:e,value:r.isList?D(v):v()};case"image":return{value:r.isList?t?D($):[]:t?{id:y(),...$()}:void 0,attributes:e};case"product":return{attributes:e,value:r.isList?D(h):h()};case"richtext":return{attributes:e,value:r.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!`};case"boolean":return{attributes:e,value:!0};case"url":return{attributes:e,value:"https://google.com"};default:case"text":return{attributes:e,value:r.isList?["Lorem ipsum","Lorem ipsum","Lorem ipsum"]:"Lorem ipsum dolor sit amet consectetur adipisicing elit."}}};var C=class{constructor(e,t){this.request=async(e,t)=>{let{data:n}=await(await fetch(this.url,{method:"POST",body:JSON.stringify({query:e,variables:t||{}}),headers:{...this.headers||{},"Content-Type":"application/json"}})).json();return n};this.url=e,this.headers=t}},H=class{constructor(e,{storefrontName:t,storefrontAccessToken:n,defaultNameSpaces:i},c){this.getMetafieldsByIds=async e=>{var i,c;if(!e.length)return[{},[]];let t=await this.client.request(R(!1,{customProductMetafields:(c=(i=this.params)==null?void 0:i.product)==null?void 0:c.customMetafieldsIdentifiers}),{ids:e});if(!t)return[{},[]];let{metafields:n}=t;return[b(n,this),n]};this.getMetafieldsForProductsAndCollection=async({collection:e,product:t,locale:n})=>{var p,g;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:(g=(p=this.params)==null?void 0:p.product)==null?void 0:g.customMetafieldsIdentifiers}),{productIdentifiers:this.buildIdentifiers((t==null?void 0:t.namespaces)||[],n),collectionIdentifiers:this.buildIdentifiers((e==null?void 0:e.namespaces)||[],n)});if(!i)return[{},[],[]];let{products:c,collections:a}=i,o=c?u(c):[],f=a?u(a):[],m=o.map(k=>k.content).flat(),s=f.map(k=>k.content).flat(),d=b(m,this),l=b(s,this);return[{...d,...l},m.concat(s),o.concat(f)]};this.getContentForNamespaces=async e=>{var s,d,l;if(this.locale=e.locale||"en",!e.global)return{content:{},params:e};this.params=e;let{shop:{content:t}}=await this.client.request(w(!0,{customProductMetafields:(s=e.product)==null?void 0:s.customMetafieldsIdentifiers}),{productIdentifiers:this.buildIdentifiers(((d=e.product)==null?void 0:d.namespaces)||[],e.locale),collectionIdentifiers:this.buildIdentifiers(((l=e.collection)==null?void 0:l.namespaces)||[],e.locale),identifiers:this.buildIdentifiers(e.global.concat(this.defaultNameSpaces),e.locale)}),[[n,i],[c,a,o]]=await Promise.all([this.getMetafieldsByIds(V(t)),this.getMetafieldsForProductsAndCollection(e)]),[f]=await this.getMetafieldsByIds(B(t.concat(i).concat(a))),m=j({client:this,content:t,jsonMetafields:f,nestedContent:{...n,...c}});return o.forEach(({id:p,content:g})=>{j({client:this,initalizer:m,id:p,content:g,jsonMetafields:f,nestedContent:{...n,...c}})}),{params:{...e,global:e.global.concat(this.defaultNameSpaces)},content:m}};this.t=(e,t,{productId:n,collectionId:i},c)=>{let[a,o]=e.split("."),{kind:f,schema:m}=this.config[a];n&&f==="product"&&(o=`${o}@${n}`),i&&f==="collection"&&(o=`${o}@${i}`);let s=m[o],d=()=>N(this.config[a],`${o}.default`,()=>{let l=J(s),p={itemID:y(),itemType:l,itemProp:`${a}:${o}`},g=G(s,p);return c?c(g,s):g});return N(t,`${a}.${o}`,d)};this.buildIdentifiers=(e,t)=>e?e.reduce((n,i)=>{let c=this.config[i];return c&&Object.keys(c.schema).forEach(a=>{n.push({namespace:`${i}@${t}`,key:a})}),n},[]):[];this.locale="en",this.adapter=c||(a=>a),this.config=e,this.defaultNameSpaces=i||[],this.client=new C(`https://${t}.myshopify.com/api/2022-10/graphql.json`,{"X-SHOPIFY-STOREFRONT-ACCESS-TOKEN":n})}};var se=async(r,{storefrontName:e,storefrontAccessToken:t,adminAccessToken:n},i)=>{console.log(i);let c=new C(`https://${e}.myshopify.com/api/2022-10/graphql.json`,{"X-SHOPIFY-STOREFRONT-ACCESS-TOKEN":t}),a=new C(`https://${e}.myshopify.com/admin/api/2022-10/graphql.json`,{"X-Shopify-Access-Token":n}),{shop:{id:o}}=await c.request(A);return await a.request(_,{value:JSON.stringify(r),key:i,ownerId:o})};0&&(module.exports={GraphqlClient,MetaCms,buildQuery,collectionFragment,config,contentQuery,fieldToMetafieldType,getLoremCollectionData,getLoremImageData,getLoremProductData,getMetaCMSConfigQuery,getOwnerIdQuery,imageFragment,isId,mediaImageFragment,metafieldFragment,metafieldsForNodes,metafieldsQuery,mockValue,normalizeEdges,productFragment,randomId,setInNamespace,setMetaCMSConfigMutation,syncConfig}); | ||
}`;var m=t=>t.edges.reduce((e,{node:n})=>(e.push(n),e),[]),ne=t=>t,y=()=>{let t="meta-cms-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict",e="",n=10;for(;n--;)e+=t[Math.random()*t.length|0];return`meta-cms-${e}`},I=t=>typeof t=="string"?t.startsWith("gid://shopify/Metafield/"):!1,T=({content:t,namespace:e,key:n,id:r,value:i})=>{let s=r?`${n}@${r}`:n;t[e]||Object.assign(t,{[e]:{}}),Object.assign(t[e],{[s]:i})},P=({handles:t,ids:e})=>{let n=r=>r.replace("gid://shopify/Product/","");return(t==null?void 0:t.length)||(e==null?void 0:e.length)?`${(e||[]).map((r,i,s)=>`id:${n(r)}${s.length-1>i?" or ":t!=null&&t.length?" or":""}`).join("")} ${(t||[]).map((r,i,s)=>`handle:${r}${s.length-1>i?" or ":""}`).join("")}`.trim():void 0};var O=({acc:t,item:e,locale:n,jsonMetafields:r,nestedContent:i})=>{var f;let s={itemID:e.id,itemType:e.type,itemProp:`${e.namespace}:${e.key}`},o,d=({id:a,key:c,namespace:l,objectId:u})=>{i&&t&&n&&T({content:t,key:c,id:u,namespace:l.replace(`@${n}`,""),value:i[a]})};switch(e.type){case"collection_reference":{o={attributes:s,value:{...e.collection,products:m(e.collection.products).map(({content:a,...c})=>(a==null||a.forEach(l=>d({...l,objectId:c.id})),{...c,variants:m(c.variants)}))}};break}case"file_reference":{(!e.image.image.alt||!e.image.image.alt.length)&&(e.image.image.alt="image"),o={attributes:s,value:e.image.image};break}case"product_reference":{let{product:a}=e;(f=a.content)==null||f.forEach(c=>d({...c,objectId:a.id})),o={attributes:s,value:{...a,variants:m(e.product.variants)}};break}case"list.collection_reference":{o={attributes:s,value:m(e.values).map(a=>{var c;return(c=a.content)==null||c.forEach(l=>d({...l,objectId:a.id})),{...a,products:m(a.products).map(l=>{var u;return(u=l.content)==null||u.forEach(p=>d({...p,objectId:l.id})),{...l,variants:m(l.variants)}})}})};break}case"list.file_reference":{o={attributes:s,value:m(e.values).map(a=>a.image)};break}case"list.product_reference":{o={attributes:s,value:m(e.values).map(a=>{var c;return(c=a.content)==null||c.forEach(l=>d({...l,objectId:a.id})),{...a,variants:m(a.variants)}})};break}case"json":{let a=B(e.value,r);o={attributes:s,value:a.value||a};break}case"list.multi_line_text_field":case"list.single_line_text_field":{o={attributes:s,value:JSON.parse(e.value)};break}case"multi_line_text_field":case"single_line_text_field":{o={attributes:s,value:e.value||""};break}case"boolean":{o={attributes:s,value:e.value==="true"};break}case"url":{o={attributes:s,value:e.value||""};break}}return o},j=({client:t,id:e,initalizer:n,content:r,jsonMetafields:i,nestedContent:s})=>{var d,f;let o=n||{};for(let a=0;a<r.length;a++){let c=r[a];if(c){if(!((f=(d=t.config[c.namespace.replace(`@${t.locale}`,"")])==null?void 0:d.schema)==null?void 0:f[c.key]))return o;let u=O({client:t,acc:o,item:c,jsonMetafields:i,nestedContent:s});T({content:o,key:c.key,namespace:c.namespace.replace(`@${t.locale}`,""),value:t.adapter(u)})}}return o};var re=t=>{let e=[];return t&&JSON.parse(t,(n,r)=>(Array.isArray(r)?r.forEach(i=>{I(i)&&e.push(i)}):I(r)&&e.push(r),r)),e},D=t=>{let e=[];return t.forEach(n=>{!n||n.type==="json"&&(e=e.concat(re(n.value)))}),e},b=(t,e)=>t.reduce((n,r)=>{if(r){let i=O({item:r,client:e});n[r.id]=e.adapter(i)}return n},{}),B=(t,e)=>e?JSON.parse(t,(n,r)=>Array.isArray(r)?r.map(i=>I(i)?e[i]:i):I(r)?e[r]:r):JSON.parse(t);var G=t=>{let n={text:"single_line_text_field",richtext:"multi_line_text_field",image:"file_reference",product:"product_reference",collection:"collection_reference",object:"json",boolean:"boolean",url:"url"}[t.type];return t.isList?`list.${n}`:n},$=(t=400,e=300)=>({alt:"image",src:`https://picsum.photos/${t}/${e}`,width:t,height:e}),h=()=>({id:y(),descriptionHtml:"Lorem ipsum",title:"Lorem ipsum",handle:"",featuredImage:$(),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:y(),title:"Lorem ipsum",descriptionHtml:"Lorem ipsum",handle:"",image:$(),products:[h(),h(),h()]}),Q=(t,e=3)=>{let n=[];for(let r=0;r<4;r++)n.push(t());return n},ie=t=>{if(t==="text"||t==="richtext")return"Lorem ipsum";if(t==="image")return $();if(t==="product")return h();if(t==="collection")return v()},J=(t,e=!0)=>Object.entries(t.schema).reduce((n,[r,i])=>(i.isList?i.type==="object"?n[r]=J(i,!1):n[r]=[]:i.type==="object"?n[r]=J(i,!1):n[r]=ie(i.type),n),t.type==="object"&&e?{}:t.isList?[]:{}),H=(t,e,n=!0)=>{switch(t.type){case"object":return t.isList?{attributes:e,value:[]}:{attributes:e,value:J(t)};case"collection":return{attributes:e,value:t.isList?Q(v):v()};case"image":return{value:t.isList?n?Q($):[]:n?{id:y(),...$()}:void 0,attributes:e};case"product":return{attributes:e,value:t.isList?Q(h):h()};case"richtext":return{attributes:e,value:t.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!`};case"boolean":return{attributes:e,value:!0};case"url":return{attributes:e,value:"https://google.com"};default:case"text":return{attributes:e,value:t.isList?["Lorem ipsum","Lorem ipsum","Lorem ipsum"]:"Lorem ipsum dolor sit amet consectetur adipisicing elit."}}};var C=class{constructor(e,n){this.request=async(e,n)=>{let{data:r}=await(await fetch(this.url,{method:"POST",body:JSON.stringify({query:e,variables:n||{}}),headers:{...this.headers||{},"Content-Type":"application/json"}})).json();return r};this.url=e,this.headers=n}},V=class{constructor(e,{storefrontName:n,storefrontAccessToken:r,defaultNameSpaces:i},s){this.getMetafieldsByIds=async e=>{var i,s;if(!e.length)return[{},[]];let n=await this.client.request(R(!1,{customProductMetafields:(s=(i=this.params)==null?void 0:i.product)==null?void 0:s.customMetafieldsIdentifiers}),{ids:e});if(!n)return[{},[]];let{metafields:r}=n;return[b(r,this),r]};this.getMetafieldsForProductsAndCollection=async({collection:e,product:n,locale:r})=>{var p,g;if(!(e!=null&&e.ids)&&!(e!=null&&e.handles)&&!(e!=null&&e.namespaces)&&!(n!=null&&n.ids)&&!(n!=null&&n.handles)&&!(n!=null&&n.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:n==null?void 0:n.ids,handles:n==null?void 0:n.handles})},{customProductMetafields:(g=(p=this.params)==null?void 0:p.product)==null?void 0:g.customMetafieldsIdentifiers}),{productIdentifiers:this.buildIdentifiers((n==null?void 0:n.namespaces)||[],r),collectionIdentifiers:this.buildIdentifiers((e==null?void 0:e.namespaces)||[],r)});if(!i)return[{},[],[]];let{products:s,collections:o}=i,d=s?m(s):[],f=o?m(o):[],a=d.map(k=>k.content).flat(),c=f.map(k=>k.content).flat(),l=b(a,this),u=b(c,this);return[{...l,...u},a.concat(c),d.concat(f)]};this.getContentForNamespaces=async e=>{var c,l,u;if(this.locale=e.locale||"en",console.log("this.locale",this.locale),!e.global)return{content:{},params:e};this.params=e;let{shop:{content:n}}=await this.client.request(w(!0,{customProductMetafields:(c=e.product)==null?void 0:c.customMetafieldsIdentifiers}),{productIdentifiers:this.buildIdentifiers(((l=e.product)==null?void 0:l.namespaces)||[],e.locale),collectionIdentifiers:this.buildIdentifiers(((u=e.collection)==null?void 0:u.namespaces)||[],e.locale),identifiers:this.buildIdentifiers(e.global.concat(this.defaultNameSpaces),e.locale)}),[[r,i],[s,o,d]]=await Promise.all([this.getMetafieldsByIds(W(n)),this.getMetafieldsForProductsAndCollection(e)]),[f]=await this.getMetafieldsByIds(D(n.concat(i).concat(o)));console.log({jsonMetafields:f},D(n));let a=j({client:this,content:n,jsonMetafields:f,nestedContent:{...r,...s}});return d.forEach(({id:p,content:g})=>{j({client:this,initalizer:a,id:p,content:g,jsonMetafields:f,nestedContent:{...r,...s}})}),{params:{...e,global:e.global.concat(this.defaultNameSpaces)},content:a}};this.t=(e,n,{productId:r,collectionId:i},s)=>{let[o,d]=e.split("."),{kind:f,schema:a}=this.config[o];r&&f==="product"&&(d=`${d}@${r}`),i&&f==="collection"&&(d=`${d}@${i}`);let c=a[d],l=()=>N(this.config[o],`${d}.default`,()=>{let u=G(c),p={itemID:y(),itemType:u,itemProp:`${o}:${d}`},g=H(c,p);return s?s(g,c):g});return N(n,`${o}.${d}`,l)};this.buildIdentifiers=(e,n)=>e?e.reduce((r,i)=>{let s=this.config[i];return s&&Object.keys(s.schema).forEach(o=>{r.push({namespace:`${i}@${n}`,key:o})}),r},[]):[];this.locale="en",this.adapter=s||(o=>o),this.config=e,this.defaultNameSpaces=i||[],this.client=new C(`https://${n}.myshopify.com/api/2022-10/graphql.json`,{"X-SHOPIFY-STOREFRONT-ACCESS-TOKEN":r})}};var se=async(t,{storefrontName:e,storefrontAccessToken:n,adminAccessToken:r},i)=>{console.log(i);let s=new C(`https://${e}.myshopify.com/api/2022-10/graphql.json`,{"X-SHOPIFY-STOREFRONT-ACCESS-TOKEN":n}),o=new C(`https://${e}.myshopify.com/admin/api/2022-10/graphql.json`,{"X-Shopify-Access-Token":r}),{shop:{id:d}}=await s.request(A);return await o.request(_,{value:JSON.stringify(t),key:i,ownerId:d})};0&&(module.exports={GraphqlClient,MetaCms,buildQuery,collectionFragment,config,contentQuery,fieldToMetafieldType,getLoremCollectionData,getLoremImageData,getLoremProductData,getMetaCMSConfigQuery,getOwnerIdQuery,imageFragment,isId,mediaImageFragment,metafieldFragment,metafieldsForNodes,metafieldsQuery,mockValue,normalizeEdges,productFragment,randomId,setInNamespace,setMetaCMSConfigMutation,syncConfig}); |
{ | ||
"name": "@meta-cms/core", | ||
"version": "1.0.54", | ||
"version": "1.0.55", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32596