mirror of
https://github.com/vercel/commerce.git
synced 2025-07-25 19:21:23 +00:00
Add Spree as allowed Framework
This commit is contained in:
1
framework/spree/api/endpoints/cart/index.ts
Normal file
1
framework/spree/api/endpoints/cart/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export default function noopApi(...args: any[]): void {}
|
1
framework/spree/api/endpoints/catalog/index.ts
Normal file
1
framework/spree/api/endpoints/catalog/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export default function noopApi(...args: any[]): void {}
|
1
framework/spree/api/endpoints/catalog/products.ts
Normal file
1
framework/spree/api/endpoints/catalog/products.ts
Normal file
@@ -0,0 +1 @@
|
||||
export default function noopApi(...args: any[]): void {}
|
1
framework/spree/api/endpoints/checkout/index.ts
Normal file
1
framework/spree/api/endpoints/checkout/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export default function noopApi(...args: any[]): void {}
|
1
framework/spree/api/endpoints/customer/index.ts
Normal file
1
framework/spree/api/endpoints/customer/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export default function noopApi(...args: any[]): void {}
|
1
framework/spree/api/endpoints/login/index.ts
Normal file
1
framework/spree/api/endpoints/login/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export default function noopApi(...args: any[]): void {}
|
1
framework/spree/api/endpoints/logout/index.ts
Normal file
1
framework/spree/api/endpoints/logout/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export default function noopApi(...args: any[]): void {}
|
1
framework/spree/api/endpoints/signup/index.ts
Normal file
1
framework/spree/api/endpoints/signup/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export default function noopApi(...args: any[]): void {}
|
1
framework/spree/api/endpoints/wishlist/index.tsx
Normal file
1
framework/spree/api/endpoints/wishlist/index.tsx
Normal file
@@ -0,0 +1 @@
|
||||
export default function noopApi(...args: any[]): void {}
|
39
framework/spree/api/index.ts
Normal file
39
framework/spree/api/index.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import type { APIProvider, CommerceAPI, CommerceAPIConfig } from '@commerce/api'
|
||||
import { getCommerceApi as commerceApi } from '@commerce/api'
|
||||
import createApiFetch from './utils/create-api-fetch'
|
||||
|
||||
import getAllPages from './operations/get-all-pages'
|
||||
import getPage from './operations/get-page'
|
||||
import getSiteInfo from './operations/get-site-info'
|
||||
import getCustomerWishlist from './operations/get-customer-wishlist'
|
||||
import getAllProductPaths from './operations/get-all-product-paths'
|
||||
import getAllProducts from './operations/get-all-products'
|
||||
import getProduct from './operations/get-product'
|
||||
|
||||
export interface SpreeApiConfig extends CommerceAPIConfig {}
|
||||
|
||||
const config: SpreeApiConfig = {
|
||||
commerceUrl: '',
|
||||
apiToken: '',
|
||||
cartCookie: '',
|
||||
customerCookie: '',
|
||||
cartCookieMaxAge: 2592000,
|
||||
fetch: createApiFetch(() => getCommerceApi().getConfig()),
|
||||
}
|
||||
|
||||
const operations = {
|
||||
getAllPages,
|
||||
getPage,
|
||||
getSiteInfo,
|
||||
getCustomerWishlist,
|
||||
getAllProductPaths,
|
||||
getAllProducts,
|
||||
getProduct,
|
||||
}
|
||||
|
||||
export const provider: APIProvider = { config, operations }
|
||||
|
||||
export type SpreeApiProvider = APIProvider
|
||||
|
||||
export const getCommerceApi = (customProvider: APIProvider = provider) =>
|
||||
commerceApi(customProvider)
|
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'
|
154
framework/spree/api/utils/create-api-fetch.ts
Normal file
154
framework/spree/api/utils/create-api-fetch.ts
Normal file
@@ -0,0 +1,154 @@
|
||||
// import { FetcherError } from '@commerce/utils/errors'
|
||||
// import type { GraphQLFetcher } from '@commerce/api'
|
||||
// import type { BigcommerceConfig } from '../index'
|
||||
|
||||
import { GraphQLFetcher, GraphQLFetcherResult } from '@commerce/api'
|
||||
import { SpreeApiConfig } from '..'
|
||||
import { errors, makeClient } from '@spree/storefront-api-v2-sdk'
|
||||
import { requireConfigValue } from 'framework/spree/isomorphicConfig'
|
||||
import convertSpreeErrorToGraphQlError from 'framework/spree/utils/convertSpreeErrorToGraphQlError'
|
||||
import type { ResultResponse } from '@spree/storefront-api-v2-sdk/types/interfaces/ResultResponse'
|
||||
import type {
|
||||
JsonApiDocument,
|
||||
JsonApiListResponse,
|
||||
} from '@spree/storefront-api-v2-sdk/types/interfaces/JsonApi'
|
||||
// import fetch from './fetch'
|
||||
|
||||
// const fetchGraphqlApi: (getConfig: () => BigcommerceConfig) => GraphQLFetcher =
|
||||
// (getConfig) =>
|
||||
// async (query: string, { variables, preview } = {}, fetchOptions) => {
|
||||
// // log.warn(query)
|
||||
// const config = getConfig()
|
||||
// const res = await fetch(config.commerceUrl + (preview ? '/preview' : ''), {
|
||||
// ...fetchOptions,
|
||||
// method: 'POST',
|
||||
// headers: {
|
||||
// Authorization: `Bearer ${config.apiToken}`,
|
||||
// ...fetchOptions?.headers,
|
||||
// 'Content-Type': 'application/json',
|
||||
// },
|
||||
// body: JSON.stringify({
|
||||
// query,
|
||||
// variables,
|
||||
// }),
|
||||
// })
|
||||
|
||||
// const json = await res.json()
|
||||
// if (json.errors) {
|
||||
// throw new FetcherError({
|
||||
// errors: json.errors ?? [{ message: 'Failed to fetch Bigcommerce API' }],
|
||||
// status: res.status,
|
||||
// })
|
||||
// }
|
||||
|
||||
// return { data: json.data, res }
|
||||
// }
|
||||
|
||||
// export default fetchGraphqlApi
|
||||
|
||||
const createApiFetch: (
|
||||
getConfig: () => SpreeApiConfig
|
||||
) => GraphQLFetcher<
|
||||
GraphQLFetcherResult<JsonApiDocument | JsonApiListResponse>
|
||||
> = (getConfig) => {
|
||||
const client = makeClient({ host: requireConfigValue('spreeApiHost') })
|
||||
|
||||
// FIXME: Allow Spree SDK to use fetch instead of axios.
|
||||
return async (query, queryData = {}, fetchOptions = {}) => {
|
||||
const url = query
|
||||
console.log('ydsfgasgdfagsdf', url)
|
||||
const { variables } = queryData
|
||||
let prev = null // FIXME:
|
||||
const clientEndpointMethod = url
|
||||
.split('.')
|
||||
.reduce((clientNode: any, pathPart) => {
|
||||
prev = clientNode
|
||||
//FIXME: use actual type instead of any.
|
||||
// TODO: Fix clientNode type
|
||||
return clientNode[pathPart]
|
||||
}, client)
|
||||
.bind(prev)
|
||||
|
||||
console.log('aisdfuiuashdf', clientEndpointMethod)
|
||||
|
||||
const storeResponse: ResultResponse<JsonApiDocument | JsonApiListResponse> =
|
||||
await clientEndpointMethod() // FIXME: Not the best to use variables here as it's type is any.
|
||||
// await clientEndpointMethod(...variables.args) // FIXME: Not the best to use variables here as it's type is any.
|
||||
|
||||
console.log('87868767868', storeResponse)
|
||||
|
||||
if (storeResponse.success()) {
|
||||
return {
|
||||
data: storeResponse.success(),
|
||||
res: storeResponse as any, //FIXME: MUST BE FETCH RESPONSE
|
||||
}
|
||||
}
|
||||
|
||||
const storeResponseError = storeResponse.fail()
|
||||
|
||||
if (storeResponseError instanceof errors.SpreeError) {
|
||||
throw convertSpreeErrorToGraphQlError(storeResponseError)
|
||||
}
|
||||
|
||||
throw storeResponseError
|
||||
// throw getError(
|
||||
// [
|
||||
// {
|
||||
// message: `${err} \n Most likely related to an unexpected output. e.g the store might be protected with password or not available.`,
|
||||
// },
|
||||
// ],
|
||||
// 500
|
||||
// )
|
||||
// console.log('jsdkfhjasdf', getConfig())
|
||||
// // await
|
||||
// return {
|
||||
// data: [],
|
||||
// res: ,
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
export default createApiFetch
|
||||
|
||||
// LOCAL
|
||||
|
||||
// fetch<Data = any, Variables = any>(
|
||||
// query: string,
|
||||
// queryData?: CommerceAPIFetchOptions<Variables>,
|
||||
// fetchOptions?: RequestInit
|
||||
// ): Promise<GraphQLFetcherResult<Data>>
|
||||
|
||||
// import { FetcherError } from '@commerce/utils/errors'
|
||||
// import type { GraphQLFetcher } from '@commerce/api'
|
||||
// import type { LocalConfig } from '../index'
|
||||
// import fetch from './fetch'
|
||||
|
||||
// const fetchGraphqlApi: (getConfig: () => LocalConfig) => GraphQLFetcher =
|
||||
// (getConfig) =>
|
||||
// async (query: string, { variables, preview } = {}, fetchOptions) => {
|
||||
// const config = getConfig()
|
||||
// const res = await fetch(config.commerceUrl, {
|
||||
// ...fetchOptions,
|
||||
// method: 'POST',
|
||||
// headers: {
|
||||
// ...fetchOptions?.headers,
|
||||
// 'Content-Type': 'application/json',
|
||||
// },
|
||||
// body: JSON.stringify({
|
||||
// query,
|
||||
// variables,
|
||||
// }),
|
||||
// })
|
||||
|
||||
// const json = await res.json()
|
||||
// if (json.errors) {
|
||||
// throw new FetcherError({
|
||||
// errors: json.errors ?? [{ message: 'Failed to fetch for API' }],
|
||||
// status: res.status,
|
||||
// })
|
||||
// }
|
||||
|
||||
// return { data: json.data, res }
|
||||
// }
|
||||
|
||||
// export default fetchGraphqlApi
|
3
framework/spree/api/utils/fetch.ts
Normal file
3
framework/spree/api/utils/fetch.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import zeitFetch from '@vercel/fetch'
|
||||
|
||||
export default zeitFetch()
|
Reference in New Issue
Block a user