mirror of
https://github.com/vercel/commerce.git
synced 2025-07-22 20:26:49 +00:00
Dynamic API routes (#836)
* Add dynamic API endpoints * Add missing dependency * Update api handlers * Updates * Fix build errors * Update package.json * Add checkout endpoint parser & update errors * Update tsconfig.json * Update cart.ts * Update parser * Update errors.ts * Update errors.ts * Move to Edge runtime * Revert to local * Fix switchable runtimes * Make nodejs default runtime * Update pnpm-lock.yaml * Update handlers * Fix build errors * Change headers
This commit is contained in:
@@ -48,9 +48,7 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@vercel/commerce": "workspace:*",
|
||||
"@vercel/fetch": "^6.2.0",
|
||||
"commerce-sdk": "^2.7.0",
|
||||
"node-fetch": "^2.6.7"
|
||||
"commerce-sdk": "^2.7.0"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"next": "^12",
|
||||
|
@@ -1 +0,0 @@
|
||||
export default function noopApi(...args: any[]): void {}
|
@@ -2,9 +2,7 @@ import { normalizeSearchProducts } from '../../../utils/normalise-product'
|
||||
import { ProductsEndpoint } from '.'
|
||||
|
||||
const getProducts: ProductsEndpoint['handlers']['getProducts'] = async ({
|
||||
req,
|
||||
res,
|
||||
body: { search, categoryId, brandId, sort },
|
||||
body: { search, categoryId },
|
||||
config,
|
||||
}) => {
|
||||
const { sdk } = config
|
||||
@@ -20,13 +18,15 @@ const getProducts: ProductsEndpoint['handlers']['getProducts'] = async ({
|
||||
limit: 20,
|
||||
},
|
||||
})
|
||||
|
||||
let products = []
|
||||
let found = false
|
||||
if (searchResults.total) {
|
||||
found = true
|
||||
products = normalizeSearchProducts(searchResults.hits) as any[]
|
||||
}
|
||||
res.status(200).json({ data: { products, found } })
|
||||
|
||||
return { data: { products, found } }
|
||||
}
|
||||
|
||||
export default getProducts
|
||||
|
@@ -1 +0,0 @@
|
||||
export default function noopApi(...args: any[]): void {}
|
@@ -1 +0,0 @@
|
||||
export default function noopApi(...args: any[]): void {}
|
@@ -1 +0,0 @@
|
||||
export default function noopApi(...args: any[]): void {}
|
@@ -1 +0,0 @@
|
||||
export default function noopApi(...args: any[]): void {}
|
13
packages/sfcc/src/api/endpoints/index.ts
Normal file
13
packages/sfcc/src/api/endpoints/index.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import type { Provider, SFCCProviderAPI } from '..'
|
||||
|
||||
import createEndpoints from '@vercel/commerce/api/endpoints'
|
||||
|
||||
import products from './catalog/products'
|
||||
|
||||
const endpoints = {
|
||||
'catalog/products': products,
|
||||
}
|
||||
|
||||
export default function sfccApi(commerce: SFCCProviderAPI) {
|
||||
return createEndpoints(commerce, endpoints)
|
||||
}
|
@@ -1 +0,0 @@
|
||||
export default function noopApi(...args: any[]): void {}
|
@@ -1 +0,0 @@
|
||||
export default function noopApi(...args: any[]): void {}
|
@@ -1 +0,0 @@
|
||||
export default function noopApi(...args: any[]): void {}
|
@@ -1 +0,0 @@
|
||||
export default function noopApi(...args: any[]): void {}
|
@@ -1,17 +1,15 @@
|
||||
import { FetcherError } from '@vercel/commerce/utils/errors'
|
||||
import type { GraphQLFetcher } from '@vercel/commerce/api'
|
||||
import type { SFCCConfig } from '../index'
|
||||
import fetch from './fetch'
|
||||
|
||||
const fetchGraphqlApi: (getConfig: () => SFCCConfig) => GraphQLFetcher =
|
||||
(getConfig) =>
|
||||
async (query: string, { variables, preview } = {}, fetchOptions) => {
|
||||
async (query: string, { variables, preview } = {}, headers?: HeadersInit) => {
|
||||
const config = getConfig()
|
||||
const res = await fetch(config.commerceUrl, {
|
||||
...fetchOptions,
|
||||
method: 'POST',
|
||||
headers: {
|
||||
...fetchOptions?.headers,
|
||||
...headers,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
|
@@ -5,11 +5,10 @@ export default useSearch as UseSearch<typeof handler>
|
||||
|
||||
export const handler: SWRHook<SearchProductsHook> = {
|
||||
fetchOptions: {
|
||||
url: '/api/catalog/products',
|
||||
url: '/api/commerce/catalog/products',
|
||||
method: 'GET',
|
||||
},
|
||||
fetcher({ input: { search, categoryId, brandId, sort }, options, fetch }) {
|
||||
console.log('search', search, categoryId, options)
|
||||
// Use a dummy base as we only care about the relative path
|
||||
const url = new URL(options.url!, 'http://a')
|
||||
|
||||
|
Reference in New Issue
Block a user