Skip to content

Commit 41317b5

Browse files
Merge pull request #224 from jwenger100/master
updates for node-fetch
2 parents 1b03599 + bde3829 commit 41317b5

File tree

1 file changed

+21
-27
lines changed

1 file changed

+21
-27
lines changed

lib/AvaTaxClient.js

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* @link https://github.com/avadev/AvaTax-REST-V2-JS-SDK
1515
*/
1616

17-
import fetch from 'isomorphic-fetch';
17+
import fetch from 'node-fetch';
1818
import { createBasicAuthHeader } from './utils/basic_auth';
1919
import { withTimeout } from './utils/withTimeout';
2020

@@ -30,14 +30,14 @@ export default class AvaTaxClient {
3030
* @param number timeout Specify the timeout for AvaTax requests; default value 20 minutes.
3131
*/
3232
constructor({ appName, appVersion, machineName, environment, timeout = 1200000 }) {
33-
this.appNM=appName;
34-
this.appVer=appVersion;
35-
this.machineNM=machineName
33+
this.appNM = appName;
34+
this.appVer = appVersion;
35+
this.machineNM = machineName;
3636
this.baseUrl = 'https://rest.avatax.com';
3737
if (environment == 'sandbox') {
3838
this.baseUrl = 'https://sandbox-rest.avatax.com';
3939
} else if (
40-
typeof environment !== "undefined" &&
40+
typeof environment !== 'undefined' &&
4141
(environment.substring(0, 8) == 'https://' ||
4242
environment.substring(0, 7) == 'http://')
4343
) {
@@ -74,45 +74,38 @@ export default class AvaTaxClient {
7474
* @param string verb The HTTP verb being used in this request
7575
* @param string payload The request body, if this is being sent to a POST/PUT API call
7676
*/
77-
restCall({ url, verb, payload, clientId="",mapHeader = new Map() }) {
78-
const reqHeaders = new Headers({
77+
restCall({ url, verb, payload, clientId = '', mapHeader = new Map() }) {
78+
const reqHeaders = {
7979
Accept: 'application/json',
8080
'Content-Type': 'application/json',
8181
Authorization: this.auth,
8282
'X-Avalara-Client': clientId
83-
});
83+
};
8484
for (let [key, value] of mapHeader) {
85-
reqHeaders.append(key,value);
85+
reqHeaders[key] = value;
8686
}
8787
return withTimeout(this.timeout, fetch(url, {
8888
method: verb,
8989
headers: reqHeaders,
90-
body: JSON.stringify(payload)
90+
body: payload == null ? null : JSON.stringify(payload)
9191
})).then(res => {
92-
var contentType = res.headers._headers['content-type'];
93-
var contentLength = res.headers._headers['content-length'];
94-
if (typeof contentLength !== "undefined" && contentLength != null)
95-
{
96-
contentLength = res.headers._headers['content-length'][0];
97-
}
98-
99-
if (contentType[0] === 'application/vnd.ms-excel' || contentType[0] === 'text/csv') {
92+
const contentType = res.headers.get('content-type');
93+
const contentLength = res.headers.get('content-length');
94+
if (contentType === 'application/vnd.ms-excel' || contentType === 'text/csv') {
10095
return res;
10196
}
10297

103-
if (res.headers._headers['content-type'].includes('application/json'))
104-
{
105-
if (typeof contentLength !== "undefined" && contentLength != null && contentLength == 0 && parseInt(res.status/100)==2 ){
106-
return null;
98+
if (contentType && contentType.includes('application/json')) {
99+
if (contentLength == 0 && parseInt(res.status/100)==2 ){
100+
return null;
101+
}
107102
}
108-
}
109103
return res.json().catch((error) => {
110-
let ex = new Error("The server returned the response is in an unexpected format");
104+
let ex = new Error('The server returned the response is in an unexpected format');
111105
ex.code = 'FormatException';
112106
ex.details = error;
113107
throw ex;
114-
});
115-
}).then(json => {
108+
}).then(json => {
116109
// handle error
117110
if (json && json.error) {
118111
let ex = new Error(json.error.message);
@@ -123,7 +116,8 @@ export default class AvaTaxClient {
123116
} else {
124117
return json;
125118
}
126-
})
119+
});
120+
});
127121
}
128122

129123
/**

0 commit comments

Comments
 (0)