import { BadRequestException, Body, Controller, Post } from "@nestjs/common"; import { ApiBody, ApiConsumes, ApiOperation, ApiResponse, ApiTags, } from "@nestjs/swagger"; import { YAML_CONTENT_TYPES } from "../common/body-parsers"; import { ImportService } from "./import.service"; @ApiTags("import") @Controller("import") export class ImportController { constructor(private readonly importService: ImportService) {} @Post() @ApiOperation({ summary: "Import any mix of environments, credentials, snippets and scenarios", description: "Send YAML (Content-Type application/yaml): `---`-separated documents, each holding one exported item or a list of them. " + "A JSON array of items (or a single item) is accepted too. Every item needs a `kind`. " + "All items are validated before anything is imported, then imported in dependency order: environments, credentials, snippets, scenarios.", }) @ApiConsumes(...YAML_CONTENT_TYPES, "application/json") @ApiBody({ schema: { type: "string" } }) @ApiResponse({ status: 201, description: "Imported items" }) @ApiResponse({ status: 400, description: "Invalid YAML or items; nothing was imported", }) import(@Body() body: unknown) { if (typeof body === "string") { return this.importService.importYaml(body); } if (Array.isArray(body)) return this.importService.importItems(body); if (body && typeof body === "object" && Object.keys(body).length > 0) { return this.importService.importItems([body]); } throw new BadRequestException( "Send YAML with Content-Type application/yaml, or a JSON item or array of items", ); } }