tesla-inventory 3.2.32 → 3.2.34
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/README.md +7 -1
- package/package.json +2 -2
- package/src/index.js +53 -50
- package/src/prices/au.json +1 -0
- package/src/prices/no.json +1 -0
package/README.md
CHANGED
|
@@ -40,7 +40,7 @@ Type: `function`
|
|
|
40
40
|
|
|
41
41
|
The fetcher function used for performing the networking calls. It should return text ([example](https://github.com/teslahunt/inventory/blob/master/test/index.js#L6)).
|
|
42
42
|
|
|
43
|
-
### .teslaInventory([query], [
|
|
43
|
+
### .teslaInventory([inventory], [query], [fetcherOpts])
|
|
44
44
|
|
|
45
45
|
#### inventory
|
|
46
46
|
|
|
@@ -62,6 +62,12 @@ These options can be:
|
|
|
62
62
|
- **model**: ms|mx|m3
|
|
63
63
|
- **order**: asc|desc
|
|
64
64
|
|
|
65
|
+
#### fetcherOpts
|
|
66
|
+
|
|
67
|
+
Type: `object`
|
|
68
|
+
|
|
69
|
+
The options to be passed against `fetcher`.
|
|
70
|
+
|
|
65
71
|
## License
|
|
66
72
|
|
|
67
73
|
**tesla-inventory** © [Tesla Hunt](https://teslahunt.io), released under the [MIT](https://github.com/teslahunt/inventory/blob/master/LICENSE.md) License.<br>
|
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.
|
|
5
|
+
"version": "3.2.34",
|
|
6
6
|
"main": "src/index.js",
|
|
7
7
|
"exports": {
|
|
8
8
|
".": "./src/index.js",
|
|
@@ -82,7 +82,7 @@
|
|
|
82
82
|
"markdown-tables-to-json": "latest",
|
|
83
83
|
"nano-staged": "latest",
|
|
84
84
|
"npm-check-updates": "latest",
|
|
85
|
-
"puppeteer": "
|
|
85
|
+
"puppeteer": "21.2.1",
|
|
86
86
|
"signal-exit": "latest",
|
|
87
87
|
"simple-git-hooks": "latest",
|
|
88
88
|
"standard": "latest",
|
package/src/index.js
CHANGED
|
@@ -15,58 +15,61 @@ const uniqBy = (arr, prop) =>
|
|
|
15
15
|
|
|
16
16
|
const ITEMS_PER_PAGE = 50
|
|
17
17
|
|
|
18
|
-
module.exports =
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
if (opts.model && !opts.model.startsWith('m')) {
|
|
24
|
-
opts.model = `m${opts.model}`
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const { country, ...query } = { ...inventories[inventory], ...opts }
|
|
28
|
-
|
|
29
|
-
const duration = timestamp()
|
|
30
|
-
|
|
31
|
-
const paginate = async (offset = 0) => {
|
|
32
|
-
const url = new URL(
|
|
33
|
-
`https://www.tesla.com/inventory/api/v1/inventory-results?${new URLSearchParams({
|
|
34
|
-
query: JSON.stringify({
|
|
35
|
-
query,
|
|
36
|
-
count: ITEMS_PER_PAGE,
|
|
37
|
-
offset,
|
|
38
|
-
outsideOffset: 0,
|
|
39
|
-
outsideSearch: false
|
|
40
|
-
})
|
|
41
|
-
}).toString()}`
|
|
42
|
-
).toString()
|
|
43
|
-
|
|
44
|
-
debug({ url, ...query })
|
|
45
|
-
|
|
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
|
|
18
|
+
module.exports =
|
|
19
|
+
fetcher =>
|
|
20
|
+
async (inventory, opts, { retries = 2, ...fetcherOpts } = {}) => {
|
|
21
|
+
if (!inventories[inventory]) {
|
|
22
|
+
throw new TypeError(`Tesla inventory \`${inventory}\` not found!`)
|
|
54
23
|
}
|
|
55
|
-
)
|
|
56
24
|
|
|
57
|
-
|
|
58
|
-
|
|
25
|
+
if (opts.model && !opts.model.startsWith('m')) {
|
|
26
|
+
opts.model = `m${opts.model}`
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const { country, ...query } = { ...inventories[inventory], ...opts }
|
|
30
|
+
|
|
31
|
+
const duration = timestamp()
|
|
32
|
+
|
|
33
|
+
const paginate = async (offset = 0) => {
|
|
34
|
+
const url = new URL(
|
|
35
|
+
`https://www.tesla.com/inventory/api/v1/inventory-results?${new URLSearchParams({
|
|
36
|
+
query: JSON.stringify({
|
|
37
|
+
query,
|
|
38
|
+
count: ITEMS_PER_PAGE,
|
|
39
|
+
offset,
|
|
40
|
+
outsideOffset: 0,
|
|
41
|
+
outsideSearch: false
|
|
42
|
+
})
|
|
43
|
+
}).toString()}`
|
|
44
|
+
).toString()
|
|
45
|
+
|
|
46
|
+
debug({ url, ...query })
|
|
47
|
+
|
|
48
|
+
const result = await pRetry(
|
|
49
|
+
() =>
|
|
50
|
+
fetcher(url, fetcherOpts).then(async text => {
|
|
51
|
+
const body = JSON.parse(text)
|
|
52
|
+
return { items: body.results ?? [] }
|
|
53
|
+
}),
|
|
54
|
+
{
|
|
55
|
+
onFailedAttempt: debug.error,
|
|
56
|
+
retries
|
|
57
|
+
}
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
return result
|
|
61
|
+
}
|
|
59
62
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
+
let offset = 0
|
|
64
|
+
let items = []
|
|
65
|
+
let page
|
|
63
66
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
67
|
+
do {
|
|
68
|
+
page = await paginate(offset)
|
|
69
|
+
items = uniqBy(items.concat(page.items), 'VIN')
|
|
70
|
+
offset = items.length
|
|
71
|
+
debug.info({ ...opts, items: items.length, duration: duration() })
|
|
72
|
+
} while (page.items.length >= ITEMS_PER_PAGE)
|
|
70
73
|
|
|
71
|
-
|
|
72
|
-
}
|
|
74
|
+
return items.filter(item => item.Model === opts.model)
|
|
75
|
+
}
|
package/src/prices/au.json
CHANGED