@meta-cms/core
Advanced tools
Comparing version 1.0.2 to 1.0.3
198
client.ts
@@ -23,6 +23,6 @@ import { | ||
export class MetaCms { | ||
config: Config; | ||
export class MetaCms<C extends Config> { | ||
config: C; | ||
client: GraphQLClient; | ||
constructor(config: Config, { storefrontName, storefrontAcessToken }: MetaCMSConfig) { | ||
constructor(config: C, { storefrontName, storefrontAcessToken }: MetaCMSConfig) { | ||
this.config = config; | ||
@@ -35,16 +35,73 @@ this.client = new GraphQLClient(`https://${storefrontName}.myshopify.com/api/2022-10/graphql.json`, { | ||
} | ||
getContentForNamespaces = (namespaces: Namespaces<typeof this.config>[], locale: "fra" = "fra") => { | ||
return getContentForNamespaces(this.config, this.client, namespaces, locale); | ||
getContentForNamespaces = async (namespaces: Namespaces<C>[], locale: "fra" = "fra"): Promise<Content<C>> => { | ||
const { | ||
shop: { content } | ||
} = await this.client.request<{ shop: { content: MetafieldContent[] } }>(contentQuery, { | ||
identifiers: this.buildIdentifiers(namespaces, locale) | ||
}); | ||
return content.reduce<Content<C>>((acc, item) => { | ||
if (!item) { | ||
return acc; | ||
} | ||
const ns = item.namespace.replace(`_${locale}`, "") as Namespaces<C>; | ||
if (!acc[ns]) { | ||
Object.assign(acc, { [ns]: {} }); | ||
} | ||
// console.log({ item }); | ||
if (!!item.image?.image) { | ||
Object.assign(acc[ns], { [item.key as Paths<C>]: item.image.image as ImageContent }); | ||
} else if (!!item.product?.descriptionHtml) { | ||
Object.assign(acc[ns], { | ||
[item.key as Paths<C>]: { ...item.product, variants: normalizeEdges(item.product.variants) } | ||
}); | ||
} else if (!!item.collection?.products) { | ||
Object.assign(acc[ns], { | ||
[item.key as Paths<C>]: { ...item.collection, products: normalizeEdges(item.collection.products) } | ||
}); | ||
} else if (item.values?.edges.length) { | ||
const nodes = normalizeEdges(item.values); | ||
const [first] = nodes; | ||
if (first.__typename === "Collection") { | ||
Object.assign(acc[ns], { | ||
[item.key as Paths<C>]: (nodes as MetafieldCollection[]).map((collection) => ({ | ||
...collection, | ||
products: normalizeEdges(collection.products) | ||
})) | ||
}); | ||
} else if (first.__typename === "MediaImage") { | ||
Object.assign(acc[ns], { [item.key as Paths<C>]: (nodes as MetafieldImage[]).map((node) => node.image) }); | ||
} else { | ||
Object.assign(acc[ns], { | ||
[item.key as Paths<C>]: (nodes as MetafieldProduct[]).map((node) => ({ | ||
...node, | ||
variants: normalizeEdges(node.variants) | ||
})) | ||
}); | ||
} | ||
} else { | ||
if (item.type === "list.single_line_text_field") { | ||
const obj: TextContent[] = (JSON.parse(item.value) as string[]).map((value, i) => ({ | ||
id: `${item.id}#${i}`, | ||
dangerouslySetInnerHTML: { __html: value } | ||
})); | ||
Object.assign(acc[ns], { [item.key as Paths<C>]: obj }); | ||
} else { | ||
const obj: TextContent = { id: item.id, dangerouslySetInnerHTML: { __html: item.value } }; | ||
Object.assign(acc[ns], { [item.key as Paths<C>]: obj }); | ||
} | ||
} | ||
return acc; | ||
}, {} as Content<C>); | ||
}; | ||
t = <P extends Paths<typeof this.config>>( | ||
p: P, | ||
locale: "fr" = "fr", | ||
params: TParams, | ||
content: Content<typeof this.config> | ||
) => { | ||
t = <P extends Paths<C>>(p: P, locale: "fr" = "fr", params: TParams, content: Content<C>) => { | ||
console.log({ params, locale }); | ||
const [ns, ...path] = p.split("."); | ||
const defaultValue = () => | ||
get(this.config[ns as keyof typeof this.config], path).default as GetType<typeof this.config, P>; | ||
const defaultValue = () => get(this.config[ns as keyof C], path).default as GetType<C, P>; | ||
if (!content) { | ||
@@ -56,75 +113,29 @@ return defaultValue(); | ||
}; | ||
} | ||
const getContentForNamespaces = async <C extends Config>( | ||
config: C, | ||
client: GraphQLClient, | ||
namespaces: Namespaces<C>[], | ||
locale: "fra" = "fra" | ||
): Promise<Content<C>> => { | ||
const { | ||
shop: { content } | ||
} = await client.request<{ shop: { content: MetafieldContent[] } }>(contentQuery, { | ||
identifiers: buildIdentifiers<C>(config, namespaces, locale) | ||
}); | ||
buildIdentifiers = (namespaces: Namespaces<C>[], locale: "fra" = "fra"): Identifier[] => { | ||
const identifiers = namespaces.reduce<Identifier[]>((acc, namespace) => { | ||
const namespaceConfig = this.config[namespace]; | ||
const keys = Object.entries(namespaceConfig).reduce<string[]>((acc, [key, value]: [string, any]) => { | ||
if (value.config) { | ||
Object.keys(value.config).forEach((subKey) => { | ||
acc.push(`${key}.${subKey}`); | ||
}); | ||
} else { | ||
acc.push(key); | ||
} | ||
return content.reduce<Content<C>>((acc, item) => { | ||
if (!item) { | ||
return acc; | ||
} | ||
const ns = item.namespace.replace(`_${locale}`, "") as Namespaces<C>; | ||
if (!acc[ns]) { | ||
Object.assign(acc, { [ns]: {} }); | ||
} | ||
return acc; | ||
}, []); | ||
// console.log({ item }); | ||
if (!!item.image?.image) { | ||
Object.assign(acc[ns], { [item.key as Paths<C>]: item.image.image as ImageContent }); | ||
} else if (!!item.product?.descriptionHtml) { | ||
Object.assign(acc[ns], { | ||
[item.key as Paths<C>]: { ...item.product, variants: normalizeEdges(item.product.variants) } | ||
keys.forEach((key) => { | ||
acc.push({ namespace: `${namespace}_${locale}`, key }); | ||
}); | ||
} else if (!!item.collection?.products) { | ||
Object.assign(acc[ns], { | ||
[item.key as Paths<C>]: { ...item.collection, products: normalizeEdges(item.collection.products) } | ||
}); | ||
} else if (item.values?.edges.length) { | ||
const nodes = normalizeEdges(item.values); | ||
const [first] = nodes; | ||
return acc; | ||
}, []); | ||
if (first.__typename === "Collection") { | ||
Object.assign(acc[ns], { | ||
[item.key as Paths<C>]: (nodes as MetafieldCollection[]).map((collection) => ({ | ||
...collection, | ||
products: normalizeEdges(collection.products) | ||
})) | ||
}); | ||
} else if (first.__typename === "MediaImage") { | ||
Object.assign(acc[ns], { [item.key as Paths<C>]: (nodes as MetafieldImage[]).map((node) => node.image) }); | ||
} else { | ||
Object.assign(acc[ns], { | ||
[item.key as Paths<C>]: (nodes as MetafieldProduct[]).map((node) => ({ | ||
...node, | ||
variants: normalizeEdges(node.variants) | ||
})) | ||
}); | ||
} | ||
} else { | ||
if (item.type === "list.single_line_text_field") { | ||
const obj: TextContent[] = (JSON.parse(item.value) as string[]).map((value, i) => ({ | ||
id: `${item.id}#${i}`, | ||
dangerouslySetInnerHTML: { __html: value } | ||
})); | ||
Object.assign(acc[ns], { [item.key as Paths<C>]: obj }); | ||
} else { | ||
const obj: TextContent = { id: item.id, dangerouslySetInnerHTML: { __html: item.value } }; | ||
Object.assign(acc[ns], { [item.key as Paths<C>]: obj }); | ||
} | ||
} | ||
return identifiers; | ||
}; | ||
} | ||
return acc; | ||
}, {} as Content<C>); | ||
}; | ||
const normalizeEdges = <T extends WithEdge<T>>(item: T): Nodes<T> => { | ||
@@ -136,30 +147,1 @@ return item.edges.reduce<Nodes<T>>((acc, { node }) => { | ||
}; | ||
const buildIdentifiers = <C extends Config>( | ||
config: C, | ||
namespaces: Namespaces<C>[], | ||
locale: "fra" = "fra" | ||
): Identifier[] => { | ||
const identifiers = namespaces.reduce<Identifier[]>((acc, namespace) => { | ||
const namespaceConfig = config[namespace]; | ||
const keys = Object.entries(namespaceConfig).reduce<string[]>((acc, [key, value]: [string, any]) => { | ||
if (value.config) { | ||
Object.keys(value.config).forEach((subKey) => { | ||
acc.push(`${key}.${subKey}`); | ||
}); | ||
} else { | ||
acc.push(key); | ||
} | ||
return acc; | ||
}, []); | ||
keys.forEach((key) => { | ||
acc.push({ namespace: `${namespace}_${locale}`, key }); | ||
}); | ||
return acc; | ||
}, []); | ||
return identifiers; | ||
}; |
{ | ||
"name": "@meta-cms/core", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
33502
972