@fye/ingram-api
Advanced tools
Comparing version 1.0.2 to 1.1.0
84
index.js
@@ -1,5 +0,5 @@ | ||
/* eslint-disable no-restricted-syntax */ | ||
/* eslint-disable no-await-in-loop */ | ||
/* eslint-disable no-underscore-dangle */ | ||
const Logger = require('@fye/logger'); | ||
const fetch = require('node-fetch'); | ||
const AbortController = require('abort-controller'); | ||
@@ -11,3 +11,11 @@ const ALLOWED_PRODUCT_TYPES = ['movies', 'games', 'music']; | ||
class AccessIngramClient { | ||
constructor({ account, email, password, host = 'testapi.ingramentertainment.com' }) { | ||
constructor({ | ||
account, | ||
email, | ||
password, | ||
host = 'testapi.ingramentertainment.com', | ||
requestTimeout = 60000, | ||
maxRequestAttempts = 2, | ||
protocol = 'https', | ||
}) { | ||
if (!account) throw new Error('account is required'); | ||
@@ -20,5 +28,40 @@ if (!email) throw new Error('email is required'); | ||
this.password = password; | ||
this.baseUrl = `https://${host}`; | ||
this.baseUrl = `${protocol}://${host}`; | ||
this._requestTimeout = requestTimeout; | ||
this.maxRequestAttempts = maxRequestAttempts; | ||
} | ||
set requestTimout(value) { | ||
this._requestTimeout = value; | ||
} | ||
async fetch({ url, options, requestCount = 0 }) { | ||
Logger.debug('fetch() url:', url); | ||
Logger.debug('fetch() options:', options); | ||
const controller = new AbortController(); | ||
const timeout = setTimeout(() => { | ||
controller.abort(); | ||
}, this._requestTimeout); | ||
try { | ||
// eslint-disable-next-line no-param-reassign | ||
requestCount += 1; | ||
Logger.debug(`Making request ${requestCount}`); | ||
// fetch will not be returned. Instead we await and return the result | ||
const res = await fetch(url, { | ||
...options, | ||
signal: controller.signal, | ||
}); | ||
return res; | ||
} catch (error) { | ||
if (error.message.includes('The user aborted a request') && requestCount < this.maxRequestAttempts) { | ||
Logger.info('authenticate() request aborted due to timeout, retrying'); | ||
return this.fetch({ url, options, requestCount }); | ||
} | ||
throw error; | ||
} finally { | ||
clearTimeout(timeout); | ||
} | ||
} | ||
async request({ version = 'v1', path, method = 'GET', body }) { | ||
@@ -32,6 +75,9 @@ const authToken = await this.authenticate(); | ||
const res = await fetch(`${this.baseUrl}/${version}/${path}`, { | ||
headers, | ||
method, | ||
body: typeof body === 'object' ? JSON.stringify(body) : body, | ||
const res = await this.fetch({ | ||
url: `${this.baseUrl}/${version}/${path}`, | ||
options: { | ||
headers, | ||
method, | ||
body: typeof body === 'object' ? JSON.stringify(body) : body, | ||
}, | ||
}); | ||
@@ -58,12 +104,16 @@ | ||
Logger.debug('authenticate() fetching auth token'); | ||
const res = await fetch(`${this.baseUrl}/authenticate`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Accept: 'application/json', | ||
const res = await this.fetch({ | ||
url: `${this.baseUrl}/authenticate`, | ||
options: { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Accept: 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
email: this.email, | ||
password: this.password, | ||
}), | ||
}, | ||
body: JSON.stringify({ | ||
email: this.email, | ||
password: this.password, | ||
}), | ||
}); | ||
@@ -70,0 +120,0 @@ |
{ | ||
"name": "@fye/ingram-api", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Client for the AccessIngram API", | ||
@@ -14,3 +14,3 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "echo \"No tests for the Ingram Api package.\"" | ||
"test": "ava" | ||
}, | ||
@@ -40,2 +40,3 @@ "ava": { | ||
"@fye/logger": "^1.0.1", | ||
"abort-controller": "^3.0.0", | ||
"node-fetch": "^2.6.7" | ||
@@ -52,3 +53,3 @@ }, | ||
}, | ||
"gitHead": "8ba28e50e2ab88b878ff4cef2bd76cfc8e5e144a" | ||
"gitHead": "24291578fc8cc55685566acb52738e116ab40b18" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
6908
175
1
1
3
+ Addedabort-controller@^3.0.0
+ Addedabort-controller@3.0.0(transitive)
+ Addedevent-target-shim@5.0.1(transitive)