-
-
Notifications
You must be signed in to change notification settings - Fork 150
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
1 parent
07fbfb7
commit 55d7c2c
Showing
4 changed files
with
100 additions
and
43 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { AxiosHeaders, AxiosResponse, AxiosResponseHeaders, RawAxiosResponseHeaders } from "axios"; | ||
import { Headers, Response } from "../types"; | ||
|
||
function convertAxiosHeaders( | ||
axiosHeaders: AxiosHeaders | RawAxiosResponseHeaders | AxiosResponseHeaders | undefined | ||
): Headers { | ||
if (typeof axiosHeaders === "undefined") return {}; | ||
return Object.keys(axiosHeaders).reduce((output: Headers, headerName: string) => { | ||
if (typeof axiosHeaders[headerName] === "string") { | ||
return { ...output, [headerName]: axiosHeaders[headerName] }; | ||
} else if (Array.isArray(axiosHeaders[headerName])) { | ||
// Take the first only | ||
return { ...output, [headerName]: axiosHeaders[headerName][0] }; | ||
} else if (typeof axiosHeaders[headerName] === "number") { | ||
return { ...output, [headerName]: `${axiosHeaders[headerName]}` }; | ||
} else if (typeof axiosHeaders[headerName] === "boolean") { | ||
return { ...output, [headerName]: axiosHeaders[headerName].toString() }; | ||
} else if (axiosHeaders[headerName] instanceof AxiosHeaders) { | ||
return { | ||
...output, | ||
...convertAxiosHeaders(axiosHeaders[headerName]) | ||
}; | ||
} else { | ||
throw new Error( | ||
`Invalid header value type: ${headerName}: ${typeof axiosHeaders[headerName]}` | ||
); | ||
} | ||
}, {}); | ||
} | ||
|
||
export function mapAxiosResponseToInterface(axiosResponse: AxiosResponse<any, any>): Response { | ||
return { | ||
data: axiosResponse.data, | ||
status: axiosResponse.status, | ||
headers: convertAxiosHeaders(axiosResponse.headers), | ||
statusText: axiosResponse.statusText | ||
}; | ||
} |
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