mirror of
https://github.com/vercel/commerce.git
synced 2025-07-23 04:36: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:
@@ -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": {
|
||||
|
@@ -1 +0,0 @@
|
||||
export default function () {}
|
@@ -1 +0,0 @@
|
||||
export default function () {}
|
@@ -1 +0,0 @@
|
||||
export default function () {}
|
@@ -1 +0,0 @@
|
||||
export default function () {}
|
@@ -1 +0,0 @@
|
||||
export default function () {}
|
@@ -1 +0,0 @@
|
||||
export default function () {}
|
@@ -1 +0,0 @@
|
||||
export default function (_commerce: any) {}
|
@@ -1 +0,0 @@
|
||||
export default function (_commerce: any) {}
|
@@ -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 }
|
||||
|
@@ -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 {}
|
12
packages/swell/src/api/endpoints/index.ts
Normal file
12
packages/swell/src/api/endpoints/index.ts
Normal 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)
|
||||
}
|
@@ -1 +0,0 @@
|
||||
export default function (_commerce: any) {}
|
@@ -1 +0,0 @@
|
||||
export default function (_commerce: any) {}
|
@@ -1 +0,0 @@
|
||||
export default function (_commerce: any) {}
|
@@ -1 +0,0 @@
|
||||
export default function (_commerce: any) {}
|
@@ -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> {
|
||||
|
@@ -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)
|
||||
|
@@ -1,2 +0,0 @@
|
||||
import zeitFetch from '@vercel/fetch'
|
||||
export default zeitFetch()
|
@@ -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
|
||||
}
|
Reference in New Issue
Block a user