mirror of
https://github.com/vercel/commerce.git
synced 2025-07-25 19:21:23 +00:00
initial provider
This commit is contained in:
1
framework/medusa/api/endpoints/cart/index.ts
Normal file
1
framework/medusa/api/endpoints/cart/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export default function noopApi(...args: any[]): void {}
|
1
framework/medusa/api/endpoints/catalog/index.ts
Normal file
1
framework/medusa/api/endpoints/catalog/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export default function noopApi(...args: any[]): void {}
|
1
framework/medusa/api/endpoints/catalog/products.ts
Normal file
1
framework/medusa/api/endpoints/catalog/products.ts
Normal file
@@ -0,0 +1 @@
|
||||
export default function noopApi(...args: any[]): void {}
|
1
framework/medusa/api/endpoints/checkout/index.ts
Normal file
1
framework/medusa/api/endpoints/checkout/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export default function noopApi(...args: any[]): void {}
|
1
framework/medusa/api/endpoints/customer/index.ts
Normal file
1
framework/medusa/api/endpoints/customer/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export default function noopApi(...args: any[]): void {}
|
1
framework/medusa/api/endpoints/login/index.ts
Normal file
1
framework/medusa/api/endpoints/login/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export default function noopApi(...args: any[]): void {}
|
1
framework/medusa/api/endpoints/logout/index.ts
Normal file
1
framework/medusa/api/endpoints/logout/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export default function noopApi(...args: any[]): void {}
|
1
framework/medusa/api/endpoints/signup/index.ts
Normal file
1
framework/medusa/api/endpoints/signup/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export default function noopApi(...args: any[]): void {}
|
1
framework/medusa/api/endpoints/wishlist/index.tsx
Normal file
1
framework/medusa/api/endpoints/wishlist/index.tsx
Normal file
@@ -0,0 +1 @@
|
||||
export default function noopApi(...args: any[]): void {}
|
30
framework/medusa/api/index.ts
Normal file
30
framework/medusa/api/index.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import type { CommerceAPIConfig } from '@commerce/api'
|
||||
import { CommerceAPI, getCommerceApi as commerceApi } from '@commerce/api'
|
||||
import fetchApi from './utils/fetch-medusa-api'
|
||||
import { MEDUSA_CART_ID_COOKIE } from '../const'
|
||||
|
||||
import * as operations from './operations'
|
||||
|
||||
export interface MedusaConfig extends CommerceAPIConfig {
|
||||
fetch: any
|
||||
}
|
||||
|
||||
const config: MedusaConfig = {
|
||||
commerceUrl: '',
|
||||
apiToken: '',
|
||||
cartCookie: MEDUSA_CART_ID_COOKIE,
|
||||
customerCookie: '',
|
||||
cartCookieMaxAge: 60 * 60 * 24 * 30,
|
||||
fetch: fetchApi,
|
||||
}
|
||||
|
||||
export const provider = { config, operations }
|
||||
|
||||
export type Provider = typeof provider
|
||||
export type MedusaAPI<P extends Provider = Provider> = CommerceAPI<P | any>
|
||||
|
||||
export function getCommerceApi<P extends Provider>(
|
||||
customProvider: P = provider as any
|
||||
): MedusaAPI<P> {
|
||||
return commerceApi(customProvider as any)
|
||||
}
|
19
framework/medusa/api/operations/get-all-pages.ts
Normal file
19
framework/medusa/api/operations/get-all-pages.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
export type Page = { url: string }
|
||||
export type GetAllPagesResult = { pages: Page[] }
|
||||
import type { MedusaConfig } from '..'
|
||||
|
||||
export default function getAllPagesOperation() {
|
||||
function getAllPages({
|
||||
config,
|
||||
preview,
|
||||
}: {
|
||||
url?: string
|
||||
config?: Partial<MedusaConfig>
|
||||
preview?: boolean
|
||||
}): Promise<GetAllPagesResult> {
|
||||
return Promise.resolve({
|
||||
pages: [],
|
||||
})
|
||||
}
|
||||
return getAllPages
|
||||
}
|
35
framework/medusa/api/operations/get-all-product-paths.ts
Normal file
35
framework/medusa/api/operations/get-all-product-paths.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { OperationContext } from '@commerce/api/operations'
|
||||
import { Product } from '@commerce/types/product'
|
||||
import { Product as MedusaProduct } from '@medusajs/medusa-js/lib/types'
|
||||
import { MedusaConfig } from '..'
|
||||
|
||||
export type GetAllProductPathsResult = {
|
||||
products: Array<{ path: string }>
|
||||
}
|
||||
|
||||
export default function getAllProductPathsOperation({
|
||||
commerce,
|
||||
}: OperationContext<any>) {
|
||||
async function getAllProductsPaths({
|
||||
config: cfg,
|
||||
}: {
|
||||
config?: Partial<MedusaConfig>
|
||||
preview?: boolean
|
||||
} = {}): Promise<{ products: Product[] | any[] }> {
|
||||
const config = commerce.getConfig(cfg)
|
||||
|
||||
const results = await config.fetch('products', 'list', {})
|
||||
|
||||
const productHandles = results.data?.products
|
||||
? results.data.products.map(({ handle }: MedusaProduct) => ({
|
||||
path: `/${handle}`,
|
||||
}))
|
||||
: []
|
||||
|
||||
return {
|
||||
products: productHandles,
|
||||
}
|
||||
}
|
||||
|
||||
return getAllProductsPaths
|
||||
}
|
41
framework/medusa/api/operations/get-all-products.ts
Normal file
41
framework/medusa/api/operations/get-all-products.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { Product } from '@commerce/types/product'
|
||||
import type { OperationContext } from '@commerce/api/operations'
|
||||
import type { MedusaConfig } from '../'
|
||||
import { normalizeProduct } from '@framework/utils/normalizers/normalize-products'
|
||||
import { Product as MedusaProduct } from '@medusajs/medusa-js/lib/types'
|
||||
|
||||
export type ProductVariables = { first?: number }
|
||||
|
||||
export default function getAllProductsOperation({
|
||||
commerce,
|
||||
}: OperationContext<any>) {
|
||||
async function getAllProducts({
|
||||
config: cfg,
|
||||
variables,
|
||||
}: {
|
||||
query?: string
|
||||
variables?: ProductVariables
|
||||
config?: Partial<MedusaConfig>
|
||||
preview?: boolean
|
||||
} = {}): Promise<{ products: Product[] | any[] }> {
|
||||
const config = commerce.getConfig(cfg)
|
||||
const query = variables?.first && { limit: variables.first }
|
||||
|
||||
const results = await config.fetch(
|
||||
'products',
|
||||
'list',
|
||||
query ? { query: query } : {}
|
||||
)
|
||||
|
||||
const products: Product[] = results.data?.products
|
||||
? results.data.products.map((product: MedusaProduct) =>
|
||||
normalizeProduct(product)
|
||||
)
|
||||
: []
|
||||
|
||||
return {
|
||||
products,
|
||||
}
|
||||
}
|
||||
return getAllProducts
|
||||
}
|
6
framework/medusa/api/operations/get-customer-wishlist.ts
Normal file
6
framework/medusa/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/medusa/api/operations/get-page.ts
Normal file
13
framework/medusa/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
|
||||
}
|
46
framework/medusa/api/operations/get-product.ts
Normal file
46
framework/medusa/api/operations/get-product.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { Product } from '@commerce/types/product'
|
||||
import { GetProductOperation } from '@commerce/types/product'
|
||||
import type { OperationContext } from '@commerce/api/operations'
|
||||
import { Product as MedusaProduct } from '@medusajs/medusa-js/lib/types'
|
||||
import { normalizeProduct } from '@framework/utils/normalizers/normalize-products'
|
||||
import { MedusaConfig } from '..'
|
||||
|
||||
export default function getProductOperation({
|
||||
commerce,
|
||||
}: OperationContext<any>) {
|
||||
async function getProduct<T extends GetProductOperation>({
|
||||
variables,
|
||||
config: cfg,
|
||||
}: {
|
||||
query?: string
|
||||
variables?: T['variables']
|
||||
config?: Partial<MedusaConfig>
|
||||
preview?: boolean
|
||||
} = {}): Promise<Product | {} | any> {
|
||||
const config = commerce.getConfig(cfg)
|
||||
|
||||
const response = await config.fetch('products', 'list', {})
|
||||
|
||||
if (response.data?.products) {
|
||||
const products: MedusaProduct[] = response.data.products
|
||||
const product = products
|
||||
? products.find(({ handle }) => handle === variables!.slug)
|
||||
: null
|
||||
|
||||
/**
|
||||
* Commerce only provides us with the slug/path for finding
|
||||
* the specified product. We do not have a endpoint for retrieving
|
||||
* products using handles. Perhaps we should ask Vercel if we can
|
||||
* change this so the variables also expose the product_id, which
|
||||
* we can use for retrieving products.
|
||||
*/
|
||||
if (product) {
|
||||
return {
|
||||
product: normalizeProduct(product),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return getProduct
|
||||
}
|
33
framework/medusa/api/operations/get-site-info.ts
Normal file
33
framework/medusa/api/operations/get-site-info.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { OperationContext } from '@commerce/api/operations'
|
||||
import { Category } from '@commerce/types/site'
|
||||
import { MedusaConfig } from '..'
|
||||
|
||||
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<MedusaConfig>
|
||||
preview?: boolean
|
||||
} = {}): Promise<GetSiteInfoResult> {
|
||||
/** We should add collections to our Storefront API,
|
||||
* so we can populate the site with collections here
|
||||
*/
|
||||
return Promise.resolve({
|
||||
categories: [],
|
||||
brands: [],
|
||||
})
|
||||
}
|
||||
|
||||
return getSiteInfo
|
||||
}
|
6
framework/medusa/api/operations/index.ts
Normal file
6
framework/medusa/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'
|
8
framework/medusa/api/utils/fetch-medusa-api.ts
Normal file
8
framework/medusa/api/utils/fetch-medusa-api.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { callMedusa } from '@framework/utils/call-medusa'
|
||||
|
||||
const fetchApi = async (query: string, method: string, variables: any) => {
|
||||
const response = await callMedusa(method, query, variables)
|
||||
|
||||
return response
|
||||
}
|
||||
export default fetchApi
|
3
framework/medusa/api/utils/fetch.ts
Normal file
3
framework/medusa/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