@meta-cms/core
Advanced tools
Comparing version 1.0.22 to 1.0.23
@@ -104,4 +104,4 @@ import { Get } from 'type-fest'; | ||
default?: ProductContent; | ||
isList?: false; | ||
config: Model; | ||
isList?: boolean; | ||
schema: Model; | ||
}; | ||
@@ -167,6 +167,13 @@ declare type Schema = Record<string, Model>; | ||
declare type ConfigType = "json" | "file" | "product" | "collection" | "object"; | ||
declare type GetConfigType<S extends Schema, P extends Paths<S>> = Split<P>[2] extends undefined ? Split<P>[1] extends keyof S[Split<P>[0]] ? S[Split<P>[0]][Split<P>[1]]["type"] : never : Split<P>[1] extends keyof S[Split<P>[0]] ? S[Split<P>[0]][Split<P>[1]] extends ObjectField ? Split<P>[2] extends keyof S[Split<P>[0]][Split<P>[1]]["config"] ? S[Split<P>[0]][Split<P>[1]]["config"][Split<P>[2]]["type"] : never : never : never; | ||
declare type isList<S extends Schema, P extends Paths<S>> = Split<P>[2] extends undefined ? S[Split<P>[0]][Split<P>[1]]["isList"] : Get<Get<Get<S[Split<P>[0]][Split<P>[1]], "config">, [Split<P>[2]]>, "isList">; | ||
declare type toList<S extends Schema, P extends Paths<S>, T, TS> = isList<S, P> extends true ? TS : T; | ||
declare type GetType<S extends Schema, P extends Paths<S>> = GetConfigType<S, P> extends "string" ? toList<S, P, TextContent, TextsContent> : GetConfigType<S, P> extends "collection" ? toList<S, P, CollectionContent, CollectionsContent> : GetConfigType<S, P> extends "product" ? toList<S, P, ProductContent, ProductsContent> : GetConfigType<S, P> extends "richtext" ? toList<S, P, HTMLContent, HTMLsContent> : toList<S, P, ImageContent, ImagesContent>; | ||
declare type GetConfigType<S extends Schema, P extends Paths<S>> = Split<P>[2] extends undefined ? Split<P>[1] extends keyof S[Split<P>[0]] ? S[Split<P>[0]][Split<P>[1]]["type"] : never : Split<P>[1] extends keyof S[Split<P>[0]] ? S[Split<P>[0]][Split<P>[1]] extends ObjectField ? Split<P>[2] extends keyof S[Split<P>[0]][Split<P>[1]]["schema"] ? S[Split<P>[0]][Split<P>[1]]["schema"][Split<P>[2]]["type"] : never : never : never; | ||
declare type isList<S extends Schema, P extends Paths<S>> = Split<P>[2] extends undefined ? S[Split<P>[0]][Split<P>[1]]["isList"] : Get<Get<Get<S[Split<P>[0]][Split<P>[1]], "schema">, [Split<P>[2]]>, "isList">; | ||
declare type toList<S extends Schema, P extends Paths<S>, T, TS = undefined> = isList<S, P> extends true ? TS extends undefined ? T[] : TS : T; | ||
declare type ObjectSchema<S extends Schema, P extends Paths<S>> = Split<P>[0] extends keyof S ? Split<P>[1] extends keyof S[Split<P>[0]] ? S[Split<P>[0]][Split<P>[1]] : never : never; | ||
declare type getNestedType<T> = T extends "string" ? string : T extends "image" ? ImageData : T extends "product" ? ProductData : CollectionData; | ||
declare type ObjectContent<O extends Model> = { | ||
[K in keyof O]: "schema" extends keyof O[K] ? O[K]["schema"] extends Model ? O[K]["isList"] extends true ? ObjectContent<O[K]["schema"]>[] : ObjectContent<O[K]["schema"]> : never : getNestedType<O[K]["type"]>; | ||
}; | ||
declare type GetType<S extends Schema, P extends Paths<S>> = GetConfigType<S, P> extends "string" ? toList<S, P, TextContent, TextsContent> : GetConfigType<S, P> extends "collection" ? toList<S, P, CollectionContent, CollectionsContent> : GetConfigType<S, P> extends "product" ? toList<S, P, ProductContent, ProductsContent> : GetConfigType<S, P> extends "richtext" ? toList<S, P, HTMLContent, HTMLsContent> : GetConfigType<S, P> extends "object" ? ObjectSchema<S, P> extends ObjectField ? ObjectSchema<S, P>["schema"] extends Model ? Record<Split<P>[1], toList<S, P, ObjectContent<ObjectSchema<S, P>["schema"]>>> & { | ||
attributes: ItemAttributes; | ||
} : never : never : toList<S, P, ImageContent, ImagesContent>; | ||
declare type PathForNamespace<S extends Schema, N extends Namespaces<S>, V extends String> = V extends `${infer T}.${infer U}` ? T extends N ? U : V extends `${infer T}.${infer U}.${infer V}` ? `${U}.${V}` : never : never; | ||
@@ -215,2 +222,4 @@ declare type Identifier = { | ||
} | { | ||
type: "json"; | ||
} | { | ||
type: "list.file_reference"; | ||
@@ -293,3 +302,4 @@ values: { | ||
"single_line_text_field", | ||
"multi_line_text_field" | ||
"multi_line_text_field", | ||
"json" | ||
][number]; | ||
@@ -296,0 +306,0 @@ declare type metafieldType = simpleMetafieldType | `list.${simpleMetafieldType}`; |
@@ -1,2 +0,2 @@ | ||
"use strict";var p=Object.defineProperty;var I=Object.getOwnPropertyDescriptor;var x=Object.getOwnPropertyNames;var M=Object.prototype.hasOwnProperty;var F=(s,e)=>{for(var r in e)p(s,r,{get:e[r],enumerable:!0})},P=(s,e,r,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of x(e))!M.call(s,n)&&n!==r&&p(s,n,{get:()=>e[n],enumerable:!(i=I(e,n))||i.enumerable});return s};var T=s=>P(p({},"__esModule",{value:!0}),s);var w={};F(w,{GraphqlClient:()=>f,MetaCms:()=>y,config:()=>v,contentQuery:()=>g,getMetaCMSConfigQuery:()=>_,getOwnerIdQuery:()=>u,setMetaCMSConfigMutation:()=>m,syncConfig:()=>k});module.exports=T(w);var m=` | ||
"use strict";var p=Object.defineProperty;var I=Object.getOwnPropertyDescriptor;var x=Object.getOwnPropertyNames;var M=Object.prototype.hasOwnProperty;var F=(s,e)=>{for(var r in e)p(s,r,{get:e[r],enumerable:!0})},P=(s,e,r,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of x(e))!M.call(s,n)&&n!==r&&p(s,n,{get:()=>e[n],enumerable:!(i=I(e,n))||i.enumerable});return s};var T=s=>P(p({},"__esModule",{value:!0}),s);var O={};F(O,{GraphqlClient:()=>f,MetaCms:()=>y,config:()=>v,contentQuery:()=>g,getMetaCMSConfigQuery:()=>_,getOwnerIdQuery:()=>u,setMetaCMSConfigMutation:()=>m,syncConfig:()=>k});module.exports=T(O);var m=` | ||
mutation ($ownerId: ID!, $value: String!) { | ||
@@ -160,2 +160,2 @@ metafieldsSet( | ||
} | ||
`;function S(s,e,r){if(!s)return r;var i,n;if(Array.isArray(e)&&(i=e.slice(0)),typeof e=="string"&&(i=e.split(".")),typeof e=="symbol"&&(i=[e]),!Array.isArray(i))throw new Error("props arg must be an array, a string or a symbol");for(;i.length;)if(n=i.shift(),!s||(s=s[n],s===void 0))return r;return s}var f=class{constructor(e,r){this.request=async(e,r)=>(await(await fetch(this.url,{method:"POST",body:JSON.stringify({query:e,variables:r||{}}),headers:{...this.headers||{},"Content-Type":"application/json"}})).json()).data;this.url=e,this.headers=r}},y=class{constructor(e,{storefrontName:r,storefrontAccessToken:i,defaultNameSpaces:n}){this.getContentForNamespaces=async(e,r="fra")=>{let{shop:{content:i}}=await this.client.request(g,{identifiers:this.buildIdentifiers(e.concat(this.defaultNameSpaces),r)});return i.reduce((n,t)=>{if(!t)return n;let o=t.namespace.replace(`_${r}`,"");n[o]||Object.assign(n,{[o]:{}});let c={itemID:t.id,itemType:t.type,itemProp:`${t.namespace}:${t.key}`},a;switch(t.type){case"collection_reference":{a={attributes:c,collection:{...t.collection,products:l(t.collection.products)}};break}case"file_reference":{t.image.image.alt||(t.image.image.alt=""),a={attributes:c,image:t.image.image};break}case"product_reference":{a={attributes:c,product:{...t.product,variants:l(t.product.variants)}};break}case"list.collection_reference":{a={attributes:c,collections:l(t.values).map(d=>({...d,products:l(d.products)}))};break}case"list.file_reference":{a={attributes:c,images:l(t.values).map(d=>d.image)};break}case"list.product_reference":{a={attributes:c,products:l(t.values).map(d=>({...d,variants:l(d.variants)}))};break}case"list.multi_line_text_field":case"list.single_line_text_field":{a={attributes:c,texts:JSON.parse(t.value)};break}case"multi_line_text_field":case"single_line_text_field":{a={attributes:c,text:t.value};break}}return Object.assign(n[o],{[t.key]:a}),n},{})};this.t=(e,r="fr",i)=>{let[n,...t]=e.split("."),o=()=>S(this.config[n],t).default;return i&&i[n][t.join(".")]||o()};this.buildIdentifiers=(e,r="fra")=>e.reduce((n,t)=>{let o=this.config[t];return Object.entries(o).reduce((a,[d,h])=>(h.config?Object.keys(h.config).forEach(C=>{a.push(`${d}.${C}`)}):a.push(d),a),[]).forEach(a=>{n.push({namespace:`${t}_${r}`,key:a})}),n},[]);this.config=e,this.defaultNameSpaces=n||[],this.client=new f(`https://${r}.myshopify.com/api/2022-10/graphql.json`,{"X-SHOPIFY-STOREFRONT-ACCESS-TOKEN":i})}},l=s=>s.edges.reduce((e,{node:r})=>(e.push(r),e),[]),v=s=>s;var k=async(s,{storefrontName:e,storefrontAccessToken:r,adminAccessToken:i})=>{let n=new f(`https://${e}.myshopify.com/api/2022-10/graphql.json`,{"X-SHOPIFY-STOREFRONT-ACCESS-TOKEN":r}),t=new f(`https://${e}.myshopify.com/admin/api/2022-10/graphql.json`,{"X-Shopify-Access-Token":i}),{shop:{id:o}}=await n.request(u);return await t.request(m,{value:JSON.stringify(s),ownerId:o})};0&&(module.exports={GraphqlClient,MetaCms,config,contentQuery,getMetaCMSConfigQuery,getOwnerIdQuery,setMetaCMSConfigMutation,syncConfig}); | ||
`;function S(s,e,r){if(!s)return r;var i,n;if(Array.isArray(e)&&(i=e.slice(0)),typeof e=="string"&&(i=e.split(".")),typeof e=="symbol"&&(i=[e]),!Array.isArray(i))throw new Error("props arg must be an array, a string or a symbol");for(;i.length;)if(n=i.shift(),!s||(s=s[n],s===void 0))return r;return s}var f=class{constructor(e,r){this.request=async(e,r)=>(await(await fetch(this.url,{method:"POST",body:JSON.stringify({query:e,variables:r||{}}),headers:{...this.headers||{},"Content-Type":"application/json"}})).json()).data;this.url=e,this.headers=r}},y=class{constructor(e,{storefrontName:r,storefrontAccessToken:i,defaultNameSpaces:n}){this.getContentForNamespaces=async(e,r="fra")=>{let{shop:{content:i}}=await this.client.request(g,{identifiers:this.buildIdentifiers(e.concat(this.defaultNameSpaces),r)});return console.log({content:i}),i.reduce((n,t)=>{if(!t)return n;let c=t.namespace.replace(`_${r}`,"");n[c]||Object.assign(n,{[c]:{}});let o={itemID:t.id,itemType:t.type,itemProp:`${t.namespace}:${t.key}`},a;switch(t.type){case"collection_reference":{a={attributes:o,collection:{...t.collection,products:l(t.collection.products)}};break}case"file_reference":{t.image.image.alt||(t.image.image.alt=""),a={attributes:o,image:t.image.image};break}case"product_reference":{a={attributes:o,product:{...t.product,variants:l(t.product.variants)}};break}case"list.collection_reference":{a={attributes:o,collections:l(t.values).map(d=>({...d,products:l(d.products)}))};break}case"list.file_reference":{a={attributes:o,images:l(t.values).map(d=>d.image)};break}case"list.product_reference":{a={attributes:o,products:l(t.values).map(d=>({...d,variants:l(d.variants)}))};break}case"json":{a={attributes:o,[t.key]:JSON.parse(t.value)};break}case"list.multi_line_text_field":case"list.single_line_text_field":{a={attributes:o,texts:JSON.parse(t.value)};break}case"multi_line_text_field":case"single_line_text_field":{a={attributes:o,text:t.value};break}}return Object.assign(n[c],{[t.key]:a}),n},{})};this.t=(e,r="fr",i)=>{let[n,...t]=e.split("."),c=()=>S(this.config[n],t).default;return i&&i[n][t.join(".")]||c()};this.buildIdentifiers=(e,r="fra")=>e.reduce((n,t)=>{let c=this.config[t];return Object.entries(c).reduce((a,[d,h])=>(h.config?Object.keys(h.config).forEach(C=>{a.push(`${d}.${C}`)}):a.push(d),a),[]).forEach(a=>{n.push({namespace:`${t}_${r}`,key:a})}),n},[]);this.config=e,this.defaultNameSpaces=n||[],this.client=new f(`https://${r}.myshopify.com/api/2022-10/graphql.json`,{"X-SHOPIFY-STOREFRONT-ACCESS-TOKEN":i})}},l=s=>s.edges.reduce((e,{node:r})=>(e.push(r),e),[]),v=s=>s;var k=async(s,{storefrontName:e,storefrontAccessToken:r,adminAccessToken:i})=>{let n=new f(`https://${e}.myshopify.com/api/2022-10/graphql.json`,{"X-SHOPIFY-STOREFRONT-ACCESS-TOKEN":r}),t=new f(`https://${e}.myshopify.com/admin/api/2022-10/graphql.json`,{"X-Shopify-Access-Token":i}),{shop:{id:c}}=await n.request(u);return await t.request(m,{value:JSON.stringify(s),ownerId:c})};0&&(module.exports={GraphqlClient,MetaCms,config,contentQuery,getMetaCMSConfigQuery,getOwnerIdQuery,setMetaCMSConfigMutation,syncConfig}); |
{ | ||
"name": "@meta-cms/core", | ||
"version": "1.0.22", | ||
"version": "1.0.23", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
22406
511