feat(browser): add POST /exec for custom Playwright code execution

- accepts sessionName, optional url, and arbitrary JS code string
- restores cookies and localStorage from session before execution
- exposes page and context to user code as async function arguments
- session not found now returns 404 instead of 400
This commit is contained in:
ars9 committed 2026-04-07 12:36:27 +03:00
1 parent 9e5a813e9d
commit 9898fb6472
3 files changed
+110 -3

No files matched your search

+21 -2
View File
@@ -1,7 +1,8 @@
import { Body, Controller, Post } from '@nestjs/common';
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { BrowserService, OpenResult } from './browser.service';
import { BrowserService, ExecResult, OpenResult } from './browser.service';
import { OpenDto } from './dto/open.dto';
import { ExecDto } from './dto/exec.dto';
@ApiTags('browser')
@Controller()
@@ -21,9 +22,27 @@ export class BrowserController {
},
},
})
@ApiResponse({ status: 400, description: 'Session not found or invalid input' })
@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);
}
@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' })
exec(@Body() dto: ExecDto): Promise<ExecResult> {
return this.browserService.exec(dto.sessionName, dto.code, dto.url);
}
}