I have implemented pattern that has to make two calls to different api and combine the response, I need help here from experts if there is any better way or best practice to acheive these task.
In getBalanceController calling both api's to retrieve the responses using axios, what would be good design to follow in below case scenario ? just wanted to make sure i am not doing anything redundant.
getBalanceController.ts
import {Controller,Body} from 'tsoa';
import {AdminHandler} from "./AdminHandler";
import {ClientHandler} from "./ClientHandler";
export class GetBalanceController extends Controller {
public static async process(@Request() request: ExpressRequest,
response: ExpressResponse): Promise < any > {
try {
const instance = new GetBalanceController();
const data = await instance.execute(request, response);
response.status(200);
response.send(data);
} catch (err) {
response.status(200);
response.send(err.message);
}
}
private _request: IRequestURL[] = [];
constructor() {
super();
}
private async execute(@Request() request: ExpressRequest, response: ExpressResponse): Promise < any > {
buildResponses:any = [];
try {
const _adminData = await AdminHandler.process(request,response);
const _clientData = await ClientHandler.process(request,response);
buildResponses.Details.push(_adminData, _caremarkData);
} catch (err) {
return Promise.reject(err);
}
return buildResponses;
}
}
adminHandler.ts
import {Controller,Body} from 'tsoa';
export class AdminHandler extends Controller {
public static async process(@Request() request: any,
response: any): Promise<any> {
try {
const instance = new AdminHandler();
const data = await instance.execute(request);
response.status(200);
response.send(data);
} catch (err) {
response.status(200);
response.send(err.message);
}
}
constructor() {
super();
}
public async execute(@Body() _request: any): Promise<any> {
let result: any;
try {
const url = "adminUrl;
const requestKeys: string[] = Object.keys(_request);
if (requestKeys.length !== 1 ||
!(_request[requestKeys[0]].details && _request[requestKeys[0]].details.tokenId)) {
throw new Error("Invalid request");
}
const response: any = await axios.post(url, _request);
result = response.data;
} catch (err) {
return Promise.reject(err);
}
return Promise.resolve(result);
}
// there will be another method that will be called again from main controller to customize the response.
}
clientHandler.ts
import {Controller,Body} from 'tsoa';
export class ClientHandler extends Controller {
public static async process(@Request() request: any,
response: any): Promise<any> {
try {
const instance = new ClientHandler();
const data = await instance.execute(request);
response.status(200);
response.send(data);
} catch (err) {
response.status(200);
response.send(err.message);
}
}
constructor() {
super();
}
public async execute(@Body() _request: any): Promise<any> {
let result: any;
try {
const url = "clientUrl;
const requestKeys: string[] = Object.keys(_request);
if (requestKeys.length !== 1 ||
!(_request[requestKeys[0]].details && _request[requestKeys[0]].details.tokenId)) {
throw new Error("Invalid request");
}
const response: any = await axios.post(url, _request);
result = response.data;
} catch (err) {
return Promise.reject(err);
}
return Promise.resolve(result);
}
// there will be another method that will be called again from main controller to customize the response.
}
Aucun commentaire:
Enregistrer un commentaire