All files / src Interceptor.ts

100% Statements 31/31
100% Branches 8/8
100% Functions 6/6
100% Lines 31/31

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 52 53                  12x 12x 12x   12x 12x 12x 12x 12x 1x 1x   12x 1x 1x   12x 14x   14x   14x 1x 1x   14x 14x     12x 14x   14x   14x 1x 1x   14x 14x 12x   1x  
import {
  EasyFetchRequestType,
  EasyFetchResponse,
} from './types/easyFetch.type';
import type {
  InterceptorArgs,
  InterceptorCallbackType,
} from './types/interceptor.type';
 
class Interceptor {
  requestCbArr: InterceptorArgs<'request'>[];
  responseCbArr: InterceptorArgs<'response'>[];
 
  constructor() {
    this.requestCbArr = [];
    this.responseCbArr = [];
  }
  request: InterceptorCallbackType<'request'> = (onFulfilled, onRejected) => {
    this.requestCbArr.unshift([onFulfilled, onRejected]);
  };
 
  response: InterceptorCallbackType<'response'> = (onFulfilled, onRejected) => {
    this.responseCbArr.push([onFulfilled, onRejected]);
  };
 
  async flushRequestInterceptors(initVal: Promise<EasyFetchRequestType>) {
    const flushArr = this.requestCbArr;
 
    let promiseInit = initVal;
 
    for (let i = 0; i < flushArr.length; i++) {
      promiseInit = promiseInit.then(...flushArr[i]);
    }
 
    return promiseInit;
  }
 
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  async flushResPonseInterceptors(initVal: Promise<EasyFetchResponse<any>>) {
    const flushArr = this.responseCbArr;
 
    let promiseInit = initVal;
 
    for (let i = 0; i < flushArr.length; i++) {
      promiseInit = promiseInit.then(...flushArr[i]);
    }
 
    return promiseInit;
  }
}
 
export default Interceptor;