-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
30 lines (23 loc) · 824 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
'use strict'
const cheerio = require('cheerio')
const request = require('request-promise-native')
module.exports = ({ lat, lng, city, region, country } = {}) => {
const apiUrl = 'https://www.starbucks.com/store-locator'
return new Promise((resolve, reject) => {
if ((!lat || !lng, !city, !region, !country)) {
return reject(new TypeError('Options are required'))
}
const api = `${apiUrl}?map=${lat},${lng}&place=${city},${region},${country}`
request(api).then(res => {
if (res) {
const $ = cheerio.load(res)
const json = JSON.parse($('#bootstrapData').text())
const stores = json.storeLocator.locationState.locations
resolve(stores)
}
reject(
new TypeError(`Couldn't find any Starbucks store on this location`)
)
})
})
}