Skip to content
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
wants to merge 1 commit into
base: feat/locale-and-currency-sdk-update
Choose a base branch
from

Conversation

letelete
Copy link
Collaborator

Description

Related Issue

Motivation and Context

How Has This Been Tested?

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@@ -92,55 +95,88 @@ const tokenExtension: ApiClientExtension = {
}
};

const injectClientTokens = async (context: ApiContext): Promise<ApiContext> => {
const [orderToken, bearerToken] = await Promise.all([
Copy link
Collaborator Author

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

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.

Copy link
Collaborator

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>(
Copy link
Collaborator Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants