-
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
base: feat/locale-and-currency-sdk-update
Are you sure you want to change the base?
Conversation
@@ -92,55 +95,88 @@ const tokenExtension: ApiClientExtension = { | |||
} | |||
}; | |||
|
|||
const injectClientTokens = async (context: ApiContext): Promise<ApiContext> => { | |||
const [orderToken, bearerToken] = await Promise.all([ |
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.
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
import { IOrder } from '@spree/storefront-api-v2-sdk/types/interfaces/Order';
import { ApiContext, GetChangeCartParams } from '../../types';
import getCurrentCartToken from '../authentication/getCurrentCartToken';
export default async function changeCurrency(
{ client, config }: ApiContext,
{ newCurrency }: GetChangeCartParams
): Promise<IOrder> {
const token = await getCurrentCartToken(config);
const response = await client.withOrderToken(token.orderToken).cart.changeCurrency({
new_currency: newCurrency
});
if (response.isSuccess()) {
return response.success();
} else {
console.log(response.fail());
throw response.fail();
}
}
Approach 2 - Stick to SDK design and avoid chaining bearer and order tokens
This is the current SDK behavior.
import { IOrder } from '@spree/storefront-api-v2-sdk/types/interfaces/Order';
import { ApiContext, GetChangeCartParams } from '../../types';
import getCurrentCartToken from '../authentication/getCurrentCartToken';
export default async function changeCurrency(
{ client, config }: ApiContext,
{ newCurrency }: GetChangeCartParams
): Promise<IOrder> {
const token = await getCurrentCartToken(config);
const response = await client.cart.changeCurrency({
order_token: token.orderToken,
new_currency: newCurrency
});
if (response.isSuccess()) {
return response.success();
} else {
console.log(response.fail());
throw response.fail();
}
}
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?
}; | ||
}; | ||
|
||
const withEndpointMiddleware = <RESPONSE = any>( |
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.
This has been created in order to deal with async bearer token refreshment, which is not possible inside of onCreate
or extension methods of the API client.
Description
Related Issue
Motivation and Context
How Has This Been Tested?
Screenshots (if appropriate):
Types of changes
Checklist: