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

@@ -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 +1,3 @@
export default function noopApi(...args: any[]): void {}
export default function getCheckout(..._args: any[]) {
return Promise.resolve({ data: null })
}

View File

@@ -3,16 +3,15 @@ import checkoutEndpoint from '@vercel/commerce/api/endpoints/checkout'
import type { CheckoutSchema } from '@vercel/commerce/types/checkout'
import type { CommercejsAPI } from '../..'
import submitCheckout from './submit-checkout'
import getCheckout from './get-checkout'
import submitCheckout from './submit-checkout'
export type CheckoutAPI = GetAPISchema<CommercejsAPI, CheckoutSchema>
export type CheckoutEndpoint = CheckoutAPI['endpoint']
export const handlers: CheckoutEndpoint['handlers'] = {
submitCheckout,
getCheckout,
submitCheckout,
}
const checkoutApi = createEndpoint<CheckoutAPI>({

View File

@@ -5,7 +5,6 @@ import sdkFetcherFunction from '../../utils/sdk-fetch'
import { normalizeTestCheckout } from '../../../utils/normalize-checkout'
const submitCheckout: CheckoutEndpoint['handlers']['submitCheckout'] = async ({
res,
body: { item, cartId },
config: { sdkFetch },
}) => {
@@ -38,7 +37,7 @@ const submitCheckout: CheckoutEndpoint['handlers']['submitCheckout'] = async ({
// Capture the order
await sdkFetcher('checkout', 'capture', checkoutToken, checkoutData)
res.status(200).json({ data: null, errors: [] })
return { data: null }
}
export default submitCheckout

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,15 @@
import type { CommercejsAPI } from '..'
import createEndpoints from '@vercel/commerce/api/endpoints'
import login from './login'
import checkout from './checkout'
const endpoints = {
login,
checkout,
}
export default function commercejsAPI(commerce: CommercejsAPI) {
return createEndpoints(commerce, endpoints)
}

View File

@@ -1,32 +1,33 @@
import type { LoginEndpoint } from '.'
import { serialize } from 'cookie'
import sdkFetcherFunction from '../../utils/sdk-fetch'
import { getDeploymentUrl } from '../../../utils/get-deployment-url'
import type { LoginEndpoint } from '.'
const login: LoginEndpoint['handlers']['login'] = async ({
req,
res,
config: { sdkFetch, customerCookie },
}) => {
const sdkFetcher: typeof sdkFetcherFunction = sdkFetch
const redirectUrl = getDeploymentUrl()
try {
const loginToken = req.query?.token as string
if (!loginToken) {
res.redirect(redirectUrl)
}
const { jwt } = await sdkFetcher('customer', 'getToken', loginToken, false)
res.setHeader(
'Set-Cookie',
serialize(customerCookie, jwt, {
const { searchParams } = new URL(req.url)
const loginToken = searchParams.get('token')
if (!loginToken) {
return { redirectTo: redirectUrl }
}
const { jwt } = await sdkFetcher('customer', 'getToken', loginToken, false)
return {
headers: {
'Set-Cookie': serialize(customerCookie, jwt, {
secure: process.env.NODE_ENV === 'production',
maxAge: 60 * 60 * 24,
path: '/',
})
)
res.redirect(redirectUrl)
} catch {
res.redirect(redirectUrl)
}),
},
}
}

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

@@ -11,7 +11,7 @@ export default useSubmitCheckout as UseSubmitCheckout<typeof handler>
export const handler: MutationHook<SubmitCheckoutHook> = {
fetchOptions: {
url: '/api/checkout',
url: '/api/commerce/checkout',
method: 'POST',
},
async fetcher({ input: item, options, fetch }) {

View File

@@ -1,11 +1,16 @@
import type { SWRHook } from '@vercel/commerce/utils/types'
import type { CustomerHook } from '@vercel/commerce/types/customer'
import Cookies from 'js-cookie'
import { decode } from 'jsonwebtoken'
import { SWRHook } from '@vercel/commerce/utils/types'
import { decode, type JwtPayload } from 'jsonwebtoken'
import useCustomer, {
UseCustomer,
type UseCustomer,
} from '@vercel/commerce/customer/use-customer'
import { CUSTOMER_COOKIE, API_URL } from '../constants'
import type { CustomerHook } from '@vercel/commerce/types/customer'
type JwtData = JwtPayload & {
cid: string
}
export default useCustomer as UseCustomer<typeof handler>
export const handler: SWRHook<CustomerHook> = {
@@ -20,7 +25,8 @@ export const handler: SWRHook<CustomerHook> = {
return null
}
const decodedToken = decode(token) as { cid: string }
const decodedToken = decode(token) as JwtData
const customer = await fetch<any>({
query: options.query,
method: options.method,

View File

@@ -4,8 +4,8 @@ const getFilterVariables = ({
search,
categoryId,
}: {
search?: string
categoryId?: string | number
search?: string | null
categoryId?: string | number | null
}) => {
let filterVariables: { [key: string]: any } = {}
if (search) {
@@ -17,7 +17,7 @@ const getFilterVariables = ({
return filterVariables
}
const getSortVariables = ({ sort }: { sort?: string }) => {
const getSortVariables = ({ sort }: { sort?: string | null }) => {
let sortVariables: { [key: string]: any } = {}
switch (sort) {
case 'trending-desc':