|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const axios = require('axios'); |
| 4 | + |
| 5 | +/** |
| 6 | + * Check HTTP Status as per settings and return status code and other |
| 7 | + * information for further process. |
| 8 | + */ |
| 9 | +function axiosRequest(urlToCheck, axiosOptions, redirect) { |
| 10 | + var httpStatus = [ |
| 11 | + '', |
| 12 | + 0, |
| 13 | + '', |
| 14 | + urlToCheck, |
| 15 | + '' |
| 16 | + ]; |
| 17 | + |
| 18 | + if (!redirect) { |
| 19 | + httpStatus[0] = urlToCheck; |
| 20 | + } |
| 21 | + |
| 22 | + return new Promise((resolve) => { |
| 23 | + axios.get(urlToCheck, axiosOptions) |
| 24 | + .then((response) => { |
| 25 | + httpStatus[2] = response.status; |
| 26 | + if (redirect) { |
| 27 | + httpStatus[1] = ''; |
| 28 | + } |
| 29 | + |
| 30 | + resolve(httpStatus); |
| 31 | + }) |
| 32 | + .catch(async (error) => { |
| 33 | + var statusLists = []; |
| 34 | + |
| 35 | + if (error.response && error.response.status) { |
| 36 | + httpStatus[2] = error.response.status; |
| 37 | + if (error.response.status >= 300 && error.response.status < 400) { |
| 38 | + const redUrl = error.response.headers.location; |
| 39 | + const checkType = await axiosRequest(redUrl, axiosOptions, true); |
| 40 | + |
| 41 | + httpStatus[3] = error.response.headers.location; |
| 42 | + statusLists[0] = httpStatus; |
| 43 | + |
| 44 | + if (redirect) { |
| 45 | + httpStatus[1] = ''; |
| 46 | + } else if (typeof checkType[0] === 'object') { |
| 47 | + httpStatus[1] = checkType.length; |
| 48 | + } else { |
| 49 | + httpStatus[1] = 1; |
| 50 | + } |
| 51 | + |
| 52 | + if (!redirect && typeof checkType[0] === 'object') { |
| 53 | + let childLinks = 1; |
| 54 | + checkType.forEach((element) => { |
| 55 | + statusLists[childLinks] = element; |
| 56 | + childLinks += 1; |
| 57 | + }); |
| 58 | + } else { |
| 59 | + statusLists[1] = checkType; |
| 60 | + } |
| 61 | + } else { |
| 62 | + statusLists = httpStatus; |
| 63 | + } |
| 64 | + } else { |
| 65 | + httpStatus[1] = ''; |
| 66 | + httpStatus[3] = ''; |
| 67 | + if (error.message) { |
| 68 | + httpStatus[4] = error.message; |
| 69 | + } else { |
| 70 | + httpStatus[4] = error; |
| 71 | + } |
| 72 | + |
| 73 | + statusLists = httpStatus; |
| 74 | + } |
| 75 | + |
| 76 | + resolve(statusLists); |
| 77 | + }); |
| 78 | + }); |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Call main function to generate array with all the required information |
| 83 | + * and await until that all are not completed. |
| 84 | + */ |
| 85 | +async function checkStatusCode(urlToCheck, options) { |
| 86 | + const statusList = []; |
| 87 | + |
| 88 | + var axiosOptions = { |
| 89 | + 'maxRedirects': 0 |
| 90 | + }; |
| 91 | + var statusArray = []; |
| 92 | + |
| 93 | + // Custom headers to be sent. |
| 94 | + if (options.headers) { |
| 95 | + axiosOptions.headers = options.headers; |
| 96 | + } |
| 97 | + |
| 98 | + // Specifies the number of milliseconds before the request times out. |
| 99 | + if (options.timeout) { |
| 100 | + axiosOptions.timeout = options.timeout; |
| 101 | + } |
| 102 | + |
| 103 | + // Indicates that HTTP Basic auth should be used, and supplies credentials. |
| 104 | + if (options.auth) { |
| 105 | + axiosOptions.auth = options.auth; |
| 106 | + } |
| 107 | + |
| 108 | + statusArray = await axiosRequest(urlToCheck, axiosOptions); |
| 109 | + if (typeof statusArray[0] === 'object') { |
| 110 | + statusArray.forEach((row) => { |
| 111 | + statusList.push(row); |
| 112 | + }); |
| 113 | + } else { |
| 114 | + statusList.push(statusArray); |
| 115 | + } |
| 116 | + |
| 117 | + return Promise.resolve(statusList); |
| 118 | +} |
| 119 | + |
| 120 | +module.exports = checkStatusCode; |
0 commit comments