Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional axios timeout option to requests #21

Merged
merged 6 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const obix = new ObixInstance({
host: String (Niagara IP Address),
port: String | Number (Niagara web service port),
username: String (Obix username),
password: String (Obix password)
password: String (Obix password),
timeout: Optional Number (ms until request timeout)
})
```

Expand All @@ -30,7 +31,8 @@ const bql = new BQLInstance({
host: String (Niagara IP Address),
port: String | Number (Niagara web service port),
username: String (HTTPBasic username),
password: String (HTTPBasic password)
password: String (HTTPBasic password),
timeout: Optional Number (ms until request timeout)
})
```

Expand Down
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ const { WatcherRequestInstance } = require('./src/requests/watcher');
const { BQLQueryInstance } = require('./src/requests/bql');

class ObixInstance {
constructor({ protocol = 'https', host = 'localhost', port = '443', username, password }) {
const axiosInstance = createInstance({ protocol, host, port, username, password });
constructor({ protocol = 'https', host = 'localhost', port = '443', username, password, timeout }) {
const axiosInstance = createInstance({ protocol, host, port, username, password, timeout });
this.rawRequestInstance = new RawRequestInstance({ axiosInstance });
this.historyRequestInstance = new HistoryRequestInstance({ axiosInstance });
this.batchRequestInstance = new BatchRequestInstance({ axiosInstance });
Expand Down Expand Up @@ -54,8 +54,8 @@ class ObixInstance {
}

class BQLInstance {
constructor({ protocol = 'https', host = 'localhost', port = '443', username, password }) {
const axiosInstance = createInstance({ protocol, host, port, username, password, isBQL: true });
constructor({ protocol = 'https', host = 'localhost', port = '443', username, password, timeout }) {
const axiosInstance = createInstance({ protocol, host, port, username, password, isBQL: true, timeout });
this.bqlQueryInstance = new BQLQueryInstance({ axiosInstance });
}

Expand Down
34 changes: 17 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obix-js",
"version": "1.2.4",
"version": "1.2.5",
"description": "Core Obix Niagara functions",
"main": "index.js",
"scripts": {
Expand All @@ -27,7 +27,7 @@
},
"homepage": "https://github.com/adamzman/obix-js#readme",
"dependencies": {
"axios": "^1.2.1",
"axios": "^1.6.2",
"cheerio": "^1.0.0-rc.12",
"dayjs": "^1.11.9",
"xml-js": "^1.6.11"
Expand Down
6 changes: 3 additions & 3 deletions src/axios.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ class ProtocolError extends Error {
}
}

const createInstance = ({ protocol, host, port, username, password, isBQL = false }) => {
const createInstance = ({ protocol, host, port, username, password, isBQL = false, timeout }) => {
if (protocol != 'https' && protocol != 'http') throw new ProtocolError();

if (isBQL) {
const axiosInstance = axios.create({
baseURL: `${protocol}://${host}:${port}`,
timeout: 10000,
timeout: timeout || 10000,
auth: { username, password },
httpsAgent: new https.Agent({ rejectUnauthorized: false }),
});
Expand All @@ -37,7 +37,7 @@ const createInstance = ({ protocol, host, port, username, password, isBQL = fals
} else {
const axiosInstance = axios.create({
baseURL: `${protocol}://${host}:${port}/obix/`,
timeout: 2000,
timeout: timeout || 2000,
auth: { username, password },
httpsAgent: new https.Agent({ rejectUnauthorized: false }),
transformResponse: [
Expand Down
17 changes: 17 additions & 0 deletions tests/axios.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ describe('Axios', () => {
expect(axiosInstance.defaults.transformResponse).toContainEqual(expect.any(Function));
});

test('creates an axios instance with timeout override', () => {
const protocol = 'https';
const host = 'example.com';
const port = 443;
const username = 'user';
const password = 'pass';
const timeout = 5000;

const axiosInstance = createInstance({ protocol, host, port, username, password, timeout });

expect(axiosInstance.defaults.baseURL).toBe(`https://example.com:443/obix/`);
expect(axiosInstance.defaults.timeout).toBe(timeout);
expect(axiosInstance.defaults.auth).toEqual({ username, password });
expect(axiosInstance.defaults.httpsAgent).toBeInstanceOf(https.Agent);
expect(axiosInstance.defaults.transformResponse).toContainEqual(expect.any(Function));
});

test('throws ProtocolError for invalid protocol', () => {
expect.assertions(4);
const invalidProtocol = 'ftp';
Expand Down