video-ad-sdk
Version:
VAST/VPAID SDK that allows video ads to be played on top of any player
28 lines (27 loc) • 972 B
JavaScript
class FetchError extends Error {
constructor(message) {
super(message);
this.name = 'FetchError';
Object.setPrototypeOf(this, FetchError.prototype);
}
}
const isValidContentType = (contentType) => {
const normalisedCT = contentType.toLowerCase();
return ['text/plain', 'text/html'].some((allowedType) => normalisedCT.includes(allowedType));
};
const BAD_REQUEST = 400;
export const fetchHtml = async (endpoint) => {
const response = await fetch(endpoint);
const contentType = response.headers.get('Content-Type');
if (response.status >= BAD_REQUEST) {
const error = new FetchError(response.statusText);
error.response = response;
throw error;
}
if (!contentType || !isValidContentType(contentType)) {
const error = new FetchError(`fetchHtml error, invalid Content-Type ${contentType}`);
error.response = response;
throw error;
}
return response.text();
};