Implement Shopify Provider

This commit is contained in:
cond0r
2021-02-04 13:23:44 +02:00
parent c06d9dae3a
commit 14c3f961b3
61 changed files with 16405 additions and 20 deletions

View File

@@ -0,0 +1,29 @@
import { CollectionEdge } from '@framework/schema'
import { getConfig, ShopifyConfig } from '../api'
import getAllCollectionsQuery from '../utils/queries/get-all-collections-query'
const getAllCollections = async (options?: {
variables?: any
config: ShopifyConfig
preview?: boolean
}) => {
let { config, variables = { first: 250 } } = options ?? {}
config = getConfig(config)
const { data } = await config.fetch(getAllCollectionsQuery, { variables })
const edges = data.collections?.edges ?? []
const categories = edges.map(
({ node: { id: entityId, title: name, handle } }: CollectionEdge) => ({
entityId,
name,
path: `/${handle}`,
})
)
return {
categories,
}
}
export default getAllCollections

View File

@@ -0,0 +1,34 @@
import { getConfig, ShopifyConfig } from '../api'
import { ProductEdge } from '../schema'
import getAllProductsPathsQuery from '../utils/queries/get-all-products-paths-query'
type ReturnType = {
products: any[]
}
const getAllProductPaths = async (options?: {
variables?: any
config?: ShopifyConfig
previe?: boolean
}): Promise<ReturnType> => {
let { config, variables = { first: 250 } } = options ?? {}
config = getConfig(config)
const { data } = await config.fetch(getAllProductsPathsQuery, {
variables,
})
const edges = data.products?.edges
const productInfo = data.products?.productInfo
const hasNextPage = productInfo?.hasNextPage
return {
products: edges.map(({ node: { handle } }: ProductEdge) => ({
node: {
path: `/${handle}`,
},
})),
}
}
export default getAllProductPaths

View File

@@ -0,0 +1,39 @@
import { GraphQLFetcherResult } from '@commerce/api'
import toCommerceProducts from '../utils/to-commerce-products'
import { getConfig, ShopifyConfig } from '../api'
import { Product } from '../schema'
import { getAllProductsQuery } from '../utils/queries'
export type ProductNode = Product
type Variables = {
first?: number
field?: string
}
type ReturnType = {
products: any[]
}
const getAllProducts = async (options: {
variables?: Variables
config?: ShopifyConfig
preview?: boolean
}): Promise<ReturnType> => {
let { config, variables = { first: 250 } } = options ?? {}
config = getConfig(config)
const { data }: GraphQLFetcherResult = await config.fetch(
getAllProductsQuery,
{ variables }
)
const shopifyProducts = data.products?.edges
const products = toCommerceProducts(shopifyProducts)
return {
products,
}
}
export default getAllProducts

View File

@@ -0,0 +1,37 @@
import { GraphQLFetcherResult } from '@commerce/api'
import { getConfig, ShopifyConfig } from '../api'
import { Product } from '../schema'
import { toCommerceProduct } from '../utils/to-commerce-products'
import getProductQuery from '../utils/queries/get-product-query'
export type ProductNode = Product
type Variables = {
slug: string
}
type Options = {
variables: Variables
config: ShopifyConfig
preview?: boolean
}
type ReturnType = {
product: any
}
const getProduct = async (options: Options): Promise<ReturnType> => {
let { config, variables = { first: 250 } } = options ?? {}
config = getConfig(config)
const {
data: { productByHandle: product },
}: GraphQLFetcherResult = await config.fetch(getProductQuery, { variables })
return {
product: product ? toCommerceProduct(product) : null,
}
}
export default getProduct

View File

@@ -0,0 +1,2 @@
export * from '@commerce/use-price'
export { default } from '@commerce/use-price'

View File

@@ -0,0 +1,86 @@
import useCommerceSearch from '@commerce/products/use-search'
import toCommerceProducts from '@framework/utils/to-commerce-products'
import getAllProductsQuery from '@framework/utils/queries/get-all-products-query'
import type { Product } from 'framework/bigcommerce/schema'
import type { HookFetcher } from '@commerce/utils/types'
import type { SwrOptions } from '@commerce/utils/use-data'
import type { ProductEdge } from '@framework/schema'
import {
searchByProductType,
searchByTag,
} from '@framework/utils/get-search-variables'
import sortBy from '@framework/utils/get-sort-variables'
export type CommerceProductEdge = {
node: Product
}
export type SearchProductsInput = {
search?: string
categoryPath?: string
sort?: string
}
export type SearchRequestProductsData = {
products?: ProductEdge[]
}
export type SearchProductsData = {
products: Product[]
found: boolean
}
export const fetcher: HookFetcher<
SearchRequestProductsData,
SearchProductsInput
> = (options, { search, categoryPath, sort }, fetch) => {
return fetch({
query: options?.query,
method: options?.method,
variables: {
...searchByProductType(search),
...searchByTag(categoryPath),
...sortBy(sort),
},
}).then(
({ products }): SearchProductsData => {
return {
products: toCommerceProducts(products.edges),
found: !!products.edges.length,
}
}
)
}
export function extendHook(
customFetcher: typeof fetcher,
swrOptions?: SwrOptions<SearchProductsData, SearchProductsInput>
) {
const useSearch = (input: SearchProductsInput = {}) => {
const response = useCommerceSearch(
{
query: getAllProductsQuery,
method: 'POST',
},
[
['search', input.search],
['categoryPath', input.categoryPath],
['sort', input.sort],
],
customFetcher,
{ revalidateOnFocus: false, ...swrOptions }
)
return response
}
useSearch.extend = extendHook
return useSearch
}
export default extendHook(fetcher)