diff --git a/lib/AvaTaxClient.js b/lib/AvaTaxClient.js index 6210d17f..966e9cbe 100644 --- a/lib/AvaTaxClient.js +++ b/lib/AvaTaxClient.js @@ -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'; @@ -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://') ) { @@ -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); @@ -123,7 +116,8 @@ export default class AvaTaxClient { } else { return json; } - }) + }); + }); } /**