2021-12-23 14:14:06 +00:00
|
|
|
import { AxiosResponse } from 'axios'
|
2022-04-17 12:03:53 +00:00
|
|
|
import { UnexpectedResponseStatusError } from './UnexpectedResponseStatusError'
|
|
|
|
import { UnexpectedContentTypeError } from './UnexpectedContentTypeError'
|
2021-12-23 14:14:06 +00:00
|
|
|
|
2022-09-18 11:32:25 +00:00
|
|
|
export const assertSuccessJsonResponse = (
|
|
|
|
response: AxiosResponse<unknown>
|
|
|
|
): void => {
|
2022-04-17 12:03:53 +00:00
|
|
|
const expectedStatus = 200
|
|
|
|
const actualStatus = response.status
|
|
|
|
if (actualStatus !== expectedStatus) {
|
|
|
|
throw new UnexpectedResponseStatusError(expectedStatus, actualStatus)
|
2021-12-23 14:14:06 +00:00
|
|
|
}
|
2022-04-17 12:03:53 +00:00
|
|
|
const expectedContentType = 'application/json'
|
2022-11-23 22:34:05 +00:00
|
|
|
const actualContentType = String(response.headers['content-type'])
|
2022-04-17 12:03:53 +00:00
|
|
|
if (!actualContentType.startsWith(expectedContentType)) {
|
2022-11-23 22:34:05 +00:00
|
|
|
throw new UnexpectedContentTypeError(actualContentType, expectedContentType)
|
2021-12-23 14:14:06 +00:00
|
|
|
}
|
|
|
|
}
|