From a29216e59df29cd4c9e5655773192e08000537a3 Mon Sep 17 00:00:00 2001 From: letelete Date: Mon, 22 Aug 2022 15:35:57 +0200 Subject: [PATCH] feat: inject auth tokens into client through endpoints middleware layer --- packages/api-client/src/index.server.ts | 130 +++++++++++++++--------- 1 file changed, 83 insertions(+), 47 deletions(-) diff --git a/packages/api-client/src/index.server.ts b/packages/api-client/src/index.server.ts index 4cd6d4cf..915de2fb 100644 --- a/packages/api-client/src/index.server.ts +++ b/packages/api-client/src/index.server.ts @@ -49,6 +49,9 @@ import updateAddress from './api/updateAddress'; import updateCurrentUser from './api/updateCurrentUser'; import updateItemQuantity from './api/updateItemQuantity'; import createAxiosFetcher from '@spree/storefront-api-v2-sdk/dist/server/createAxiosFetcher'; +import { ApiContext } from './types'; +import getCurrentBearerToken from './api/authentication/getCurrentBearerToken'; +import getCurrentCartToken from './api/authentication/getCurrentCartToken'; const defaultSettings = { backendUrl: 'https://demo.spreecommerce.org', @@ -92,55 +95,88 @@ const tokenExtension: ApiClientExtension = { } }; +const injectClientTokens = async (context: ApiContext): Promise => { + const [orderToken, bearerToken] = await Promise.all([ + getCurrentCartToken(context.config), + getCurrentBearerToken(context) + ]); + return { + ...context, + client: context.client + .withBearerToken(bearerToken) + .withOrderToken(orderToken?.orderToken) + }; +}; + +const withEndpointMiddleware = ( + endpointFunction: (...params: any) => Promise +) => { + return async ( + endpointContext: ApiContext, + ...args: any + ): Promise => { + const context = await injectClientTokens(endpointContext); + return await endpointFunction(context, ...args); + }; +}; + +const api = Object.entries({ + getProduct, + getProducts, + getCategory, + getCurrentUser, + updateCurrentUser, + logIn, + logOut, + isGuest, + changePassword, + registerUser, + addAddress, + getAddresses, + getAvailableCountries, + getCountryDetails, + updateAddress, + getCart, + addToCart, + updateItemQuantity, + removeFromCart, + clearCart, + applyCoupon, + removeCoupon, + saveCheckoutShippingAddress, + saveCheckoutBillingAddress, + getOrCreateCart, + getOrder, + getOrders, + saveGuestCheckoutEmail, + getShipments, + saveShippingMethod, + getPaymentMethods, + savePaymentMethod, + getPaymentConfirmationData, + handlePaymentConfirmationResponse, + makeOrder, + forgotPassword, + resetPassword, + getWishlist, + addToWishlist, + removeFromWishlist, + deleteWishlist, + changeCurrency, + deleteAddress, + getMenus, + getCMSPage +}).reduce( + (obj, [endpointKey, endpointArgs]) => ({ + ...obj, + [endpointKey]: withEndpointMiddleware(endpointArgs) + }), + {} +); + const { createApiClient } = apiClientFactory({ onCreate, - api: { - getProduct, - getProducts, - getCategory, - getCurrentUser, - updateCurrentUser, - logIn, - logOut, - isGuest, - changePassword, - registerUser, - addAddress, - getAddresses, - getAvailableCountries, - getCountryDetails, - updateAddress, - getCart, - addToCart, - updateItemQuantity, - removeFromCart, - clearCart, - applyCoupon, - removeCoupon, - saveCheckoutShippingAddress, - saveCheckoutBillingAddress, - getOrCreateCart, - getOrder, - getOrders, - saveGuestCheckoutEmail, - getShipments, - saveShippingMethod, - getPaymentMethods, - savePaymentMethod, - getPaymentConfirmationData, - handlePaymentConfirmationResponse, - makeOrder, - forgotPassword, - resetPassword, - getWishlist, - addToWishlist, - removeFromWishlist, - deleteWishlist, - changeCurrency, - deleteAddress, - getMenus, - getCMSPage - }, + api, extensions: [tokenExtension] });