mirror of
https://github.com/vercel/commerce.git
synced 2025-07-27 04:01:23 +00:00
Add Spree as allowed Framework
This commit is contained in:
37
framework/spree/api/operations/get-all-pages.ts
Normal file
37
framework/spree/api/operations/get-all-pages.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
export type Page = { url: string }
|
||||
import { OperationContext, OperationOptions } from '@commerce/api/operations'
|
||||
import { GetAllPagesOperation } from '@commerce/types/page'
|
||||
import type { SpreeApiConfig, SpreeApiProvider } from '../index'
|
||||
|
||||
export default function getAllPagesOperation({
|
||||
commerce,
|
||||
}: OperationContext<SpreeApiProvider>) {
|
||||
async function getAllPages<T extends GetAllPagesOperation>(options?: {
|
||||
config?: Partial<SpreeApiConfig>
|
||||
preview?: boolean
|
||||
}): Promise<T['data']>
|
||||
|
||||
async function getAllPages<T extends GetAllPagesOperation>(
|
||||
opts: {
|
||||
config?: Partial<SpreeApiConfig>
|
||||
preview?: boolean
|
||||
} & OperationOptions
|
||||
): Promise<T['data']>
|
||||
|
||||
async function getAllPages<T extends GetAllPagesOperation>({
|
||||
config,
|
||||
preview,
|
||||
query,
|
||||
}: {
|
||||
url?: string
|
||||
config?: Partial<SpreeApiConfig>
|
||||
preview?: boolean
|
||||
query?: string
|
||||
} = {}): Promise<T['data']> {
|
||||
return {
|
||||
pages: [],
|
||||
}
|
||||
}
|
||||
|
||||
return getAllPages
|
||||
}
|
17
framework/spree/api/operations/get-all-product-paths.ts
Normal file
17
framework/spree/api/operations/get-all-product-paths.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
// import data from '../../data.json'
|
||||
|
||||
export type GetAllProductPathsResult = {
|
||||
products: Array<{ path: string }>
|
||||
}
|
||||
|
||||
export default function getAllProductPathsOperation() {
|
||||
function getAllProductPaths(): Promise<GetAllProductPathsResult> {
|
||||
return Promise.resolve({
|
||||
// products: data.products.map(({ path }) => ({ path })),
|
||||
// TODO: Return Storefront [{ path: '/long-sleeve-shirt' }, ...] from Spree products. Paths using product IDs are fine too.
|
||||
products: [],
|
||||
})
|
||||
}
|
||||
|
||||
return getAllProductPaths
|
||||
}
|
79
framework/spree/api/operations/get-all-products.ts
Normal file
79
framework/spree/api/operations/get-all-products.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
import { Product } from '@commerce/types/product'
|
||||
import { GetAllProductsOperation } from '@commerce/types/product'
|
||||
import type { OperationContext } from '@commerce/api/operations'
|
||||
import type { LocalConfig, Provider, SpreeApiProvider } from '../index'
|
||||
import type { IProducts } from '@spree/storefront-api-v2-sdk/types/interfaces/Product'
|
||||
// import data from '../../../local/data.json'
|
||||
|
||||
export default function getAllProductsOperation({
|
||||
commerce,
|
||||
}: OperationContext<SpreeApiProvider>) {
|
||||
async function getAllProducts<T extends GetAllProductsOperation>({
|
||||
query = 'products.list',
|
||||
variables = { first: 10 },
|
||||
config: userConfig,
|
||||
}: {
|
||||
query?: string
|
||||
variables?: T['variables']
|
||||
config?: Partial<LocalConfig>
|
||||
} = {}): Promise<{ products: Product[] | any[] }> {
|
||||
const config = commerce.getConfig(userConfig)
|
||||
const { fetch: apiFetch /*, locale*/ } = config
|
||||
const first = variables.first // How many products to fetch.
|
||||
|
||||
console.log(
|
||||
'sdfuasdufahsdf variables = ',
|
||||
variables,
|
||||
'query = ',
|
||||
query,
|
||||
'config = ',
|
||||
config
|
||||
)
|
||||
|
||||
console.log('sdfasdg')
|
||||
|
||||
const { data } = await apiFetch<IProducts>(
|
||||
query,
|
||||
{ variables }
|
||||
// {
|
||||
// ...(locale && {}),
|
||||
// }
|
||||
)
|
||||
|
||||
console.log('asuidfhasdf', data)
|
||||
|
||||
// return {
|
||||
// products: data.products.edges.map(({ node }) =>
|
||||
// normalizeProduct(node as ShopifyProduct)
|
||||
// ),
|
||||
// }
|
||||
|
||||
const normalizedProducts: Product[] = data.data.map((spreeProduct) => {
|
||||
return {
|
||||
id: spreeProduct.id,
|
||||
name: spreeProduct.attributes.name,
|
||||
description: spreeProduct.attributes.description,
|
||||
images: [],
|
||||
variants: [],
|
||||
options: [],
|
||||
price: {
|
||||
value: 10,
|
||||
currencyCode: 'USD',
|
||||
retailPrice: 8,
|
||||
salePrice: 7,
|
||||
listPrice: 6,
|
||||
extendedSalePrice: 2,
|
||||
extendedListPrice: 1,
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
return {
|
||||
// products: data.products,
|
||||
// TODO: Return Spree products.
|
||||
products: normalizedProducts,
|
||||
}
|
||||
}
|
||||
|
||||
return getAllProducts
|
||||
}
|
6
framework/spree/api/operations/get-customer-wishlist.ts
Normal file
6
framework/spree/api/operations/get-customer-wishlist.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
export default function getCustomerWishlistOperation() {
|
||||
function getCustomerWishlist(): any {
|
||||
return { wishlist: {} }
|
||||
}
|
||||
return getCustomerWishlist
|
||||
}
|
13
framework/spree/api/operations/get-page.ts
Normal file
13
framework/spree/api/operations/get-page.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
export type Page = any
|
||||
export type GetPageResult = { page?: Page }
|
||||
|
||||
export type PageVariables = {
|
||||
id: number
|
||||
}
|
||||
|
||||
export default function getPageOperation() {
|
||||
function getPage(): Promise<GetPageResult> {
|
||||
return Promise.resolve({})
|
||||
}
|
||||
return getPage
|
||||
}
|
28
framework/spree/api/operations/get-product.ts
Normal file
28
framework/spree/api/operations/get-product.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import type { LocalConfig } from '../index'
|
||||
import { Product } from '@commerce/types/product'
|
||||
import { GetProductOperation } from '@commerce/types/product'
|
||||
import data from '../../../local/data.json'
|
||||
import type { OperationContext } from '@commerce/api/operations'
|
||||
|
||||
export default function getProductOperation({
|
||||
commerce,
|
||||
}: OperationContext<any>) {
|
||||
async function getProduct<T extends GetProductOperation>({
|
||||
query = '',
|
||||
variables,
|
||||
config,
|
||||
}: {
|
||||
query?: string
|
||||
variables?: T['variables']
|
||||
config?: Partial<LocalConfig>
|
||||
preview?: boolean
|
||||
} = {}): Promise<Product | {} | any> {
|
||||
return {
|
||||
product: data.products.find(({ slug }) => slug === variables!.slug),
|
||||
// TODO: Return Spree product.
|
||||
// product: {},
|
||||
}
|
||||
}
|
||||
|
||||
return getProduct
|
||||
}
|
44
framework/spree/api/operations/get-site-info.ts
Normal file
44
framework/spree/api/operations/get-site-info.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { OperationContext } from '@commerce/api/operations'
|
||||
import { Category } from '@commerce/types/site'
|
||||
import { LocalConfig } from '../index'
|
||||
|
||||
export type GetSiteInfoResult<
|
||||
T extends { categories: any[]; brands: any[] } = {
|
||||
categories: Category[]
|
||||
brands: any[]
|
||||
}
|
||||
> = T
|
||||
|
||||
export default function getSiteInfoOperation({}: OperationContext<any>) {
|
||||
// TODO: Get Spree categories for display in React components.
|
||||
function getSiteInfo({
|
||||
query,
|
||||
variables,
|
||||
config: cfg,
|
||||
}: {
|
||||
query?: string
|
||||
variables?: any
|
||||
config?: Partial<LocalConfig>
|
||||
preview?: boolean
|
||||
} = {}): Promise<GetSiteInfoResult> {
|
||||
return Promise.resolve({
|
||||
categories: [
|
||||
{
|
||||
id: 'new-arrivals',
|
||||
name: 'New Arrivals',
|
||||
slug: 'new-arrivals',
|
||||
path: '/new-arrivals',
|
||||
},
|
||||
{
|
||||
id: 'featured',
|
||||
name: 'Featured',
|
||||
slug: 'featured',
|
||||
path: '/featured',
|
||||
},
|
||||
],
|
||||
brands: [],
|
||||
})
|
||||
}
|
||||
|
||||
return getSiteInfo
|
||||
}
|
6
framework/spree/api/operations/index.ts
Normal file
6
framework/spree/api/operations/index.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
export { default as getPage } from './get-page'
|
||||
export { default as getSiteInfo } from './get-site-info'
|
||||
export { default as getAllPages } from './get-all-pages'
|
||||
export { default as getProduct } from './get-product'
|
||||
export { default as getAllProducts } from './get-all-products'
|
||||
export { default as getAllProductPaths } from './get-all-product-paths'
|
Reference in New Issue
Block a user