Skip to content

Vercel Commerce GetAddressesHook fetcherInput difference #130

Description

@omerfe

Hi I'm using vercel's commerce and BC as a provider, having problems with getting the customer addresses via useAddresses hook atm. In vercel's version fetcherInput was cartId (but optional), but in here it is customerId. And in the API docs v3 doesn't require neither of them, but v2 requires customerId. So I'm kinda confused on which to use...
Here is the vercel's GetAddressesHook at address.ts:

export type GetAddressesHook<
  T extends CustomerAddressTypes = CustomerAddressTypes
> = {
  data: T['address'][] | null
  input: {}
  fetcherInput: { cartId?: string }
  swrState: { isEmpty: boolean }
}

Here is my implementation:

export const handler: SWRHook<GetAddressesHook> = {
  fetchOptions: {
    url: '/api/customer/address',
    method: 'GET',
  },
  async fetcher({ input: { cartId }, options, fetch }) {
    if (!cartId) return null
    const url = new URL(options?.url!, 'http://a')
    return fetch({
      url: url.pathname,
      method: options.method,
    })
  },
  useHook:
    ({ useData }) =>
    (input) => {
      const { data: cart } = useCart()
      const response = useData({
        swrOptions: {
          revalidateOnFocus: false,
          ...input?.swrOptions,
        },
        input: [['cartId', cart?.id]],
      })

      return useMemo(
        () =>
          Object.create(response, {
            isEmpty: {
              get() {
                return response.data ?? null
              },
              enumerable: true,
            },
          }),
        [response]
      )
    },
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions