import { BadRequestException, Injectable } from "@nestjs/common"; import { ClassConstructor, plainToInstance } from "class-transformer"; import { validateSync, ValidationError } from "class-validator"; import { parseAllDocuments } from "yaml"; import { CredentialExportDto } from "../credential/dto/credential-export.dto"; import { CredentialService } from "../credential/credential.service"; import { EnvironmentExportDto } from "../environment/dto/environment-export.dto"; import { EnvironmentService } from "../environment/environment.service"; import { ScenarioExportDto } from "../scenario/dto/scenario-export.dto"; import { ScenarioService } from "../scenario/scenario.service"; import { SnippetExportDto } from "../snippet/dto/snippet-export.dto"; import { SnippetService } from "../snippet/snippet.service"; export type ImportKind = "environment" | "credential" | "snippet" | "scenario"; /** Import order: later kinds may reference earlier ones (scenarios → environments/credentials). */ const KIND_ORDER: ImportKind[] = [ "environment", "credential", "snippet", "scenario", ]; const DTO_BY_KIND: Record> = { environment: EnvironmentExportDto, credential: CredentialExportDto, snippet: SnippetExportDto, scenario: ScenarioExportDto, }; export interface ImportedItem { kind: ImportKind; id: string; name: string; /** Version stored for versioned kinds (snippets, scenarios). */ version?: number; } export interface ImportResult { imported: ImportedItem[]; } function formatErrors(errors: ValidationError[], prefix = ""): string[] { return errors.flatMap((e) => { const field = prefix ? `${prefix}.${e.property}` : e.property; return [ ...Object.values(e.constraints ?? {}).map((msg) => `${field}: ${msg}`), ...formatErrors(e.children ?? [], field), ]; }); } /** * Imports any mix of exported environments, credentials, snippets and * scenarios in one go. Each item must carry its `kind`. */ @Injectable() export class ImportService { constructor( private readonly environmentService: EnvironmentService, private readonly credentialService: CredentialService, private readonly snippetService: SnippetService, private readonly scenarioService: ScenarioService, ) {} /** * Parses a YAML stream: `---`-separated documents, each holding a single * item or a list of items. JSON is valid YAML, so JSON input works too. */ parseYaml(text: string): unknown[] { const docs = parseAllDocuments(text); const items: unknown[] = []; docs.forEach((doc, index) => { if (doc.errors.length > 0) { throw new BadRequestException( `YAML document ${index + 1}: ${doc.errors[0].message}`, ); } const value: unknown = doc.toJS(); if (value == null) return; // empty document, e.g. a trailing `---` items.push(...(Array.isArray(value) ? value : [value])); }); return items; } async importYaml(text: string): Promise { return this.importItems(this.parseYaml(text)); } /** * Validates every item before importing any, then imports them in * dependency order (environments, credentials, snippets, scenarios). */ async importItems(items: unknown[]): Promise { if (items.length === 0) { throw new BadRequestException("Nothing to import"); } const errors: string[] = []; const validated: { kind: ImportKind; dto: object }[] = []; items.forEach((item, index) => { const label = `Item ${index + 1}`; if (item == null || typeof item !== "object" || Array.isArray(item)) { errors.push(`${label}: must be an object`); return; } const kind = (item as { kind?: unknown }).kind; if (typeof kind !== "string" || !(kind in DTO_BY_KIND)) { errors.push( `${label}: kind must be one of ${KIND_ORDER.join(", ")} (got ${JSON.stringify(kind)})`, ); return; } const dto = plainToInstance(DTO_BY_KIND[kind as ImportKind], item); const itemErrors = formatErrors(validateSync(dto, { whitelist: true })); if (itemErrors.length > 0) { errors.push(`${label} (${kind}): ${itemErrors.join("; ")}`); return; } validated.push({ kind: kind as ImportKind, dto }); }); if (errors.length > 0) { throw new BadRequestException(errors); } const imported: ImportedItem[] = []; for (const kind of KIND_ORDER) { for (const item of validated.filter((v) => v.kind === kind)) { imported.push(await this.importOne(kind, item.dto)); } } return { imported }; } private async importOne( kind: ImportKind, dto: object, ): Promise { switch (kind) { case "environment": { const env = await this.environmentService.importEnvironment( dto as EnvironmentExportDto, ); return { kind, id: env.id, name: env.name }; } case "credential": { const cred = await this.credentialService.importCredential( dto as CredentialExportDto, ); return { kind, id: cred.id, name: cred.name }; } case "snippet": { const snippet = await this.snippetService.importSnippet( dto as SnippetExportDto, ); return { kind, id: snippet.id, name: snippet.alias, version: snippet.version, }; } case "scenario": { const scenario = await this.scenarioService.importScenario( dto as ScenarioExportDto, ); return { kind, id: scenario.id, name: scenario.name, version: scenario.version, }; } } } }