@meta-cms/core
Advanced tools
Comparing version 1.0.93 to 1.0.94
26
index.js
@@ -1,2 +0,2 @@ | ||
"use strict";var A=Object.defineProperty;var Z=Object.getOwnPropertyDescriptor;var ee=Object.getOwnPropertyNames;var te=Object.prototype.hasOwnProperty;var re=(t,e)=>{for(var r in e)A(t,r,{get:e[r],enumerable:!0})},ne=(t,e,r,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of ee(e))!te.call(t,i)&&i!==r&&A(t,i,{get:()=>e[i],enumerable:!(n=Z(e,i))||n.enumerable});return t};var ie=t=>ne(A({},"__esModule",{value:!0}),t);var ue={};re(ue,{GraphqlClient:()=>y,MetaCms:()=>H,buildQuery:()=>P,collectionFragment:()=>j,config:()=>ae,contentQuery:()=>D,fieldToMetafieldType:()=>_,getLoremCollectionData:()=>J,getLoremImageData:()=>v,getLoremProductData:()=>x,getMetaCMSConfigQuery:()=>se,getOwnerIdQuery:()=>w,imageFragment:()=>$,isId:()=>C,mediaImageFragment:()=>E,metafieldFragment:()=>b,metafieldsForNodes:()=>R,metafieldsQuery:()=>O,mockValue:()=>F,mustaching:()=>Y,normalizeEdges:()=>m,productFragment:()=>M,randomId:()=>g,setInNamespace:()=>T,setMetaCMSConfigMutation:()=>N,syncConfig:()=>ce,templating:()=>de,traverse:()=>S,videoFragment:()=>L});module.exports=ie(ue);var z=t=>{let e=[];return t.forEach(r=>{!r||(r.type==="product_reference"&&r.product.content&&r.product.content.map(n=>{e.push(n.id)}),r.type==="list.product_reference"&&r.values.edges.map(({node:n})=>{n.content&&n.content.map(i=>{e.push(i.id)})}),r.type==="collection_reference"&&r.collection.content&&r.collection.content.map(n=>{e.push(n.id)}),r.type==="list.collection_reference"&&r.values.edges.map(({node:n})=>{n.content&&n.content.map(i=>{e.push(i.id)})}))}),e};var N=` | ||
"use strict";var A=Object.defineProperty;var Z=Object.getOwnPropertyDescriptor;var ee=Object.getOwnPropertyNames;var te=Object.prototype.hasOwnProperty;var re=(t,e)=>{for(var r in e)A(t,r,{get:e[r],enumerable:!0})},ne=(t,e,r,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of ee(e))!te.call(t,i)&&i!==r&&A(t,i,{get:()=>e[i],enumerable:!(n=Z(e,i))||n.enumerable});return t};var ie=t=>ne(A({},"__esModule",{value:!0}),t);var fe={};re(fe,{GraphqlClient:()=>y,MetaCms:()=>H,buildQuery:()=>P,collectionFragment:()=>j,config:()=>ae,contentQuery:()=>D,fieldToMetafieldType:()=>_,getLoremCollectionData:()=>J,getLoremImageData:()=>b,getLoremProductData:()=>x,getMetaCMSConfigQuery:()=>se,getOwnerIdQuery:()=>N,imageFragment:()=>$,isId:()=>C,mediaImageFragment:()=>E,metafieldFragment:()=>M,metafieldsForNodes:()=>R,metafieldsQuery:()=>O,mockValue:()=>F,mustaching:()=>Y,normalizeEdges:()=>m,productFragment:()=>v,randomId:()=>g,setInNamespace:()=>S,setMetaCMSConfigMutation:()=>w,syncConfig:()=>le,templating:()=>ue,traverse:()=>T,videoFragment:()=>L});module.exports=ie(fe);var z=t=>{let e=[];return t.forEach(r=>{!r||(r.type==="product_reference"&&r.product.content&&r.product.content.map(n=>{e.push(n.id)}),r.type==="list.product_reference"&&r.values.edges.map(({node:n})=>{n.content&&n.content.map(i=>{e.push(i.id)})}),r.type==="collection_reference"&&r.collection.content&&r.collection.content.map(n=>{e.push(n.id)}),r.type==="list.collection_reference"&&r.values.edges.map(({node:n})=>{n.content&&n.content.map(i=>{e.push(i.id)})}))}),e};var w=` | ||
mutation ($ownerId: ID!, $value: String!, $key: String!) { | ||
@@ -18,3 +18,3 @@ metafieldsSet(metafields: [{ key: $key, namespace: "meta-cms", ownerId: $ownerId, value: $value, type: "json" }]) { | ||
} | ||
`;var w=` | ||
`;var N=` | ||
query getOwnerId { | ||
@@ -47,3 +47,3 @@ shop { | ||
} | ||
`,M=(t,e)=>` | ||
`,v=(t,e)=>` | ||
id | ||
@@ -124,3 +124,3 @@ descriptionHtml | ||
node { | ||
${M(t,e)} | ||
${v(t,e)} | ||
} | ||
@@ -136,3 +136,3 @@ } | ||
} | ||
`:"",b=(t,e)=>` | ||
`:"",M=(t,e)=>` | ||
type | ||
@@ -157,3 +157,3 @@ value | ||
... on Product { | ||
${M(t,e)} | ||
${v(t,e)} | ||
} | ||
@@ -183,3 +183,3 @@ ...on Video { | ||
... on Product { | ||
${M(t,e)} | ||
${v(t,e)} | ||
} | ||
@@ -195,3 +195,3 @@ } | ||
content: metafields(identifiers: $identifiers) { | ||
${b(t)} | ||
${M(t)} | ||
} | ||
@@ -204,3 +204,3 @@ } | ||
... on Metafield { | ||
${b(t,e)} | ||
${M(t,e)} | ||
} | ||
@@ -221,3 +221,3 @@ } | ||
content: metafields(identifiers: $productIdentifiers) { | ||
${b(!1,r)} | ||
${M(!1,r)} | ||
} | ||
@@ -233,3 +233,3 @@ } | ||
content: metafields(identifiers: $collectionIdentifiers) { | ||
${b(!1,r)} | ||
${M(!1,r)} | ||
} | ||
@@ -240,3 +240,3 @@ } | ||
}`;var m=t=>t.edges.reduce((e,{node:r})=>(e.push(r),e),[]),ae=t=>t,g=()=>{let t="meta-cms-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict",e="",r=10;for(;r--;)e+=t[Math.random()*t.length|0];return`meta-cms-${e}`},C=t=>typeof t=="string"?t.startsWith("gid://shopify/Metafield/"):!1,T=({content:t,namespace:e,key:r,id:n,value:i})=>{let s=n?`${r}@${n}`:r;t[e]||Object.assign(t,{[e]:{}}),Object.assign(t[e],{[s]:i})},P=({handles:t,ids:e})=>{let r=n=>n.replace("gid://shopify/Product/","");return(t==null?void 0:t.length)||(e==null?void 0:e.length)?`${(e||[]).map((n,i,s)=>`id:${r(n)}${s.length-1>i?" or ":t!=null&&t.length?" or":""}`).join("")} ${(t||[]).map((n,i,s)=>`handle:${n}${s.length-1>i?" or ":""}`).join("")}`.trim():void 0},S=(t,e,r=i=>i,n=[])=>typeof t!="object"?t:(Array.isArray(t)?t:Object.entries(t)).reduce((s,a,c)=>{if(Array.isArray(t))s.push(S(a,e,r,n.concat(c)));else{let[l,u]=a;Array.isArray(u)?(s[l]=[],u.forEach((o,d)=>s[l].push(S(o,e,r,n.concat(l).concat(d))))):u&&(u.attributes?s[l]=e(u,n.concat(l)):s[l]=r(S(u,e,r,n.concat(l)),n.concat(l)))}return s},Array.isArray(t)?[]:{});var q=({acc:t,item:e,client:r,locale:n,jsonMetafields:i,nestedContent:s})=>{var u;let a={itemID:e.id,itemType:e.type,itemProp:`${e.namespace}:${e.key}`},c,l=({id:o,key:d,namespace:f,objectId:p})=>{s&&t&&n&&T({content:t,key:d,id:p,namespace:f.replace(`@${n}`,""),value:s[o]})};switch(e.type){case"collection_reference":{c={attributes:a,value:{...e.collection,products:m(e.collection.products).map(({content:o,...d})=>(o==null||o.forEach(f=>l({...f,objectId:d.id})),{...d,variants:m(d.variants)}))}};break}case"file_reference":{if("image"in e&&"image"in(e==null?void 0:e.image)){(!e.image.image.alt||!e.image.image.alt.length)&&(e.image.image.alt="image"),c={attributes:a,value:{...e.image.image,id:e.image.id}};break}if("video"in e){c={attributes:a,value:e.video};break}break}case"product_reference":{let{product:o}=e;(u=o.content)==null||u.forEach(d=>l({...d,objectId:o.id})),c={attributes:a,value:{...o,variants:m(e.product.variants)}};break}case"list.collection_reference":{c={attributes:a,value:m(e.values).map(o=>{var d;return(d=o.content)==null||d.forEach(f=>l({...f,objectId:o.id})),{...o,products:m(o.products).map(f=>{var p;return(p=f.content)==null||p.forEach(h=>l({...h,objectId:f.id})),{...f,variants:m(f.variants)}})}})};break}case"list.file_reference":{c={attributes:a,value:m(e.values).map(o=>{if("image"in o)return{...o.image,id:o.id};if("video"in o)return o})};break}case"list.product_reference":{c={attributes:a,value:m(e.values).map(o=>{var d;return(d=o.content)==null||d.forEach(f=>l({...f,objectId:o.id})),{...o,variants:m(o.variants)}})};break}case"json":{let o=JSON.parse(e.value);if(o.country&&o.address&&o.locality&&o.postal_code)c={attributes:a,value:o};else{let d=B(e.value,i);c={attributes:a,value:d.value||d}}break}case"list.multi_line_text_field":case"list.single_line_text_field":{c={attributes:a,value:JSON.parse(e.value)};break}case"multi_line_text_field":case"single_line_text_field":{c={attributes:a,value:e.value||""};break}case"boolean":{c={attributes:a,value:e.value==="true"};break}case"number_decimal":{c={attributes:a,value:Number(e.value)};break}case"list.number_decimal":{c={attributes:a,value:JSON.parse(e.value,(o,d)=>Number(d))};break}case"url":{c={attributes:a,value:e.value};break}case"date_time":{c={attributes:a,value:(()=>{try{return new Date(e.value)}catch{return new Date}})()};break}}return c},V=({client:t,id:e,initalizer:r,content:n,jsonMetafields:i,nestedContent:s})=>{var c,l;let a=r||{};for(let u=0;u<n.length;u++){let o=n[u];if(o){if(!((l=(c=t.config[o.namespace.replace(`@${t.locale}`,"")])==null?void 0:c.schema)==null?void 0:l[o.key]))return a;let f=q({client:t,acc:a,item:o,jsonMetafields:i,nestedContent:s});T({content:a,key:o.key,namespace:o.namespace.replace(`@${t.locale}`,""),value:f})}}return a};var oe=t=>{let e=[];return t&&JSON.parse(t,(r,n)=>(Array.isArray(n)?n.forEach(i=>{C(i)&&e.push(i)}):C(n)&&e.push(n),n)),e},U=t=>{let e=[];return t.forEach(r=>{!r||r.type==="json"&&(e=e.concat(oe(r.value)))}),e},k=(t,e)=>t.reduce((r,n)=>(n&&(r[n.id]=q({item:n,client:e})),r),{}),B=(t,e)=>e?JSON.parse(t,(r,n)=>Array.isArray(n)?n.map(i=>C(i)?e[i]:i):C(n)?e[n]:n):JSON.parse(t);var _=t=>{let r={text:"multi_line_text_field",richtext:"multi_line_text_field",image:"file_reference",product:"product_reference",collection:"collection_reference",object:"json",boolean:"boolean",video:"file_reference",url:"single_line_text_field",list:"single_line_text_field",number:"number_decimal",address:"json",date:"date_time"}[t.type];return t.isList&&r!=="json"?`list.${r}`:r},v=(t=400,e=300)=>({alt:"image",src:`https://picsum.photos/${t}/${e}`,width:t,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:[]}),J=()=>({id:g(),title:"Lorem ipsum",descriptionHtml:"Lorem ipsum",handle:"",image:v(),products:[x(),x(),x()]}),G=(t,e=3)=>{let r=[];for(let n=0;n<4;n++)r.push(t());return r};var Q=(t,e=!0,r)=>Object.entries(t.schema).reduce((n,[i,s])=>{if(s.isList)s.type==="object"?n[i]=Q(s,!1,r):n[i]=[];else if(s.type==="object")n[i]=Q(s,!1,r);else{let a=_(s),c={itemID:g(),itemType:a,itemProp:""};n[i]=F(s,c)}return n},t.type==="object"&&e?{}:t.isList?[]:{}),F=(t,e,r=!0,n)=>{var i;switch(t.type){case"object":return t.isList?{attributes:e,value:[]}:{attributes:e,value:Q(t,!0,(i=e.itemProp)==null?void 0:i.split(":")[0])};case"collection":return{attributes:e,value:t.isList?G(J):J()};case"image":return{value:t.isList?r?G(v):[]:r?{id:g(),...v()}:void 0,attributes:e};case"video":return{value:void 0,attributes:e};case"product":return{attributes:e,value:t.isList?G(x):x()};case"richtext":return{attributes:e,value:t.isList?n?[n]:["Lorem ipsum","Lorem ipsum","Lorem ipsum"]:n||`<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:!1};case"url":return{attributes:e,value:"/"};case"list":return{attributes:e,value:t.options[0].value};case"number":return{attributes:e,value:0};case"date":return{attributes:e,value:new Date};default:case"text":return{attributes:e,value:t.isList?n?[n]:["Lorem ipsum","Lorem ipsum","Lorem ipsum"]:n||"Lorem ipsum dolor sit amet consectetur adipisicing elit."}}};var y=class{constructor(e,r){this.request=async(e,r)=>{let n=await await fetch(this.url,{method:"POST",body:JSON.stringify({query:e,variables:r||{}}),headers:{...this.headers||{},"Content-Type":"application/json"}});if(n.ok){let{data:i}=await n.json();return i}else throw new Error(await n.text())};this.url=e,this.headers=r}},H=class{constructor(e,{storefrontName:r,storefrontAccessToken:n,defaultNameSpaces:i},s){this.getMetafieldsByIds=async e=>{var i,s;if(!e.length)return[{},[]];let r=await this.client.request(O(!1,{customProductMetafields:(s=(i=this.params)==null?void 0:i.product)==null?void 0:s.customMetafieldsIdentifiers}),{ids:e});if(!r)return[{},[]];let{metafields:n}=r;return[k(n,this),n]};this.getMetafieldsForProductsAndCollection=async({collection:e,product:r,locale:n})=>{var p,h;if(!(e!=null&&e.ids)&&!(e!=null&&e.handles)&&!(e!=null&&e.namespaces)&&!(r!=null&&r.ids)&&!(r!=null&&r.handles)&&!(r!=null&&r.namespaces))return[{},[],[]];let i=await this.client.request(R({collectionQuery:P({ids:e==null?void 0:e.ids,handles:e==null?void 0:e.handles}),productQuery:P({ids:r==null?void 0:r.ids,handles:r==null?void 0:r.handles})},{customProductMetafields:(h=(p=this.params)==null?void 0:p.product)==null?void 0:h.customMetafieldsIdentifiers}),{productIdentifiers:this.buildIdentifiers((r==null?void 0:r.namespaces)||[],n),collectionIdentifiers:this.buildIdentifiers((e==null?void 0:e.namespaces)||[],n)});if(!i)return[{},[],[]];let{products:s,collections:a}=i,c=s?m(s):[],l=a?m(a):[],u=c.map(I=>I.content).flat(),o=l.map(I=>I.content).flat(),d=k(u,this),f=k(o,this);return[{...d,...f},u.concat(o),c.concat(l)]};this.translate=async(e,r)=>{let n=Object.keys(this.config)};this.getContentForNamespaces=async e=>{var d,f,p;if(this.locale=e.locale||"en",!e.global)return{content:{},params:e};this.params=e;let{shop:{content:r}}=await this.client.request(D(!0,{customProductMetafields:(d=e.product)==null?void 0:d.customMetafieldsIdentifiers}),{productIdentifiers:this.buildIdentifiers(((f=e.product)==null?void 0:f.namespaces)||[],e.locale),collectionIdentifiers:this.buildIdentifiers(((p=e.collection)==null?void 0:p.namespaces)||[],e.locale),identifiers:this.buildIdentifiers(e.global.concat(this.defaultNameSpaces),e.locale)}),[[n,i],[s,a,c]]=await Promise.all([this.getMetafieldsByIds(z(r)),this.getMetafieldsForProductsAndCollection(e)]),[l,u]=await this.getMetafieldsByIds(U(r.concat(i).concat(a))),o=V({client:this,content:r,jsonMetafields:l,nestedContent:{...n,...s}});return c.forEach(({id:h,content:I})=>{V({client:this,initalizer:o,id:h,content:I,jsonMetafields:l,nestedContent:{...n,...s}})}),{params:{...e,global:e.global.concat(this.defaultNameSpaces)},content:o}};this.t=(e,r,{productId:n,collectionId:i})=>{let[s,a]=e.split(".");if(!(s in this.config))throw new Error(`Namespace "${s}" is not defined in the schema`);let{kind:c,schema:l}=this.config[s];if(n&&c==="product"&&(a=`${a}@${n}`),i&&c==="collection"&&(a=`${a}@${i}`),!(a in l))throw new Error(`Path "${a}" is not defined in the "${s}" schema`);let u=l[a],o=()=>{let d=_(u),f={itemID:g(),itemType:d,itemProp:`${s}:${a}`},p=this.config[s].schema[a].default;return F(u,f,!0,p)};return s in r||Object.assign(r,{[s]:{}}),[r[s][a]||o(),u,this.locale]};this.buildIdentifiers=(e,r)=>e?e.reduce((n,i)=>{let s=this.config[i];return s&&Object.keys(s.schema).forEach(a=>{n.push({namespace:`${i}@${r}`,key:a})}),n},[]):[];this.locale="en",this.adapter=s||(a=>a),this.config=e,this.defaultNameSpaces=i||[],this.client=new y(`https://${r}.myshopify.com/api/2022-10/graphql.json`,{"X-SHOPIFY-STOREFRONT-ACCESS-TOKEN":n})}};var ce=async(t,{storefrontName:e,storefrontAccessToken:r,adminAccessToken:n},i)=>{let s=new y(`https://${e}.myshopify.com/api/2022-10/graphql.json`,{"X-SHOPIFY-STOREFRONT-ACCESS-TOKEN":r}),a=new y(`https://${e}.myshopify.com/admin/api/2022-10/graphql.json`,{"X-Shopify-Access-Token":n}),{shop:{id:c}}=await s.request(w);return await a.request(N,{value:JSON.stringify(t),key:i,ownerId:c})};var K=/{{([\:\/\-\\\,\.a-zA-Z0-9\s])*}}/g,Y=(t,e)=>(e&&Object.keys(e).forEach(r=>{t=t.replace(new RegExp(`{{${r}}}`,"g"),String(e[r]))}),t.replace(K,"")),X=/\(\(([\:\/\-\\\,\s\u0030-\u4351\u4e00-\u9fa5])*\)\)/g,le=(t,e)=>{if(!e)return t;let r=t.matchAll(X);return Array.from(r).forEach(n=>{let[i]=n,[s,a,c]=i.replace("((","").replace("))","").split(","),l=typeof e[s]=="number"?Number(e[s])>1:!!e[s];c?l===!0?t=t.replace(i,c.trim()):t=t.replace(i,a.trim()):l===!0?(c=a,t=t.replace(i,c.trim())):t=t.replace(X,"")}),t},de=(t,e)=>le(Y(t,e),e);0&&(module.exports={GraphqlClient,MetaCms,buildQuery,collectionFragment,config,contentQuery,fieldToMetafieldType,getLoremCollectionData,getLoremImageData,getLoremProductData,getMetaCMSConfigQuery,getOwnerIdQuery,imageFragment,isId,mediaImageFragment,metafieldFragment,metafieldsForNodes,metafieldsQuery,mockValue,mustaching,normalizeEdges,productFragment,randomId,setInNamespace,setMetaCMSConfigMutation,syncConfig,templating,traverse,videoFragment}); | ||
}`;var m=t=>t.edges.reduce((e,{node:r})=>(e.push(r),e),[]),ae=t=>t,g=()=>{let t="meta-cms-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict",e="",r=10;for(;r--;)e+=t[Math.random()*t.length|0];return`meta-cms-${e}`},C=t=>typeof t=="string"?t.startsWith("gid://shopify/Metafield/"):!1,S=({content:t,namespace:e,key:r,id:n,value:i})=>{let s=n?`${r}@${n}`:r;t[e]||Object.assign(t,{[e]:{}}),Object.assign(t[e],{[s]:i})},P=({handles:t,ids:e})=>{let r=n=>n.replace("gid://shopify/Product/","");return(t==null?void 0:t.length)||(e==null?void 0:e.length)?`${(e||[]).map((n,i,s)=>`id:${r(n)}${s.length-1>i?" or ":t!=null&&t.length?" or":""}`).join("")} ${(t||[]).map((n,i,s)=>`handle:${n}${s.length-1>i?" or ":""}`).join("")}`.trim():void 0},T=(t,e,r=i=>i,n=[])=>typeof t!="object"?(console.log({data:t}),t):(Array.isArray(t)?t:Object.entries(t)).reduce((s,a,c)=>{if(Array.isArray(t))s.push(T(a,e,r,n.concat(c)));else{let[l,u]=a;Array.isArray(u)?(s[l]=[],u.forEach((o,d)=>s[l].push(T(o,e,r,n.concat(l).concat(d))))):u&&(u.attributes?s[l]=e(u,n.concat(l)):s[l]=r(T(u,e,r,n.concat(l)),n.concat(l)))}return s},Array.isArray(t)?[]:{});var q=({acc:t,item:e,client:r,locale:n,jsonMetafields:i,nestedContent:s})=>{var u;let a={itemID:e.id,itemType:e.type,itemProp:`${e.namespace}:${e.key}`},c,l=({id:o,key:d,namespace:f,objectId:p})=>{s&&t&&n&&S({content:t,key:d,id:p,namespace:f.replace(`@${n}`,""),value:s[o]})};switch(e.type){case"collection_reference":{c={attributes:a,value:{...e.collection,products:m(e.collection.products).map(({content:o,...d})=>(o==null||o.forEach(f=>l({...f,objectId:d.id})),{...d,variants:m(d.variants)}))}};break}case"file_reference":{if("image"in e&&"image"in(e==null?void 0:e.image)){(!e.image.image.alt||!e.image.image.alt.length)&&(e.image.image.alt="image"),c={attributes:a,value:{...e.image.image,id:e.image.id}};break}if("video"in e){c={attributes:a,value:e.video};break}break}case"product_reference":{let{product:o}=e;(u=o.content)==null||u.forEach(d=>l({...d,objectId:o.id})),c={attributes:a,value:{...o,variants:m(e.product.variants)}};break}case"list.collection_reference":{c={attributes:a,value:m(e.values).map(o=>{var d;return(d=o.content)==null||d.forEach(f=>l({...f,objectId:o.id})),{...o,products:m(o.products).map(f=>{var p;return(p=f.content)==null||p.forEach(h=>l({...h,objectId:f.id})),{...f,variants:m(f.variants)}})}})};break}case"list.file_reference":{c={attributes:a,value:m(e.values).map(o=>{if("image"in o)return{...o.image,id:o.id};if("video"in o)return o})};break}case"list.product_reference":{c={attributes:a,value:m(e.values).map(o=>{var d;return(d=o.content)==null||d.forEach(f=>l({...f,objectId:o.id})),{...o,variants:m(o.variants)}})};break}case"json":{let o=JSON.parse(e.value);if(o.country&&o.address&&o.locality&&o.postal_code)c={attributes:a,value:o};else{let d=B(e.value,i);c={attributes:a,value:d.value||d}}break}case"list.multi_line_text_field":case"list.single_line_text_field":{c={attributes:a,value:JSON.parse(e.value)};break}case"multi_line_text_field":case"single_line_text_field":{c={attributes:a,value:e.value||""};break}case"boolean":{c={attributes:a,value:e.value==="true"};break}case"number_decimal":{c={attributes:a,value:Number(e.value)};break}case"list.number_decimal":{c={attributes:a,value:JSON.parse(e.value,(o,d)=>Number(d))};break}case"url":{c={attributes:a,value:e.value};break}case"date_time":{c={attributes:a,value:(()=>{try{return new Date(e.value)}catch{return new Date}})()};break}}return c},V=({client:t,id:e,initalizer:r,content:n,jsonMetafields:i,nestedContent:s})=>{var c,l;let a=r||{};for(let u=0;u<n.length;u++){let o=n[u];if(o){if(!((l=(c=t.config[o.namespace.replace(`@${t.locale}`,"")])==null?void 0:c.schema)==null?void 0:l[o.key]))return a;let f=q({client:t,acc:a,item:o,jsonMetafields:i,nestedContent:s});S({content:a,key:o.key,namespace:o.namespace.replace(`@${t.locale}`,""),value:f})}}return a};var oe=t=>{let e=[];return t&&JSON.parse(t,(r,n)=>(Array.isArray(n)?n.forEach(i=>{C(i)&&e.push(i)}):C(n)&&e.push(n),n)),e},U=t=>{let e=[];return t.forEach(r=>{!r||r.type==="json"&&(e=e.concat(oe(r.value)))}),e},k=(t,e)=>t.reduce((r,n)=>(n&&(r[n.id]=q({item:n,client:e})),r),{}),B=(t,e)=>e?JSON.parse(t,(r,n)=>Array.isArray(n)?n.map(i=>C(i)?e[i]:i):C(n)?e[n]:n):JSON.parse(t);var _=t=>{let r={text:"multi_line_text_field",richtext:"multi_line_text_field",image:"file_reference",product:"product_reference",collection:"collection_reference",object:"json",boolean:"boolean",video:"file_reference",url:"single_line_text_field",list:"single_line_text_field",number:"number_decimal",address:"json",date:"date_time"}[t.type];return t.isList&&r!=="json"?`list.${r}`:r},b=(t=400,e=300)=>({alt:"image",src:`https://picsum.photos/${t}/${e}`,width:t,height:e}),x=()=>({id:g(),descriptionHtml:"Lorem ipsum",title:"Lorem ipsum",handle:"",featuredImage:b(),compareAtPriceRange:{maxVariantPrice:{amount:"0",currencyCode:"EUR"},minVariantPrice:{amount:"0",currencyCode:"EUR"}},priceRange:{maxVariantPrice:{amount:"0",currencyCode:"EUR"},minVariantPrice:{amount:"0",currencyCode:"EUR"}},variants:[]}),J=()=>({id:g(),title:"Lorem ipsum",descriptionHtml:"Lorem ipsum",handle:"",image:b(),products:[x(),x(),x()]}),G=(t,e=3)=>{let r=[];for(let n=0;n<4;n++)r.push(t());return r};var Q=(t,e=!0,r)=>Object.entries(t.schema).reduce((n,[i,s])=>{if(s.isList)s.type==="object"?n[i]=Q(s,!1,r):n[i]=[];else if(s.type==="object")n[i]=Q(s,!1,r);else{let a=_(s),c={itemID:g(),itemType:a,itemProp:""};n[i]=F(s,c)}return n},t.type==="object"&&e?{}:t.isList?[]:{}),F=(t,e,r=!0,n)=>{var i;switch(t.type){case"object":return t.isList?{attributes:e,value:[]}:{attributes:e,value:Q(t,!0,(i=e.itemProp)==null?void 0:i.split(":")[0])};case"collection":return{attributes:e,value:t.isList?G(J):J()};case"image":return{value:t.isList?r?G(b):[]:r?{id:g(),...b()}:void 0,attributes:e};case"video":return{value:void 0,attributes:e};case"product":return{attributes:e,value:t.isList?G(x):x()};case"richtext":return{attributes:e,value:t.isList?n?[n]:["Lorem ipsum","Lorem ipsum","Lorem ipsum"]:n||`<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:!1};case"url":return{attributes:e,value:"/"};case"list":return{attributes:e,value:t.options[0].value};case"number":return{attributes:e,value:0};case"date":return{attributes:e,value:new Date};default:case"text":return{attributes:e,value:t.isList?n?[n]:["Lorem ipsum","Lorem ipsum","Lorem ipsum"]:n||"Lorem ipsum dolor sit amet consectetur adipisicing elit."}}};var ce=()=>new Promise(t=>{setTimeout(()=>{t(!0)},3)}),y=class{constructor(e,r){this.request=async(e,r)=>{let n=await await fetch(this.url,{method:"POST",body:JSON.stringify({query:e,variables:r||{}}),headers:{...this.headers||{},"Content-Type":"application/json"}});if(n.status===429)return await ce(),this.request(e,r);if(n.ok){let{data:i}=await n.json();return i}else throw new Error(await n.text())};this.url=e,this.headers=r}},H=class{constructor(e,{storefrontName:r,storefrontAccessToken:n,defaultNameSpaces:i},s){this.getMetafieldsByIds=async e=>{var i,s;if(!e.length)return[{},[]];let r=await this.client.request(O(!1,{customProductMetafields:(s=(i=this.params)==null?void 0:i.product)==null?void 0:s.customMetafieldsIdentifiers}),{ids:e});if(!r)return[{},[]];let{metafields:n}=r;return[k(n,this),n]};this.getMetafieldsForProductsAndCollection=async({collection:e,product:r,locale:n})=>{var p,h;if(!(e!=null&&e.ids)&&!(e!=null&&e.handles)&&!(e!=null&&e.namespaces)&&!(r!=null&&r.ids)&&!(r!=null&&r.handles)&&!(r!=null&&r.namespaces))return[{},[],[]];let i=await this.client.request(R({collectionQuery:P({ids:e==null?void 0:e.ids,handles:e==null?void 0:e.handles}),productQuery:P({ids:r==null?void 0:r.ids,handles:r==null?void 0:r.handles})},{customProductMetafields:(h=(p=this.params)==null?void 0:p.product)==null?void 0:h.customMetafieldsIdentifiers}),{productIdentifiers:this.buildIdentifiers((r==null?void 0:r.namespaces)||[],n),collectionIdentifiers:this.buildIdentifiers((e==null?void 0:e.namespaces)||[],n)});if(!i)return[{},[],[]];let{products:s,collections:a}=i,c=s?m(s):[],l=a?m(a):[],u=c.map(I=>I.content).flat(),o=l.map(I=>I.content).flat(),d=k(u,this),f=k(o,this);return[{...d,...f},u.concat(o),c.concat(l)]};this.translate=async(e,r)=>{let n=Object.keys(this.config);console.log({ns:n})};this.getContentForNamespaces=async e=>{var d,f,p;if(this.locale=e.locale||"en",!e.global)return{content:{},params:e};this.params=e;let{shop:{content:r}}=await this.client.request(D(!0,{customProductMetafields:(d=e.product)==null?void 0:d.customMetafieldsIdentifiers}),{productIdentifiers:this.buildIdentifiers(((f=e.product)==null?void 0:f.namespaces)||[],e.locale),collectionIdentifiers:this.buildIdentifiers(((p=e.collection)==null?void 0:p.namespaces)||[],e.locale),identifiers:this.buildIdentifiers(e.global.concat(this.defaultNameSpaces),e.locale)}),[[n,i],[s,a,c]]=await Promise.all([this.getMetafieldsByIds(z(r)),this.getMetafieldsForProductsAndCollection(e)]),[l,u]=await this.getMetafieldsByIds(U(r.concat(i).concat(a))),o=V({client:this,content:r,jsonMetafields:l,nestedContent:{...n,...s}});return c.forEach(({id:h,content:I})=>{V({client:this,initalizer:o,id:h,content:I,jsonMetafields:l,nestedContent:{...n,...s}})}),{params:{...e,global:e.global.concat(this.defaultNameSpaces)},content:o}};this.t=(e,r,{productId:n,collectionId:i})=>{let[s,a]=e.split(".");if(!(s in this.config))throw new Error(`Namespace "${s}" is not defined in the schema`);let{kind:c,schema:l}=this.config[s];if(n&&c==="product"&&(a=`${a}@${n}`),i&&c==="collection"&&(a=`${a}@${i}`),!(a in l))throw new Error(`Path "${a}" is not defined in the "${s}" schema`);let u=l[a],o=()=>{let d=_(u),f={itemID:g(),itemType:d,itemProp:`${s}:${a}`},p=this.config[s].schema[a].default;return F(u,f,!0,p)};return s in r||Object.assign(r,{[s]:{}}),[r[s][a]||o(),u,this.locale]};this.buildIdentifiers=(e,r)=>e?e.reduce((n,i)=>{let s=this.config[i];return s&&Object.keys(s.schema).forEach(a=>{n.push({namespace:`${i}@${r}`,key:a})}),n},[]):[];this.locale="en",this.adapter=s||(a=>a),this.config=e,this.defaultNameSpaces=i||[],this.client=new y(`https://${r}.myshopify.com/api/2022-10/graphql.json`,{"X-SHOPIFY-STOREFRONT-ACCESS-TOKEN":n})}};var le=async(t,{storefrontName:e,storefrontAccessToken:r,adminAccessToken:n},i)=>{console.log(i);let s=new y(`https://${e}.myshopify.com/api/2022-10/graphql.json`,{"X-SHOPIFY-STOREFRONT-ACCESS-TOKEN":r}),a=new y(`https://${e}.myshopify.com/admin/api/2022-10/graphql.json`,{"X-Shopify-Access-Token":n}),{shop:{id:c}}=await s.request(N);return await a.request(w,{value:JSON.stringify(t),key:i,ownerId:c})};var K=/{{([\:\/\-\\\,\.a-zA-Z0-9\s])*}}/g,Y=(t,e)=>(e&&Object.keys(e).forEach(r=>{t=t.replace(new RegExp(`{{${r}}}`,"g"),String(e[r]))}),t.replace(K,"")),X=/\(\(([\:\/\-\\\,\s\u0030-\u4351\u4e00-\u9fa5])*\)\)/g,de=(t,e)=>{if(!e)return t;let r=t.matchAll(X);return Array.from(r).forEach(n=>{let[i]=n,[s,a,c]=i.replace("((","").replace("))","").split(","),l=typeof e[s]=="number"?Number(e[s])>1:!!e[s];c?l===!0?t=t.replace(i,c.trim()):t=t.replace(i,a.trim()):l===!0?(c=a,t=t.replace(i,c.trim())):t=t.replace(X,"")}),t},ue=(t,e)=>de(Y(t,e),e);0&&(module.exports={GraphqlClient,MetaCms,buildQuery,collectionFragment,config,contentQuery,fieldToMetafieldType,getLoremCollectionData,getLoremImageData,getLoremProductData,getMetaCMSConfigQuery,getOwnerIdQuery,imageFragment,isId,mediaImageFragment,metafieldFragment,metafieldsForNodes,metafieldsQuery,mockValue,mustaching,normalizeEdges,productFragment,randomId,setInNamespace,setMetaCMSConfigMutation,syncConfig,templating,traverse,videoFragment}); |
{ | ||
"name": "@meta-cms/core", | ||
"version": "1.0.93", | ||
"version": "1.0.94", | ||
"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
38437