Untitled

mail@pastecode.io avatar
unknown
plain_text
5 months ago
1.1 kB
2
Indexable
export const responseInterceptor = async (instance) => {
  try {
    const token = await AsyncStorage.getItem('access_token')
    const withPing = ['/klk/register', '/klk/activate/profile','/klk/validate-password']
    instance?.interceptors?.request?.use(
      async (res) => {
        const api = create()
        if (withPing?.includes(res?.url)) {
          const getPing = await api.getPing()
          const newRes = {...res, headers: {...res.headers, "x-request-id": getPing?.headers?.["x-request-id"]}}
          return newRes
        } else {
          return res
        }
      },
      (err) => {
        console.log('err abc', err)
        return err
      }
    )
    instance?.interceptors?.response?.use(
      async (res) => {
        if (!isEmpty(token) &&
          res?.data?.error === true &&
          res?.data?.status === 403 &&
          includes(res?.config?.url, '/klk/summarize-profile')) {
          return forceLogout()
        }
        return res
      },
      (err) => {
        return err
      }
    )
  } catch (error) {
  }
}
Leave a Comment