apple-maps-server-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/src/globals.ts CHANGED
@@ -1,146 +1,145 @@
1
- // OBJECTS
2
- type PoiCategory =
3
- | "Airport"
4
- | "AirportGate"
5
- | "AirportTerminal"
6
- | "AmusementPark"
7
- | "ATM"
8
- | "Aquarium"
9
- | "Bakery"
10
- | "Bank"
11
- | "Beach"
12
- | "Brewery"
13
- | "Cafe"
14
- | "Campground"
15
- | "CarRental"
16
- | "EVCharger"
17
- | "FireStation"
18
- | "FitnessCenter"
19
- | "FoodMarket"
20
- | "GasStation"
21
- | "Hospital"
22
- | "Hotel"
23
- | "Laundry"
24
- | "Library"
25
- | "Marina"
26
- | "MovieTheater"
27
- | "Museum"
28
- | "NationalPark"
29
- | "Nightlife"
30
- | "Park"
31
- | "Parking"
32
- | "Pharmacy"
33
- | "Playground"
34
- | "Police"
35
- | "PostOffice"
36
- | "PublicTransport"
37
- | "ReligiousSite"
38
- | "Restaurant"
39
- | "Restroom"
40
- | "School"
41
- | "Stadium"
42
- | "Store"
43
- | "Theater"
44
- | "University"
45
- | "Winery"
46
- | "Zoo";
47
-
48
- type TransportType = "Automobile" | "Transit" | "Walking";
49
-
50
- interface Location {
51
- latitude: number;
52
- longitude: number;
53
- }
54
-
55
- interface MapRegion {
56
- eastLongitude: number;
57
- northLatitude: number;
58
- southLatitude: number;
59
- westLongitude: number;
60
- }
61
-
62
- interface StructuredAddress {
63
- administrativeArea: string;
64
- administrativeAreaCode: string;
65
- areasOfInterest: string[];
66
- dependentLocalities: string[];
67
- fullThoroughfare: string;
68
- locality: string;
69
- postCode: string;
70
- subLocality: string;
71
- subThoroughfare: string;
72
- thoroughfare: string;
73
- }
74
-
75
- interface Place {
76
- country: string;
77
- countryCode: string;
78
- displayMapRegion: MapRegion;
79
- formattedAddressLines: string[];
80
- name: string;
81
- coordinate: Location;
82
- structuredAddress: StructuredAddress;
83
- }
84
-
85
- interface ETA {
86
- destination: Location;
87
- distanceMeters: number;
88
- expectedTravelTimeSeconds: number;
89
- staticTravelTimeSeconds: number;
90
- transportType: TransportType;
91
- }
92
-
93
- interface SearchResponsePlace extends Place {
94
- poiCategory: PoiCategory;
95
- }
96
-
97
- export interface GeocodeInput {
98
- q: string;
99
- limitToCountries?: string[];
100
- lang?: string;
101
- searchLocation?: string;
102
- searchRegion?: string;
103
- userLocation?: string;
104
- }
105
-
106
- export interface ReverseGeocodeInput {
107
- loc: string;
108
- lang?: string;
109
- }
110
-
111
- export interface ETAInput {
112
- origin: string;
113
- destinations: string[];
114
- transportType?: TransportType;
115
- departureDate?: string;
116
- arrivalDate?: string;
117
- }
118
-
119
- export interface SearchInput {
120
- q: string;
121
- excludePoiCategories?: PoiCategory[];
122
- includePoiCategories?: PoiCategory[];
123
- limitToCountries?: string[];
124
- resultTypeFilter?: "Poi" | "Address";
125
- lang?: string;
126
- searchLocation?: string;
127
- searchRegion?: string;
128
- userLocation?: string;
129
- }
130
-
131
- export interface GeocodeResponse {
132
- results: Place[];
133
- }
134
-
135
- export interface ReverseGeocodeResponse {
136
- results: Place[];
137
- }
138
-
139
- export interface ETAResponse {
140
- etas: ETA[];
141
- }
142
-
143
- export interface SearchResponse {
144
- displayMapRegion: MapRegion;
145
- results: SearchResponsePlace[];
146
- }
1
+ type PoiCategory =
2
+ | "Airport"
3
+ | "AirportGate"
4
+ | "AirportTerminal"
5
+ | "AmusementPark"
6
+ | "ATM"
7
+ | "Aquarium"
8
+ | "Bakery"
9
+ | "Bank"
10
+ | "Beach"
11
+ | "Brewery"
12
+ | "Cafe"
13
+ | "Campground"
14
+ | "CarRental"
15
+ | "EVCharger"
16
+ | "FireStation"
17
+ | "FitnessCenter"
18
+ | "FoodMarket"
19
+ | "GasStation"
20
+ | "Hospital"
21
+ | "Hotel"
22
+ | "Laundry"
23
+ | "Library"
24
+ | "Marina"
25
+ | "MovieTheater"
26
+ | "Museum"
27
+ | "NationalPark"
28
+ | "Nightlife"
29
+ | "Park"
30
+ | "Parking"
31
+ | "Pharmacy"
32
+ | "Playground"
33
+ | "Police"
34
+ | "PostOffice"
35
+ | "PublicTransport"
36
+ | "ReligiousSite"
37
+ | "Restaurant"
38
+ | "Restroom"
39
+ | "School"
40
+ | "Stadium"
41
+ | "Store"
42
+ | "Theater"
43
+ | "University"
44
+ | "Winery"
45
+ | "Zoo";
46
+
47
+ type TransportType = "Automobile" | "Transit" | "Walking";
48
+
49
+ interface Location {
50
+ latitude: number;
51
+ longitude: number;
52
+ }
53
+
54
+ interface MapRegion {
55
+ eastLongitude: number;
56
+ northLatitude: number;
57
+ southLatitude: number;
58
+ westLongitude: number;
59
+ }
60
+
61
+ interface StructuredAddress {
62
+ administrativeArea: string;
63
+ administrativeAreaCode: string;
64
+ areasOfInterest: string[];
65
+ dependentLocalities: string[];
66
+ fullThoroughfare: string;
67
+ locality: string;
68
+ postCode: string;
69
+ subLocality: string;
70
+ subThoroughfare: string;
71
+ thoroughfare: string;
72
+ }
73
+
74
+ interface Place {
75
+ country: string;
76
+ countryCode: string;
77
+ displayMapRegion: MapRegion;
78
+ formattedAddressLines: string[];
79
+ name: string;
80
+ coordinate: Location;
81
+ structuredAddress: StructuredAddress;
82
+ }
83
+
84
+ interface ETA {
85
+ destination: Location;
86
+ distanceMeters: number;
87
+ expectedTravelTimeSeconds: number;
88
+ staticTravelTimeSeconds: number;
89
+ transportType: TransportType;
90
+ }
91
+
92
+ interface SearchResponsePlace extends Place {
93
+ poiCategory: PoiCategory;
94
+ }
95
+
96
+ export interface GeocodeInput {
97
+ q: string;
98
+ limitToCountries?: string[];
99
+ lang?: string;
100
+ searchLocation?: string;
101
+ searchRegion?: string;
102
+ userLocation?: string;
103
+ }
104
+
105
+ export interface ReverseGeocodeInput {
106
+ loc: string;
107
+ lang?: string;
108
+ }
109
+
110
+ export interface ETAInput {
111
+ origin: string;
112
+ destinations: string[];
113
+ transportType?: TransportType;
114
+ departureDate?: string;
115
+ arrivalDate?: string;
116
+ }
117
+
118
+ export interface SearchInput {
119
+ q: string;
120
+ excludePoiCategories?: PoiCategory[];
121
+ includePoiCategories?: PoiCategory[];
122
+ limitToCountries?: string[];
123
+ resultTypeFilter?: "Poi" | "Address";
124
+ lang?: string;
125
+ searchLocation?: string;
126
+ searchRegion?: string;
127
+ userLocation?: string;
128
+ }
129
+
130
+ export interface GeocodeResponse {
131
+ results: Place[];
132
+ }
133
+
134
+ export interface ReverseGeocodeResponse {
135
+ results: Place[];
136
+ }
137
+
138
+ export interface ETAResponse {
139
+ etas: ETA[];
140
+ }
141
+
142
+ export interface SearchResponse {
143
+ displayMapRegion: MapRegion;
144
+ results: SearchResponsePlace[];
145
+ }
package/src/index.ts CHANGED
@@ -1,161 +1,147 @@
1
- import axios, { AxiosError, AxiosInstance } from "axios";
2
- import {
3
- GeocodeInput,
4
- GeocodeResponse,
5
- ReverseGeocodeInput,
6
- ReverseGeocodeResponse,
7
- ETAInput,
8
- ETAResponse,
9
- SearchInput,
10
- SearchResponse,
11
- } from "./globals";
12
-
13
- class AppleMaps {
14
- accessToken: string;
15
- authorizationToken: string;
16
- accessTokenRetries: number;
17
- apiClient: AxiosInstance;
18
-
19
- constructor({ authorizationToken }: { authorizationToken: string }) {
20
- this.accessToken = "";
21
- this.accessTokenRetries = 0;
22
- this.authorizationToken = authorizationToken;
23
-
24
- if (!authorizationToken) {
25
- throw new Error("'authorizationToken' param is required");
26
- }
27
-
28
- this.apiClient = axios.create({
29
- baseURL: "https://maps-api.apple.com/v1",
30
- });
31
-
32
- this.getAccessToken();
33
- }
34
-
35
- async getAccessToken(): Promise<undefined> {
36
- try {
37
- const response = await this.apiClient.get("/token", {
38
- headers: {
39
- Authorization: `Bearer ${this.authorizationToken}`,
40
- },
41
- });
42
-
43
- this.accessToken = response.data.accessToken;
44
- } catch (error) {
45
- throw error;
46
- }
47
-
48
- return;
49
- }
50
-
51
- async geocode(input: GeocodeInput): Promise<GeocodeResponse> {
52
- try {
53
- const response = await this.apiClient.get("/geocode", {
54
- headers: {
55
- Authorization: `Bearer ${this.accessToken}`,
56
- },
57
- params: input,
58
- });
59
-
60
- return response.data;
61
- } catch (error) {
62
- if (error instanceof AxiosError) {
63
- if (error.response?.status === 401) {
64
- if (this.accessTokenRetries > 3) {
65
- throw new Error("Unable to get access token");
66
- }
67
-
68
- this.accessTokenRetries++;
69
-
70
- await this.getAccessToken();
71
- return this.geocode(input);
72
- } else {
73
- throw error;
74
- }
75
- } else throw error;
76
- }
77
- }
78
-
79
- async reverseGeocode(input: ReverseGeocodeInput): Promise<ReverseGeocodeResponse> {
80
- try {
81
- const response = await this.apiClient.get("/reverseGeocode", {
82
- headers: {
83
- Authorization: `Bearer ${this.accessToken}`,
84
- },
85
- params: input,
86
- });
87
-
88
- return response.data;
89
- } catch (error) {
90
- if (error instanceof AxiosError) {
91
- if (error.response?.status === 401) {
92
- if (this.accessTokenRetries > 3) {
93
- throw new Error("Unable to get access token");
94
- }
95
-
96
- this.accessTokenRetries++;
97
- await this.getAccessToken();
98
- return this.reverseGeocode(input);
99
- } else {
100
- throw error.response?.data;
101
- }
102
- } else throw error;
103
- }
104
- }
105
-
106
- async eta(input: ETAInput): Promise<ETAResponse> {
107
- try {
108
- const response = await this.apiClient.get("/etas", {
109
- headers: {
110
- Authorization: `Bearer ${this.accessToken}`,
111
- },
112
- params: input,
113
- });
114
-
115
- return response.data;
116
- } catch (error) {
117
- if (error instanceof AxiosError) {
118
- if (error.response?.status === 401) {
119
- if (this.accessTokenRetries > 3) {
120
- throw new Error("Unable to get access token");
121
- }
122
-
123
- this.accessTokenRetries++;
124
- await this.getAccessToken();
125
- return this.eta(input);
126
- } else {
127
- throw error.response?.data;
128
- }
129
- } else throw error;
130
- }
131
- }
132
-
133
- async search(input: SearchInput): Promise<SearchResponse> {
134
- try {
135
- const response = await this.apiClient.get("/search", {
136
- headers: {
137
- Authorization: `Bearer ${this.accessToken}`,
138
- },
139
- params: input,
140
- });
141
-
142
- return response.data;
143
- } catch (error) {
144
- if (error instanceof AxiosError) {
145
- if (error.response?.status === 401) {
146
- if (this.accessTokenRetries > 3) {
147
- throw new Error("Unable to get access token");
148
- }
149
-
150
- this.accessTokenRetries++;
151
- await this.getAccessToken();
152
- return this.search(input);
153
- } else {
154
- throw error.response?.data;
155
- }
156
- } else throw error;
157
- }
158
- }
159
- }
160
-
161
- export default AppleMaps;
1
+ import axios, { AxiosError, AxiosInstance } from "axios";
2
+
3
+ import {
4
+ GeocodeInput,
5
+ GeocodeResponse,
6
+ ReverseGeocodeInput,
7
+ ReverseGeocodeResponse,
8
+ ETAInput,
9
+ ETAResponse,
10
+ SearchInput,
11
+ SearchResponse,
12
+ } from "./globals";
13
+
14
+ class AppleMaps {
15
+ accessToken: string;
16
+ authorizationToken: string;
17
+ accessTokenRetries: number;
18
+ apiClient: AxiosInstance;
19
+
20
+ constructor({ authorizationToken }: { authorizationToken: string }) {
21
+ this.accessToken = "";
22
+ this.accessTokenRetries = 0;
23
+ this.authorizationToken = authorizationToken;
24
+
25
+ if (!authorizationToken) {
26
+ throw new Error("'authorizationToken' param is required");
27
+ }
28
+
29
+ this.apiClient = axios.create({
30
+ baseURL: "https://maps-api.apple.com/v1",
31
+ });
32
+
33
+ this.getAccessToken();
34
+ }
35
+
36
+ /**
37
+ * Take an authorization token and fetch an access token from apples servers
38
+ */
39
+ private async getAccessToken(): Promise<undefined> {
40
+ try {
41
+ const response = await this.apiClient.get("/token", {
42
+ headers: {
43
+ Authorization: `Bearer ${this.authorizationToken}`,
44
+ },
45
+ });
46
+
47
+ this.accessToken = response.data.accessToken;
48
+ } catch (error) {
49
+ throw error;
50
+ }
51
+
52
+ return;
53
+ }
54
+
55
+ private async handleError<T>(err: unknown, callback: () => Promise<T>) {
56
+ if (err instanceof AxiosError) {
57
+ if (err.response?.status === 401) {
58
+ if (this.accessTokenRetries > 3) {
59
+ throw new Error("Unable to get access token");
60
+ }
61
+
62
+ this.accessTokenRetries++;
63
+
64
+ await this.getAccessToken();
65
+ return callback();
66
+ }
67
+ }
68
+
69
+ throw err;
70
+ }
71
+
72
+ /**
73
+ * Returns the latitude and longitude of the address you specify.
74
+ */
75
+ public async geocode(input: GeocodeInput): Promise<GeocodeResponse> {
76
+ try {
77
+ const response = await this.apiClient.get("/geocode", {
78
+ headers: {
79
+ Authorization: `Bearer ${this.accessToken}`,
80
+ },
81
+ params: input,
82
+ });
83
+
84
+ return response.data;
85
+ } catch (error) {
86
+ return this.handleError(error, () => this.geocode(input));
87
+ }
88
+ }
89
+
90
+ /**
91
+ * Returns an array of addresses present at the coordinates you provide.
92
+ */
93
+ public async reverseGeocode(
94
+ input: ReverseGeocodeInput
95
+ ): Promise<ReverseGeocodeResponse> {
96
+ try {
97
+ const response = await this.apiClient.get("/reverseGeocode", {
98
+ headers: {
99
+ Authorization: `Bearer ${this.accessToken}`,
100
+ },
101
+ params: input,
102
+ });
103
+
104
+ return response.data;
105
+ } catch (error) {
106
+ return this.handleError(error, () => this.reverseGeocode(input));
107
+ }
108
+ }
109
+
110
+ /**
111
+ * Returns the estimated time of arrival (ETA) and distance between starting and ending locations.
112
+ */
113
+ public async eta(input: ETAInput): Promise<ETAResponse> {
114
+ try {
115
+ const response = await this.apiClient.get("/etas", {
116
+ headers: {
117
+ Authorization: `Bearer ${this.accessToken}`,
118
+ },
119
+ params: input,
120
+ });
121
+
122
+ return response.data;
123
+ } catch (error) {
124
+ return this.handleError(error, () => this.eta(input));
125
+ }
126
+ }
127
+
128
+ /**
129
+ * Find places by name or by specific search criteria.
130
+ */
131
+ public async search(input: SearchInput): Promise<SearchResponse> {
132
+ try {
133
+ const response = await this.apiClient.get("/search", {
134
+ headers: {
135
+ Authorization: `Bearer ${this.accessToken}`,
136
+ },
137
+ params: input,
138
+ });
139
+
140
+ return response.data;
141
+ } catch (error) {
142
+ return this.handleError(error, () => this.search(input));
143
+ }
144
+ }
145
+ }
146
+
147
+ export default AppleMaps;
package/tsconfig.json CHANGED
@@ -1,13 +1,13 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES6",
4
- "module": "commonjs",
5
- "declaration": true,
6
- "outDir": "./lib",
7
- "strict": true,
8
- "esModuleInterop": true,
9
-
10
- },
11
- "include": ["src"],
12
- "exclude": ["node_modules", "**/__tests__/*"]
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES6",
4
+ "module": "commonjs",
5
+ "declaration": true,
6
+ "outDir": "./lib",
7
+ "strict": true,
8
+ "esModuleInterop": true,
9
+
10
+ },
11
+ "include": ["src"],
12
+ "exclude": ["node_modules", "**/__tests__/*"]
13
13
  }