- format long import statements with consistent line wrapping - apply consistent indentation across client and server modules - remove generated tsconfig.tsbuildinfo file
90 lines
3.1 KiB
TypeScript
90 lines
3.1 KiB
TypeScript
import { Body, Controller, Post } from "@nestjs/common";
|
|
import { ApiOperation, ApiResponse, ApiTags } from "@nestjs/swagger";
|
|
import { CodeExecutorService } from "../code-executor/code-executor.service";
|
|
import { CredentialService } from "../credential/credential.service";
|
|
import { EnvironmentService } from "../environment/environment.service";
|
|
import { BrowserService, ExecResult, OpenResult } from "./browser.service";
|
|
import { ExecDto } from "./dto/exec.dto";
|
|
import { OpenDto } from "./dto/open.dto";
|
|
|
|
@ApiTags("browser")
|
|
@Controller()
|
|
export class BrowserController {
|
|
constructor(
|
|
private readonly browserService: BrowserService,
|
|
private readonly codeExecutor: CodeExecutorService,
|
|
private readonly environmentService: EnvironmentService,
|
|
private readonly credentialService: CredentialService,
|
|
) {}
|
|
|
|
@Post("open")
|
|
@ApiOperation({
|
|
summary: "Open a URL with a stored session (cookies + localStorage)",
|
|
})
|
|
@ApiResponse({
|
|
status: 201,
|
|
description: "Page loaded successfully",
|
|
schema: {
|
|
properties: {
|
|
url: { type: "string" },
|
|
title: { type: "string" },
|
|
content: { type: "string" },
|
|
},
|
|
},
|
|
})
|
|
@ApiResponse({ status: 400, description: "Invalid input" })
|
|
@ApiResponse({ status: 404, description: "Session not found" })
|
|
@ApiResponse({ status: 500, description: "Browser automation failed" })
|
|
open(@Body() dto: OpenDto): Promise<OpenResult> {
|
|
return this.browserService.open(
|
|
dto.sessionName,
|
|
dto.url,
|
|
dto.readerMode ?? false,
|
|
dto.selector,
|
|
);
|
|
}
|
|
|
|
@Post("exec")
|
|
@ApiOperation({
|
|
summary: "Execute custom Playwright JavaScript within a stored session",
|
|
description:
|
|
"The `code` string is executed as an async function body with `page` (Playwright Page) and `context` (BrowserContext) in scope. The return value is serialised and returned as `result`.",
|
|
})
|
|
@ApiResponse({
|
|
status: 201,
|
|
description: "Code executed successfully",
|
|
schema: { properties: { result: {} } },
|
|
})
|
|
@ApiResponse({ status: 400, description: "Invalid input" })
|
|
@ApiResponse({ status: 404, description: "Session not found" })
|
|
@ApiResponse({ status: 500, description: "Execution failed" })
|
|
async exec(@Body() dto: ExecDto): Promise<ExecResult> {
|
|
this.codeExecutor.validate(dto.code);
|
|
|
|
// Resolve environment from DB
|
|
let environment: Record<string, string | undefined> | undefined;
|
|
if (dto.environmentId) {
|
|
const env = await this.environmentService.findOne(dto.environmentId);
|
|
environment = env.data;
|
|
}
|
|
|
|
// Resolve credentials from DB (alias → payload)
|
|
let credentials: Record<string, unknown> | undefined;
|
|
if (dto.credentials && Object.keys(dto.credentials).length > 0) {
|
|
credentials = {};
|
|
for (const [alias, credId] of Object.entries(dto.credentials)) {
|
|
const cred = await this.credentialService.findOne(credId);
|
|
credentials[alias] = cred.data ? JSON.parse(cred.data) : {};
|
|
}
|
|
}
|
|
|
|
return this.browserService.exec(
|
|
dto.sessionName,
|
|
dto.code,
|
|
dto.url,
|
|
environment,
|
|
credentials,
|
|
);
|
|
}
|
|
}
|