Skip to content

Commit

Permalink
Forward index endpoints (multiversx#1223)
Browse files Browse the repository at this point in the history
* proxy endpoints for index

* renamed proxy controller to gateway proxy controller

* removed endpoints in gateway controller

* implemented error handling
  • Loading branch information
tanghel authored and IosifGabriel committed May 22, 2024
1 parent 9e868f0 commit 9ea0389
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/endpoints/endpoints.controllers.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { TagController } from "./nfttags/tag.controller";
import { NodeController } from "./nodes/node.controller";
import { ProcessNftsPublicController } from "./process-nfts/process.nfts.public.controller";
import { ProviderController } from "./providers/provider.controller";
import { ProxyController } from "./proxy/proxy.controller";
import { GatewayProxyController } from "./proxy/gateway.proxy.controller";
import { ProxyModule } from "./proxy/proxy.module";
import { RoundController } from "./rounds/round.controller";
import { SmartContractResultController } from "./sc-results/scresult.controller";
Expand All @@ -43,7 +43,7 @@ export class EndpointsControllersModule {
const controllers: Type<any>[] = [
AccountController, BlockController, CollectionController, DelegationController, DelegationLegacyController, IdentitiesController,
KeysController, MiniBlockController, NetworkController, NftController, TagController, NodeController,
ProviderController, ProxyController, RoundController, SmartContractResultController, ShardController, StakeController, StakeController,
ProviderController, GatewayProxyController, RoundController, SmartContractResultController, ShardController, StakeController, StakeController,
TokenController, TransactionController, UsernameController, VmQueryController, WaitingListController,
HealthCheckController, DappConfigController, WebsocketController, TransferController,
ProcessNftsPublicController, TransactionsBatchController,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import { CacheService, NoCache } from "@multiversx/sdk-nestjs-cache";
import { OriginLogger } from "@multiversx/sdk-nestjs-common";
import { DeepHistoryInterceptor } from "src/interceptors/deep-history.interceptor";
import { DisableFieldsInterceptorOnController } from "@multiversx/sdk-nestjs-http";

@Controller()
@ApiTags('proxy')
@ApiExcludeController()
@DisableFieldsInterceptorOnController()
export class ProxyController {
private readonly logger = new OriginLogger(ProxyController.name);
export class GatewayProxyController {
private readonly logger = new OriginLogger(GatewayProxyController.name);

constructor(
private readonly gatewayService: GatewayService,
Expand Down
76 changes: 76 additions & 0 deletions src/endpoints/proxy/index.proxy.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { Body, Controller, Get, HttpException, Param, Post, Req } from "@nestjs/common";
import { ApiExcludeController, ApiTags } from "@nestjs/swagger";
import { Request } from "express";
import { ApiService, DisableFieldsInterceptorOnController } from "@multiversx/sdk-nestjs-http";
import { ApiConfigService } from "src/common/api-config/api.config.service";

@Controller('index')
@ApiTags('proxy')
@ApiExcludeController()
@DisableFieldsInterceptorOnController()
export class IndexProxyController {
constructor(
private readonly apiService: ApiService,
private readonly apiConfigService: ApiConfigService,
) { }

@Get('/:collection/_search')
async forwardIndexSearchGet(
@Param('collection') collection: string,
@Req() request: Request,
) {
return await this.performIndexGetRequest(collection, '_search', request.query);
}

@Get('/:collection/_count')
async forwardIndexCountGet(
@Param('collection') collection: string,
@Req() request: Request,
) {
return await this.performIndexGetRequest(collection, '_count', request.query);
}

@Post('/:collection/_search')
async forwardIndexSearchPost(
@Param('collection') collection: string,
@Body() body: any,
@Req() request: Request,
) {
return await this.performIndexPostRequest(collection, '_search', request.query, body);
}

@Post('/:collection/_count')
async forwardIndexCountPost(
@Param('collection') collection: string,
@Body() body: any,
@Req() request: Request,
) {
return await this.performIndexPostRequest(collection, '_count', request.query, body);
}

private async performIndexGetRequest(collection: string, suffix: string, params: any) {
const url = `${this.apiConfigService.getElasticUrl()}/${collection}/${suffix}`;

try {
const { data } = await this.apiService.get(url, { params });

return data;
} catch (error) {
// @ts-ignore
throw new HttpException(error.response, error.status);
}
}

private async performIndexPostRequest(collection: string, suffix: string, params: any, body: any) {
const url = `${this.apiConfigService.getElasticUrl()}/${collection}/${suffix}`;

try {
const { data } = await this.apiService.post(url, body, { params });

return data;
} catch (error) {
// @ts-ignore
throw new HttpException(error.response, error.status);
}
}
}
6 changes: 4 additions & 2 deletions src/endpoints/proxy/proxy.module.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { Module } from "@nestjs/common";
import { PluginModule } from "src/plugins/plugin.module";
import { VmQueryModule } from "../vm.query/vm.query.module";
import { ProxyController } from "./proxy.controller";
import { GatewayProxyController } from "./gateway.proxy.controller";
import { IndexProxyController } from "./index.proxy.controller";

@Module({
imports: [
VmQueryModule,
PluginModule,
],
controllers: [
ProxyController,
GatewayProxyController,
IndexProxyController,
],
})
export class ProxyModule { }
4 changes: 2 additions & 2 deletions src/interceptors/transaction.logging.interceptor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { OriginLogger } from "@multiversx/sdk-nestjs-common";
import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from "@nestjs/common";
import { Observable } from "rxjs";
import { ProxyController } from "src/endpoints/proxy/proxy.controller";
import { GatewayProxyController } from "src/endpoints/proxy/gateway.proxy.controller";
import { TransactionController } from "src/endpoints/transactions/transaction.controller";
import winston from "winston";
import DailyRotateFile from "winston-daily-rotate-file";
Expand Down Expand Up @@ -52,7 +52,7 @@ export class TransactionLoggingInterceptor implements NestInterceptor {
const request = context.getArgByIndex(0);

const isCreateTransactionCall = context.getClass().name === TransactionController.name && context.getHandler().name === 'createTransaction';
const isSendTransactionCall = context.getClass().name === ProxyController.name && context.getHandler().name === 'transactionSend';
const isSendTransactionCall = context.getClass().name === GatewayProxyController.name && context.getHandler().name === 'transactionSend';

if (isCreateTransactionCall || isSendTransactionCall) {
const logBody = {
Expand Down

0 comments on commit 9ea0389

Please sign in to comment.