Repository: medusajs/medusa
File: packages/core/core-flows/src/cart/steps/validate-cart-shipping-options.ts (TypeScript)

Code before:
  /**
   * The cart to validate shipping options for.
   */
  cart?: CartDTO
  /**
   * The context to validate the shipping options.
   */
  shippingOptionsContext?: {
    /**
     * Validate whether the shipping options are enabled in the store.
     */
    enabled_in_store?: "true" | "false"
    /**
     * Validate whether the shipping options are used for returns.
     */
    is_return?: "true" | "false"
  }
  /**
   * The IDs of the shipping options to validate.
   */
  option_ids: string[]
  /**
   * Pre-fetched shipping options. If provided, validation will be done against these
   * instead of querying the database.
   */
  shippingOptions?: Array<{ id: string }>
}

export const validateCartShippingOptionsStepId =
  "validate-cart-shipping-options"
/**
 * This step validates shipping options to ensure they can be applied on a cart.
 * If not valid, the step throws an error.
 * 
 * @example
 * const data = validateCartShippingOptionsStep({
 *   // retrieve the details of the cart from another workflow
 *   // or in another step using the Cart Module's service
 *   cart,
 *   option_ids: ["so_123"],
 *   shippingOptionsContext: {}
 * })
 */
export const validateCartShippingOptionsStep = createStep(
  validateCartShippingOptionsStepId,
  async (data: ValidateCartShippingOptionsStepInput, { container }) => {
    const { option_ids: optionIds = [], cart, shippingOptionsContext, shippingOptions } = data

    if (!optionIds.length) {
      return new StepResponse(void 0)
    }

Review comment (suggestion):
To avoid breaking changes, I basically changed that step so that it can take pre-fetched shipping options. If they are defined, we skip the refetching of the shipping options (which is not ideal) and simply run the validation on the ones that we received in arguments

Code after:
  /**
   * The context to validate the shipping options.
   */
  shippingOptionsContext?: {
    /**
     * Validate whether the shipping options are enabled in the store.
     */
    enabled_in_store?: "true" | "false"
    /**
     * Validate whether the shipping options are used for returns.
     */
    is_return?: "true" | "false"
  }
  /**
   * The IDs of the shipping options to validate.
   */
  option_ids: string[]
  /**
   * Pre-fetched shipping options. If provided, validation will be done against these
   * instead of querying the database.
   */
  prefetched_shipping_options?: { id: string }[]
}

export const validateCartShippingOptionsStepId =
  "validate-cart-shipping-options"
/**
 * This step validates shipping options to ensure they can be applied on a cart.
 * If not valid, the step throws an error.
 * 
 * @example
 * const data = validateCartShippingOptionsStep({
 *   // retrieve the details of the cart from another workflow
 *   // or in another step using the Cart Module's service
 *   cart,
 *   option_ids: ["so_123"],
 *   shippingOptionsContext: {}
 * })
 */
export const validateCartShippingOptionsStep = createStep(
  validateCartShippingOptionsStepId,
  async (data: ValidateCartShippingOptionsStepInput, { container }) => {
    const { option_ids: optionIds = [], cart, shippingOptionsContext, prefetched_shipping_options: prefetchedShippingOptions } = data

    if (!optionIds.length) {
      return new StepResponse(void 0)
    }

    let validShippingOptionIds: string[]
    if (!prefetchedShippingOptions) {
      if (!cart || !shippingOptionsContext) {