import { IExportAllResult, IExportConfig, IExportData } from './export.models.js'; import colors from 'colors'; import { defaultRetryStrategy, defaultHttpService, printEnvironmentInfoToConsoleAsync, executeWithTrackingAsync } from '../core/index.js'; import { ContentTypeModels, createManagementClient, ManagementClient, TaxonomyModels } from '@kontent-ai/management-sdk'; import { libMetadata } from '../metadata.js'; export class ExportService { private readonly managementClient: ManagementClient; constructor(private readonly config: IExportConfig) { const retryStrategy = config.retryStrategy ?? defaultRetryStrategy; this.managementClient = createManagementClient({ environmentId: config.environmentId, retryStrategy: retryStrategy, httpService: defaultHttpService, apiKey: config.apiKey, baseUrl: config.baseUrl }); } async exportAllAsync(): Promise { return await executeWithTrackingAsync({ event: { action: 'export', tool: 'contentModelAccelerator', package: { name: libMetadata.name, version: libMetadata.version }, result: 'unknown', relatedEnvironmentId: this.config.environmentId }, func: async () => { const environment = await printEnvironmentInfoToConsoleAsync(this.config.log, this.managementClient); const contentTypes = await this.getContentTypesAsync(); this.config.log?.({ type: 'Fetch', message: `Fetched '${colors.yellow(contentTypes.length.toString())}' content types` }); const taxonomies = await this.getTaxonomiesAsync(); this.config.log?.({ type: 'Fetch', message: `Fetched '${colors.yellow(taxonomies.length.toString())}' taxonomies` }); const contentTypeSnippets = await this.getContentTypeSnippetsAsync(); this.config.log?.({ type: 'Fetch', message: `Fetched '${colors.yellow(contentTypeSnippets.length.toString())}' content type snippets` }); const data: IExportData = { contentTypes: contentTypes, contentTypeSnippets: contentTypeSnippets, taxonomies: taxonomies }; return { metadata: { project: environment.name, environment: environment.environment, created: new Date() }, data }; } }); } private async getTaxonomiesAsync(): Promise { const response = await this.managementClient.listTaxonomies().toAllPromise(); return response.data.items; } private async getContentTypeSnippetsAsync(): Promise { const response = await this.managementClient.listContentTypeSnippets().toAllPromise(); return response.data.items; } private async getContentTypesAsync(): Promise { const response = await this.managementClient.listContentTypes().toAllPromise(); return response.data.items; } }