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

@@ -49,10 +49,8 @@
},
"dependencies": {
"@vercel/commerce": "workspace:*",
"@vercel/fetch": "^6.2.0",
"js-cookie": "^3.0.1",
"lodash.debounce": "^4.0.8",
"node-fetch": "^2.6.7",
"swell-js": "^4.0.0-next.0"
},
"peerDependencies": {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -5,16 +5,14 @@ import checkoutEndpoint from '@vercel/commerce/api/endpoints/checkout'
const getCheckout: CheckoutEndpoint['handlers']['getCheckout'] = async ({
req,
res,
config,
}) => {
const { cookies } = req
const checkoutUrl = cookies[SWELL_CHECKOUT_URL_COOKIE]
const checkoutUrl = cookies.get(SWELL_CHECKOUT_URL_COOKIE)
if (checkoutUrl) {
res.redirect(checkoutUrl)
return { redirectTo: checkoutUrl }
} else {
res.redirect('/cart')
return { redirectTo: '/cart' }
}
}
export const handlers: CheckoutEndpoint['handlers'] = { 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,12 @@
import type { Provider, SwellAPI } from '..'
import createEndpoints from '@vercel/commerce/api/endpoints'
import checkout from './checkout'
const endpoints = {
checkout,
}
export default function handler(commerce: SwellAPI) {
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

@@ -46,6 +46,8 @@ export const provider = { config, operations }
export type Provider = typeof provider
export type SwellAPI<P extends Provider = Provider> = CommerceAPI<P>
export function getCommerceApi<P extends Provider>(
customProvider: P = provider as any
): CommerceAPI<P> {

View File

@@ -1,9 +1,9 @@
import type { ServerResponse } from 'http'
import type {
OperationContext,
OperationOptions,
} from '@vercel/commerce/api/operations'
import type { LoginOperation } from '@vercel/commerce/types/login'
import { Provider, SwellConfig } from '..'
export default function loginOperation({
@@ -12,14 +12,14 @@ export default function loginOperation({
async function login<T extends LoginOperation>(opts: {
variables: T['variables']
config?: Partial<SwellConfig>
res: ServerResponse
res: Response
}): Promise<T['data']>
async function login<T extends LoginOperation>(
opts: {
variables: T['variables']
config?: Partial<SwellConfig>
res: ServerResponse
res: Response
} & OperationOptions
): Promise<T['data']>
@@ -30,7 +30,7 @@ export default function loginOperation({
}: {
query?: string
variables: T['variables']
res: ServerResponse
res: Response
config?: Partial<SwellConfig>
}): Promise<T['data']> {
const config = commerce.getConfig(cfg)

View File

@@ -1,2 +0,0 @@
import zeitFetch from '@vercel/fetch'
export default zeitFetch()

View File

@@ -1,28 +0,0 @@
import type { NextApiRequest, NextApiResponse } from 'next'
export default function isAllowedMethod(
req: NextApiRequest,
res: NextApiResponse,
allowedMethods: string[]
) {
const methods = allowedMethods.includes('OPTIONS')
? allowedMethods
: [...allowedMethods, 'OPTIONS']
if (!req.method || !methods.includes(req.method)) {
res.status(405)
res.setHeader('Allow', methods.join(', '))
res.end()
return false
}
if (req.method === 'OPTIONS') {
res.status(200)
res.setHeader('Allow', methods.join(', '))
res.setHeader('Content-Length', '0')
res.end()
return false
}
return true
}