All files / src createInstance.ts

100% Statements 19/19
100% Branches 3/3
100% Functions 2/2
100% Lines 19/19

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 321x 1x 1x   1x             1x 12x 12x 12x   12x 60x 60x 12x   12x 12x   1x 12x   12x 12x   1x  
/* eslint-disable @typescript-eslint/no-explicit-any */
import { METHOD_WITH_BODY, METHOD_WITHOUT_BODY } from './constant';
import EasyFetch from './EasyFetch';
import type { EasyFetchWithAPIMethodsType } from './types/method.type';
import createPrototypeAPIMethod from './utils/createPrototypeAPIMethod';
 
export interface EasyFetchDefaultConfig {
  baseUrl?: string | URL;
  headers?: HeadersInit;
}
 
const mergeEasyFetchWithAPIMethod = (
  defaultConfig?: EasyFetchDefaultConfig
) => {
  const instance = new EasyFetch(defaultConfig);
 
  [...METHOD_WITH_BODY, ...METHOD_WITHOUT_BODY].forEach((method) => {
    (EasyFetch.prototype as EasyFetchWithAPIMethodsType)[method] =
      createPrototypeAPIMethod(method);
  });
 
  return instance as EasyFetchWithAPIMethodsType;
};
 
const createInstance = (defaultConfig?: EasyFetchDefaultConfig) => {
  const instance = mergeEasyFetchWithAPIMethod(defaultConfig);
 
  return instance;
};
 
export const easyFetch = createInstance;