4
0
forked from crowetic/commerce

update get-page and get-all-pages content hooks

This commit is contained in:
Greg Hoskin 2021-04-09 16:51:35 -06:00
parent abd86329d5
commit 644f196c97
5 changed files with 18 additions and 24 deletions

View File

@ -3,11 +3,10 @@ import { swellConfig } from '../../index'
const fetchSwellApi = async (
query: string,
method: string,
variables: object | string
variables: [] = []
) => {
const { swell } = swellConfig
const res = await swell[query][method](variables)
return res
return await swell[query][method](...variables)
}
export default fetchSwellApi

View File

@ -25,16 +25,12 @@ const getAllPages = async (options?: {
}): Promise<ReturnType> => {
let { config, variables = { first: 250 } } = options ?? {}
config = getConfig(config)
const { locale } = config
const { data } = await config.fetch(getAllPagesQuery, { variables })
const pages = data.pages?.edges?.map(
({ node: { title: name, handle, ...node } }: PageEdge) => ({
...node,
url: `/${locale}/${handle}`,
name,
})
)
const { locale, fetchSwell } = config
const { results } = await fetchSwell('content', 'list', ['pages'])
const pages = results.map(({ slug, ...rest }) => ({
url: `/${locale}/${slug}`,
...rest,
}))
return { pages }
}

View File

@ -17,18 +17,15 @@ const getPage = async (options: {
config = getConfig(config)
const { locale } = config
const { data } = await config.fetch(getPageQuery, {
variables,
})
const page = data.node
const { id } = variables
const result = await config.fetchSwell('content', 'get', ['pages', id])
const page = result
return {
page: page
? {
...page,
name: page.title,
url: `/${locale}/${page.handle}`,
url: `/${locale}/${page.slug}`,
}
: null,
}

View File

@ -21,9 +21,11 @@ const getAllProducts = async (options: {
}): Promise<ReturnType> => {
let { config, variables = { first: 250 } } = options ?? {}
config = getConfig(config)
const { results } = await config.fetchSwell('products', 'list', {
limit: variables.first,
})
const { results } = await config.fetchSwell('products', 'list', [
{
limit: variables.first,
},
])
const products = results.map((product) => normalizeProduct(product)) ?? []
return {
products,

View File

@ -18,7 +18,7 @@ const getProduct = async (options: {
let { config, variables } = options ?? {}
config = getConfig(config)
const product = await config.fetchSwell('products', 'get', variables.slug)
const product = await config.fetchSwell('products', 'get', [variables.slug])
return {
product: product ? normalizeProduct(product) : null,