36 lines
1.0 KiB
TypeScript
36 lines
1.0 KiB
TypeScript
import { Observable, lastValueFrom } from 'rxjs';
|
|
import { Inject, Injectable, OnModuleInit } from '@nestjs/common';
|
|
import { ClientGrpc } from '@nestjs/microservices';
|
|
import { GRPC_GEOROUTER_SERVICE_NAME } from '@src/app.constants';
|
|
import {
|
|
GeorouterPort,
|
|
RouteRequest,
|
|
RouteResponse,
|
|
} from '../core/application/ports/georouter.port';
|
|
import { GEOGRAPHY_PACKAGE } from '../ad.di-tokens';
|
|
|
|
interface GeorouterService {
|
|
getRoute(request: RouteRequest): Observable<RouteResponse>;
|
|
}
|
|
|
|
@Injectable()
|
|
export class Georouter implements GeorouterPort, OnModuleInit {
|
|
private georouterService: GeorouterService;
|
|
|
|
constructor(@Inject(GEOGRAPHY_PACKAGE) private readonly client: ClientGrpc) {}
|
|
|
|
onModuleInit() {
|
|
this.georouterService = this.client.getService<GeorouterService>(
|
|
GRPC_GEOROUTER_SERVICE_NAME,
|
|
);
|
|
}
|
|
|
|
getRoute = async (request: RouteRequest): Promise<RouteResponse> => {
|
|
try {
|
|
return await lastValueFrom(this.georouterService.getRoute(request));
|
|
} catch (error: any) {
|
|
throw error;
|
|
}
|
|
};
|
|
}
|