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:
Catalin Pinte
2022-10-30 20:41:21 +02:00
committed by GitHub
parent a5b367a747
commit c75b0fc001
316 changed files with 2482 additions and 2176 deletions

View File

@@ -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",

View File

@@ -1 +0,0 @@
export default function noopApi(...args: any[]): void {}

View File

@@ -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

View File

@@ -1 +0,0 @@
export default function noopApi(...args: any[]): void {}

View File

@@ -1 +0,0 @@
export default function noopApi(...args: any[]): void {}

View File

@@ -1 +0,0 @@
export default function noopApi(...args: any[]): void {}

View File

@@ -1 +0,0 @@
export default function noopApi(...args: any[]): void {}

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

View File

@@ -1 +0,0 @@
export default function noopApi(...args: any[]): void {}

View File

@@ -1 +0,0 @@
export default function noopApi(...args: any[]): void {}

View File

@@ -1 +0,0 @@
export default function noopApi(...args: any[]): void {}

View File

@@ -1 +0,0 @@
export default function noopApi(...args: any[]): void {}

View File

@@ -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({

View File

@@ -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')