forked from multiversx/mx-api-service
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Forward index endpoints (multiversx#1223)
* proxy endpoints for index * renamed proxy controller to gateway proxy controller * removed endpoints in gateway controller * implemented error handling
- Loading branch information
1 parent
9e868f0
commit 9ea0389
Showing
5 changed files
with
87 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters