Skip to content

Commit

Permalink
updates for node-fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Wenger committed May 27, 2022
1 parent 1b03599 commit bde3829
Showing 1 changed file with 21 additions and 27 deletions.
48 changes: 21 additions & 27 deletions lib/AvaTaxClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @link https://github.com/avadev/AvaTax-REST-V2-JS-SDK
*/

import fetch from 'isomorphic-fetch';
import fetch from 'node-fetch';
import { createBasicAuthHeader } from './utils/basic_auth';
import { withTimeout } from './utils/withTimeout';

Expand All @@ -30,14 +30,14 @@ export default class AvaTaxClient {
* @param number timeout Specify the timeout for AvaTax requests; default value 20 minutes.
*/
constructor({ appName, appVersion, machineName, environment, timeout = 1200000 }) {
this.appNM=appName;
this.appVer=appVersion;
this.machineNM=machineName
this.appNM = appName;
this.appVer = appVersion;
this.machineNM = machineName;
this.baseUrl = 'https://rest.avatax.com';
if (environment == 'sandbox') {
this.baseUrl = 'https://sandbox-rest.avatax.com';
} else if (
typeof environment !== "undefined" &&
typeof environment !== 'undefined' &&
(environment.substring(0, 8) == 'https://' ||
environment.substring(0, 7) == 'http://')
) {
Expand Down Expand Up @@ -74,45 +74,38 @@ export default class AvaTaxClient {
* @param string verb The HTTP verb being used in this request
* @param string payload The request body, if this is being sent to a POST/PUT API call
*/
restCall({ url, verb, payload, clientId="",mapHeader = new Map() }) {
const reqHeaders = new Headers({
restCall({ url, verb, payload, clientId = '', mapHeader = new Map() }) {
const reqHeaders = {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: this.auth,
'X-Avalara-Client': clientId
});
};
for (let [key, value] of mapHeader) {
reqHeaders.append(key,value);
reqHeaders[key] = value;
}
return withTimeout(this.timeout, fetch(url, {
method: verb,
headers: reqHeaders,
body: JSON.stringify(payload)
body: payload == null ? null : JSON.stringify(payload)
})).then(res => {
var contentType = res.headers._headers['content-type'];
var contentLength = res.headers._headers['content-length'];
if (typeof contentLength !== "undefined" && contentLength != null)
{
contentLength = res.headers._headers['content-length'][0];
}

if (contentType[0] === 'application/vnd.ms-excel' || contentType[0] === 'text/csv') {
const contentType = res.headers.get('content-type');
const contentLength = res.headers.get('content-length');
if (contentType === 'application/vnd.ms-excel' || contentType === 'text/csv') {
return res;
}

if (res.headers._headers['content-type'].includes('application/json'))
{
if (typeof contentLength !== "undefined" && contentLength != null && contentLength == 0 && parseInt(res.status/100)==2 ){
return null;
if (contentType && contentType.includes('application/json')) {
if (contentLength == 0 && parseInt(res.status/100)==2 ){
return null;
}
}
}
return res.json().catch((error) => {
let ex = new Error("The server returned the response is in an unexpected format");
let ex = new Error('The server returned the response is in an unexpected format');
ex.code = 'FormatException';
ex.details = error;
throw ex;
});
}).then(json => {
}).then(json => {
// handle error
if (json && json.error) {
let ex = new Error(json.error.message);
Expand All @@ -123,7 +116,8 @@ export default class AvaTaxClient {
} else {
return json;
}
})
});
});
}

/**
Expand Down

0 comments on commit bde3829

Please sign in to comment.