All files / src/utils mergeRequestConfig.ts

89.74% Statements 35/39
83.33% Branches 5/6
100% Functions 2/2
89.74% Lines 35/39

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 521x       1x 4x 4x 4x 4x 4x 4x 4x   4x 4x 44x 44x 44x 4x 4x   4x 4x   1x 4x 4x     4x 4x 4x   4x 4x   4x 4x 4x 4x 4x 4x 4x           4x 4x    
import { METHOD_WITHOUT_BODY, REQUEST_INIT_KEYS } from '../constant';
import { EasyFetchRequestType } from '../types/easyFetch.type';
import { MethodWithoutBodyType } from '../types/method.type';
 
const createMergedRequestInit = async (
  request: Request,
  requestInit?: EasyFetchRequestType[1]
) => {
  const mergedRequestConfig = new Request(request, requestInit);
  const isGetOrDeleteMethod = METHOD_WITHOUT_BODY.includes(
    mergedRequestConfig.method.toLowerCase() as MethodWithoutBodyType
  );
 
  const newRequestInit = REQUEST_INIT_KEYS.reduce(
    (acc, cur) => ({
      ...acc,
      [cur]: mergedRequestConfig[cur],
    }),
    isGetOrDeleteMethod ? {} : { body: await mergedRequestConfig.arrayBuffer() }
  ) as NonNullable<EasyFetchRequestType[1]>;
 
  return newRequestInit;
};
 
const mergeRequestConfig = async (
  request: Request,
  requestInit?: EasyFetchRequestType[1]
): Promise<
  [Exclude<EasyFetchRequestType[0], URL>, EasyFetchRequestType[1]]
> => {
  const fetchURL = request.url;
  let requestConfig: EasyFetchRequestType[1];
 
  if (requestInit) {
    const { next, priority, window, ...rest } = requestInit;
 
    requestConfig = {
      ...(await createMergedRequestInit(request, rest)),
      priority,
      window,
      next,
    };
  } else {
    requestConfig = {
      ...(await createMergedRequestInit(request)),
    };
  }
 
  return [fetchURL, requestConfig];
};
export { mergeRequestConfig };