-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.js
210 lines (186 loc) · 5.07 KB
/
common.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
"use strict";
/* global document */
/**
* @file generic function of heremap
* @author devbab
*/
const request = require("superagent");
const path = require("path");
// by default, unless specified fby calling config
// environment usable with node
let APP_ID = process.env.APP_ID;
let APP_CODE = process.env.APP_CODE;
let CIT = ""; // production by default
let PROTOCOL = "https:"; // by default
let _useHTTPS = true; // by default
let _home = "./";
// find out where we are and relative position for png/svg files
// pay attention if library is build in ./dist
if (process.browser) {
let _scripts = document.getElementsByTagName("script");
const len = _scripts.length - 1;
for (let i = 0; i < len; i++) {
const src = _scripts[i].src;
const file = path.basename(src);
if (file == "libhere.min.js") _home = path.dirname(src)+"/";
}
}
/**
* To configure app_id, app_code and optionally use CIT and http
* @alias hm:config
*
* @param opt {Object} - `opt` with parameters.
* @param [opt.app_id] {string} - the app_id from developer.here.com
* @param [opt.app_code] {string} - the app_code from developer.here.com
* @param [opt.useCIT=false] {boolean} - true to use CIT environment.
* @param [opt.useHTTP=false] {string} - true to use HTTP.
* @param [opt.useHTTPS=true] {string} - true to use HTTPS.
*
* @example
* ```js
* hm.config({
* app_id: "YOUR APP_ID",
* app_code: "YOUR APP_CODE",
* });
* ```
*/
function config(opt) {
if (opt.app_id) APP_ID = opt.app_id;
if (opt.app_code) APP_CODE = opt.app_code;
if (opt.useCIT) CIT = ".cit";
if (opt.useHTTP) {
PROTOCOL = "http:";
_useHTTPS = false;
}
if (opt.useHTTPS) {
PROTOCOL = "https:";
_useHTTPS = true;
}
}
/**
* return URL of module home directory.
* svg icons are under getHome()+"/svg/"
* images are under getHome()+"/img/"
* @ignore
* @alias hm:getHome
* @return {string} url of home directory including http or https.
*/
function getHome() {
return _home;
}
/**
* returns app_id
* @ignore
* @alias hm:getAppId
* @return {string} app_id
*/
function getAppId() {
return APP_ID;
}
/**
* return app_code
* @ignore
* @alias hm:getHome
* @alias hm:getAppCode
* @return {string} app_code
*/
function getAppCode() {
return APP_CODE;
}
/**
* return true if using CIT
* @ignore
* @alias hm:getCIT
* @return {booolean} true if using CIT
*/
function getCIT() {
return CIT;
}
/**
* return protocol used, http:// or https://
* @ignore
* @alias hm:getProtocol
* @return {string} - protocol
*/
function getProtocol() {
return PROTOCOL;
}
/**
* return true is https is used
* @ignore
* @alias hm:useHTTPS
* @return {boolean} - true if https is used
*/
function useHTTPS() {
return _useHTTPS;
}
/**
* add credentials to object provided
* @ignore
* @alias hm:addCredentials
* @param {...objects} list of `objects`
* @return {object} object with all input objectconcatenated, and app_id/app_code inserted
*/
function addCredentials(...obj) {
return Object.assign({
app_id: APP_ID,
app_code: APP_CODE
}, ...obj);
}
/**
* build HERE REST full url, taking in account protocol and cit. for instance cm.buildUrl("geocoder", "api.here.com/6.2/geocode.json"
* @ignore
* @alias hm:buildUrl
* @param {string} base - base name
* @param {string} endpoint - end point
* @return {string} full url
*/
function buildUrl(base, endpoint) {
return PROTOCOL + "//" + base + CIT + "." + endpoint;
}
/**
* does get/post request to HERE RESDT backend and manage main errors
* @ignore
* @alias hm:hereRest
* @param {string} url - url to call , for instance from buildUrl
* @param {object} settings - settings to add in request
* @param {string} mode=get - mode "get" or "post"
* @return {promise} - promise to resolve/reject
*/
async function hereRest(url, settings, mode = "get", needresp = "true") {
let p = request.get(url);
if (mode == "post")
p = request.post(url);
return p
.query(settings)
.then(res => {
// res.body, res.headers, res.status
if (res.status != "200") {
let e = new Error("Error " + res.status + ":" + res.body); // e.message
throw (e);
}
if (needresp && !res.body.Response && !res.body.response) { // one of the two should be in the answer
console.error(res.body);
let e = new Error("Query error:" + res.body); // e.message
throw (e);
}
if (res.body.response && res.body.response.type == "ApplicationError") {
console.error(res.body);
let e = new Error("Error" + res.body.response.details); // e.message
throw (e);
}
return res;
});
}
module.exports = {
config: config,
buildUrl: buildUrl,
getAppId: getAppId,
getAppCode: getAppCode,
getCIT: getCIT,
getProtocol: getProtocol,
getHome: getHome,
useHTTPS: useHTTPS,
addCredentials: addCredentials,
hereRest: hereRest
};