function NtlmClient(credentials, AxiosConfig) {
    var _this = this;
    var config = AxiosConfig !== null && AxiosConfig !== void 0 ? AxiosConfig : {};
    if (!config.httpAgent) {
        config.httpAgent = new http.Agent({ keepAlive: true });
    }
    if (!config.httpsAgent) {
        config.httpsAgent = new https.Agent({ keepAlive: true });
    }
    var client = axios_1.default.create(config);
    client.interceptors.response.use(function (response) {
        return response;
    }, function (err) { return __awaiter(_this, void 0, void 0, function () {
        var error, t1Msg, t2Msg, t3Msg, stream_1;
        var _a;
        return __generator(this, function (_b) {
            switch (_b.label) {
                case 0:
                    error = err.response;
                    // The header may look like this: `Negotiate, NTLM, Basic realm="itsahiddenrealm.example.net"`Add commentMore actions
                    // so extract the 'NTLM' part first
                    const ntlmheader = error.headers['www-authenticate'].split(',').find(_ => _.match(/ *NTLM/))?.trim() || '';
                    if (!(error && error.status === 401
                        && error.headers['www-authenticate']
                        && error.headers['www-authenticate'].includes('NTLM'))) return [3 /*break*/, 3];
                    // This length check is a hack because SharePoint is awkward and will
                    // include the Negotiate option when responding with the T2 message
                    // There is nore we could do to ensure we are processing correctly,
                    // but this is the easiest option for now
                    if (ntlmheader.length < 50) {
                        t1Msg = ntlm.createType1Message(credentials.workstation, credentials.domain);
                        error.config.headers["Authorization"] = t1Msg;
                    }
                    else {
                        t2Msg = ntlm.decodeType2Message((ntlmheader.match(/^NTLM\s+(.+?)(,|\s+|$)/) || [])[1]);
                        t3Msg = ntlm.createType3Message(t2Msg, credentials.username, credentials.password, credentials.workstation, credentials.domain);
                        error.config.headers["X-retry"] = "false";
                        error.config.headers["Authorization"] = t3Msg;
                    }
                    if (!(error.config.responseType === "stream")) return [3 /*break*/, 2];
                    stream_1 = (_a = err.response) === null || _a === void 0 ? void 0 : _a.data;
                    if (!(stream_1 && !stream_1.readableEnded)) return [3 /*break*/, 2];
                    return [4 /*yield*/, new Promise(function (resolve) {
                            stream_1.pipe((0, dev_null_1.default)());
                            stream_1.once('close', resolve);
                        })];
                case 1:
                    _b.sent();
                    _b.label = 2;
                case 2: return [2 /*return*/, client(error.config)];
                case 3: throw err;
            }
        });
    }); });
    return client;
}
@param credentials An NtlmCredentials object containing the username and password
@param AxiosConfig The Axios config for the instance you wish to create

@returns This function returns an axios instance configured to use the provided credentials