Skip to content
This repository was archived by the owner on Jan 16, 2025. It is now read-only.

Commit 0ca7937

Browse files
committed
Added option for returning full axios result
1 parent 3ecdad9 commit 0ca7937

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

README.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -105,26 +105,34 @@ const App = () => {
105105
<summary>Parameters</summary>
106106
<ul>
107107
<li>url (string) <i>(required)</i></li>
108+
<li>returnFullResponse (bool) <i>(optional) <b>default = false</b></li>
109+
</ul>
108110
</details>
109111
- `apiPost` (function) - posts the provided data to the URL using the users id_token
110112
<details>
111113
<summary>Parameters</summary>
112114
<ul>
113115
<li>url (string) <i>(required)</i></li>
114-
<li>data <i>(required)</i></ul>
116+
<li>data <i>(required)</i>
117+
<li>returnFullResponse (bool) <i>(optional) <b>default = false</b></li>
118+
</ul>
115119
</details>
116120
- `apiPut` (function) - updates/put the provided data to the URL using the users id_token
117121
<details>
118122
<summary>Parameters</summary>
119123
<ul>
120124
<li>url (string) <i>(required)</i></li>
121-
<li>data <i>(required)</i></ul>
125+
<li>data <i>(required)</i>
126+
<li>returnFullResponse (bool) <i>(optional) <b>default = false</b></li>
127+
</ul>
122128
</details>
123129
- `apiDelete` (function) - deletes data from provided URL using the users id_token
124130
<details>
125131
<summary>Parameters</summary>
126132
<ul>
127133
<li>url (string) <i>(required)</i></li>
134+
<li>returnFullResponse (bool) <i>(optional) <b>default = false</b></li>
135+
</ul>
128136
</details>
129137

130138
#### User object

src/lib/auth-provider.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -222,21 +222,21 @@ export const MsalProvider = ({
222222
const is401 = error => /401/.test(error.message)
223223
const isValid = (token, expires) => token && expires > new Date().getTime()
224224

225-
const retry = async func => {
225+
const retry = async (func, returnFullResponse) => {
226226
if (isValid(idToken, expires)) {
227227
axios.defaults.headers.common.Authorization = `Bearer ${idToken}`
228228
try {
229-
const { data } = await func()
230-
return data
229+
const res = await func()
230+
return !returnFullResponse ? res.data : res
231231
} catch (error) {
232232
if (is401(error)) {
233233
const accounts = publicClient.getAllAccounts()
234234
if (accounts && accounts.length > 0) await updateToken(accounts[0])
235235

236236
axios.defaults.headers.common.Authorization = `Bearer ${idToken}`
237237
try {
238-
const { data } = await func()
239-
return data
238+
const res = await func()
239+
return !returnFullResponse ? res.data : res
240240
} catch (error) {
241241
console.error(error)
242242
return false
@@ -255,10 +255,10 @@ export const MsalProvider = ({
255255
}
256256
}
257257

258-
const apiGet = url => retry(() => axios.get(url))
259-
const apiPost = (url, payload) => retry(() => axios.post(url, payload))
260-
const apiPut = (url, payload) => retry(() => axios.put(url, payload))
261-
const apiDelete = url => retry(() => axios.delete(url))
258+
const apiGet = (url, returnFullResponse = false) => retry(() => axios.get(url), returnFullResponse)
259+
const apiPost = (url, payload, returnFullResponse = false) => retry(() => axios.post(url, payload), returnFullResponse)
260+
const apiPut = (url, payload, returnFullResponse = false) => retry(() => axios.put(url, payload), returnFullResponse)
261+
const apiDelete = (url, returnFullResponse = false) => retry(() => axios.delete(url), returnFullResponse)
262262

263263
return (
264264
<MsalContext.Provider

0 commit comments

Comments
 (0)