-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
107 additions
and
112 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
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,33 +1,6 @@ | ||
// TODO: Determine how to authenticate for the target API | ||
function makeAuthHeader(auth) { | ||
const { username, password } = auth; | ||
export function makeBasicAuth(username, password) { | ||
const buff = Buffer.from(`${username}:${password}`); | ||
const credentials = buff.toString('base64'); | ||
return { Authorization: `Basic ${credentials}` }; | ||
} | ||
|
||
export async function request(url, options) { | ||
const { auth } = options; | ||
delete options.auth; | ||
|
||
const response = await fetch(url, { | ||
...options, | ||
headers: { | ||
'content-type': 'application/json', | ||
...makeAuthHeader(auth), | ||
}, | ||
}); | ||
|
||
// TODO: Handle errors based on the behavior of the target API | ||
if (response.status === 404) { | ||
throw new Error('Page not found'); | ||
} else if (response.status === 500) { | ||
throw new Error('Server error'); | ||
} else if (!response.ok) { | ||
throw new Error(`HTTP error! status: ${response.status}`); | ||
} | ||
|
||
// TODO: Extract data from response based on the target API | ||
const data = await response.json(); | ||
return data; | ||
return `Basic ${credentials}`; | ||
} |
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
This file was deleted.
Oops, something went wrong.