feat(snippets): add snippets entity, crud, and auto-browser-per-run
- add Snippet entity with name, description, code; full CRUD backend - add snippets pages (list, create, edit, detail) and nav entry - add runSnippet helper in code-executor using new Function with args array - add result param to execute() so validateCode can access exec output - remove sessionName from steps; each run now spawns its own fresh browser - fix waitForURL race by polling localStorage for token instead
This commit is contained in:
1 parent
1efbbb38a3
commit
1164289173
26 files changed
+876
-89
No files matched your search
@@ -0,0 +1,19 @@
|
||||
import { ApiProperty, ApiPropertyOptional } from "@nestjs/swagger";
|
||||
import { IsNotEmpty, IsOptional, IsString } from "class-validator";
|
||||
|
||||
export class CreateSnippetDto {
|
||||
@ApiProperty({ example: "clickLoginButton" })
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
name: string;
|
||||
|
||||
@ApiPropertyOptional({ example: "Clicks the login button and waits for navigation" })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
description?: string;
|
||||
|
||||
@ApiProperty({ example: "await page.click('#login-btn');\nawait page.waitForNavigation();" })
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
code: string;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import { ApiPropertyOptional } from "@nestjs/swagger";
|
||||
import { IsNotEmpty, IsOptional, IsString } from "class-validator";
|
||||
|
||||
export class UpdateSnippetDto {
|
||||
@ApiPropertyOptional({ example: "clickLoginButton" })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
name?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
description?: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
code?: string;
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
Get,
|
||||
HttpCode,
|
||||
Param,
|
||||
ParseIntPipe,
|
||||
Patch,
|
||||
Post,
|
||||
Query,
|
||||
} from "@nestjs/common";
|
||||
import { ApiOperation, ApiResponse, ApiTags } from "@nestjs/swagger";
|
||||
import { SnippetService, SnippetOrderBy } from "./snippet.service";
|
||||
import { CreateSnippetDto } from "./dto/create-snippet.dto";
|
||||
import { UpdateSnippetDto } from "./dto/update-snippet.dto";
|
||||
import { PaginationQueryDto } from "../common/dto/pagination.dto";
|
||||
|
||||
@ApiTags("snippets")
|
||||
@Controller("snippets")
|
||||
export class SnippetController {
|
||||
constructor(private readonly snippetService: SnippetService) {}
|
||||
|
||||
@Post()
|
||||
@ApiOperation({ summary: "Create a new snippet" })
|
||||
@ApiResponse({ status: 201, description: "Snippet created" })
|
||||
@ApiResponse({ status: 409, description: "Name already taken" })
|
||||
create(@Body() dto: CreateSnippetDto) {
|
||||
return this.snippetService.create(dto);
|
||||
}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({ summary: "List all snippets (paginated)" })
|
||||
@ApiResponse({ status: 200, description: "Paginated snippets" })
|
||||
findAll(@Query() query: PaginationQueryDto<SnippetOrderBy>) {
|
||||
return this.snippetService.findAll(query);
|
||||
}
|
||||
|
||||
@Get(":id")
|
||||
@ApiOperation({ summary: "Get snippet by ID" })
|
||||
@ApiResponse({ status: 200, description: "Snippet record" })
|
||||
@ApiResponse({ status: 404, description: "Snippet not found" })
|
||||
findOne(@Param("id", ParseIntPipe) id: number) {
|
||||
return this.snippetService.findOne(id);
|
||||
}
|
||||
|
||||
@Patch(":id")
|
||||
@ApiOperation({ summary: "Update a snippet" })
|
||||
@ApiResponse({ status: 200, description: "Snippet updated" })
|
||||
@ApiResponse({ status: 404, description: "Snippet not found" })
|
||||
@ApiResponse({ status: 409, description: "Name already taken" })
|
||||
update(
|
||||
@Param("id", ParseIntPipe) id: number,
|
||||
@Body() dto: UpdateSnippetDto,
|
||||
) {
|
||||
return this.snippetService.update(id, dto);
|
||||
}
|
||||
|
||||
@Delete(":id")
|
||||
@HttpCode(204)
|
||||
@ApiOperation({ summary: "Delete a snippet" })
|
||||
@ApiResponse({ status: 204, description: "Snippet deleted" })
|
||||
@ApiResponse({ status: 404, description: "Snippet not found" })
|
||||
remove(@Param("id", ParseIntPipe) id: number) {
|
||||
return this.snippetService.remove(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import {
|
||||
Entity,
|
||||
PrimaryGeneratedColumn,
|
||||
Column,
|
||||
CreateDateColumn,
|
||||
UpdateDateColumn,
|
||||
} from "typeorm";
|
||||
|
||||
@Entity("snippets")
|
||||
export class SnippetEntity {
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
|
||||
/** Unique identifier used to call the snippet: helpers.runSnippet('mySnippet', ...) */
|
||||
@Column({ unique: true })
|
||||
name: string;
|
||||
|
||||
@Column("text", { nullable: true })
|
||||
description: string | null;
|
||||
|
||||
/**
|
||||
* The snippet body — written as a regular async function body.
|
||||
* It receives the same `page`, `context`, and `helpers` as exec code, plus
|
||||
* any positional `...args` passed by the caller.
|
||||
*/
|
||||
@Column("text")
|
||||
code: string;
|
||||
|
||||
@CreateDateColumn()
|
||||
createdAt: Date;
|
||||
|
||||
@UpdateDateColumn()
|
||||
updatedAt: Date;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { Module } from "@nestjs/common";
|
||||
import { TypeOrmModule } from "@nestjs/typeorm";
|
||||
import { SnippetEntity } from "./snippet.entity";
|
||||
import { SnippetService } from "./snippet.service";
|
||||
import { SnippetController } from "./snippet.controller";
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([SnippetEntity])],
|
||||
controllers: [SnippetController],
|
||||
providers: [SnippetService],
|
||||
exports: [SnippetService],
|
||||
})
|
||||
export class SnippetModule {}
|
||||
@@ -0,0 +1,76 @@
|
||||
import {
|
||||
ConflictException,
|
||||
Injectable,
|
||||
NotFoundException,
|
||||
} from "@nestjs/common";
|
||||
import { InjectRepository } from "@nestjs/typeorm";
|
||||
import { Repository } from "typeorm";
|
||||
import { SnippetEntity } from "./snippet.entity";
|
||||
import { CreateSnippetDto } from "./dto/create-snippet.dto";
|
||||
import { UpdateSnippetDto } from "./dto/update-snippet.dto";
|
||||
import {
|
||||
PaginationQueryDto,
|
||||
PaginatedResult,
|
||||
} from "../common/dto/pagination.dto";
|
||||
|
||||
export type SnippetOrderBy = "id" | "name" | "createdAt" | "updatedAt";
|
||||
|
||||
@Injectable()
|
||||
export class SnippetService {
|
||||
constructor(
|
||||
@InjectRepository(SnippetEntity)
|
||||
private readonly repo: Repository<SnippetEntity>,
|
||||
) {}
|
||||
|
||||
async create(dto: CreateSnippetDto): Promise<SnippetEntity> {
|
||||
const existing = await this.repo.findOneBy({ name: dto.name });
|
||||
if (existing) {
|
||||
throw new ConflictException(`Snippet "${dto.name}" already exists`);
|
||||
}
|
||||
return this.repo.save(
|
||||
this.repo.create({ ...dto, description: dto.description ?? null }),
|
||||
);
|
||||
}
|
||||
|
||||
async findAll(
|
||||
query: PaginationQueryDto<SnippetOrderBy> = {},
|
||||
): Promise<PaginatedResult<SnippetEntity>> {
|
||||
const page = query.page ?? 1;
|
||||
const limit = query.limit ?? 50;
|
||||
const orderBy = query.orderBy ?? "id";
|
||||
const orderDir = query.orderDir ?? "ASC";
|
||||
const [data, total] = await this.repo.findAndCount({
|
||||
order: { [orderBy]: orderDir },
|
||||
skip: (page - 1) * limit,
|
||||
take: limit,
|
||||
});
|
||||
return { data, total, page, limit };
|
||||
}
|
||||
|
||||
async findOne(id: number): Promise<SnippetEntity> {
|
||||
const snippet = await this.repo.findOneBy({ id });
|
||||
if (!snippet) throw new NotFoundException(`Snippet ${id} not found`);
|
||||
return snippet;
|
||||
}
|
||||
|
||||
async update(id: number, dto: UpdateSnippetDto): Promise<SnippetEntity> {
|
||||
const snippet = await this.findOne(id);
|
||||
if (dto.name && dto.name !== snippet.name) {
|
||||
const conflict = await this.repo.findOneBy({ name: dto.name });
|
||||
if (conflict) throw new ConflictException(`Snippet "${dto.name}" already exists`);
|
||||
}
|
||||
Object.assign(snippet, dto);
|
||||
return this.repo.save(snippet);
|
||||
}
|
||||
|
||||
async remove(id: number): Promise<void> {
|
||||
await this.findOne(id);
|
||||
await this.repo.delete(id);
|
||||
}
|
||||
|
||||
/** Returns a name→code map for all snippets (used by the executor). */
|
||||
async buildSnippetMap(): Promise<Record<string, string>> {
|
||||
const { data } = await this.findAll({ limit: 1000 });
|
||||
return Object.fromEntries(data.map((s) => [s.name, s.code]));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user