mirror of
https://github.com/vercel/commerce.git
synced 2025-07-26 19:51:23 +00:00
Shopify Provider (#186)
* Start of Shopify provider * add missing comment to documentation * add missing env vars to documentation * update reference to types file
This commit is contained in:
31
framework/shopify/product/get-all-product-paths.ts
Normal file
31
framework/shopify/product/get-all-product-paths.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import Client from 'shopify-buy'
|
||||
import { getConfig } from '../api'
|
||||
import { Product } from '../types'
|
||||
import toCommerceProducts from '../utils/to-commerce-products'
|
||||
|
||||
type ReturnType = {
|
||||
products: any[]
|
||||
}
|
||||
|
||||
const getAllProductPaths = async (): Promise<ReturnType> => {
|
||||
const config = getConfig()
|
||||
|
||||
const client = Client.buildClient({
|
||||
storefrontAccessToken: config.apiToken,
|
||||
domain: config.commerceUrl,
|
||||
})
|
||||
|
||||
const res = (await client.product.fetchAll()) as Product[]
|
||||
|
||||
const products = toCommerceProducts(res)
|
||||
|
||||
return {
|
||||
products: products.map((product) => {
|
||||
return {
|
||||
node: { ...product },
|
||||
}
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
export default getAllProductPaths
|
40
framework/shopify/product/get-all-products.ts
Normal file
40
framework/shopify/product/get-all-products.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import Client from 'shopify-buy'
|
||||
import { ShopifyConfig } from '../api'
|
||||
import { Product } from '../types'
|
||||
import toCommerceProducts from '../utils/to-commerce-products'
|
||||
|
||||
export type ProductNode = Product
|
||||
|
||||
type Variables = {
|
||||
first?: number
|
||||
field?: string
|
||||
}
|
||||
|
||||
type Options = {
|
||||
variables: Variables
|
||||
config: ShopifyConfig
|
||||
preview?: boolean
|
||||
}
|
||||
|
||||
type ReturnType = {
|
||||
products: any[]
|
||||
}
|
||||
|
||||
const getAllProducts = async (options: Options): Promise<ReturnType> => {
|
||||
const { config } = options
|
||||
|
||||
const client = Client.buildClient({
|
||||
storefrontAccessToken: config.apiToken,
|
||||
domain: config.commerceUrl,
|
||||
})
|
||||
|
||||
const res = (await client.product.fetchAll()) as Product[]
|
||||
|
||||
const products = toCommerceProducts(res)
|
||||
|
||||
return {
|
||||
products,
|
||||
}
|
||||
}
|
||||
|
||||
export default getAllProducts
|
37
framework/shopify/product/get-product.ts
Normal file
37
framework/shopify/product/get-product.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import Client from 'shopify-buy'
|
||||
import { ShopifyConfig } from '../api'
|
||||
import { Product } from '../types'
|
||||
import toCommerceProducts from '../utils/to-commerce-products'
|
||||
|
||||
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> => {
|
||||
const { variables, config } = options
|
||||
|
||||
const client = Client.buildClient({
|
||||
storefrontAccessToken: config.apiToken,
|
||||
domain: config.commerceUrl,
|
||||
})
|
||||
|
||||
const res = (await client.product.fetchByHandle(variables.slug)) as Product
|
||||
|
||||
return {
|
||||
product: toCommerceProducts([res])[0],
|
||||
}
|
||||
}
|
||||
|
||||
export default getProduct
|
2
framework/shopify/product/use-price.tsx
Normal file
2
framework/shopify/product/use-price.tsx
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from '@commerce/use-price'
|
||||
export { default } from '@commerce/use-price'
|
41
framework/shopify/product/use-search.tsx
Normal file
41
framework/shopify/product/use-search.tsx
Normal file
@@ -0,0 +1,41 @@
|
||||
import type { HookFetcher } from '@commerce/utils/types'
|
||||
import type { SwrOptions } from '@commerce/utils/use-data'
|
||||
import useCommerceSearch from '@commerce/products/use-search'
|
||||
import { ProductEdge } from '../types'
|
||||
|
||||
const defaultOpts = {}
|
||||
|
||||
export type SearchProductsInput = {
|
||||
search?: string
|
||||
categoryId?: number
|
||||
brandId?: number
|
||||
sort?: string
|
||||
}
|
||||
|
||||
export type SearchProductsData = {
|
||||
products: ProductEdge[]
|
||||
found: boolean
|
||||
}
|
||||
|
||||
export const fetcher: HookFetcher<SearchProductsData, SearchProductsInput> = (
|
||||
options,
|
||||
{ search, categoryId, brandId, sort },
|
||||
fetch
|
||||
) => {
|
||||
return { found: false, products: [] }
|
||||
}
|
||||
|
||||
export function extendHook(
|
||||
customFetcher: typeof fetcher,
|
||||
swrOptions?: SwrOptions<SearchProductsData, SearchProductsInput>
|
||||
) {
|
||||
const useSearch = (input: SearchProductsInput = {}) => {
|
||||
return {}
|
||||
}
|
||||
|
||||
useSearch.extend = extendHook
|
||||
|
||||
return useSearch
|
||||
}
|
||||
|
||||
export default extendHook(fetcher)
|
Reference in New Issue
Block a user