mirror of
https://github.com/vercel/commerce.git
synced 2025-05-18 07:26:59 +00:00
fix: fix type errors
This commit is contained in:
parent
59d25e3492
commit
217a8f34c0
@ -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
|
@ -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<CommerceAPI, CheckoutSchema>
|
||||
export type CheckoutEndpoint = CheckoutAPI['endpoint']
|
||||
|
@ -0,0 +1 @@
|
||||
export default function noopApi(...args: any[]): void {}
|
1
framework/commercelayer/api/endpoints/customer/card.ts
Normal file
1
framework/commercelayer/api/endpoints/customer/card.ts
Normal file
@ -0,0 +1 @@
|
||||
export default function noopApi(...args: any[]): void {}
|
@ -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<any>
|
||||
>['endpoint']['handler'] = async (ctx) => {
|
||||
const { req, res, handlers } = ctx
|
||||
if (
|
||||
!isAllowedOperation(req, res, {
|
||||
POST: handlers['login'],
|
||||
export type LoginAPI = GetAPISchema<CommercelayerAPI, LoginSchema>
|
||||
|
||||
export type LoginEndpoint = LoginAPI['endpoint']
|
||||
|
||||
export const handlers: LoginEndpoint['handlers'] = { login }
|
||||
|
||||
const loginApi = createEndpoint<LoginAPI>({
|
||||
handler: loginEndpoint,
|
||||
handlers,
|
||||
})
|
||||
) {
|
||||
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
|
||||
export default loginApi;
|
33
framework/commercelayer/api/endpoints/login/login.ts
Normal file
33
framework/commercelayer/api/endpoints/login/login.ts
Normal file
@ -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<any>
|
||||
>['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
|
@ -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,
|
||||
|
@ -33,7 +33,7 @@ export const handler: MutationHook<any> = {
|
||||
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) {
|
||||
|
@ -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<string | undefined>('')
|
||||
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)
|
||||
|
@ -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({
|
||||
|
Loading…
x
Reference in New Issue
Block a user