tesla-inventory 3.0.237 → 3.0.239

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 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.0.237",
5
+ "version": "3.0.239",
6
6
  "main": "src/index.js",
7
7
  "author": {
8
8
  "email": "josefrancisco.verdu@gmail.com",
@@ -82,6 +82,9 @@
82
82
  "update:dependencies": "ncu -u"
83
83
  },
84
84
  "license": "MIT",
85
+ "ava": {
86
+ "timeout": "2m"
87
+ },
85
88
  "commitlint": {
86
89
  "extends": [
87
90
  "@commitlint/config-conventional"
package/src/index.js CHANGED
@@ -1,17 +1,26 @@
1
1
  'use strict'
2
2
 
3
+ const debug = require('debug-logfmt')('tesla-inventory')
3
4
  const inventories = require('./inventories')
4
5
 
6
+ const timestamp = (start = process.hrtime.bigint()) => () =>
7
+ Math.round(Number(process.hrtime.bigint() - start) / 1e6) + 'ms'
8
+
5
9
  const got = require('got').extend({
6
10
  url: 'https://www.tesla.com/inventory/api/v1/inventory-results',
7
11
  responseType: 'json',
8
12
  resolveBodyOnly: true
9
13
  })
10
14
 
11
- const toLowerCase = ({ results: items, total_matches_found: totalMatchesFound }) => ({
12
- items,
13
- total: Number(totalMatchesFound)
14
- })
15
+ const toLowerCase = ({ results: items, total_matches_found: totalMatchesFound, ...rest }) => {
16
+ return {
17
+ items,
18
+ total: Number(totalMatchesFound)
19
+ }
20
+ }
21
+
22
+ const uniqBy = (arr, prop) =>
23
+ arr.filter((x, i, self) => i === self.findIndex(y => x[prop] === y[prop]))
15
24
 
16
25
  const ITEMS_PER_PAGE = 50
17
26
 
@@ -26,12 +35,14 @@ module.exports = async (inventory, opts, { headers, ...gotOpts } = {}) => {
26
35
  opts.model = `m${opts.model}`
27
36
  }
28
37
 
29
- const paginate = (outsideOffset = 0) =>
38
+ const duration = timestamp()
39
+
40
+ const paginate = async (outsideOffset = 0) =>
30
41
  got({
31
42
  searchParams: {
32
43
  query: JSON.stringify({
33
44
  outsideOffset,
34
- count: 0,
45
+ count: ITEMS_PER_PAGE,
35
46
  query: {
36
47
  ...inventoryProps,
37
48
  ...opts
@@ -42,22 +53,16 @@ module.exports = async (inventory, opts, { headers, ...gotOpts } = {}) => {
42
53
  headers: { 'user-agent': undefined, ...headers }
43
54
  }).then(toLowerCase)
44
55
 
45
- const page = await paginate()
46
-
47
- const allPages = async () => {
48
- const nRequests = Math.ceil(page.total / ITEMS_PER_PAGE) - 1
49
- const offsets = [...Array(nRequests).keys()].map(n => (n + 1) * page.items.length)
50
- const pages = await Promise.all(offsets.map(paginate))
51
- return [page, ...pages].reduce((acc, page) => {
52
- page.items.forEach(item => {
53
- const isAlready = acc.some(({ VIN }) => VIN === item.VIN)
54
- if (!isAlready) acc.push(item)
55
- })
56
- return acc
57
- }, [])
58
- }
56
+ let offset = 0
57
+ let items = []
58
+ let page
59
59
 
60
- const promise = page.total < ITEMS_PER_PAGE ? Promise.resolve(page.items) : allPages()
60
+ do {
61
+ page = await paginate(offset)
62
+ items = uniqBy(items.concat(page.items), 'VIN')
63
+ offset = items.length
64
+ debug({ items: items.length, duration: duration() })
65
+ } while (page.items.length >= ITEMS_PER_PAGE)
61
66
 
62
- return promise.then(items => items.filter(item => item.Model === opts.model))
67
+ return items.filter(item => item.Model === opts.model)
63
68
  }
@@ -20,7 +20,7 @@
20
20
  "MT304": 136600,
21
21
  "MT308": 55720,
22
22
  "MT310": 115720,
23
- "MT311": 136600,
23
+ "MT311": 124434,
24
24
  "MT314": 84810,
25
25
  "MT315": 115720,
26
26
  "MT316": 136600,
@@ -40,7 +40,7 @@
40
40
  "MT317": 15760,
41
41
  "MT320": 6760,
42
42
  "MT322": 6760,
43
- "MT323": 15760,
43
+ "MT323": 15010,
44
44
  "MT325": 17010,
45
45
  "MT328": 15010,
46
46
  "MT337": 6760,
@@ -14,7 +14,7 @@
14
14
  "MT322": 174900,
15
15
  "MT324": 334900,
16
16
  "MTS14": 1665000,
17
- "MTX13": 1035000,
17
+ "MTX13": 675000,
18
18
  "MTY05": 360600,
19
19
  "MTY11": 240600,
20
20
  "PBSB": 32400,
@@ -41,21 +41,21 @@
41
41
  "IWW00": 2000,
42
42
  "MT10A": 15000,
43
43
  "MT10L": 36590,
44
- "MT301": 8490,
44
+ "MT301": 7990,
45
45
  "MT302": 8900,
46
46
  "MT303": 13990,
47
47
  "MT304": 18990,
48
48
  "MT305": 15900,
49
- "MT308": 8490,
49
+ "MT308": 7990,
50
50
  "MT310": 14990,
51
51
  "MT311": 18990,
52
- "MT314": 8490,
52
+ "MT314": 7990,
53
53
  "MT315": 14990,
54
54
  "MT317": 18990,
55
55
  "MT321": 14990,
56
- "MT322": 8490,
56
+ "MT322": 7990,
57
57
  "MT324": 14990,
58
- "MT337": 8490,
58
+ "MT337": 7990,
59
59
  "MT75A": 15000,
60
60
  "MT75R": 18000,
61
61
  "MT90A": 15000,