fix: fix type errors

This commit is contained in:
Bolaji Ayodeji 2022-01-05 02:37:53 +01:00 committed by Alessandro Casazza
parent 59d25e3492
commit 217a8f34c0
No known key found for this signature in database
GPG Key ID: 3AF41B06C6495D3D
10 changed files with 60 additions and 40 deletions

View File

@ -2,7 +2,7 @@ import type { CheckoutEndpoint } from '.'
import getCredentials from '../../utils/getCredentials' import getCredentials from '../../utils/getCredentials'
import { Order } from '@commercelayer/js-sdk' import { Order } from '@commercelayer/js-sdk'
const checkout: CheckoutEndpoint['handlers']['checkout'] = async ({ const getCheckout: CheckoutEndpoint['handlers']['getCheckout'] = async ({
req, req,
res, res,
}) => { }) => {
@ -33,4 +33,4 @@ const checkout: CheckoutEndpoint['handlers']['checkout'] = async ({
} }
} }
export default checkout export default getCheckout

View File

@ -1,9 +1,9 @@
import { GetAPISchema, createEndpoint, CommerceAPI } from '@commerce/api' import { GetAPISchema, createEndpoint, CommerceAPI } from '@commerce/api'
import checkoutEndpoint from '@commerce/api/endpoints/checkout' import checkoutEndpoint from '@commerce/api/endpoints/checkout'
import type { CheckoutSchema } from '@commerce/types/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 CheckoutAPI = GetAPISchema<CommerceAPI, CheckoutSchema>
export type CheckoutEndpoint = CheckoutAPI['endpoint'] export type CheckoutEndpoint = CheckoutAPI['endpoint']

View File

@ -0,0 +1 @@
export default function noopApi(...args: any[]): void {}

View File

@ -0,0 +1 @@
export default function noopApi(...args: any[]): void {}

View File

@ -1,33 +1,18 @@
import { GetAPISchema } from '@commerce/api' import { GetAPISchema, createEndpoint } from '@commerce/api'
import { CommerceAPIError } from '@commerce/api/utils/errors' import loginEndpoint from '@commerce/api/endpoints/login'
import isAllowedOperation from '@commerce/api/utils/is-allowed-operation' import type { LoginSchema } from '@commerce/types/login'
import { LoginSchema } from '@commerce/types/login' import type { CommercelayerAPI } from '../..'
import login from './login'
const loginEndpoint: GetAPISchema< export type LoginAPI = GetAPISchema<CommercelayerAPI, LoginSchema>
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 = export type LoginEndpoint = LoginAPI['endpoint']
error instanceof CommerceAPIError
? 'An unexpected error ocurred with the Commerce API'
: 'An unexpected error ocurred'
res.status(500).json({ data: null, errors: [{ message }] }) export const handlers: LoginEndpoint['handlers'] = { login }
}
}
export default loginEndpoint const loginApi = createEndpoint<LoginAPI>({
handler: loginEndpoint,
handlers,
})
export default loginApi;

View 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

View File

@ -25,7 +25,7 @@ export default function getAllProductsOperation({
clientId: process.env.NEXT_PUBLIC_COMMERCELAYER_CLIENT_ID as string, clientId: process.env.NEXT_PUBLIC_COMMERCELAYER_CLIENT_ID as string,
scope: process.env.NEXT_PUBLIC_COMMERCELAYER_MARKET_SCOPE as string, scope: process.env.NEXT_PUBLIC_COMMERCELAYER_MARKET_SCOPE as string,
}) })
if (credentials.accessToken) { if (credentials?.accessToken) {
const skus: string[] = [] const skus: string[] = []
const config = { const config = {
accessToken: credentials.accessToken, accessToken: credentials.accessToken,

View File

@ -33,7 +33,7 @@ export const handler: MutationHook<any> = {
setCookie('CL_CUSTOMER_TOKEN', token.accessToken, { setCookie('CL_CUSTOMER_TOKEN', token.accessToken, {
expires: token.expires, 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.`) alert(`User "${email}" has successfully been logged in.`)
return token return token
} catch (error) { } catch (error) {

View File

@ -3,7 +3,7 @@ import { getSalesChannelToken } from '@commercelayer/js-auth'
import { useEffect, useState } from 'react' import { useEffect, useState } from 'react'
export default function useToken() { export default function useToken() {
const [token, setToken] = useState('') const [token, setToken] = useState<string | undefined>('')
useEffect(() => { useEffect(() => {
const cookieToken = Cookies.get('CL_TOKEN') const cookieToken = Cookies.get('CL_TOKEN')
const getToken = async () => { const getToken = async () => {
@ -12,10 +12,10 @@ export default function useToken() {
clientId: process.env.NEXT_PUBLIC_COMMERCELAYER_CLIENT_ID as string, clientId: process.env.NEXT_PUBLIC_COMMERCELAYER_CLIENT_ID as string,
scope: process.env.NEXT_PUBLIC_COMMERCELAYER_MARKET_SCOPE as string, scope: process.env.NEXT_PUBLIC_COMMERCELAYER_MARKET_SCOPE as string,
}) })
Cookies.set('CL_TOKEN', credentials.accessToken, { Cookies.set('CL_TOKEN', credentials?.accessToken ?? '', {
expires: credentials.expires, expires: credentials?.expires,
}) })
setToken(credentials.accessToken) setToken(credentials?.accessToken)
} }
if (!cookieToken) getToken() if (!cookieToken) getToken()
else setToken(cookieToken) else setToken(cookieToken)

View File

@ -18,7 +18,7 @@ export const fetcher: Fetcher = async ({ url, method, variables, query }) => {
method, method,
headers: { headers: {
Accept: 'application/vnd.api+json', Accept: 'application/vnd.api+json',
Authorization: `Bearer ${token.accessToken}`, Authorization: `Bearer ${token?.accessToken}`,
'Content-Type': 'application/vnd.api+json', 'Content-Type': 'application/vnd.api+json',
}, },
body: JSON.stringify({ body: JSON.stringify({