WIP OrderCloud provider

This commit is contained in:
goncy
2021-08-06 16:23:33 -03:00
parent 0e7e7b7d5f
commit f765590484
50 changed files with 691 additions and 4 deletions

View File

@@ -0,0 +1,19 @@
export type Page = { url: string }
export type GetAllPagesResult = { pages: Page[] }
import type { OrdercloudConfig } from '../index'
export default function getAllPagesOperation() {
function getAllPages({
config,
preview,
}: {
url?: string
config?: Partial<OrdercloudConfig>
preview?: boolean
}): Promise<GetAllPagesResult> {
return Promise.resolve({
pages: [],
})
}
return getAllPages
}

View File

@@ -0,0 +1,16 @@
import data from '../../data.json'
export type GetAllProductPathsResult = {
products: Array<{ path: string }>
}
export default function getAllProductPathsOperation() {
function getAllProductPaths(): Promise<GetAllProductPathsResult> {
return Promise.resolve({
products: []
// products: data.products.map(({ path }) => ({ path })),
})
}
return getAllProductPaths
}

View File

@@ -0,0 +1,44 @@
import { Product } from '@commerce/types/product'
import { GetAllProductsOperation } from '@commerce/types/product'
import type { OperationContext } from '@commerce/api/operations'
import type { OrdercloudConfig, Provider } from '../index'
import {
PriceSchedule,
RawProduct,
RawProductWithPrice,
} from '@framework/types/product'
import { normalize as normalizeProduct } from '@framework/utils/product'
export default function getAllProductsOperation({
commerce,
}: OperationContext<Provider>) {
async function getAllProducts<T extends GetAllProductsOperation>({
config,
}: {
query?: string
variables?: T['variables']
config?: Partial<OrdercloudConfig>
preview?: boolean
} = {}): Promise<{ products: Product[] }> {
const { fetch } = commerce.getConfig(config)
const rawProducts: RawProduct[] = await fetch<{ Items: RawProduct[] }>(
'GET',
'/products'
).then((response) => response.Items)
const rawProductsWithPrice: RawProductWithPrice[] = await Promise.all(
rawProducts.map(async (product) => ({
...product,
priceSchedule: await fetch<PriceSchedule>(
'GET',
`/priceschedules/${product.ID}`
),
}))
)
return {
products: rawProductsWithPrice.map(normalizeProduct),
}
}
return getAllProducts
}

View File

@@ -0,0 +1,6 @@
export default function getCustomerWishlistOperation() {
function getCustomerWishlist(): any {
return { wishlist: {} }
}
return getCustomerWishlist
}

View 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
}

View File

@@ -0,0 +1,26 @@
import type { OrdercloudConfig } from '../index'
import { Product } from '@commerce/types/product'
import { GetProductOperation } from '@commerce/types/product'
import data from '../../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<OrdercloudConfig>
preview?: boolean
} = {}): Promise<Product | {} | any> {
return {
product: null // data.products.find(({ slug }) => slug === variables!.slug),
}
}
return getProduct
}

View File

@@ -0,0 +1,43 @@
import { OperationContext } from '@commerce/api/operations'
import { Category } from '@commerce/types/site'
import { OrdercloudConfig } from '../index'
export type GetSiteInfoResult<
T extends { categories: any[]; brands: any[] } = {
categories: Category[]
brands: any[]
}
> = T
export default function getSiteInfoOperation({}: OperationContext<any>) {
function getSiteInfo({
query,
variables,
config: cfg,
}: {
query?: string
variables?: any
config?: Partial<OrdercloudConfig>
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
}

View 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'