-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: update index.server.ts with bearer and order tokens #259
Draft
letelete
wants to merge
1
commit into
feat/locale-and-currency-sdk-update
Choose a base branch
from
feat/tokens
base: feat/locale-and-currency-sdk-update
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<ApiContext> => { | ||
const [orderToken, bearerToken] = await Promise.all([ | ||
getCurrentCartToken(context.config), | ||
getCurrentBearerToken(context) | ||
]); | ||
return { | ||
...context, | ||
client: context.client | ||
.withBearerToken(bearerToken) | ||
.withOrderToken(orderToken?.orderToken) | ||
}; | ||
}; | ||
|
||
const withEndpointMiddleware = <RESPONSE = any>( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This has been created in order to deal with async bearer token refreshment, which is not possible inside of |
||
endpointFunction: (...params: any) => Promise<RESPONSE> | ||
) => { | ||
return async ( | ||
endpointContext: ApiContext, | ||
...args: any | ||
): Promise<RESPONSE> => { | ||
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<any, any>({ | ||
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] | ||
}); | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rafalcymerys
Apparently, this won't work, because of the Spree logic related to receiving order/bearer tokens in particular responses.
Another alternative to this solution would be to use getCurrentBearerOrCartToken method to maintain a priority of passing bearer and only bearer if it's available. That would lead to passing order tokens if the bearer is not available.
This, however, leads to another problem with endpoints that must only attach the order token. With such functions, if the bearer token is available, these won't receive the order token. Additionally, some functions require both order and bearer tokens.
A possible approach is to avoid creating middleware for auth tokens and let endpoint functions decide, which token should be passed with the SDK call.
This is achievable with a new fluent interface approach available from spree/spree-api-v2-js-sdk#344.
Approach 1 - Chainable
withOrderToken
This requires refactoring SDK in order to enforce user using chained methods
Approach 2 - Stick to SDK design and avoid chaining bearer and order tokens
This is the current SDK behavior.
As far as managing locale/currency from one place makes sense (as done in #258), seems like refactoring SDK to handle a fluent interface approach in terms of handling auth tokens may be too problematic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if we add fluent interface for order/bearer token to the SDK an option (I think it may still be of some use on the frontend), but we keep the current implementation in VSF?