@@ -30,7 +30,6 @@ import { errorHandler, asyncHandler } from './middleware/errorHandler';
3030import express , { Request , Response } from 'express' ;
3131import { LRUCache , generateGeocacheKey } from './utils/LRUCache' ;
3232import { uspsDeduplicator , googleMapsDeduplicator } from './utils/RequestDeduplicator' ;
33- import { uspsCircuitBreaker , googleMapsCircuitBreaker } from './utils/CircuitBreaker' ;
3433import { normalizeAddress } from './utils/normalizeAddress' ;
3534
3635// Suppress dotenv logging by intercepting console.log temporarily
@@ -137,31 +136,29 @@ async function getUSPSToken(): Promise<string | null> {
137136
138137 // Use deduplication and circuit breaker for token requests
139138 return uspsDeduplicator . execute ( 'usps-token' , async ( ) => {
140- return uspsCircuitBreaker . execute ( async ( ) => {
141- const body = new URLSearchParams ( {
142- grant_type : 'client_credentials' ,
143- client_id : config . usps . consumerKey ,
144- client_secret : config . usps . consumerSecret ,
145- scope : 'addresses' ,
146- } ) ;
139+ const body = new URLSearchParams ( {
140+ grant_type : 'client_credentials' ,
141+ client_id : config . usps . consumerKey ,
142+ client_secret : config . usps . consumerSecret ,
143+ scope : 'addresses' ,
144+ } ) ;
147145
148- try {
149- const response = await uspsAxios . post ( config . usps . tokenUrl , body . toString ( ) , {
150- headers : { 'Content-Type' : 'application/x-www-form-urlencoded' } ,
151- } ) ;
146+ try {
147+ const response = await uspsAxios . post ( config . usps . tokenUrl , body . toString ( ) , {
148+ headers : { 'Content-Type' : 'application/x-www-form-urlencoded' } ,
149+ } ) ;
152150
153- const data = response . data as USPSTokenResponse ;
154- cachedToken = data . access_token ;
155- tokenExpiresAt = Date . now ( ) + data . expires_in * 1000 ;
151+ const data = response . data as USPSTokenResponse ;
152+ cachedToken = data . access_token ;
153+ tokenExpiresAt = Date . now ( ) + data . expires_in * 1000 ;
156154
157- logger . info ( 'USPS token refreshed successfully' ) ;
158- return cachedToken ;
159- } catch ( error : unknown ) {
160- const errorMessage = error instanceof Error ? error . message : String ( error ) ;
161- logger . error ( 'Failed to get USPS token' , { error : errorMessage } ) ;
162- throw new Error ( errorMessage ) ; // Let circuit breaker handle the failure
163- }
164- } ) ;
155+ logger . info ( 'USPS token refreshed successfully' ) ;
156+ return cachedToken ;
157+ } catch ( error : unknown ) {
158+ const errorMessage = error instanceof Error ? error . message : String ( error ) ;
159+ logger . error ( 'Failed to get USPS token' , { error : errorMessage } ) ;
160+ throw new Error ( errorMessage ) ;
161+ }
165162 } ) as Promise < string | null > ;
166163}
167164
@@ -250,12 +247,10 @@ export async function correctAddress({
250247 const data = ( await uspsDeduplicator . execute (
251248 addressKey ,
252249 async ( ) : Promise < USPSAddressResponse > => {
253- return uspsCircuitBreaker . execute ( async ( ) => {
254- const response = await uspsAxios . get ( url , {
255- headers : { Authorization : `Bearer ${ token } ` } ,
256- } ) ;
257- return response . data as USPSAddressResponse ;
250+ const response = await uspsAxios . get ( url , {
251+ headers : { Authorization : `Bearer ${ token } ` } ,
258252 } ) ;
253+ return response . data as USPSAddressResponse ;
259254 }
260255 ) ) as USPSAddressResponse ;
261256
@@ -311,12 +306,10 @@ export async function correctAddress({
311306 const retryData = ( await uspsDeduplicator . execute (
312307 zipOnlyKey ,
313308 async ( ) : Promise < USPSAddressResponse > => {
314- return uspsCircuitBreaker . execute ( async ( ) => {
315- const response = await uspsAxios . get ( zipOnlyUrl , {
316- headers : { Authorization : `Bearer ${ token } ` } ,
317- } ) ;
318- return response . data as USPSAddressResponse ;
309+ const response = await uspsAxios . get ( zipOnlyUrl , {
310+ headers : { Authorization : `Bearer ${ token } ` } ,
319311 } ) ;
312+ return response . data as USPSAddressResponse ;
320313 }
321314 ) ) as USPSAddressResponse ;
322315
@@ -482,14 +475,12 @@ async function fetchGeoCoordinatesStandard(
482475 const result = ( await googleMapsDeduplicator . execute (
483476 geocodeKey ,
484477 async ( ) : Promise < GeocodingResult | null > => {
485- return googleMapsCircuitBreaker . execute ( async ( ) => {
486- const url = `https://maps.googleapis.com/maps/api/geocode/json?address=${ encodeURIComponent (
487- formattedAddress
488- ) } &key=${ config . googleMaps . apiKey } `;
478+ const url = `https://maps.googleapis.com/maps/api/geocode/json?address=${ encodeURIComponent (
479+ formattedAddress
480+ ) } &key=${ config . googleMaps . apiKey } `;
489481
490- const response = await googleAxios . get ( url ) ;
491- return parseFirstGmapsResult ( response . data ) ;
492- } ) ;
482+ const response = await googleAxios . get ( url ) ;
483+ return parseFirstGmapsResult ( response . data ) ;
493484 }
494485 ) ) as GeocodingResult | null ;
495486
@@ -520,28 +511,26 @@ async function fetchCountyByCoordinates(geo: Geo): Promise<CountyResult | null>
520511 const result = ( await googleMapsDeduplicator . execute (
521512 countyKey ,
522513 async ( ) : Promise < CountyResult | null > => {
523- return googleMapsCircuitBreaker . execute ( async ( ) => {
524- // Use result_type filter to specifically get county
525- const url = `https://maps.googleapis.com/maps/api/geocode/json?latlng=${ lat } ,${ lng } &result_type=administrative_area_level_2&key=${ config . googleMaps . apiKey } ` ;
526- const response = await googleAxios . get ( url ) ;
527-
528- const responseData = response . data as GoogleMapsResponse ;
529- if ( responseData . status === 'OK' && responseData . results ?. length > 0 ) {
530- const countyResult = responseData . results [ 0 ] ;
531- const countyComponent = countyResult . address_components ?. find (
532- ( c : GoogleAddressComponent ) => c . types . includes ( 'administrative_area_level_2' )
533- ) ;
534-
535- if ( countyComponent ) {
536- const county = countyComponent . long_name . replace ( / \s + C o u n t y $ / i, '' ) ;
537- logger . info ( 'County fetched via reverse geocoding with filter' , { county } ) ;
538- const result = { county } ;
539- countyCache . set ( countyKey , result ) ;
540- return result ;
541- }
514+ // Use result_type filter to specifically get county
515+ const url = `https://maps.googleapis.com/maps/api/geocode/json?latlng=${ lat } ,${ lng } &result_type=administrative_area_level_2&key=${ config . googleMaps . apiKey } ` ;
516+ const response = await googleAxios . get ( url ) ;
517+
518+ const responseData = response . data as GoogleMapsResponse ;
519+ if ( responseData . status === 'OK' && responseData . results ?. length > 0 ) {
520+ const countyResult = responseData . results [ 0 ] ;
521+ const countyComponent = countyResult . address_components ?. find (
522+ ( c : GoogleAddressComponent ) => c . types . includes ( 'administrative_area_level_2' )
523+ ) ;
524+
525+ if ( countyComponent ) {
526+ const county = countyComponent . long_name . replace ( / \s + C o u n t y $ / i, '' ) ;
527+ logger . info ( 'County fetched via reverse geocoding with filter' , { county } ) ;
528+ const result = { county } ;
529+ countyCache . set ( countyKey , result ) ;
530+ return result ;
542531 }
543- return null ;
544- } ) ;
532+ }
533+ return null ;
545534 }
546535 ) ) as CountyResult | null ;
547536
@@ -570,11 +559,9 @@ async function fetchAddressFromCoordinates(geo: Geo): Promise<GeocodingResult |
570559 const result = ( await googleMapsDeduplicator . execute (
571560 reverseGeocodeKey ,
572561 async ( ) : Promise < GeocodingResult | null > => {
573- return googleMapsCircuitBreaker . execute ( async ( ) => {
574- const url = `https://maps.googleapis.com/maps/api/geocode/json?latlng=${ lat } ,${ lng } &key=${ config . googleMaps . apiKey } ` ;
575- const response = await googleAxios . get ( url ) ;
576- return parseFirstGmapsResult ( response . data ) ;
577- } ) ;
562+ const url = `https://maps.googleapis.com/maps/api/geocode/json?latlng=${ lat } ,${ lng } &key=${ config . googleMaps . apiKey } ` ;
563+ const response = await googleAxios . get ( url ) ;
564+ return parseFirstGmapsResult ( response . data ) ;
578565 }
579566 ) ) as GeocodingResult | null ;
580567
@@ -873,10 +860,6 @@ app.get('/health', (_req: Request, res: Response) => {
873860 usps : uspsDeduplicator . getStats ( ) ,
874861 googleMaps : googleMapsDeduplicator . getStats ( ) ,
875862 } ,
876- circuitBreakers : {
877- usps : uspsCircuitBreaker . getStats ( ) ,
878- googleMaps : googleMapsCircuitBreaker . getStats ( ) ,
879- } ,
880863 } ;
881864
882865 res . status ( 200 ) . json ( health ) ;
0 commit comments