feat: enhance query execution with response handling and add QueryResponse type
This commit is contained in:
@ -4,7 +4,11 @@ import { Query } from "../entities/query.entity";
|
||||
import { Repository } from "typeorm";
|
||||
import { Vm } from "../../vm/vm.class";
|
||||
import { VModule } from "src/vm/module.class";
|
||||
import { registeredModules, registeredPlugins } from "src/vm/vm.constants";
|
||||
import {
|
||||
QueryResponse,
|
||||
registeredModules,
|
||||
registeredPlugins,
|
||||
} from "src/vm/vm.constants";
|
||||
import { DatabaseManagerService } from "src/databaseManager/database/database.manager.service";
|
||||
|
||||
@Injectable()
|
||||
@ -38,7 +42,7 @@ export class QueryExecuterService {
|
||||
token: string,
|
||||
queryData: any,
|
||||
headers: Record<string, any> = {}
|
||||
) {
|
||||
): Promise<QueryResponse> {
|
||||
const query = await this.queryRepository.findOne({
|
||||
where: { id: token },
|
||||
relations: ["project"],
|
||||
@ -55,7 +59,11 @@ export class QueryExecuterService {
|
||||
headers
|
||||
);
|
||||
|
||||
return { message: "Query executed", result, query: queryData };
|
||||
if (this.checkResponse(result)) {
|
||||
throw new Error(`Error initializing VM: ${JSON.stringify(result)}`);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private async createVm(query: Query) {
|
||||
@ -97,4 +105,19 @@ export class QueryExecuterService {
|
||||
|
||||
return await vm.init();
|
||||
}
|
||||
|
||||
private checkResponse(obj: any): obj is QueryResponse {
|
||||
return (
|
||||
obj !== null &&
|
||||
typeof obj === "object" &&
|
||||
typeof obj.statusCode === "number" &&
|
||||
typeof obj.response === "object" &&
|
||||
obj.response !== null &&
|
||||
typeof obj.headers === "object" &&
|
||||
obj.headers !== null &&
|
||||
Object.keys(obj.headers).every(
|
||||
(key) => typeof obj.headers[key] === "string"
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user