-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): fixed credentials handling
- Loading branch information
Xairoo
committed
Sep 18, 2022
1 parent
cd2dcd7
commit 2acbccb
Showing
1 changed file
with
13 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,32 @@ | ||
// Fetch function for SWR | ||
export default async function fetcher({ url, method, token }) { | ||
if (!url) { | ||
throw new Error('Missing `url` key') | ||
throw new Error("Missing `url` key"); | ||
} | ||
|
||
const res = await fetch(url, { | ||
method: method ? method : 'GET', | ||
method: method ? method : "GET", | ||
credentials: "include", | ||
headers: { | ||
...(token && { Authorization: `Bearer ${token}` }), | ||
}, | ||
}) | ||
}); | ||
|
||
// If the status code is not in the range 200-299, | ||
if (!res.ok) { | ||
let error | ||
if (process.env.NODE_ENV !== 'production') { | ||
error = {} | ||
error = new Error('An error occurred while fetching the data.') | ||
let error; | ||
if (process.env.NODE_ENV !== "production") { | ||
error = {}; | ||
error = new Error("An error occurred while fetching the data."); | ||
} else { | ||
} | ||
// Attach extra info to the error object. | ||
error.info = await res.json() | ||
error.status = res.status | ||
if (process.env.NODE_ENV !== 'production') { | ||
throw error | ||
error.info = await res.json(); | ||
error.status = res.status; | ||
if (process.env.NODE_ENV !== "production") { | ||
throw error; | ||
} | ||
} | ||
|
||
return res.json() | ||
return res.json(); | ||
} |