mirror of
https://github.com/vercel/commerce.git
synced 2025-05-17 15:06:59 +00:00
feat: mock api routes
This commit is contained in:
parent
8c6f2754dd
commit
2e30281e69
1
packages/sylius/src/api/endpoints/cart/index.ts
Normal file
1
packages/sylius/src/api/endpoints/cart/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export default function noopApi(...args: any[]): void {}
|
1
packages/sylius/src/api/endpoints/catalog/index.ts
Normal file
1
packages/sylius/src/api/endpoints/catalog/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export default function noopApi(...args: any[]): void {}
|
1
packages/sylius/src/api/endpoints/catalog/products.ts
Normal file
1
packages/sylius/src/api/endpoints/catalog/products.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export default function noopApi(...args: any[]): void {}
|
1
packages/sylius/src/api/endpoints/checkout/index.ts
Normal file
1
packages/sylius/src/api/endpoints/checkout/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export default function noopApi(...args: any[]): void {}
|
1
packages/sylius/src/api/endpoints/customer/address.ts
Normal file
1
packages/sylius/src/api/endpoints/customer/address.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export default function noopApi(...args: any[]): void {}
|
1
packages/sylius/src/api/endpoints/customer/card.ts
Normal file
1
packages/sylius/src/api/endpoints/customer/card.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export default function noopApi(...args: any[]): void {}
|
1
packages/sylius/src/api/endpoints/customer/index.ts
Normal file
1
packages/sylius/src/api/endpoints/customer/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export default function noopApi(...args: any[]): void {}
|
1
packages/sylius/src/api/endpoints/login/index.ts
Normal file
1
packages/sylius/src/api/endpoints/login/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export default function noopApi(...args: any[]): void {}
|
1
packages/sylius/src/api/endpoints/logout/index.ts
Normal file
1
packages/sylius/src/api/endpoints/logout/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export default function noopApi(...args: any[]): void {}
|
1
packages/sylius/src/api/endpoints/signup/index.ts
Normal file
1
packages/sylius/src/api/endpoints/signup/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export default function noopApi(...args: any[]): void {}
|
1
packages/sylius/src/api/endpoints/wishlist/index.tsx
Normal file
1
packages/sylius/src/api/endpoints/wishlist/index.tsx
Normal file
@ -0,0 +1 @@
|
|||||||
|
export default function noopApi(...args: any[]): void {}
|
42
packages/sylius/src/api/index.ts
Normal file
42
packages/sylius/src/api/index.ts
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
import type { CommerceAPI, CommerceAPIConfig } from '@vercel/commerce/api'
|
||||||
|
import { getCommerceApi as commerceApi } from '@vercel/commerce/api'
|
||||||
|
import createFetcher from './utils/fetch-local'
|
||||||
|
|
||||||
|
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 LocalConfig extends CommerceAPIConfig {}
|
||||||
|
const config: LocalConfig = {
|
||||||
|
commerceUrl: '',
|
||||||
|
apiToken: '',
|
||||||
|
cartCookie: '',
|
||||||
|
customerCookie: '',
|
||||||
|
cartCookieMaxAge: 2592000,
|
||||||
|
fetch: createFetcher(() => getCommerceApi().getConfig()),
|
||||||
|
}
|
||||||
|
|
||||||
|
const operations = {
|
||||||
|
getAllPages,
|
||||||
|
getPage,
|
||||||
|
getSiteInfo,
|
||||||
|
getCustomerWishlist,
|
||||||
|
getAllProductPaths,
|
||||||
|
getAllProducts,
|
||||||
|
getProduct,
|
||||||
|
}
|
||||||
|
|
||||||
|
export const provider = { config, operations }
|
||||||
|
|
||||||
|
export type Provider = typeof provider
|
||||||
|
export type LocalAPI<P extends Provider = Provider> = CommerceAPI<P | any>
|
||||||
|
|
||||||
|
export function getCommerceApi<P extends Provider>(
|
||||||
|
customProvider: P = provider as any
|
||||||
|
): LocalAPI<P> {
|
||||||
|
return commerceApi(customProvider as any)
|
||||||
|
}
|
19
packages/sylius/src/api/operations/get-all-pages.ts
Normal file
19
packages/sylius/src/api/operations/get-all-pages.ts
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
export type Page = { url: string }
|
||||||
|
export type GetAllPagesResult = { pages: Page[] }
|
||||||
|
import type { LocalConfig } from '../index'
|
||||||
|
|
||||||
|
export default function getAllPagesOperation() {
|
||||||
|
function getAllPages({
|
||||||
|
config,
|
||||||
|
preview,
|
||||||
|
}: {
|
||||||
|
url?: string
|
||||||
|
config?: Partial<LocalConfig>
|
||||||
|
preview?: boolean
|
||||||
|
}): Promise<GetAllPagesResult> {
|
||||||
|
return Promise.resolve({
|
||||||
|
pages: [],
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return getAllPages
|
||||||
|
}
|
13
packages/sylius/src/api/operations/get-all-product-paths.ts
Normal file
13
packages/sylius/src/api/operations/get-all-product-paths.ts
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
export type GetAllProductPathsResult = {
|
||||||
|
products: Array<{ path: string }>
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function getAllProductPathsOperation() {
|
||||||
|
function getAllProductPaths(): Promise<GetAllProductPathsResult> {
|
||||||
|
return Promise.resolve({
|
||||||
|
products: [],
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return getAllProductPaths
|
||||||
|
}
|
24
packages/sylius/src/api/operations/get-all-products.ts
Normal file
24
packages/sylius/src/api/operations/get-all-products.ts
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
import { Product } from '@vercel/commerce/types/product'
|
||||||
|
import { GetAllProductsOperation } from '@vercel/commerce/types/product'
|
||||||
|
import type { OperationContext } from '@vercel/commerce/api/operations'
|
||||||
|
import type { LocalConfig, Provider } from '../index'
|
||||||
|
|
||||||
|
export default function getAllProductsOperation({
|
||||||
|
commerce,
|
||||||
|
}: OperationContext<any>) {
|
||||||
|
async function getAllProducts<T extends GetAllProductsOperation>({
|
||||||
|
query = '',
|
||||||
|
variables,
|
||||||
|
config,
|
||||||
|
}: {
|
||||||
|
query?: string
|
||||||
|
variables?: T['variables']
|
||||||
|
config?: Partial<LocalConfig>
|
||||||
|
preview?: boolean
|
||||||
|
} = {}): Promise<{ products: Product[] | any[] }> {
|
||||||
|
return {
|
||||||
|
products: [],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return getAllProducts
|
||||||
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
export default function getCustomerWishlistOperation() {
|
||||||
|
function getCustomerWishlist(): any {
|
||||||
|
return { wishlist: {} }
|
||||||
|
}
|
||||||
|
return getCustomerWishlist
|
||||||
|
}
|
13
packages/sylius/src/api/operations/get-page.ts
Normal file
13
packages/sylius/src/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
|
||||||
|
}
|
25
packages/sylius/src/api/operations/get-product.ts
Normal file
25
packages/sylius/src/api/operations/get-product.ts
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
import type { LocalConfig } from '../index'
|
||||||
|
import { Product } from '@vercel/commerce/types/product'
|
||||||
|
import { GetProductOperation } from '@vercel/commerce/types/product'
|
||||||
|
import type { OperationContext } from '@vercel/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: {},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return getProduct
|
||||||
|
}
|
43
packages/sylius/src/api/operations/get-site-info.ts
Normal file
43
packages/sylius/src/api/operations/get-site-info.ts
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
import { OperationContext } from '@vercel/commerce/api/operations'
|
||||||
|
import { Category } from '@vercel/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>) {
|
||||||
|
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
packages/sylius/src/api/operations/index.ts
Normal file
6
packages/sylius/src/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'
|
34
packages/sylius/src/api/utils/fetch-local.ts
Normal file
34
packages/sylius/src/api/utils/fetch-local.ts
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
import { FetcherError } from '@vercel/commerce/utils/errors'
|
||||||
|
import type { GraphQLFetcher } from '@vercel/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
packages/sylius/src/api/utils/fetch.ts
Normal file
3
packages/sylius/src/api/utils/fetch.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
import zeitFetch from '@vercel/fetch'
|
||||||
|
|
||||||
|
export default zeitFetch()
|
@ -23,8 +23,8 @@
|
|||||||
"@components/*": ["components/*"],
|
"@components/*": ["components/*"],
|
||||||
"@commerce": ["../packages/commerce/src"],
|
"@commerce": ["../packages/commerce/src"],
|
||||||
"@commerce/*": ["../packages/commerce/src/*"],
|
"@commerce/*": ["../packages/commerce/src/*"],
|
||||||
"@framework": ["../packages/sylius/src"],
|
"@framework": ["../packages/local/src"],
|
||||||
"@framework/*": ["../packages/sylius/src/*"]
|
"@framework/*": ["../packages/local/src/*"]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"include": ["next-env.d.ts", "**/*.d.ts", "**/*.ts", "**/*.tsx", "**/*.js"],
|
"include": ["next-env.d.ts", "**/*.d.ts", "**/*.ts", "**/*.tsx", "**/*.js"],
|
||||||
|
Loading…
x
Reference in New Issue
Block a user