Skip to content

Commit

Permalink
Added option for returning full axios result
Browse files Browse the repository at this point in the history
  • Loading branch information
runely committed Mar 29, 2021
1 parent 3ecdad9 commit 0ca7937
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,26 +105,34 @@ const App = () => {
<summary>Parameters</summary>
<ul>
<li>url (string) <i>(required)</i></li>
<li>returnFullResponse (bool) <i>(optional) <b>default = false</b></li>
</ul>
</details>
- `apiPost` (function) - posts the provided data to the URL using the users id_token
<details>
<summary>Parameters</summary>
<ul>
<li>url (string) <i>(required)</i></li>
<li>data <i>(required)</i></ul>
<li>data <i>(required)</i>
<li>returnFullResponse (bool) <i>(optional) <b>default = false</b></li>
</ul>
</details>
- `apiPut` (function) - updates/put the provided data to the URL using the users id_token
<details>
<summary>Parameters</summary>
<ul>
<li>url (string) <i>(required)</i></li>
<li>data <i>(required)</i></ul>
<li>data <i>(required)</i>
<li>returnFullResponse (bool) <i>(optional) <b>default = false</b></li>
</ul>
</details>
- `apiDelete` (function) - deletes data from provided URL using the users id_token
<details>
<summary>Parameters</summary>
<ul>
<li>url (string) <i>(required)</i></li>
<li>returnFullResponse (bool) <i>(optional) <b>default = false</b></li>
</ul>
</details>

#### User object
Expand Down
18 changes: 9 additions & 9 deletions src/lib/auth-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,21 +222,21 @@ export const MsalProvider = ({
const is401 = error => /401/.test(error.message)
const isValid = (token, expires) => token && expires > new Date().getTime()

const retry = async func => {
const retry = async (func, returnFullResponse) => {
if (isValid(idToken, expires)) {
axios.defaults.headers.common.Authorization = `Bearer ${idToken}`
try {
const { data } = await func()
return data
const res = await func()
return !returnFullResponse ? res.data : res
} catch (error) {
if (is401(error)) {
const accounts = publicClient.getAllAccounts()
if (accounts && accounts.length > 0) await updateToken(accounts[0])

axios.defaults.headers.common.Authorization = `Bearer ${idToken}`
try {
const { data } = await func()
return data
const res = await func()
return !returnFullResponse ? res.data : res
} catch (error) {
console.error(error)
return false
Expand All @@ -255,10 +255,10 @@ export const MsalProvider = ({
}
}

const apiGet = url => retry(() => axios.get(url))
const apiPost = (url, payload) => retry(() => axios.post(url, payload))
const apiPut = (url, payload) => retry(() => axios.put(url, payload))
const apiDelete = url => retry(() => axios.delete(url))
const apiGet = (url, returnFullResponse = false) => retry(() => axios.get(url), returnFullResponse)
const apiPost = (url, payload, returnFullResponse = false) => retry(() => axios.post(url, payload), returnFullResponse)
const apiPut = (url, payload, returnFullResponse = false) => retry(() => axios.put(url, payload), returnFullResponse)
const apiDelete = (url, returnFullResponse = false) => retry(() => axios.delete(url), returnFullResponse)

return (
<MsalContext.Provider
Expand Down

0 comments on commit 0ca7937

Please sign in to comment.