HereClient.js 2.02 KB
Newer Older
Hanadi's avatar
Hanadi committed
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.HereClient = void 0;
const node_fetch_1 = __importDefault(require("node-fetch"));
const BASE_URL = "https://traffic.ls.hereapi.com";
const TRAFFIC_PATH = "traffic/6.1";
class HereClient {
    constructor(apiKey) {
        this.apiKey = apiKey;
    }
    flow(position, units = "metric") {
        return __awaiter(this, void 0, void 0, function* () {
            const prox = `${position.latitude},${position.longitude},${position.distance}`;
            const urlPath = `${TRAFFIC_PATH}/flow.json`;
            return this.get(urlPath, { prox, units });
        });
    }
    get(urlPath, params) {
        return __awaiter(this, void 0, void 0, function* () {
            let url = `${BASE_URL}/${urlPath}`;
            if (!params)
                params = {};
            params.apiKey = this.apiKey;
            params.responseattributes = "shape";
            url += "?";
            url += Object.entries(params)
                .map(([key, value]) => `${key}=${value}`)
                .join("&");
            return node_fetch_1.default(url).then(res => res.json());
        });
    }
}
exports.HereClient = HereClient;
//# sourceMappingURL=HereClient.js.map