Commit 03149702 authored by Hanadi's avatar Hanadi
Browse files

Initial Commit

parents
export type GeoProximity = {
latitude: number;
longitude: number;
distance: number;
}
export type UnitsType = "metric" | "imperial";
export type FlowResponse = {
/**
* A list of Roadway (RW) items.
*/
RWS: Roadways[];
/**
* Units of measurements used for flow items.
*/
UNITS: UnitsType
}
export type Roadways = {
/**
* This is the composite item for flow across an entire roadway.
* A roadway item will be present for each roadway with traffic flow information available.
*/
RW: Roadway[];
/**
* Units of measurements used for flow items in this RWS element.
*/
UNITS: UnitsType
}
export type Roadway = {
/**
* A list of Flow Item (FI) elements
*/
FIS: FlowItems[];
/**
* Unique string identifier for this Linear
*/
LI: string;
/**
* Text description of the road
*/
DE: string;
/**
* Basetimestamp used as a reference for all predictive calculations in this Roadway
*/
PBT: string;
}
export type FlowItems = {
/**
* A single flow item.
*/
FI: FlowItem[];
}
export type FlowItem = {
/**
* An ordered collection of TMC locations.
*/
TMC: TMCType;
/**
* Current Flow. This element contains details about speed and Jam Factor information for the given flow item.
*/
CF: CFType[];
/**
* A list of the flow item segments.
*/
SHP?: SegmentInformation[];
}
export type SegmentInformation = {
/**
* A value of the points in the segment represented as lat,lon string separated by spaces.
* E.g. "48.78905,9.18971 48.78916,9.18932 48.78919,9.1892 48.78921,9.1891 "
*/
value: string;
}
export type TMCType = {
/**
* Point TMC Location Code
*/
PC: number;
/**
* Description for the given location.
*/
DE: string;
/**
* Queuing direction. '+' or '-'. Note this is the opposite of the travel direction in the fully qualified ID,
* for example for location 107+03021 the QD would be '-'.
*/
QD: "+" | "-";
/**
* Length of item (based on UNITS)
*/
LE: number;
}
export type CFType = {
/**
* Used when it is needed to differentiate between different kinds of location types.
*/
TY: string;
/**
* Speed (based on UNITS) capped by speed limit.
*/
SP: number;
/**
* Speed (based on UNITS) not capped by speed limit
*/
SU: number;
/**
* The free flow speed on this stretch of road.
*/
FF: number;
/**
* The number between 0.0 and 10.0 indicating the expected quality of travel. When there is a road closure, the Jam Factor will be 10.
* As the number approaches 10.0 the quality of travel is getting worse. -1.0 indicates that a Jam Factor could not be calculated.
*/
JF: number;
/**
* Confidence, an indication of how the speed was determined. -1.0 road closed. 1.0=100% 0.7-100% Historical Usually a value between .7 and 1.0.
*/
CN: number;
}
\ No newline at end of file
import fetch from "node-fetch";
import {FlowResponse, GeoProximity, UnitsType} from "./DataTypes";
const BASE_URL = "https://traffic.ls.hereapi.com";
const TRAFFIC_PATH = "traffic/6.1";
export class HereClient {
constructor(
private apiKey: string
) {}
async flow(position: GeoProximity, units: UnitsType = "metric"): Promise<FlowResponse> {
const prox = `${position.latitude},${position.longitude},${position.distance}`;
const urlPath = `${TRAFFIC_PATH}/flow.json`;
return this.get(urlPath, {prox, units});
}
private async get(urlPath: string, params: { [key: string]: string }): Promise<any> {
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 fetch(url).then(res => res.json());
}
}
const log = (text: string, logType: "INFO" | "ERROR" | "DEBUG") => {
const time = new Date().toISOString();
console.log(`${time} ${logType} ${text}`);
};
export const info = (text: string) => log(text, "INFO");
export const error = (text: string, err?: Error) => {
log(text, "ERROR");
if (err)
console.error(err);
};
export const debug = (text: string) => log(text, "DEBUG");
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"lib": [
"es2019"
],
"sourceMap": true,
"rootDir": "./",
"outDir": "./build",
"strict": true,
"resolveJsonModule": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true
},
"exclude": [
"node_modules"
]
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment