import type { AxiosInstance , AxiosResponse } from 'axios' ; import { MapQuestSuggestionTransformer } from '../transformer' ; import { SuggestCommand } from '../../../command' ; import type { SuggestQuery } from '../../../model' ; import type { AbstractSuggestionTransformer } from '../../../transformer' ; import type { MapQuestSuggestQueryInterface } from '../interface' ; import type { MapQuestProvider } from '../map-quest.provider' ; import { MapQuestCommonCommandMixin } from './mixin' ; link export class MapQuestSuggestCommand extends MapQuestCommonCommandMixin ( SuggestCommand ) < MapQuestSuggestQueryInterface > { constructor ( httpClient , private readonly apiKey ) { super ( httpClient , apiKey ) ; } static getUrl ( ) { return 'https://www.mapquestapi.com/search/v3/prediction' ; } protected async buildQuery ( query ) { const providerQuery = { key : this . apiKey , q : query . address , limit : query . limit , collection : 'address' , countryCode : query . countryCode , languageCode : query . language , } ; if ( query . lat && query . lon ) { providerQuery . location = `${ query . lon },${ query . lat }` ; } return providerQuery ; } protected async parseResponse ( response ) { if ( ! Array . isArray ( response . data . results ) || ! response . data . results . length ) { return [ ] ; } const locations = response . data . results ; if ( ! Array . isArray ( locations ) || ! locations . length ) { return [ ] ; } return Promise . all < AbstractSuggestionTransformer < MapQuestProvider > > ( locations . map ( async ( raw ) : Promise < AbstractSuggestionTransformer < MapQuestProvider > > => new MapQuestSuggestionTransformer ( raw ) ) ) ; } }