All files / src/api/core/refresh index.ts

54.05% Statements 20/37
100% Branches 0/0
100% Functions 0/0
54.05% Lines 20/37

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 381x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                           1x 1x 1x     1x     1x 1x 1x  
import axios from 'axios';
 
import { CommonErrorResponse } from '@/types/service/common';
 
import { createApiHelper } from '../lib/apiHelper';
 
const refreshInstance = axios.create({
  baseURL: process.env.NEXT_PUBLIC_API_BASE_URL,
  timeout: 20000,
  withCredentials: true,
});
 
refreshInstance.interceptors.request.use(async (config) => {
  const isServer = typeof window === 'undefined';

  if (isServer) {
    const { cookies } = await import('next/headers');
    const cookieStore = await cookies();
    const refreshToken = cookieStore.get('refreshToken')?.value;
    if (refreshToken) {
      config.headers.Cookie = `refreshToken=${refreshToken}`;
    }
  }

  return config;
});
 
refreshInstance.interceptors.response.use(
  (response) => {
    return response;
  },
  async (error) => {
    throw new CommonErrorResponse(error.response?.data);
  },
);
 
export const refreshAPI = createApiHelper(refreshInstance);