apple-maps-server-sdk 1.0.0 → 1.0.2

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/lib/index.d.ts CHANGED
@@ -1 +1,15 @@
1
- export declare const Greeter: (name: string) => string;
1
+ import { AxiosInstance } from "axios";
2
+ declare class AppleMaps {
3
+ accessToken: string;
4
+ authorizationToken: string;
5
+ apiClient: AxiosInstance;
6
+ constructor({ authorizationToken }: {
7
+ authorizationToken: string;
8
+ });
9
+ getAccessToken(): Promise<undefined>;
10
+ geocode(input: GeocodeInput): Promise<GeocodeResponse>;
11
+ reverseGeocode(input: ReverseGeocodeInput): Promise<ReverseGeocodeResponse>;
12
+ eta(input: ETAInput): Promise<ETAResponse>;
13
+ search(input: SearchInput): Promise<SearchResponse>;
14
+ }
15
+ export default AppleMaps;
package/lib/index.js CHANGED
@@ -1,5 +1,180 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ var __importDefault = (this && this.__importDefault) || function (mod) {
35
+ return (mod && mod.__esModule) ? mod : { "default": mod };
36
+ };
2
37
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Greeter = void 0;
4
- var Greeter = function (name) { return "Hello ".concat(name); };
5
- exports.Greeter = Greeter;
38
+ const axios_1 = __importStar(require("axios"));
39
+ const qs_1 = __importDefault(require("qs"));
40
+ class AppleMaps {
41
+ constructor({ authorizationToken }) {
42
+ this.accessToken = "";
43
+ this.authorizationToken = authorizationToken;
44
+ if (!authorizationToken) {
45
+ throw new Error("'authorizationToken' param is required");
46
+ }
47
+ this.apiClient = axios_1.default.create({
48
+ baseURL: "https://maps-api.apple.com/v1",
49
+ });
50
+ }
51
+ getAccessToken() {
52
+ return __awaiter(this, void 0, void 0, function* () {
53
+ try {
54
+ const response = yield this.apiClient.get("/token", {
55
+ headers: {
56
+ Authorization: `Bearer ${this.authorizationToken}`,
57
+ },
58
+ });
59
+ this.accessToken = response.data.accessToken;
60
+ }
61
+ catch (error) {
62
+ console.error(error);
63
+ }
64
+ return;
65
+ });
66
+ }
67
+ geocode(input) {
68
+ var _a;
69
+ return __awaiter(this, void 0, void 0, function* () {
70
+ const params = qs_1.default.stringify(input);
71
+ try {
72
+ const response = yield this.apiClient.get("/geocode", {
73
+ headers: {
74
+ Authorization: `Bearer ${this.accessToken}`,
75
+ },
76
+ params,
77
+ });
78
+ return response.data;
79
+ }
80
+ catch (error) {
81
+ if (error instanceof axios_1.AxiosError) {
82
+ if (((_a = error.response) === null || _a === void 0 ? void 0 : _a.status) === 401) {
83
+ yield this.getAccessToken();
84
+ return this.geocode(input);
85
+ }
86
+ else {
87
+ throw error;
88
+ }
89
+ }
90
+ else
91
+ throw error;
92
+ }
93
+ });
94
+ }
95
+ reverseGeocode(input) {
96
+ var _a;
97
+ return __awaiter(this, void 0, void 0, function* () {
98
+ const params = qs_1.default.stringify(input);
99
+ try {
100
+ const response = yield this.apiClient.get("/reverseGeocode", {
101
+ headers: {
102
+ Authorization: `Bearer ${this.accessToken}`,
103
+ },
104
+ params,
105
+ });
106
+ return response.data;
107
+ }
108
+ catch (error) {
109
+ if (error instanceof axios_1.AxiosError) {
110
+ if (((_a = error.response) === null || _a === void 0 ? void 0 : _a.status) === 401) {
111
+ yield this.getAccessToken();
112
+ return this.reverseGeocode(input);
113
+ }
114
+ else {
115
+ throw error;
116
+ }
117
+ }
118
+ else
119
+ throw error;
120
+ }
121
+ });
122
+ }
123
+ eta(input) {
124
+ var _a;
125
+ return __awaiter(this, void 0, void 0, function* () {
126
+ const params = qs_1.default.stringify(input);
127
+ try {
128
+ const response = yield this.apiClient.get("/etas", {
129
+ headers: {
130
+ Authorization: `Bearer ${this.accessToken}`,
131
+ },
132
+ params,
133
+ });
134
+ return response.data;
135
+ }
136
+ catch (error) {
137
+ if (error instanceof axios_1.AxiosError) {
138
+ if (((_a = error.response) === null || _a === void 0 ? void 0 : _a.status) === 401) {
139
+ yield this.getAccessToken();
140
+ return this.eta(input);
141
+ }
142
+ else {
143
+ throw error;
144
+ }
145
+ }
146
+ else
147
+ throw error;
148
+ }
149
+ });
150
+ }
151
+ search(input) {
152
+ var _a;
153
+ return __awaiter(this, void 0, void 0, function* () {
154
+ const params = qs_1.default.stringify(input);
155
+ try {
156
+ const response = yield this.apiClient.get("/search", {
157
+ headers: {
158
+ Authorization: `Bearer ${this.accessToken}`,
159
+ },
160
+ params,
161
+ });
162
+ return response.data;
163
+ }
164
+ catch (error) {
165
+ if (error instanceof axios_1.AxiosError) {
166
+ if (((_a = error.response) === null || _a === void 0 ? void 0 : _a.status) === 401) {
167
+ yield this.getAccessToken();
168
+ return this.search(input);
169
+ }
170
+ else {
171
+ throw error;
172
+ }
173
+ }
174
+ else
175
+ throw error;
176
+ }
177
+ });
178
+ }
179
+ }
180
+ exports.default = AppleMaps;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "apple-maps-server-sdk",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "An SDK for the Apple Maps Server API",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -21,6 +21,11 @@
21
21
  },
22
22
  "homepage": "https://github.com/JS00001/apple-maps-server-sdk#readme",
23
23
  "devDependencies": {
24
+ "@types/qs": "^6.9.7",
24
25
  "typescript": "^4.9.4"
26
+ },
27
+ "dependencies": {
28
+ "axios": "^1.2.2",
29
+ "qs": "^6.11.0"
25
30
  }
26
31
  }
package/src/index.ts CHANGED
@@ -1 +1,135 @@
1
- export const Greeter = (name: string) => `Hello ${name}`;
1
+ import axios, { AxiosError, AxiosInstance } from "axios";
2
+ import qs from "qs";
3
+
4
+ class AppleMaps {
5
+ accessToken: string;
6
+ authorizationToken: string;
7
+ apiClient: AxiosInstance;
8
+
9
+ constructor({ authorizationToken }: { authorizationToken: string }) {
10
+ this.accessToken = "";
11
+ this.authorizationToken = authorizationToken;
12
+
13
+ if (!authorizationToken) {
14
+ throw new Error("'authorizationToken' param is required");
15
+ }
16
+
17
+ this.apiClient = axios.create({
18
+ baseURL: "https://maps-api.apple.com/v1",
19
+ });
20
+ }
21
+
22
+ async getAccessToken(): Promise<undefined> {
23
+ try {
24
+ const response = await this.apiClient.get("/token", {
25
+ headers: {
26
+ Authorization: `Bearer ${this.authorizationToken}`,
27
+ },
28
+ });
29
+
30
+ this.accessToken = response.data.accessToken;
31
+ } catch (error) {
32
+ console.error(error);
33
+ }
34
+
35
+ return;
36
+ }
37
+
38
+ async geocode(input: GeocodeInput): Promise<GeocodeResponse> {
39
+ const params = qs.stringify(input);
40
+
41
+ try {
42
+ const response = await this.apiClient.get("/geocode", {
43
+ headers: {
44
+ Authorization: `Bearer ${this.accessToken}`,
45
+ },
46
+ params,
47
+ });
48
+
49
+ return response.data;
50
+ } catch (error) {
51
+ if (error instanceof AxiosError) {
52
+ if (error.response?.status === 401) {
53
+ await this.getAccessToken();
54
+ return this.geocode(input);
55
+ } else {
56
+ throw error;
57
+ }
58
+ } else throw error;
59
+ }
60
+ }
61
+
62
+ async reverseGeocode(input: ReverseGeocodeInput): Promise<ReverseGeocodeResponse> {
63
+ const params = qs.stringify(input);
64
+
65
+ try {
66
+ const response = await this.apiClient.get("/reverseGeocode", {
67
+ headers: {
68
+ Authorization: `Bearer ${this.accessToken}`,
69
+ },
70
+ params,
71
+ });
72
+
73
+ return response.data;
74
+ } catch (error) {
75
+ if (error instanceof AxiosError) {
76
+ if (error.response?.status === 401) {
77
+ await this.getAccessToken();
78
+ return this.reverseGeocode(input);
79
+ } else {
80
+ throw error;
81
+ }
82
+ } else throw error;
83
+ }
84
+ }
85
+
86
+ async eta(input: ETAInput): Promise<ETAResponse> {
87
+ const params = qs.stringify(input);
88
+
89
+ try {
90
+ const response = await this.apiClient.get("/etas", {
91
+ headers: {
92
+ Authorization: `Bearer ${this.accessToken}`,
93
+ },
94
+ params,
95
+ });
96
+
97
+ return response.data;
98
+ } catch (error) {
99
+ if (error instanceof AxiosError) {
100
+ if (error.response?.status === 401) {
101
+ await this.getAccessToken();
102
+ return this.eta(input);
103
+ } else {
104
+ throw error;
105
+ }
106
+ } else throw error;
107
+ }
108
+ }
109
+
110
+ async search(input: SearchInput): Promise<SearchResponse> {
111
+ const params = qs.stringify(input);
112
+
113
+ try {
114
+ const response = await this.apiClient.get("/search", {
115
+ headers: {
116
+ Authorization: `Bearer ${this.accessToken}`,
117
+ },
118
+ params,
119
+ });
120
+
121
+ return response.data;
122
+ } catch (error) {
123
+ if (error instanceof AxiosError) {
124
+ if (error.response?.status === 401) {
125
+ await this.getAccessToken();
126
+ return this.search(input);
127
+ } else {
128
+ throw error;
129
+ }
130
+ } else throw error;
131
+ }
132
+ }
133
+ }
134
+
135
+ export default AppleMaps;
@@ -0,0 +1,33 @@
1
+ interface GeocodeInput {
2
+ q: string;
3
+ limitToCountries?: string[];
4
+ lang?: string;
5
+ searchLocation?: string;
6
+ searchRegion?: string;
7
+ userLocation?: string;
8
+ }
9
+
10
+ interface ReverseGeocodeInput {
11
+ q: string;
12
+ lang?: string;
13
+ }
14
+
15
+ interface ETAInput {
16
+ origin: string;
17
+ destinations: string[];
18
+ transportType?: TransportType;
19
+ departureDate?: string;
20
+ arrivalDate?: string;
21
+ }
22
+
23
+ interface SearchInput {
24
+ q: string;
25
+ excludePoiCategories?: PoiCategory[];
26
+ includePoiCategories?: PoiCategory[];
27
+ limitToCountries?: string[];
28
+ resultTypeFilter?: "Poi" | "Address";
29
+ lang?: string;
30
+ searchLocation?: string;
31
+ searchRegion?: string;
32
+ userLocation?: string;
33
+ }
@@ -0,0 +1,16 @@
1
+ interface GeocodeResponse {
2
+ response: Place[];
3
+ }
4
+
5
+ interface ReverseGeocodeResponse {
6
+ response: Place[];
7
+ }
8
+
9
+ interface ETAResponse {
10
+ etas: ETA[];
11
+ }
12
+
13
+ interface SearchResponse {
14
+ displayMapRegion: MapRegion;
15
+ results: SearchResponsePlace[];
16
+ }
package/src/types.d.ts ADDED
@@ -0,0 +1,95 @@
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
+ }
package/tsconfig.json CHANGED
@@ -1,10 +1,12 @@
1
1
  {
2
2
  "compilerOptions": {
3
- "target": "es5",
3
+ "target": "ES6",
4
4
  "module": "commonjs",
5
5
  "declaration": true,
6
6
  "outDir": "./lib",
7
- "strict": true
7
+ "strict": true,
8
+ "esModuleInterop": true,
9
+
8
10
  },
9
11
  "include": ["src"],
10
12
  "exclude": ["node_modules", "**/__tests__/*"]