mirror of
https://github.com/vercel/commerce.git
synced 2025-07-23 04:36:49 +00:00
Add swell provider folder
This commit is contained in:
42
framework/swell/common/get-all-pages.ts
Normal file
42
framework/swell/common/get-all-pages.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { getConfig, ShopifyConfig } from '../api'
|
||||
import { PageEdge } from '../schema'
|
||||
import { getAllPagesQuery } from '../utils/queries'
|
||||
|
||||
type Variables = {
|
||||
first?: number
|
||||
}
|
||||
|
||||
type ReturnType = {
|
||||
pages: Page[]
|
||||
}
|
||||
|
||||
export type Page = {
|
||||
id: string
|
||||
name: string
|
||||
url: string
|
||||
sort_order?: number
|
||||
body: string
|
||||
}
|
||||
|
||||
const getAllPages = async (options?: {
|
||||
variables?: Variables
|
||||
config: ShopifyConfig
|
||||
preview?: boolean
|
||||
}): 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,
|
||||
})
|
||||
)
|
||||
|
||||
return { pages }
|
||||
}
|
||||
|
||||
export default getAllPages
|
37
framework/swell/common/get-page.ts
Normal file
37
framework/swell/common/get-page.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { getConfig, ShopifyConfig } from '../api'
|
||||
import getPageQuery from '../utils/queries/get-page-query'
|
||||
import { Page } from './get-all-pages'
|
||||
|
||||
type Variables = {
|
||||
id: string
|
||||
}
|
||||
|
||||
export type GetPageResult<T extends { page?: any } = { page?: Page }> = T
|
||||
|
||||
const getPage = async (options: {
|
||||
variables: Variables
|
||||
config: ShopifyConfig
|
||||
preview?: boolean
|
||||
}): Promise<GetPageResult> => {
|
||||
let { config, variables } = options ?? {}
|
||||
|
||||
config = getConfig(config)
|
||||
const { locale } = config
|
||||
|
||||
const { data } = await config.fetch(getPageQuery, {
|
||||
variables,
|
||||
})
|
||||
const page = data.node
|
||||
|
||||
return {
|
||||
page: page
|
||||
? {
|
||||
...page,
|
||||
name: page.title,
|
||||
url: `/${locale}/${page.handle}`,
|
||||
}
|
||||
: null,
|
||||
}
|
||||
}
|
||||
|
||||
export default getPage
|
31
framework/swell/common/get-site-info.ts
Normal file
31
framework/swell/common/get-site-info.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import getCategories, { Category } from '../utils/get-categories'
|
||||
import getVendors, { Brands } from '../utils/get-vendors'
|
||||
|
||||
import { getConfig, ShopifyConfig } from '../api'
|
||||
|
||||
export type GetSiteInfoResult<
|
||||
T extends { categories: any[]; brands: any[] } = {
|
||||
categories: Category[]
|
||||
brands: Brands
|
||||
}
|
||||
> = T
|
||||
|
||||
const getSiteInfo = async (options?: {
|
||||
variables?: any
|
||||
config: ShopifyConfig
|
||||
preview?: boolean
|
||||
}): Promise<GetSiteInfoResult> => {
|
||||
let { config } = options ?? {}
|
||||
|
||||
config = getConfig(config)
|
||||
|
||||
const categories = await getCategories(config)
|
||||
const brands = await getVendors(config)
|
||||
|
||||
return {
|
||||
categories,
|
||||
brands,
|
||||
}
|
||||
}
|
||||
|
||||
export default getSiteInfo
|
Reference in New Issue
Block a user