Repository: medusajs/medusa
File: packages/core/core-flows/src/cart/workflows/add-shipping-method-to-cart.ts (TypeScript)

Code before:
      list: false,
      throw_if_key_not_found: true,
    })

    validateCartStep({ cart })

    const validate = createHook("validate", {
      input,
      cart,
    })

    const optionIds = transform({ input }, (data) => {
      return (data.input.options ?? []).map((i) => i.id)
    })

    const shippingOptions =
      listShippingOptionsForCartWithPricingWorkflow.runAsStep({
        input: {
          options: input.options,
          cart_id: cart.id,
          is_return: false,
          additional_data: input.additional_data,
        },
      })

    validateCartShippingOptionsStep({
      option_ids: optionIds,
      shippingOptions,
    })

    validateCartShippingOptionsPriceStep({ shippingOptions })

    const validateShippingMethodsDataInput = transform(
      { input, shippingOptions, cart },
      ({ input, shippingOptions, cart }) => {
        return input.options.map((inputOption) => {
          const shippingOption = shippingOptions.find(
            (so) => so.id === inputOption.id
          )

          return {
            id: inputOption.id,
            provider_id: shippingOption?.provider_id,
            option_data: shippingOption?.data ?? {},
            method_data: inputOption.data ?? {},
            context: {
              ...cart,
              from_location: shippingOption?.stock_location ?? {},
            },
          }
        })

Review comment (suggestion):
Moved the validation to after in order to reuse the already fetched shipping options. The only thing the validation does is checking that the shipping options we want to add to the cart can actually be queried (they fit the defined rules so we don't end up adding a shipping option to the cart that we shouldn't). I don't see why the validation could not happen after the list step

Code after:
      list: false,
      throw_if_key_not_found: true,
    })

    validateCartStep({ cart })

    const validate = createHook("validate", {
      input,
      cart,
    })

    const optionIds = transform({ input }, (data) => {
      return (data.input.options ?? []).map((i) => i.id)
    })

    const shippingOptions =
      listShippingOptionsForCartWithPricingWorkflow.runAsStep({
        input: {
          options: input.options,
          cart_id: cart.id,
          is_return: false,
          additional_data: input.additional_data,
        },
      })

    validateCartShippingOptionsStep({
      option_ids: optionIds,
      prefetched_shipping_options: shippingOptions,
    })

    validateCartShippingOptionsPriceStep({ shippingOptions })

    const validateShippingMethodsDataInput = transform(
      { input, shippingOptions, cart },
      ({ input, shippingOptions, cart }) => {
        return input.options.map((inputOption) => {
          const shippingOption = shippingOptions.find(
            (so) => so.id === inputOption.id
          )

          return {
            id: inputOption.id,
            provider_id: shippingOption?.provider_id,
            option_data: shippingOption?.data ?? {},
            method_data: inputOption.data ?? {},
            context: {
              ...cart,
              from_location: shippingOption?.stock_location ?? {},
            },
          }
        })