tesla-inventory 3.2.0-7 → 3.2.0-9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +6 -2
- package/src/index.js +13 -25
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "tesla-inventory",
|
|
3
3
|
"description": "Retrieve real-time data from Tesla Inventory.",
|
|
4
4
|
"homepage": "https://github.com/Kikobeats/tesla-inventory",
|
|
5
|
-
"version": "3.2.0-
|
|
5
|
+
"version": "3.2.0-9",
|
|
6
6
|
"main": "src/index.js",
|
|
7
7
|
"author": {
|
|
8
8
|
"email": "josefrancisco.verdu@gmail.com",
|
|
@@ -34,6 +34,9 @@
|
|
|
34
34
|
"teslaapi",
|
|
35
35
|
"teslamotors"
|
|
36
36
|
],
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"p-retry": "4"
|
|
39
|
+
},
|
|
37
40
|
"devDependencies": {
|
|
38
41
|
"@commitlint/cli": "latest",
|
|
39
42
|
"@commitlint/config-conventional": "latest",
|
|
@@ -103,7 +106,8 @@
|
|
|
103
106
|
},
|
|
104
107
|
"nano-staged": {
|
|
105
108
|
"*.js": [
|
|
106
|
-
"prettier-standard"
|
|
109
|
+
"prettier-standard",
|
|
110
|
+
"standard --fix"
|
|
107
111
|
],
|
|
108
112
|
"*.md": [
|
|
109
113
|
"standard-markdown"
|
package/src/index.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
3
|
const debug = require('debug-logfmt')('tesla-inventory')
|
|
4
|
+
const pRetry = require('p-retry')
|
|
5
|
+
|
|
4
6
|
const inventories = require('./inventories')
|
|
5
7
|
|
|
6
8
|
const timestamp =
|
|
@@ -13,14 +15,11 @@ const uniqBy = (arr, prop) =>
|
|
|
13
15
|
|
|
14
16
|
const ITEMS_PER_PAGE = 50
|
|
15
17
|
|
|
16
|
-
module.exports =
|
|
18
|
+
module.exports = fetcher => async (inventory, opts, fetcherOpts) => {
|
|
17
19
|
if (!inventories[inventory]) {
|
|
18
20
|
throw new TypeError(`Tesla inventory \`${inventory}\` not found!`)
|
|
19
21
|
}
|
|
20
22
|
|
|
21
|
-
let teardown
|
|
22
|
-
const browserless = await getBrowserless(fn => (teardown = fn))
|
|
23
|
-
|
|
24
23
|
if (opts.model && !opts.model.startsWith('m')) {
|
|
25
24
|
opts.model = `m${opts.model}`
|
|
26
25
|
}
|
|
@@ -30,14 +29,6 @@ module.exports = getBrowserless => async (inventory, opts, gotoOpts) => {
|
|
|
30
29
|
const duration = timestamp()
|
|
31
30
|
|
|
32
31
|
const paginate = async (offset = 0) => {
|
|
33
|
-
const fn = browserless.evaluate(async (page, response) => response.text(), {
|
|
34
|
-
abortTypes: ['image', 'stylesheet', 'font', 'other'],
|
|
35
|
-
adblock: false,
|
|
36
|
-
waitUntil: 'networkidle0',
|
|
37
|
-
animations: true,
|
|
38
|
-
...gotoOpts
|
|
39
|
-
})
|
|
40
|
-
|
|
41
32
|
const url = new URL(
|
|
42
33
|
`https://www.tesla.com/inventory/api/v1/inventory-results?${new URLSearchParams({
|
|
43
34
|
query: JSON.stringify({
|
|
@@ -52,17 +43,16 @@ module.exports = getBrowserless => async (inventory, opts, gotoOpts) => {
|
|
|
52
43
|
|
|
53
44
|
debug({ url, ...query })
|
|
54
45
|
|
|
55
|
-
const result = await
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
return paginate(offset)
|
|
46
|
+
const result = await pRetry(
|
|
47
|
+
() =>
|
|
48
|
+
fetcher(url, fetcherOpts).then(async text => {
|
|
49
|
+
const body = JSON.parse(text)
|
|
50
|
+
return { items: body.results }
|
|
51
|
+
}),
|
|
52
|
+
{
|
|
53
|
+
onFailedAttempt: debug.error
|
|
64
54
|
}
|
|
65
|
-
|
|
55
|
+
)
|
|
66
56
|
|
|
67
57
|
return result
|
|
68
58
|
}
|
|
@@ -78,7 +68,5 @@ module.exports = getBrowserless => async (inventory, opts, gotoOpts) => {
|
|
|
78
68
|
debug.info({ ...opts, items: items.length, duration: duration() })
|
|
79
69
|
} while (page.items.length >= ITEMS_PER_PAGE)
|
|
80
70
|
|
|
81
|
-
|
|
82
|
-
if (teardown) await teardown()
|
|
83
|
-
return result
|
|
71
|
+
return items.filter(item => item.Model === opts.model)
|
|
84
72
|
}
|