From 4adba68c4c423f5f18a1f3b8cba6daf7f0d85265 Mon Sep 17 00:00:00 2001 From: Alessandro Casazza Date: Fri, 9 Jul 2021 20:27:37 +0200 Subject: [PATCH] chore: Fix types errors --- framework/commercelayer/api/index.ts | 15 +++---- .../commercelayer/api/utils/fetch-api.ts | 7 ++-- .../commercelayer/api/utils/get-token.ts | 42 ++++++++++--------- 3 files changed, 35 insertions(+), 29 deletions(-) diff --git a/framework/commercelayer/api/index.ts b/framework/commercelayer/api/index.ts index 6016ced2e..8b7a47d76 100644 --- a/framework/commercelayer/api/index.ts +++ b/framework/commercelayer/api/index.ts @@ -1,5 +1,5 @@ import type { CommerceAPI, CommerceAPIConfig } from '@commerce/api' -import type { RequestInit } from '@vercel/fetch' +import type { RequestInit, Response, Fetch } from '@vercel/fetch' import { getCommerceApi as commerceApi } from '@commerce/api' import createFetcher from './utils/fetch-api' @@ -12,15 +12,17 @@ import getAllProducts from './operations/get-all-products' import getProduct from './operations/get-product' import { getToken } from './utils/get-token' +import fetch from './utils/fetch' -export interface CommercelayerConfig extends CommerceAPIConfig { +export interface CommercelayerConfig extends Omit { apiClientId: string - apiFetch( + apiToken: string + apiFetch( query: string, endpoint: string, fetchOptions?: RequestInit, user?: UserCredentials - ): Promise + ): Promise<{ data: any; res: Response }> } export type UserCredentials = { @@ -51,22 +53,21 @@ if (!MARKET_SCOPE) { } export async function getAccessToken(user?: UserCredentials) { - const token = await getToken({ + return await getToken({ clientId: CLIENT_ID, endpoint: ENDPOINT, scope: MARKET_SCOPE, user, }) - return token } -export interface CommercelayerConfig extends CommerceAPIConfig {} const config: CommercelayerConfig = { commerceUrl: ENDPOINT, apiClientId: CLIENT_ID, cartCookie: '', customerCookie: '', cartCookieMaxAge: 2592000, + apiToken: '', apiFetch: createFetcher(() => getCommerceApi().getConfig()), } diff --git a/framework/commercelayer/api/utils/fetch-api.ts b/framework/commercelayer/api/utils/fetch-api.ts index b9ee4aa7c..85ca08c1b 100644 --- a/framework/commercelayer/api/utils/fetch-api.ts +++ b/framework/commercelayer/api/utils/fetch-api.ts @@ -3,7 +3,8 @@ import { FetcherError } from '@commerce/utils/errors' import { CommercelayerConfig, getAccessToken, UserCredentials } from '../index' import fetch from './fetch' -const fetchApi = (getConfig: () => CommercelayerConfig) => +const fetchApi = + (getConfig: () => CommercelayerConfig) => async ( query: string, endpoint: string, @@ -12,7 +13,7 @@ const fetchApi = (getConfig: () => CommercelayerConfig) => ) => { const config = getConfig() const getToken = await getAccessToken(user) - const token = getToken.accessToken + const token = getToken?.accessToken const res = await fetch(config.commerceUrl + endpoint, { ...fetchOptions, method: 'POST', @@ -22,7 +23,7 @@ const fetchApi = (getConfig: () => CommercelayerConfig) => Authorization: `Bearer ${token}`, }, body: JSON.stringify({ - query + query, }), }) diff --git a/framework/commercelayer/api/utils/get-token.ts b/framework/commercelayer/api/utils/get-token.ts index 04fc1b0dc..7ab29b71d 100644 --- a/framework/commercelayer/api/utils/get-token.ts +++ b/framework/commercelayer/api/utils/get-token.ts @@ -1,36 +1,40 @@ -import Cookies from "js-cookie"; -import { getSalesChannelToken } from "@commercelayer/js-auth"; +import Cookies from 'js-cookie' +import { getSalesChannelToken } from '@commercelayer/js-auth' type GetTokenObj = { - clientId?: string; - endpoint?: string; - scope?: string; - user?: any; -}; + clientId?: string + endpoint?: string + scope?: string + user?: any +} export async function getToken({ clientId, endpoint, - scope = "market:all", - user + scope = 'market:all', + user, }: GetTokenObj) { - let token = "" as any; - const getCookieToken = Cookies.get("clAccessToken"); + const getCookieToken = Cookies.get('clAccessToken') if (!getCookieToken && clientId && endpoint) { const auth = await getSalesChannelToken( { clientId, endpoint, - scope + scope, }, user - ); - token = auth?.accessToken; - Cookies.set("clAccessToken", auth?.accessToken as string, { + ) + Cookies.set('clAccessToken', auth?.accessToken as string, { // @ts-ignore - expires: auth?.expires - }); - return auth ? { accessToken: auth.accessToken, customerId: auth.data.owner_id, ...auth.data } : null + expires: auth?.expires, + }) + return auth + ? { + accessToken: auth.accessToken, + customerId: auth.data.owner_id, + ...auth.data, + } + : null } - return getCookieToken || ""; + return { accessToken: getCookieToken } }