@liftitinc/geocoding 0.2.0

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.
@@ -0,0 +1,204 @@
1
+ import { GeocodeRequest, GeocodeResponse, ReverseGeocodeRequest, ReverseGeocodeResponse } from '@geocoding/shared';
2
+ export { ApiResponse, GeocodeRequest, GeocodeRequestWire, GeocodeResponse, GeocodeResponseWire, ReverseGeocodeRequest, ReverseGeocodeRequestWire, ReverseGeocodeResponse, ReverseGeocodeResponseWire, addressComponentsWireSchema, geocodeRequestWireSchema, geocodeResponseWireSchema, locationWireSchema, reverseGeocodeRequestWireSchema, reverseGeocodeResponseWireSchema, zoneWireSchema } from '@geocoding/shared';
3
+
4
+ /**
5
+ * Configuration for retry behavior
6
+ */
7
+ interface RetryConfig {
8
+ /**
9
+ * Maximum number of retry attempts (default: 3)
10
+ */
11
+ maxRetries: number;
12
+ /**
13
+ * Initial delay in milliseconds (default: 1000)
14
+ */
15
+ baseDelay: number;
16
+ /**
17
+ * Maximum delay cap in milliseconds (default: 30000)
18
+ */
19
+ maxDelay: number;
20
+ /**
21
+ * Multiplier for exponential growth (default: 2)
22
+ */
23
+ timeMultiple: number;
24
+ }
25
+ interface GeocodingClientConfig {
26
+ /**
27
+ * API key for authentication (required)
28
+ */
29
+ apiKey: string;
30
+ /**
31
+ * Base URL for the API (optional, defaults to production)
32
+ */
33
+ baseUrl?: string;
34
+ /**
35
+ * Request timeout in milliseconds (optional, defaults to 10000)
36
+ */
37
+ timeout?: number;
38
+ /**
39
+ * Maximum number of retry attempts (optional, defaults to 3)
40
+ */
41
+ maxRetries?: number;
42
+ /**
43
+ * Initial delay for retries in milliseconds (optional, defaults to 1000)
44
+ */
45
+ baseDelay?: number;
46
+ /**
47
+ * Maximum delay cap for retries in milliseconds (optional, defaults to 30000)
48
+ */
49
+ maxDelay?: number;
50
+ }
51
+ interface RequestOptions {
52
+ /**
53
+ * AbortSignal for request cancellation
54
+ */
55
+ signal?: AbortSignal;
56
+ }
57
+
58
+ /**
59
+ * Client for the Liftit Geocoding API
60
+ *
61
+ * @example
62
+ * ```typescript
63
+ * const client = new GeocodingClient({ apiKey: 'your-api-key' });
64
+ *
65
+ * // Forward geocoding
66
+ * const result = await client.geocode({
67
+ * country: 'co',
68
+ * city: 'Bogota',
69
+ * address: 'Calle 100 # 19-61'
70
+ * });
71
+ *
72
+ * // Reverse geocoding
73
+ * const address = await client.reverseGeocode({
74
+ * lat: 4.6097,
75
+ * lng: -74.0817
76
+ * });
77
+ * ```
78
+ */
79
+ declare class GeocodingClient {
80
+ private readonly config;
81
+ private readonly httpClient;
82
+ constructor(config: GeocodingClientConfig);
83
+ /**
84
+ * Convert an address to geographic coordinates
85
+ *
86
+ * @param request - Geocode request parameters
87
+ * @param options - Optional request options
88
+ * @returns Geocode response with coordinates
89
+ * @throws {ValidationError} If request parameters are invalid
90
+ * @throws {AuthenticationError} If API key is invalid
91
+ * @throws {RateLimitError} If rate limit is exceeded
92
+ * @throws {UpstreamError} If upstream API returns an error
93
+ */
94
+ geocode(request: GeocodeRequest, options?: RequestOptions): Promise<GeocodeResponse>;
95
+ /**
96
+ * Convert geographic coordinates to an address
97
+ *
98
+ * @param request - Reverse geocode request parameters
99
+ * @param options - Optional request options
100
+ * @returns Reverse geocode response with address
101
+ * @throws {ValidationError} If request parameters are invalid
102
+ * @throws {AuthenticationError} If API key is invalid
103
+ * @throws {RateLimitError} If rate limit is exceeded
104
+ * @throws {UpstreamError} If upstream API returns an error
105
+ */
106
+ reverseGeocode(request: ReverseGeocodeRequest, options?: RequestOptions): Promise<ReverseGeocodeResponse>;
107
+ }
108
+
109
+ /**
110
+ * Base error class for all SDK errors
111
+ */
112
+ declare class GeocodeError extends Error {
113
+ readonly code: string;
114
+ readonly requestId?: string;
115
+ constructor(message: string, code: string, requestId?: string);
116
+ }
117
+ /**
118
+ * Thrown when request validation fails
119
+ */
120
+ declare class ValidationError extends GeocodeError {
121
+ constructor(message: string, requestId?: string);
122
+ }
123
+ /**
124
+ * Thrown when API key is invalid or missing
125
+ */
126
+ declare class AuthenticationError extends GeocodeError {
127
+ constructor(message: string, requestId?: string);
128
+ }
129
+ /**
130
+ * Thrown when rate limit is exceeded
131
+ */
132
+ declare class RateLimitError extends GeocodeError {
133
+ readonly retryAfter?: number;
134
+ readonly limit?: number;
135
+ readonly remaining?: number;
136
+ constructor(message: string, requestId?: string, retryAfter?: number, limit?: number, remaining?: number);
137
+ /**
138
+ * Calculate optimal retry delay respecting rate limit reset time.
139
+ * Returns milliseconds to wait before retrying.
140
+ * Adds small jitter (0-1000ms) to prevent thundering herd.
141
+ *
142
+ * @returns Milliseconds to wait before retrying
143
+ */
144
+ getRetryDelay(): number;
145
+ }
146
+ /**
147
+ * Thrown when upstream Liftit API returns an error
148
+ */
149
+ declare class UpstreamError extends GeocodeError {
150
+ readonly statusCode?: number;
151
+ constructor(message: string, requestId?: string, statusCode?: number);
152
+ }
153
+
154
+ /**
155
+ * Default retry configuration values
156
+ */
157
+ declare const DEFAULT_RETRY_CONFIG: RetryConfig;
158
+ /**
159
+ * Calculate delay for retry attempt using exponential backoff with full jitter.
160
+ *
161
+ * Formula: Math.floor(Math.random() * Math.min(baseDelay * timeMultiple^attempt, maxDelay))
162
+ *
163
+ * Full jitter (random between 0 and calculated cap) prevents thundering herd
164
+ * when multiple clients retry simultaneously.
165
+ *
166
+ * @param attempt - Zero-based retry attempt number (0 for first retry)
167
+ * @param config - Retry configuration
168
+ * @returns Delay in milliseconds (integer)
169
+ */
170
+ declare function calculateDelay(attempt: number, config: RetryConfig): number;
171
+ /**
172
+ * Determine if an error is retryable (transient failure that might succeed on retry).
173
+ *
174
+ * Retryable errors:
175
+ * - RateLimitError (429) - rate limit will reset
176
+ * - UpstreamError (502, 504) - upstream service issues are transient
177
+ * - TIMEOUT_ERROR - network delays may resolve
178
+ * - NETWORK_ERROR - connectivity issues may resolve
179
+ *
180
+ * Non-retryable errors:
181
+ * - ValidationError (400) - bad request won't fix itself
182
+ * - AuthenticationError (401) - invalid API key won't become valid
183
+ * - ABORT_ERROR - user explicitly cancelled
184
+ * - Other errors - unknown, don't retry
185
+ *
186
+ * @param error - The error to check
187
+ * @returns true if the error is retryable
188
+ */
189
+ declare function isRetryableError(error: GeocodeError): boolean;
190
+ /**
191
+ * Sleep for a specified duration, with support for abort signal.
192
+ *
193
+ * If the abort signal is already aborted, rejects immediately.
194
+ * If the signal is aborted during the sleep, clears the timeout and rejects.
195
+ *
196
+ * Uses { once: true } on addEventListener to prevent memory leaks in long-running processes.
197
+ *
198
+ * @param ms - Duration to sleep in milliseconds
199
+ * @param signal - Optional AbortSignal for cancellation
200
+ * @returns Promise that resolves after the delay or rejects on abort
201
+ */
202
+ declare function sleep(ms: number, signal?: AbortSignal): Promise<void>;
203
+
204
+ export { AuthenticationError, DEFAULT_RETRY_CONFIG, GeocodeError, GeocodingClient, type GeocodingClientConfig, RateLimitError, type RequestOptions, type RetryConfig, UpstreamError, ValidationError, calculateDelay, isRetryableError, sleep };
@@ -0,0 +1,204 @@
1
+ import { GeocodeRequest, GeocodeResponse, ReverseGeocodeRequest, ReverseGeocodeResponse } from '@geocoding/shared';
2
+ export { ApiResponse, GeocodeRequest, GeocodeRequestWire, GeocodeResponse, GeocodeResponseWire, ReverseGeocodeRequest, ReverseGeocodeRequestWire, ReverseGeocodeResponse, ReverseGeocodeResponseWire, addressComponentsWireSchema, geocodeRequestWireSchema, geocodeResponseWireSchema, locationWireSchema, reverseGeocodeRequestWireSchema, reverseGeocodeResponseWireSchema, zoneWireSchema } from '@geocoding/shared';
3
+
4
+ /**
5
+ * Configuration for retry behavior
6
+ */
7
+ interface RetryConfig {
8
+ /**
9
+ * Maximum number of retry attempts (default: 3)
10
+ */
11
+ maxRetries: number;
12
+ /**
13
+ * Initial delay in milliseconds (default: 1000)
14
+ */
15
+ baseDelay: number;
16
+ /**
17
+ * Maximum delay cap in milliseconds (default: 30000)
18
+ */
19
+ maxDelay: number;
20
+ /**
21
+ * Multiplier for exponential growth (default: 2)
22
+ */
23
+ timeMultiple: number;
24
+ }
25
+ interface GeocodingClientConfig {
26
+ /**
27
+ * API key for authentication (required)
28
+ */
29
+ apiKey: string;
30
+ /**
31
+ * Base URL for the API (optional, defaults to production)
32
+ */
33
+ baseUrl?: string;
34
+ /**
35
+ * Request timeout in milliseconds (optional, defaults to 10000)
36
+ */
37
+ timeout?: number;
38
+ /**
39
+ * Maximum number of retry attempts (optional, defaults to 3)
40
+ */
41
+ maxRetries?: number;
42
+ /**
43
+ * Initial delay for retries in milliseconds (optional, defaults to 1000)
44
+ */
45
+ baseDelay?: number;
46
+ /**
47
+ * Maximum delay cap for retries in milliseconds (optional, defaults to 30000)
48
+ */
49
+ maxDelay?: number;
50
+ }
51
+ interface RequestOptions {
52
+ /**
53
+ * AbortSignal for request cancellation
54
+ */
55
+ signal?: AbortSignal;
56
+ }
57
+
58
+ /**
59
+ * Client for the Liftit Geocoding API
60
+ *
61
+ * @example
62
+ * ```typescript
63
+ * const client = new GeocodingClient({ apiKey: 'your-api-key' });
64
+ *
65
+ * // Forward geocoding
66
+ * const result = await client.geocode({
67
+ * country: 'co',
68
+ * city: 'Bogota',
69
+ * address: 'Calle 100 # 19-61'
70
+ * });
71
+ *
72
+ * // Reverse geocoding
73
+ * const address = await client.reverseGeocode({
74
+ * lat: 4.6097,
75
+ * lng: -74.0817
76
+ * });
77
+ * ```
78
+ */
79
+ declare class GeocodingClient {
80
+ private readonly config;
81
+ private readonly httpClient;
82
+ constructor(config: GeocodingClientConfig);
83
+ /**
84
+ * Convert an address to geographic coordinates
85
+ *
86
+ * @param request - Geocode request parameters
87
+ * @param options - Optional request options
88
+ * @returns Geocode response with coordinates
89
+ * @throws {ValidationError} If request parameters are invalid
90
+ * @throws {AuthenticationError} If API key is invalid
91
+ * @throws {RateLimitError} If rate limit is exceeded
92
+ * @throws {UpstreamError} If upstream API returns an error
93
+ */
94
+ geocode(request: GeocodeRequest, options?: RequestOptions): Promise<GeocodeResponse>;
95
+ /**
96
+ * Convert geographic coordinates to an address
97
+ *
98
+ * @param request - Reverse geocode request parameters
99
+ * @param options - Optional request options
100
+ * @returns Reverse geocode response with address
101
+ * @throws {ValidationError} If request parameters are invalid
102
+ * @throws {AuthenticationError} If API key is invalid
103
+ * @throws {RateLimitError} If rate limit is exceeded
104
+ * @throws {UpstreamError} If upstream API returns an error
105
+ */
106
+ reverseGeocode(request: ReverseGeocodeRequest, options?: RequestOptions): Promise<ReverseGeocodeResponse>;
107
+ }
108
+
109
+ /**
110
+ * Base error class for all SDK errors
111
+ */
112
+ declare class GeocodeError extends Error {
113
+ readonly code: string;
114
+ readonly requestId?: string;
115
+ constructor(message: string, code: string, requestId?: string);
116
+ }
117
+ /**
118
+ * Thrown when request validation fails
119
+ */
120
+ declare class ValidationError extends GeocodeError {
121
+ constructor(message: string, requestId?: string);
122
+ }
123
+ /**
124
+ * Thrown when API key is invalid or missing
125
+ */
126
+ declare class AuthenticationError extends GeocodeError {
127
+ constructor(message: string, requestId?: string);
128
+ }
129
+ /**
130
+ * Thrown when rate limit is exceeded
131
+ */
132
+ declare class RateLimitError extends GeocodeError {
133
+ readonly retryAfter?: number;
134
+ readonly limit?: number;
135
+ readonly remaining?: number;
136
+ constructor(message: string, requestId?: string, retryAfter?: number, limit?: number, remaining?: number);
137
+ /**
138
+ * Calculate optimal retry delay respecting rate limit reset time.
139
+ * Returns milliseconds to wait before retrying.
140
+ * Adds small jitter (0-1000ms) to prevent thundering herd.
141
+ *
142
+ * @returns Milliseconds to wait before retrying
143
+ */
144
+ getRetryDelay(): number;
145
+ }
146
+ /**
147
+ * Thrown when upstream Liftit API returns an error
148
+ */
149
+ declare class UpstreamError extends GeocodeError {
150
+ readonly statusCode?: number;
151
+ constructor(message: string, requestId?: string, statusCode?: number);
152
+ }
153
+
154
+ /**
155
+ * Default retry configuration values
156
+ */
157
+ declare const DEFAULT_RETRY_CONFIG: RetryConfig;
158
+ /**
159
+ * Calculate delay for retry attempt using exponential backoff with full jitter.
160
+ *
161
+ * Formula: Math.floor(Math.random() * Math.min(baseDelay * timeMultiple^attempt, maxDelay))
162
+ *
163
+ * Full jitter (random between 0 and calculated cap) prevents thundering herd
164
+ * when multiple clients retry simultaneously.
165
+ *
166
+ * @param attempt - Zero-based retry attempt number (0 for first retry)
167
+ * @param config - Retry configuration
168
+ * @returns Delay in milliseconds (integer)
169
+ */
170
+ declare function calculateDelay(attempt: number, config: RetryConfig): number;
171
+ /**
172
+ * Determine if an error is retryable (transient failure that might succeed on retry).
173
+ *
174
+ * Retryable errors:
175
+ * - RateLimitError (429) - rate limit will reset
176
+ * - UpstreamError (502, 504) - upstream service issues are transient
177
+ * - TIMEOUT_ERROR - network delays may resolve
178
+ * - NETWORK_ERROR - connectivity issues may resolve
179
+ *
180
+ * Non-retryable errors:
181
+ * - ValidationError (400) - bad request won't fix itself
182
+ * - AuthenticationError (401) - invalid API key won't become valid
183
+ * - ABORT_ERROR - user explicitly cancelled
184
+ * - Other errors - unknown, don't retry
185
+ *
186
+ * @param error - The error to check
187
+ * @returns true if the error is retryable
188
+ */
189
+ declare function isRetryableError(error: GeocodeError): boolean;
190
+ /**
191
+ * Sleep for a specified duration, with support for abort signal.
192
+ *
193
+ * If the abort signal is already aborted, rejects immediately.
194
+ * If the signal is aborted during the sleep, clears the timeout and rejects.
195
+ *
196
+ * Uses { once: true } on addEventListener to prevent memory leaks in long-running processes.
197
+ *
198
+ * @param ms - Duration to sleep in milliseconds
199
+ * @param signal - Optional AbortSignal for cancellation
200
+ * @returns Promise that resolves after the delay or rejects on abort
201
+ */
202
+ declare function sleep(ms: number, signal?: AbortSignal): Promise<void>;
203
+
204
+ export { AuthenticationError, DEFAULT_RETRY_CONFIG, GeocodeError, GeocodingClient, type GeocodingClientConfig, RateLimitError, type RequestOptions, type RetryConfig, UpstreamError, ValidationError, calculateDelay, isRetryableError, sleep };