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

@@ -50,8 +50,6 @@
},
"dependencies": {
"@vercel/commerce": "workspace:*",
"@vercel/fetch": "^6.2.0",
"node-fetch": "^2.6.7",
"js-cookie": "^3.0.1",
"lodash.debounce": "^4.0.8"
},

View File

@@ -1 +0,0 @@
export default function (_commerce: any) {}

View File

@@ -1,26 +1,14 @@
import { ProductsEndpoint } from '.'
const SORT: { [key: string]: string | undefined } = {
latest: 'id',
trending: 'total_sold',
price: 'price',
}
const LIMIT = 12
// Return current cart info
const getProducts: ProductsEndpoint['handlers']['getProducts'] = async ({
res,
body: { search, categoryId, brandId, sort },
config,
commerce,
}) => {
res.status(200).json({
data: {
products: [],
found: false,
},
})
const { products } = await commerce.getAllProducts()
const found = !!products.length
return { data: { products, found } }
}
export default getProducts

View File

@@ -1,8 +1,9 @@
import { GetAPISchema, createEndpoint } from '@vercel/commerce/api'
import productsEndpoint from '@vercel/commerce/api/endpoints/catalog/products'
import { type GetAPISchema, createEndpoint } from '@vercel/commerce/api'
import type { ProductsSchema } from '@vercel/commerce/types/product'
import type { ShopifyAPI } from '../../..'
import productsEndpoint from '@vercel/commerce/api/endpoints/catalog/products'
import getProducts from './get-products'
export type ProductsAPI = GetAPISchema<ShopifyAPI, ProductsSchema>

View File

@@ -8,19 +8,18 @@ import type { CheckoutEndpoint } from '.'
const getCheckout: CheckoutEndpoint['handlers']['getCheckout'] = async ({
req,
res,
config,
}) => {
const { cookies } = req
const checkoutUrl = cookies[SHOPIFY_CHECKOUT_URL_COOKIE]
const customerCookie = cookies[SHOPIFY_CUSTOMER_TOKEN_COOKIE]
const checkoutUrl = cookies.get(SHOPIFY_CHECKOUT_URL_COOKIE)
const customerCookie = cookies.get(SHOPIFY_CUSTOMER_TOKEN_COOKIE)
if (customerCookie) {
try {
await config.fetch(associateCustomerWithCheckoutMutation, {
variables: {
checkoutId: cookies[SHOPIFY_CHECKOUT_ID_COOKIE],
customerAccessToken: cookies[SHOPIFY_CUSTOMER_TOKEN_COOKIE],
checkoutId: cookies.get(SHOPIFY_CHECKOUT_ID_COOKIE),
customerAccessToken: cookies.get(SHOPIFY_CUSTOMER_TOKEN_COOKIE),
},
})
} catch (error) {
@@ -28,11 +27,7 @@ const getCheckout: CheckoutEndpoint['handlers']['getCheckout'] = async ({
}
}
if (checkoutUrl) {
res.redirect(checkoutUrl)
} else {
res.redirect('/cart')
}
return { redirectTo: checkoutUrl ?? '/cart' }
}
export default getCheckout

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,14 @@
import type { Provider, ShopifyAPI } from '..'
import createEndpoints from '@vercel/commerce/api/endpoints'
import checkout from './checkout'
import products from './catalog/products'
const endpoints = {
checkout,
'catalog/products': products,
}
export default function shopifyAPI(commerce: ShopifyAPI) {
return createEndpoints<Provider>(commerce, endpoints)
}

View File

@@ -1 +0,0 @@
export default function (_commerce: any) {}

View File

@@ -1 +0,0 @@
export default function (_commerce: any) {}

View File

@@ -1 +0,0 @@
export default function (_commerce: any) {}

View File

@@ -1 +0,0 @@
export default function (_commerce: any) {}

View File

@@ -51,9 +51,7 @@ export default function getAllPagesOperation({
},
{
...(locale && {
headers: {
'Accept-Language': locale,
},
'Accept-Language': locale,
}),
}
)

View File

@@ -49,9 +49,7 @@ export default function getAllProductsOperation({
{ variables },
{
...(locale && {
headers: {
'Accept-Language': locale,
},
'Accept-Language': locale,
}),
}
)

View File

@@ -50,9 +50,7 @@ export default function getPageOperation({
},
{
...(locale && {
headers: {
'Accept-Language': locale,
},
'Accept-Language': locale,
}),
}
)

View File

@@ -48,9 +48,7 @@ export default function getProductOperation({
},
{
...(locale && {
headers: {
'Accept-Language': locale,
},
'Accept-Language': locale,
}),
}
)

View File

@@ -1,4 +1,3 @@
import type { ServerResponse } from 'http'
import type { OperationContext } from '@vercel/commerce/api/operations'
import type { LoginOperation } from '@vercel/commerce/types/login'
import type { ShopifyConfig, Provider } from '..'
@@ -19,7 +18,7 @@ export default function loginOperation({
}: {
query?: string
variables: T['variables']
res: ServerResponse
res: Response
config?: ShopifyConfig
}): Promise<T['data']> {
config = commerce.getConfig(config)

View File

@@ -1,5 +1,4 @@
import type { GraphQLFetcher } from '@vercel/commerce/api'
import fetch from './fetch'
import { API_URL, API_TOKEN } from '../../const'
import { getError } from '../../utils/handle-fetch-response'
@@ -7,15 +6,14 @@ import { getError } from '../../utils/handle-fetch-response'
const fetchGraphqlApi: GraphQLFetcher = async (
query: string,
{ variables } = {},
fetchOptions
headers?: HeadersInit
) => {
try {
const res = await fetch(API_URL, {
...fetchOptions,
method: 'POST',
headers: {
'X-Shopify-Storefront-Access-Token': API_TOKEN!,
...fetchOptions?.headers,
...headers,
'Content-Type': 'application/json',
},
body: JSON.stringify({

View File

@@ -17,9 +17,7 @@ const getCategories = async ({
},
{
...(locale && {
headers: {
'Accept-Language': locale,
},
'Accept-Language': locale,
}),
}
)