feat: add TypeScript compilation support in QueryExecuterService
This commit is contained in:
@ -27,4 +27,7 @@ export class Query {
|
||||
|
||||
@Column({ type: "tinyint", default: 0 })
|
||||
isCommand: number;
|
||||
|
||||
@Column({ type: "tinyint", default: 0 })
|
||||
isTypescript: number;
|
||||
}
|
||||
|
||||
@ -19,6 +19,7 @@ import { SessionService } from "../session/session.service";
|
||||
import { TLog, TLogType } from "../logger/logger.types";
|
||||
import { LoggerService } from "../logger/logger.service";
|
||||
import { ProjectSettingService } from "src/project/settings/project.setting.service";
|
||||
import ts from "typescript";
|
||||
|
||||
@Injectable()
|
||||
export class QueryExecuterService {
|
||||
@ -89,6 +90,14 @@ export class QueryExecuterService {
|
||||
return result;
|
||||
}
|
||||
|
||||
private compileTypeScript(tsCode: string) {
|
||||
const jsCode = ts.transpileModule(tsCode, {
|
||||
compilerOptions: { module: ts.ModuleKind.CommonJS },
|
||||
}).outputText;
|
||||
|
||||
return jsCode;
|
||||
}
|
||||
|
||||
async runQuery(
|
||||
token: string,
|
||||
queryData: any,
|
||||
@ -122,17 +131,19 @@ export class QueryExecuterService {
|
||||
type: TLogType.info,
|
||||
});
|
||||
|
||||
let script = this.clearImports(query.source);
|
||||
|
||||
if (query.isTypescript) {
|
||||
script = this.compileTypeScript(script);
|
||||
}
|
||||
|
||||
const vm = await this.createVm(
|
||||
query,
|
||||
log,
|
||||
callStack,
|
||||
cookies["x-session-id"]
|
||||
);
|
||||
const result = await vm.runScript(
|
||||
this.clearImports(query.source),
|
||||
queryData,
|
||||
headers
|
||||
);
|
||||
const result = await vm.runScript(script, queryData, headers);
|
||||
|
||||
if (!this.checkResponse(result)) {
|
||||
throw new Error(`Error initializing VM: ${JSON.stringify(result)}`);
|
||||
|
||||
Reference in New Issue
Block a user