forked from crowetic/commerce
update get-page and get-all-pages content hooks
This commit is contained in:
parent
abd86329d5
commit
644f196c97
@ -3,11 +3,10 @@ import { swellConfig } from '../../index'
|
|||||||
const fetchSwellApi = async (
|
const fetchSwellApi = async (
|
||||||
query: string,
|
query: string,
|
||||||
method: string,
|
method: string,
|
||||||
variables: object | string
|
variables: [] = []
|
||||||
) => {
|
) => {
|
||||||
const { swell } = swellConfig
|
const { swell } = swellConfig
|
||||||
const res = await swell[query][method](variables)
|
|
||||||
|
|
||||||
return res
|
return await swell[query][method](...variables)
|
||||||
}
|
}
|
||||||
export default fetchSwellApi
|
export default fetchSwellApi
|
||||||
|
@ -25,16 +25,12 @@ const getAllPages = async (options?: {
|
|||||||
}): Promise<ReturnType> => {
|
}): Promise<ReturnType> => {
|
||||||
let { config, variables = { first: 250 } } = options ?? {}
|
let { config, variables = { first: 250 } } = options ?? {}
|
||||||
config = getConfig(config)
|
config = getConfig(config)
|
||||||
const { locale } = config
|
const { locale, fetchSwell } = config
|
||||||
const { data } = await config.fetch(getAllPagesQuery, { variables })
|
const { results } = await fetchSwell('content', 'list', ['pages'])
|
||||||
|
const pages = results.map(({ slug, ...rest }) => ({
|
||||||
const pages = data.pages?.edges?.map(
|
url: `/${locale}/${slug}`,
|
||||||
({ node: { title: name, handle, ...node } }: PageEdge) => ({
|
...rest,
|
||||||
...node,
|
}))
|
||||||
url: `/${locale}/${handle}`,
|
|
||||||
name,
|
|
||||||
})
|
|
||||||
)
|
|
||||||
|
|
||||||
return { pages }
|
return { pages }
|
||||||
}
|
}
|
||||||
|
@ -17,18 +17,15 @@ const getPage = async (options: {
|
|||||||
|
|
||||||
config = getConfig(config)
|
config = getConfig(config)
|
||||||
const { locale } = config
|
const { locale } = config
|
||||||
|
const { id } = variables
|
||||||
const { data } = await config.fetch(getPageQuery, {
|
const result = await config.fetchSwell('content', 'get', ['pages', id])
|
||||||
variables,
|
const page = result
|
||||||
})
|
|
||||||
const page = data.node
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
page: page
|
page: page
|
||||||
? {
|
? {
|
||||||
...page,
|
...page,
|
||||||
name: page.title,
|
url: `/${locale}/${page.slug}`,
|
||||||
url: `/${locale}/${page.handle}`,
|
|
||||||
}
|
}
|
||||||
: null,
|
: null,
|
||||||
}
|
}
|
||||||
|
@ -21,9 +21,11 @@ const getAllProducts = async (options: {
|
|||||||
}): Promise<ReturnType> => {
|
}): Promise<ReturnType> => {
|
||||||
let { config, variables = { first: 250 } } = options ?? {}
|
let { config, variables = { first: 250 } } = options ?? {}
|
||||||
config = getConfig(config)
|
config = getConfig(config)
|
||||||
const { results } = await config.fetchSwell('products', 'list', {
|
const { results } = await config.fetchSwell('products', 'list', [
|
||||||
limit: variables.first,
|
{
|
||||||
})
|
limit: variables.first,
|
||||||
|
},
|
||||||
|
])
|
||||||
const products = results.map((product) => normalizeProduct(product)) ?? []
|
const products = results.map((product) => normalizeProduct(product)) ?? []
|
||||||
return {
|
return {
|
||||||
products,
|
products,
|
||||||
|
@ -18,7 +18,7 @@ const getProduct = async (options: {
|
|||||||
let { config, variables } = options ?? {}
|
let { config, variables } = options ?? {}
|
||||||
config = getConfig(config)
|
config = getConfig(config)
|
||||||
|
|
||||||
const product = await config.fetchSwell('products', 'get', variables.slug)
|
const product = await config.fetchSwell('products', 'get', [variables.slug])
|
||||||
|
|
||||||
return {
|
return {
|
||||||
product: product ? normalizeProduct(product) : null,
|
product: product ? normalizeProduct(product) : null,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user