export type DoFetchBody = BodyInit | Record<string, unknown>

export type DoFetch = (
  method: string,
  path: string,
  body?: DoFetchBody
) => Promise<Response>;

export type ConsumeStream = (
  response: Response,
) => Promise<void>;

export const processRequest = (body: DoFetchBody, requestHeaders: Record<string, string>) => {
  if (
    body instanceof FormData
    || body instanceof URLSearchParams
  ) {
    return { body };
  }

  return {
    body: JSON.stringify(body),
    headers: {
      ...requestHeaders,
      'Content-Type': 'application/json',
    },
  };
};
