diff --git a/framework/commercelayer/api/endpoints/checkout/checkout.ts b/framework/commercelayer/api/endpoints/checkout/get-checkout.ts similarity index 90% rename from framework/commercelayer/api/endpoints/checkout/checkout.ts rename to framework/commercelayer/api/endpoints/checkout/get-checkout.ts index 69e24a135..35cbc4e40 100644 --- a/framework/commercelayer/api/endpoints/checkout/checkout.ts +++ b/framework/commercelayer/api/endpoints/checkout/get-checkout.ts @@ -2,7 +2,7 @@ import type { CheckoutEndpoint } from '.' import getCredentials from '../../utils/getCredentials' import { Order } from '@commercelayer/js-sdk' -const checkout: CheckoutEndpoint['handlers']['checkout'] = async ({ +const getCheckout: CheckoutEndpoint['handlers']['getCheckout'] = async ({ req, res, }) => { @@ -33,4 +33,4 @@ const checkout: CheckoutEndpoint['handlers']['checkout'] = async ({ } } -export default checkout +export default getCheckout diff --git a/framework/commercelayer/api/endpoints/checkout/index.ts b/framework/commercelayer/api/endpoints/checkout/index.ts index 1edab2270..9d8d25f05 100644 --- a/framework/commercelayer/api/endpoints/checkout/index.ts +++ b/framework/commercelayer/api/endpoints/checkout/index.ts @@ -1,9 +1,9 @@ import { GetAPISchema, createEndpoint, CommerceAPI } from '@commerce/api' import checkoutEndpoint from '@commerce/api/endpoints/checkout' import type { CheckoutSchema } from '@commerce/types/checkout' -import checkout from './checkout' +import getCheckout from './get-checkout' -export const handlers: CheckoutEndpoint['handlers'] = { checkout } +export const handlers: CheckoutEndpoint['handlers'] = { getCheckout } export type CheckoutAPI = GetAPISchema export type CheckoutEndpoint = CheckoutAPI['endpoint'] diff --git a/framework/commercelayer/api/endpoints/customer/address.ts b/framework/commercelayer/api/endpoints/customer/address.ts new file mode 100644 index 000000000..df4c87991 --- /dev/null +++ b/framework/commercelayer/api/endpoints/customer/address.ts @@ -0,0 +1 @@ +export default function noopApi(...args: any[]): void {} \ No newline at end of file diff --git a/framework/commercelayer/api/endpoints/customer/card.ts b/framework/commercelayer/api/endpoints/customer/card.ts new file mode 100644 index 000000000..df4c87991 --- /dev/null +++ b/framework/commercelayer/api/endpoints/customer/card.ts @@ -0,0 +1 @@ +export default function noopApi(...args: any[]): void {} \ No newline at end of file diff --git a/framework/commercelayer/api/endpoints/login/index.ts b/framework/commercelayer/api/endpoints/login/index.ts index dd1cc643d..ad01b4d6a 100644 --- a/framework/commercelayer/api/endpoints/login/index.ts +++ b/framework/commercelayer/api/endpoints/login/index.ts @@ -1,33 +1,18 @@ -import { GetAPISchema } from '@commerce/api' -import { CommerceAPIError } from '@commerce/api/utils/errors' -import isAllowedOperation from '@commerce/api/utils/is-allowed-operation' -import { LoginSchema } from '@commerce/types/login' +import { GetAPISchema, createEndpoint } from '@commerce/api' +import loginEndpoint from '@commerce/api/endpoints/login' +import type { LoginSchema } from '@commerce/types/login' +import type { CommercelayerAPI } from '../..' +import login from './login' -const loginEndpoint: GetAPISchema< - any, - LoginSchema ->['endpoint']['handler'] = async (ctx) => { - const { req, res, handlers } = ctx - if ( - !isAllowedOperation(req, res, { - POST: handlers['login'], - }) - ) { - return - } - try { - const body = req.body ?? {} - return await handlers['login']({ ...ctx, body }) - } catch (error) { - console.error(error) +export type LoginAPI = GetAPISchema - const message = - error instanceof CommerceAPIError - ? 'An unexpected error ocurred with the Commerce API' - : 'An unexpected error ocurred' +export type LoginEndpoint = LoginAPI['endpoint'] - res.status(500).json({ data: null, errors: [{ message }] }) - } -} +export const handlers: LoginEndpoint['handlers'] = { login } -export default loginEndpoint +const loginApi = createEndpoint({ + handler: loginEndpoint, + handlers, +}) + +export default loginApi; \ No newline at end of file diff --git a/framework/commercelayer/api/endpoints/login/login.ts b/framework/commercelayer/api/endpoints/login/login.ts new file mode 100644 index 000000000..dd1cc643d --- /dev/null +++ b/framework/commercelayer/api/endpoints/login/login.ts @@ -0,0 +1,33 @@ +import { GetAPISchema } from '@commerce/api' +import { CommerceAPIError } from '@commerce/api/utils/errors' +import isAllowedOperation from '@commerce/api/utils/is-allowed-operation' +import { LoginSchema } from '@commerce/types/login' + +const loginEndpoint: GetAPISchema< + any, + LoginSchema +>['endpoint']['handler'] = async (ctx) => { + const { req, res, handlers } = ctx + if ( + !isAllowedOperation(req, res, { + POST: handlers['login'], + }) + ) { + return + } + try { + const body = req.body ?? {} + return await handlers['login']({ ...ctx, body }) + } catch (error) { + console.error(error) + + const message = + error instanceof CommerceAPIError + ? 'An unexpected error ocurred with the Commerce API' + : 'An unexpected error ocurred' + + res.status(500).json({ data: null, errors: [{ message }] }) + } +} + +export default loginEndpoint diff --git a/framework/commercelayer/api/operations/get-all-products.ts b/framework/commercelayer/api/operations/get-all-products.ts index 46d231dc3..8bf2e23d6 100644 --- a/framework/commercelayer/api/operations/get-all-products.ts +++ b/framework/commercelayer/api/operations/get-all-products.ts @@ -25,7 +25,7 @@ export default function getAllProductsOperation({ clientId: process.env.NEXT_PUBLIC_COMMERCELAYER_CLIENT_ID as string, scope: process.env.NEXT_PUBLIC_COMMERCELAYER_MARKET_SCOPE as string, }) - if (credentials.accessToken) { + if (credentials?.accessToken) { const skus: string[] = [] const config = { accessToken: credentials.accessToken, diff --git a/framework/commercelayer/auth/use-login.tsx b/framework/commercelayer/auth/use-login.tsx index 049190c21..d1935bbd8 100644 --- a/framework/commercelayer/auth/use-login.tsx +++ b/framework/commercelayer/auth/use-login.tsx @@ -33,7 +33,7 @@ export const handler: MutationHook = { setCookie('CL_CUSTOMER_TOKEN', token.accessToken, { expires: token.expires, }) - Cookies.set('CL_CUSTOMER_ID', token.data.owner_id as string) + Cookies.set('CL_CUSTOMER_ID', token?.data.owner_id as string) alert(`User "${email}" has successfully been logged in.`) return token } catch (error) { diff --git a/framework/commercelayer/auth/use-token.tsx b/framework/commercelayer/auth/use-token.tsx index 44aeef009..2c8f271dc 100644 --- a/framework/commercelayer/auth/use-token.tsx +++ b/framework/commercelayer/auth/use-token.tsx @@ -3,7 +3,7 @@ import { getSalesChannelToken } from '@commercelayer/js-auth' import { useEffect, useState } from 'react' export default function useToken() { - const [token, setToken] = useState('') + const [token, setToken] = useState('') useEffect(() => { const cookieToken = Cookies.get('CL_TOKEN') const getToken = async () => { @@ -12,10 +12,10 @@ export default function useToken() { clientId: process.env.NEXT_PUBLIC_COMMERCELAYER_CLIENT_ID as string, scope: process.env.NEXT_PUBLIC_COMMERCELAYER_MARKET_SCOPE as string, }) - Cookies.set('CL_TOKEN', credentials.accessToken, { - expires: credentials.expires, + Cookies.set('CL_TOKEN', credentials?.accessToken ?? '', { + expires: credentials?.expires, }) - setToken(credentials.accessToken) + setToken(credentials?.accessToken) } if (!cookieToken) getToken() else setToken(cookieToken) diff --git a/framework/commercelayer/fetcher.ts b/framework/commercelayer/fetcher.ts index 48755246d..daf8ae178 100644 --- a/framework/commercelayer/fetcher.ts +++ b/framework/commercelayer/fetcher.ts @@ -18,7 +18,7 @@ export const fetcher: Fetcher = async ({ url, method, variables, query }) => { method, headers: { Accept: 'application/vnd.api+json', - Authorization: `Bearer ${token.accessToken}`, + Authorization: `Bearer ${token?.accessToken}`, 'Content-Type': 'application/vnd.api+json', }, body: JSON.stringify({