Skip to content

Commit 1811924

Browse files
committed
feat: custom axios instance
1 parent eb25ba5 commit 1811924

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

examples/custom-axios-client.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env node
2+
3+
import process from 'node:process';
4+
import axios from 'axios';
5+
import {HttpCookieAgent, HttpsCookieAgent} from 'http-cookie-agent/http';
6+
import Unifi from '../unifi.js';
7+
8+
// Get necessary data from cmd-line
9+
const host = process.argv[2]; // Controller host/ip
10+
const port = process.argv[3]; // Controller port
11+
const username = process.argv[4]; // Controller username
12+
const password = process.argv[5]; // Controller password
13+
const trustedCertificate = process.argv[6]; // Trusted certificate in pem format
14+
15+
const unifi = new Unifi.Controller({
16+
host,
17+
port,
18+
createAxiosInstance({cookies}) {
19+
return axios.create({
20+
httpAgent: new HttpCookieAgent({cookies}),
21+
httpsAgent: new HttpsCookieAgent({cookies, requestCert: true, ca: trustedCertificate}),
22+
});
23+
},
24+
});
25+
26+
try {
27+
// LOGIN
28+
const loginData = await unifi.login(username, password);
29+
console.log('login: ' + loginData);
30+
} catch (error) {
31+
console.log('ERROR: ' + error);
32+
}

unifi.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3151,7 +3151,7 @@ class Controller extends EventEmitter2 {
31513151
}
31523152

31533153
const jar = this._cookieJar;
3154-
this._instance = axios.create({
3154+
this._instance = this.opts.createAxiosInstance?.({cookies: {jar}}) ?? axios.create({
31553155
httpAgent: new HttpCookieAgent({cookies: {jar}}),
31563156
httpsAgent: new HttpsCookieAgent({cookies: {jar}, rejectUnauthorized: this.opts.sslverify, requestCert: true}),
31573157
});

0 commit comments

Comments
 (0)