export async function unwrapResponse<T>(response: Response): Promise<T> {
  const payload = await response.json();

  return unwrapPayload(payload) as T;
}

function unwrapPayload(value: unknown): unknown {
  let current = value;

  while (hasDataWrapper(current)) {
    current = current.data;
  }

  return current;
}

function hasDataWrapper(value: unknown): value is { data: unknown } {
  return (
    value !== null &&
    typeof value === 'object' &&
    'data' in value
  );
}
