diff --git a/src/app.module.ts b/src/app.module.ts index 966ec1834..57deaa07a 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -35,6 +35,7 @@ import { ElasticSearchModule } from './providers/elasticsearch/elasticsearch.mod import { FirebaseModule } from './providers/firebase/firebase.module'; import { GeolocationModule } from './providers/geolocation/geolocation.module'; import { GitHubModule } from './providers/github/github.module'; +import { GoogleMapsModule } from './providers/google-maps/google-maps.module'; import { MailModule } from './providers/mail/mail.module'; import { PrismaModule } from './providers/prisma/prisma.module'; import { S3Module } from './providers/s3/s3.module'; @@ -76,6 +77,7 @@ import { TasksModule } from './providers/tasks/tasks.module'; CloudinaryModule, FirebaseModule, GitHubModule, + GoogleMapsModule, ], providers: [ { diff --git a/src/config/configuration.interface.ts b/src/config/configuration.interface.ts index 039994afb..5277bd6b3 100644 --- a/src/config/configuration.interface.ts +++ b/src/config/configuration.interface.ts @@ -120,4 +120,8 @@ export interface Configuration { auth: string; userAgent?: string; }; + + googleMaps: { + apiKey: string; + }; } diff --git a/src/config/configuration.ts b/src/config/configuration.ts index 5e1ad0f73..819b79cc8 100644 --- a/src/config/configuration.ts +++ b/src/config/configuration.ts @@ -133,6 +133,9 @@ const configuration: Configuration = { auth: process.env.GITHUB_AUTH, userAgent: process.env.GITHUB_USER_AGENT, }, + googleMaps: { + apiKey: process.env.GOOGLE_MAPS_API_KEY, + }, }; const configFunction: ConfigFactory = () => configuration; diff --git a/src/providers/google-maps/google-maps.module.ts b/src/providers/google-maps/google-maps.module.ts new file mode 100644 index 000000000..d99e3e1a8 --- /dev/null +++ b/src/providers/google-maps/google-maps.module.ts @@ -0,0 +1,10 @@ +import { Module } from '@nestjs/common'; +import { ConfigModule } from '@nestjs/config'; +import { GoogleMapsService } from './google-maps.service'; + +@Module({ + imports: [ConfigModule], + providers: [GoogleMapsService], + exports: [GoogleMapsService], +}) +export class GoogleMapsModule {} diff --git a/src/providers/google-maps/google-maps.service.ts b/src/providers/google-maps/google-maps.service.ts new file mode 100644 index 000000000..204f7b6a9 --- /dev/null +++ b/src/providers/google-maps/google-maps.service.ts @@ -0,0 +1,28 @@ +import { Client } from '@googlemaps/google-maps-services-js'; +import { Injectable, Logger } from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; +import { Configuration } from '../../config/configuration.interface'; + +@Injectable() +export class GoogleMapsService { + private logger = new Logger(GoogleMapsService.name); + private config = this.configService.get( + 'googleMaps', + ); + client: Client; + + constructor(private configService: ConfigService) { + if (this.config.apiKey) this.client = new Client(); + else this.logger.warn('Google Maps API key not found'); + } + + autocomplete(query: string, components?: string[]) { + return this.client.placeAutocomplete({ + params: { + input: query, + key: this.config.apiKey, + components, + }, + }); + } +}