import HTTPMethod from "./API/HTTPMethod";
import HTML from "./Html";
import axios from "axios";
export default class WebsiteRequest {
    static async fetch(url, options) {
        const axiosOptions = {
            timeout: options.timeout || 5000,
            method: HTTPMethod.GET,
        };
        if (options.headers) {
            axiosOptions.headers = options.headers;
        }
        if (options.isHeadRequest) {
            axiosOptions.method = HTTPMethod.HEAD;
        }
        if (options.doNotFollowRedirects) {
            axiosOptions.maxRedirects = 0;
        }
        if (options.httpAgent) {
            axiosOptions.httpAgent = options.httpAgent;
        }
        if (options.httpsAgent) {
            axiosOptions.httpsAgent = options.httpsAgent;
        }
        // use axios to fetch an HTML page
        let response = null;
        try {
            response = await axios(url.toString(), axiosOptions);
        }
        catch (err) {
            if (err && options.isHeadRequest) {
                // 404 because of HEAD request. Retry with GET request.
                response = await axios(url.toString(), Object.assign(Object.assign({}, axiosOptions), { method: HTTPMethod.GET }));
            }
            else {
                throw err;
            }
        }
        // return the response
        return {
            url: url,
            requestHeaders: options.headers || {},
            responseHeaders: response.headers,
            responseStatusCode: response.status,
            responseBody: new HTML(response.data),
            isOnline: true,
        };
    }
}
//# sourceMappingURL=WebsiteRequest.js.map