-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: expanded response to include other metadata
- Loading branch information
1 parent
5d7501e
commit 06a596c
Showing
8 changed files
with
50 additions
and
23 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
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
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,9 +1,30 @@ | ||
import { RequestParameters } from "./request"; | ||
import { Transport } from "../api/configuration"; | ||
|
||
export default async function transport(params: RequestParameters) { | ||
const response = await fetch(params.url, params); | ||
if (response.status !== 200) { | ||
throw new Error(`${response.status} ${response.statusText}`); | ||
const parseHeaders = (headers: Headers) => { | ||
let res: Record<string, string> = {}; | ||
for (let [key, value] of headers.entries()) { | ||
res[key] = value; | ||
} | ||
return await response.json(); | ||
} | ||
return res; | ||
}; | ||
|
||
const transport: Transport = async function (params: RequestParameters) { | ||
const fetchResponse = await fetch(params.url, params); | ||
|
||
if (fetchResponse.ok) { | ||
const json = await fetchResponse.json(); | ||
return { | ||
data: json, | ||
statusCode: fetchResponse.status, | ||
headers: parseHeaders(fetchResponse.headers), | ||
type: fetchResponse.type, | ||
}; | ||
} else { | ||
throw new Error( | ||
`Error ${fetchResponse.status}: ${fetchResponse.statusText}` | ||
); | ||
} | ||
}; | ||
|
||
export default transport; |
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
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