ola-map-sdk 1.1.2 → 1.1.3
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 +3 -1
- package/src/client.js +14 -2
- package/src/endpoints/places.js +11 -6
- package/src/endpoints/routing.js +16 -15
- package/src/endpoints/tiles.js +10 -10
package/package.json
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ola-map-sdk",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.3",
|
|
4
4
|
"description": "A Node.js SDK for Ola Maps API",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"ola",
|
|
8
8
|
"maps",
|
|
9
9
|
"sdk",
|
|
10
|
+
"ola map",
|
|
11
|
+
"maps api",
|
|
10
12
|
"ola maps sdk",
|
|
11
13
|
"ola maps api",
|
|
12
14
|
"ola maps sdk for node.js"
|
package/src/client.js
CHANGED
|
@@ -6,16 +6,28 @@ const Routing = require('./endpoints/routing');
|
|
|
6
6
|
|
|
7
7
|
class OlaMapsClient {
|
|
8
8
|
constructor(apiKey) {
|
|
9
|
+
if (!apiKey && !process.env.OLA_MAPS_API_KEY) {
|
|
10
|
+
throw new Error('API key is required.');
|
|
11
|
+
}
|
|
12
|
+
|
|
9
13
|
this.apiKey = apiKey || process.env.OLA_MAPS_API_KEY;
|
|
10
|
-
this.client =
|
|
14
|
+
this.client = this.createAxiosClient();
|
|
15
|
+
|
|
16
|
+
this.initializeEndpoints();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
createAxiosClient() {
|
|
20
|
+
return axios.create({
|
|
11
21
|
baseURL: 'https://api.olamaps.io',
|
|
12
22
|
headers: {
|
|
13
|
-
'
|
|
23
|
+
'Accept': 'application/json',
|
|
14
24
|
'X-Request-Id': uuidv4(),
|
|
15
25
|
'X-Correlation-Id': uuidv4(),
|
|
16
26
|
},
|
|
17
27
|
});
|
|
28
|
+
}
|
|
18
29
|
|
|
30
|
+
initializeEndpoints() {
|
|
19
31
|
this.places = new Places(this.client, this.apiKey);
|
|
20
32
|
this.tiles = new Tiles(this.client, this.apiKey);
|
|
21
33
|
this.routing = new Routing(this.client, this.apiKey);
|
package/src/endpoints/places.js
CHANGED
|
@@ -6,14 +6,16 @@ class Places {
|
|
|
6
6
|
|
|
7
7
|
async request(endpoint, params) {
|
|
8
8
|
try {
|
|
9
|
-
const
|
|
10
|
-
|
|
9
|
+
const { data } = await this.client.get(endpoint, {
|
|
10
|
+
params: { ...params, api_key: this.apiKey }
|
|
11
|
+
});
|
|
12
|
+
return data;
|
|
11
13
|
} catch (error) {
|
|
12
14
|
const errorMessage = error.response
|
|
13
|
-
? `Error
|
|
14
|
-
: `
|
|
15
|
-
console.error(errorMessage);
|
|
16
|
-
throw
|
|
15
|
+
? `Error ${error.response.status}: ${error.response.data.message}`
|
|
16
|
+
: `Request failed: ${error.message}`;
|
|
17
|
+
console.error(`Places API request failed: ${errorMessage}`);
|
|
18
|
+
throw new Error(`Places API request failed: ${errorMessage}`);
|
|
17
19
|
}
|
|
18
20
|
}
|
|
19
21
|
|
|
@@ -26,6 +28,9 @@ class Places {
|
|
|
26
28
|
}
|
|
27
29
|
|
|
28
30
|
reverseGeocode(lat, lng) {
|
|
31
|
+
if (typeof lat !== 'number' || typeof lng !== 'number') {
|
|
32
|
+
throw new TypeError('Latitude and longitude must be numbers');
|
|
33
|
+
}
|
|
29
34
|
return this.request('/places/v1/reverse-geocode', { latlng: `${lat},${lng}` });
|
|
30
35
|
}
|
|
31
36
|
}
|
package/src/endpoints/routing.js
CHANGED
|
@@ -5,32 +5,33 @@ class Routing {
|
|
|
5
5
|
}
|
|
6
6
|
|
|
7
7
|
async getDirections(origin, destination, options = {}) {
|
|
8
|
+
const { lat: originLat, lon: originLon } = origin;
|
|
9
|
+
const { lat: destLat, lon: destLon } = destination;
|
|
10
|
+
|
|
8
11
|
const params = {
|
|
9
12
|
api_key: this.apiKey,
|
|
10
|
-
origin: `${
|
|
11
|
-
destination: `${
|
|
12
|
-
alternatives: options.alternatives
|
|
13
|
-
steps: options.steps
|
|
14
|
-
overview: options.overview
|
|
15
|
-
language: options.language
|
|
16
|
-
traffic_metadata: options.traffic_metadata
|
|
13
|
+
origin: `${originLat},${originLon}`,
|
|
14
|
+
destination: `${destLat},${destLon}`,
|
|
15
|
+
alternatives: options.alternatives ?? false,
|
|
16
|
+
steps: options.steps ?? false,
|
|
17
|
+
overview: options.overview ?? 'full',
|
|
18
|
+
language: options.language ?? 'en',
|
|
19
|
+
traffic_metadata: options.traffic_metadata ?? false,
|
|
17
20
|
};
|
|
18
21
|
|
|
19
22
|
try {
|
|
20
|
-
const
|
|
23
|
+
const { data } = await this.client.post('/routing/v1/directions', null, {
|
|
21
24
|
params,
|
|
22
|
-
headers: {
|
|
23
|
-
'accept': 'application/json',
|
|
24
|
-
},
|
|
25
|
+
headers: { 'Accept': 'application/json' },
|
|
25
26
|
});
|
|
26
27
|
|
|
27
|
-
return
|
|
28
|
+
return data;
|
|
28
29
|
} catch (error) {
|
|
29
30
|
const errorMessage = error.response
|
|
30
|
-
? `Error: ${error.response.status} - ${error.response.data.message}`
|
|
31
|
-
: `Error: ${error.message}`;
|
|
31
|
+
? `Routing API Error: ${error.response.status} - ${error.response.data.message}`
|
|
32
|
+
: `Routing API Error: ${error.message}`;
|
|
32
33
|
console.error(errorMessage);
|
|
33
|
-
throw
|
|
34
|
+
throw new Error(errorMessage);
|
|
34
35
|
}
|
|
35
36
|
}
|
|
36
37
|
}
|
package/src/endpoints/tiles.js
CHANGED
|
@@ -4,32 +4,32 @@ class Tiles {
|
|
|
4
4
|
this.apiKey = apiKey;
|
|
5
5
|
}
|
|
6
6
|
|
|
7
|
-
async request(endpoint, params, headers = {}, responseType = 'json') {
|
|
7
|
+
async request(endpoint, params = {}, headers = {}, responseType = 'json') {
|
|
8
8
|
try {
|
|
9
9
|
const response = await this.client.get(endpoint, {
|
|
10
10
|
params: { ...params, api_key: this.apiKey },
|
|
11
|
-
headers: { '
|
|
11
|
+
headers: { 'Accept': 'application/json', ...headers },
|
|
12
12
|
responseType,
|
|
13
13
|
});
|
|
14
14
|
return response.data;
|
|
15
15
|
} catch (error) {
|
|
16
16
|
const errorMessage = error.response
|
|
17
|
-
? `Error: ${error.response.status} - ${error.response.data.message}`
|
|
18
|
-
: `Error: ${error.message}`;
|
|
17
|
+
? `Tiles API Error: ${error.response.status} - ${error.response.data.message}`
|
|
18
|
+
: `Tiles API Error: ${error.message}`;
|
|
19
19
|
console.error(errorMessage);
|
|
20
|
-
throw
|
|
20
|
+
throw new Error(errorMessage);
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
getDataTileJSON(datasetName) {
|
|
25
|
-
return this.request(`/tiles/vector/v1/data/${datasetName}.json`);
|
|
25
|
+
return this.request(`/tiles/vector/v1/data/${encodeURIComponent(datasetName)}.json`);
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
getPBFFile(datasetName, z, x, y) {
|
|
29
29
|
return this.request(
|
|
30
|
-
`/tiles/vector/v1/data/${datasetName}/${z}/${x}/${y}.pbf`,
|
|
30
|
+
`/tiles/vector/v1/data/${encodeURIComponent(datasetName)}/${z}/${x}/${y}.pbf`,
|
|
31
31
|
{},
|
|
32
|
-
{ '
|
|
32
|
+
{ 'Accept': 'application/x-protobuf' },
|
|
33
33
|
'arraybuffer'
|
|
34
34
|
);
|
|
35
35
|
}
|
|
@@ -39,14 +39,14 @@ class Tiles {
|
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
getStyleDetail(styleName) {
|
|
42
|
-
return this.request(`/tiles/vector/v1/styles/${styleName}/style.json`);
|
|
42
|
+
return this.request(`/tiles/vector/v1/styles/${encodeURIComponent(styleName)}/style.json`);
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
getFontGlyphs(fontstack, start, end) {
|
|
46
46
|
return this.request(
|
|
47
47
|
`/tiles/vector/v1/fonts/${encodeURIComponent(fontstack)}/${start}-${end}.pbf`,
|
|
48
48
|
{},
|
|
49
|
-
{ '
|
|
49
|
+
{ 'Accept': 'application/x-protobuf' },
|
|
50
50
|
'arraybuffer'
|
|
51
51
|
);
|
|
52
52
|
}
|