Skip to content

Commit c1ed90f

Browse files
authored
Merge pull request #26 from phaxio/phaxio-node-upgrade
Phaxio node upgrade, v1.2.3
2 parents 8d71fee + 5d851e2 commit c1ed90f

File tree

9 files changed

+1996
-1367
lines changed

9 files changed

+1996
-1367
lines changed

package-lock.json

Lines changed: 1756 additions & 1031 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "phaxio-official",
3-
"version": "1.2.2",
3+
"version": "1.2.3",
44
"description": "The official Node.JS library for Phaxio API v2.1.0. https://phaxio.com",
55
"main": "index.js",
66
"scripts": {
@@ -14,15 +14,15 @@
1414
"author": "Jeremy Jackson <[email protected]> (https://voyant.com)",
1515
"license": "MIT",
1616
"dependencies": {
17-
"request": "^2.88.0",
18-
"request-promise-native": "^1.0.8"
17+
"axios": "^1.6.5",
18+
"request": "^2.88.0"
1919
},
2020
"devDependencies": {
2121
"dotenv": "^8.2.0",
2222
"eslint": "^6.6.0",
2323
"eslint-config-airbnb-base": "^14.0.0",
2424
"eslint-plugin-import": "^2.18.2",
2525
"expect.js": "^0.3.1",
26-
"mocha": "^6.2.2"
26+
"mocha": "^10.2.0"
2727
}
2828
}

src/account/index.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const request = require('request-promise-native');
1+
const request = require('axios');
22
const errorHandler = require('../error-handler');
33

44
module.exports = class {
@@ -8,23 +8,19 @@ module.exports = class {
88
this.url = url;
99
this.agentOptions = agentOptions;
1010

11-
this.auth = { user: this.apiKey, pass: this.apiSecret };
11+
this.auth = { username: this.apiKey, password: this.apiSecret };
1212
}
1313

1414
status() {
15-
return new Promise((resolve, reject) => {
16-
request({
17-
method: 'GET',
18-
url: `${this.url}/account/status`,
19-
auth: this.auth,
20-
agentOptions: this.agentOptions,
15+
return request
16+
.get(`${this.url}/account/status`, {
17+
auth: this.auth
2118
})
22-
.then((response) => {
23-
const res = JSON.parse(response);
24-
if (!res.success) return reject(errorHandler(res.message));
25-
return resolve(res);
26-
})
27-
.catch((err) => reject(err));
28-
});
19+
.then((response) => {
20+
const tempResponse = response;
21+
if (!response.data.success) return Promise.reject(errorHandler(response.data.message));
22+
return Promise.resolve(response.data);
23+
})
24+
.catch((err) => Promise.reject(err));
2925
}
3026
};

src/faxes/index.js

Lines changed: 47 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const fs = require('fs');
2-
const request = require('request-promise-native');
2+
const request = require('axios');
33
const errorHandler = require('../error-handler');
44

55
const Fax = require('./fax');
@@ -66,23 +66,14 @@ module.exports = class {
6666
test_fail: null,
6767
}) {
6868
// eslint-disable-next-line consistent-return
69-
return new Promise((resolve, reject) => {
7069
const formData = {};
7170

7271
Object.keys(options).forEach((rec) => {
7372
if (options[rec] !== null) formData[rec] = options[rec];
7473
if (typeof formData[rec] === 'boolean') formData[rec] = formData[rec].toString();
7574
});
7675

77-
const req = {
78-
method: 'POST',
79-
url: `${this.url}/faxes`,
80-
auth: this.auth,
81-
agentOptions: this.agentOptions,
82-
};
83-
84-
const caller = request(req);
85-
const form = caller.form();
76+
const form = new FormData();
8677
Object.keys(formData).forEach((key) => {
8778
if (typeof formData[key] === 'object' && key === 'tags' && formData[key] !== null) {
8879
Object.keys(formData[key]).forEach((tagkey) => {
@@ -103,47 +94,46 @@ module.exports = class {
10394
}
10495
});
10596

106-
caller
97+
return request
98+
.post(`${this.url}/account/status`, form, {
99+
auth: this.auth
100+
})
107101
.then((response) => {
108102
const res = JSON.parse(response);
109-
if (!res.success) return reject(errorHandler(res.message));
103+
if (!res.success) return Promise.reject(errorHandler(res.message));
110104
// eslint-disable-next-line max-len
111105
return resolve(new Fax(this.apiKey, this.apiSecret, this.url, res.success, res.message, res.data));
112106
})
113-
.catch((err) => reject(err));
114-
});
107+
.catch((err) => Promise.reject(err));
115108
}
116109

117110
testReceive(options = {
118111
file: null,
119112
from_number: null,
120113
to_number: null,
121114
}) {
122-
return new Promise((resolve, reject) => {
123-
const formData = { direction: 'received' };
115+
const form = new FormData();
124116

125-
Object.keys(options).forEach((rec) => {
126-
if (rec === 'file' && options[rec] !== null) {
127-
formData[rec] = fs.createReadStream(options[rec]);
128-
} else if (options[rec] !== null) {
129-
formData[rec] = options[rec];
130-
}
131-
});
117+
form.append('direction', 'received');
132118

133-
request({
134-
method: 'POST',
135-
url: `${this.url}/faxes`,
136-
auth: this.auth,
137-
formData,
138-
agentOptions: this.agentOptions,
139-
})
140-
.then((response) => {
141-
const res = JSON.parse(response);
142-
if (!res.success) return reject(errorHandler(res.message));
143-
return resolve(res);
144-
})
145-
.catch((err) => reject(err));
119+
Object.keys(options).forEach((rec) => {
120+
if (rec === 'file' && options[rec] !== null) {
121+
form.append('fileAttachment', fs.createReadStream(options[rec]));
122+
} else if (options[rec] !== null) {
123+
form.append('rec', options[rec]);
124+
}
146125
});
126+
127+
return request
128+
.post(`${this.url}/faxes`, form, {
129+
auth: this.auth
130+
})
131+
.then((response) => {
132+
const res = JSON.parse(response);
133+
if (!res.success) return Promise.reject(errorHandler(res.message));
134+
return resolve(res);
135+
})
136+
.catch((err) => Promise.reject(err));
147137
}
148138

149139
listFaxes(options = {
@@ -156,32 +146,27 @@ module.exports = class {
156146
per_page: null,
157147
page: null,
158148
}) {
159-
return new Promise((resolve, reject) => {
160-
const query = {};
161-
Object.keys(options).forEach((rec) => {
162-
if (rec === 'tags' && typeof options[rec] === 'object' && options[rec] !== null) {
163-
Object.keys(options[rec]).forEach((tag) => {
164-
const newrec = `tag[${tag}]`;
165-
query[newrec] = options[rec][tag];
166-
});
167-
} else if (options[rec] !== null) {
168-
query[rec] = options[rec];
169-
}
170-
});
149+
const query = {};
150+
Object.keys(options).forEach((rec) => {
151+
if (rec === 'tags' && typeof options[rec] === 'object' && options[rec] !== null) {
152+
Object.keys(options[rec]).forEach((tag) => {
153+
const newrec = `tag[${tag}]`;
154+
query[newrec] = options[rec][tag];
155+
});
156+
} else if (options[rec] !== null) {
157+
query[rec] = options[rec];
158+
}
159+
});
171160

172-
request({
173-
method: 'GET',
174-
url: `${this.url}/faxes`,
175-
auth: this.auth,
176-
qs: query,
177-
agentOptions: this.agentOptions,
161+
return request
162+
.get(`${this.url}/faxes`, query, {
163+
auth: this.auth
178164
})
179-
.then((response) => {
180-
const res = JSON.parse(response);
181-
if (!res.success) return reject(errorHandler(res.message));
182-
return resolve(res);
183-
})
184-
.catch((err) => reject(err));
185-
});
165+
.then((response) => {
166+
const res = JSON.parse(response);
167+
if (!res.success) return Promise.reject(errorHandler(res.message));
168+
return resolve(res);
169+
})
170+
.catch((err) => Promise.reject(err));
186171
}
187172
};

0 commit comments

Comments
 (0)