feat(export-import): add yaml export/import with id upsert for credentials, snippets, and scenarios
- all entities export a kind field (credential/snippet/scenario) for safe type checking on import - import upserts by id: overwrites if id exists, creates with explicit id otherwise - scenario export now includes id and step ids; import deletes old steps before recreating - add GET /:id/export and POST /import endpoints to credential and snippet controllers - add UuidBadge ui component: shortens uuid to first+last 4 hex chars, tooltip, clipboard copy with animated ClipboardCheck icon pop - apply UuidBadge across all entity id display sites (detail pages, card footers, table columns) - add export/import buttons to CredentialsPage, SnippetsPage, CredentialDetailPage, SnippetDetailPage
This commit is contained in:
1 parent
1164289173
commit
32be7c0a59
54 files changed
+728
-272
No files matched your search
@@ -0,0 +1,28 @@
|
||||
import { ApiProperty, ApiPropertyOptional } from "@nestjs/swagger";
|
||||
import { IsIn, IsNotEmpty, IsOptional, IsString, IsUUID } from "class-validator";
|
||||
|
||||
export class SnippetExportDto {
|
||||
@ApiPropertyOptional()
|
||||
@IsOptional()
|
||||
@IsIn(["snippet"])
|
||||
kind?: "snippet";
|
||||
|
||||
@ApiPropertyOptional()
|
||||
@IsOptional()
|
||||
@IsUUID()
|
||||
id?: string;
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
name: string;
|
||||
|
||||
@ApiPropertyOptional()
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
description?: string | null;
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
code: string;
|
||||
}
|
||||
@@ -5,7 +5,7 @@ import {
|
||||
Get,
|
||||
HttpCode,
|
||||
Param,
|
||||
ParseIntPipe,
|
||||
ParseUUIDPipe,
|
||||
Patch,
|
||||
Post,
|
||||
Query,
|
||||
@@ -14,6 +14,7 @@ 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 { SnippetExportDto } from "./dto/snippet-export.dto";
|
||||
import { PaginationQueryDto } from "../common/dto/pagination.dto";
|
||||
|
||||
@ApiTags("snippets")
|
||||
@@ -29,6 +30,13 @@ export class SnippetController {
|
||||
return this.snippetService.create(dto);
|
||||
}
|
||||
|
||||
@Post("import")
|
||||
@ApiOperation({ summary: "Import a snippet (upsert by id)" })
|
||||
@ApiResponse({ status: 201, description: "Snippet imported" })
|
||||
async importSnippet(@Body() dto: SnippetExportDto) {
|
||||
return this.snippetService.importSnippet(dto);
|
||||
}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({ summary: "List all snippets (paginated)" })
|
||||
@ApiResponse({ status: 200, description: "Paginated snippets" })
|
||||
@@ -36,11 +44,20 @@ export class SnippetController {
|
||||
return this.snippetService.findAll(query);
|
||||
}
|
||||
|
||||
@Get(":id/export")
|
||||
@ApiOperation({ summary: "Export a snippet as a plain object" })
|
||||
@ApiResponse({ status: 200, description: "Snippet export payload" })
|
||||
@ApiResponse({ status: 404, description: "Snippet not found" })
|
||||
async exportSnippet(@Param("id", ParseUUIDPipe) id: string) {
|
||||
const snippet = await this.snippetService.findOne(id);
|
||||
return this.snippetService.exportSnippet(snippet);
|
||||
}
|
||||
|
||||
@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) {
|
||||
findOne(@Param("id", ParseUUIDPipe) id: string) {
|
||||
return this.snippetService.findOne(id);
|
||||
}
|
||||
|
||||
@@ -50,7 +67,7 @@ export class SnippetController {
|
||||
@ApiResponse({ status: 404, description: "Snippet not found" })
|
||||
@ApiResponse({ status: 409, description: "Name already taken" })
|
||||
update(
|
||||
@Param("id", ParseIntPipe) id: number,
|
||||
@Param("id", ParseUUIDPipe) id: string,
|
||||
@Body() dto: UpdateSnippetDto,
|
||||
) {
|
||||
return this.snippetService.update(id, dto);
|
||||
@@ -61,7 +78,7 @@ export class SnippetController {
|
||||
@ApiOperation({ summary: "Delete a snippet" })
|
||||
@ApiResponse({ status: 204, description: "Snippet deleted" })
|
||||
@ApiResponse({ status: 404, description: "Snippet not found" })
|
||||
remove(@Param("id", ParseIntPipe) id: number) {
|
||||
remove(@Param("id", ParseUUIDPipe) id: string) {
|
||||
return this.snippetService.remove(id);
|
||||
}
|
||||
}
|
||||
@@ -8,8 +8,8 @@ import {
|
||||
|
||||
@Entity("snippets")
|
||||
export class SnippetEntity {
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
@PrimaryGeneratedColumn("uuid")
|
||||
id: string;
|
||||
|
||||
/** Unique identifier used to call the snippet: helpers.runSnippet('mySnippet', ...) */
|
||||
@Column({ unique: true })
|
||||
|
||||
@@ -8,6 +8,7 @@ import { Repository } from "typeorm";
|
||||
import { SnippetEntity } from "./snippet.entity";
|
||||
import { CreateSnippetDto } from "./dto/create-snippet.dto";
|
||||
import { UpdateSnippetDto } from "./dto/update-snippet.dto";
|
||||
import { SnippetExportDto } from "./dto/snippet-export.dto";
|
||||
import {
|
||||
PaginationQueryDto,
|
||||
PaginatedResult,
|
||||
@@ -47,13 +48,13 @@ export class SnippetService {
|
||||
return { data, total, page, limit };
|
||||
}
|
||||
|
||||
async findOne(id: number): Promise<SnippetEntity> {
|
||||
async findOne(id: string): 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> {
|
||||
async update(id: string, 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 });
|
||||
@@ -63,7 +64,7 @@ export class SnippetService {
|
||||
return this.repo.save(snippet);
|
||||
}
|
||||
|
||||
async remove(id: number): Promise<void> {
|
||||
async remove(id: string): Promise<void> {
|
||||
await this.findOne(id);
|
||||
await this.repo.delete(id);
|
||||
}
|
||||
@@ -73,4 +74,43 @@ export class SnippetService {
|
||||
const { data } = await this.findAll({ limit: 1000 });
|
||||
return Object.fromEntries(data.map((s) => [s.name, s.code]));
|
||||
}
|
||||
|
||||
exportSnippet(snippet: SnippetEntity): SnippetExportDto {
|
||||
return {
|
||||
kind: "snippet",
|
||||
id: snippet.id,
|
||||
name: snippet.name,
|
||||
description: snippet.description,
|
||||
code: snippet.code,
|
||||
};
|
||||
}
|
||||
|
||||
async importSnippet(dto: SnippetExportDto): Promise<SnippetEntity> {
|
||||
if (dto.id) {
|
||||
const existing = await this.repo.findOneBy({ id: dto.id });
|
||||
if (existing) {
|
||||
Object.assign(existing, {
|
||||
name: dto.name,
|
||||
description: dto.description ?? null,
|
||||
code: dto.code,
|
||||
});
|
||||
return this.repo.save(existing);
|
||||
}
|
||||
return this.repo.save(
|
||||
this.repo.create({
|
||||
id: dto.id,
|
||||
name: dto.name,
|
||||
description: dto.description ?? null,
|
||||
code: dto.code,
|
||||
}),
|
||||
);
|
||||
}
|
||||
return this.repo.save(
|
||||
this.repo.create({
|
||||
name: dto.name,
|
||||
description: dto.description ?? null,
|
||||
code: dto.code,
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user