Skip to content

Commit

Permalink
refactor unwrap conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
zaelgohary committed Jan 28, 2024
1 parent bcf2c40 commit b7bb5a2
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions client/src/clients/api/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,25 +87,8 @@ export abstract class ApiClientBase {
const refresh_token = localStorage.getItem("refresh_token")

// check if error indicate the token needs to be refreshed
if (access_token && refresh_token && !isValidToken(access_token)) {
if (isValidToken(refresh_token)) {
await ApiClientBase.$api.auth.refresh({ refresh: refresh_token })
} else {
ApiClientBase.$router.push('/login')
}
}
if (err && !ApiClientBase.USER && access_token !== null && refresh_token !== null) {
const user = await ApiClientBase.$api.myprofile.getUser();
ApiClientBase.USER = {...user, access_token, refresh_token}
}

if (err) {
ApiClientBase.$notifier?.notify({
type: 'error',
description: options.normalizeError?.(err, res) ?? ApiClientBase.normalizeError(err) ?? err
})

panic(err)
if (access_token && refresh_token && !isValidToken(access_token) && isValidToken(refresh_token)) {
await ApiClientBase.$api.auth.refresh({ refresh: refresh_token })
}

if (
Expand All @@ -119,6 +102,18 @@ export abstract class ApiClientBase {
})
}

if (err) {
ApiClientBase.$notifier?.notify({
type: 'error',
description: options.normalizeError?.(err, res) ?? ApiClientBase.normalizeError(err) ?? err
})
if (err.response.status == 401) {
ApiClientBase.$router.push('/login')
}
panic(err)
}


return (options.transform?.(res.data, res) ?? res.data) as R
}
}

0 comments on commit b7bb5a2

Please sign in to comment.