UNPKG

@kontent-ai/model-generator

Version:

This utility generates strongly-typed models for Delivery JS SDK, Migration toolkit or just general scripting to improve the experience when referencing Kontent.ai related objects.

126 lines 5.18 kB
import { HttpService } from "coreSdkLegacy"; import { createManagementClient } from "@kontent-ai/management-sdk"; import chalk from "chalk"; import { coreConfig } from "../config.js"; import { toSafeCommentText } from "../core/comment.utils.js"; import { extractErrorData } from "../core/error.utils.js"; export function getManagementKontentFetcher(config) { const client = createManagementClient({ environmentId: config.environmentId, apiKey: config.managementApiKey, baseUrl: config.baseUrl, httpService: new HttpService({ logErrorsToConsole: false }), headers: [{ header: coreConfig.kontentTrackingHeaderName, value: coreConfig.kontentTrackingHeaderValue }], }); return { async getEnvironmentInfoAsync() { const projectInformation = (await client.environmentInformation().toPromise()).data; console.log(`Project '${chalk.cyan(toSafeCommentText(projectInformation.project.name))}'`); console.log(`Environment '${chalk.cyan(toSafeCommentText(projectInformation.project.environment))}'\n`); return projectInformation.project; }, async getItemsAsync() { return await fetchItemsAsync({ fetch: async () => (await client .listContentItems() .withListQueryConfig({ responseFetched: (response) => { console.log(`Fetched '${chalk.yellow(response.data.items.length.toString())}' content items`); }, }) .toAllPromise()).data.items, itemType: "total content items", }); }, async getWorkflowsAsync() { return await fetchItemsAsync({ fetch: async () => (await client.listWorkflows().toPromise()).data, itemType: "workflows", }); }, async getRolesAsync() { return await fetchItemsAsync({ fetch: async () => (await client.listRoles().toPromise()).data.roles, itemType: "roles", // roles are available only for enterprise subscriptions returnEmptyArrayOnMapiError: true, }); }, async getAssetFoldersAsync() { return await fetchItemsAsync({ fetch: async () => (await client.listAssetFolders().toPromise()).data.items, itemType: "asset folders", }); }, async getCollectionsAsync() { return await fetchItemsAsync({ fetch: async () => (await client.listCollections().toPromise()).data.collections, itemType: "collections", }); }, async getWebhooksAsync() { return await fetchItemsAsync({ fetch: async () => (await client.listWebhooks().toPromise()).data.webhooks, itemType: "webhooks", }); }, async getLanguagesAsync() { return await fetchItemsAsync({ fetch: async () => (await client.listLanguages().toAllPromise()).data.items, itemType: "languages", }); }, async getTypesAsync() { return await fetchItemsAsync({ fetch: async () => (await client.listContentTypes().toAllPromise()).data.items, itemType: "types", }); }, async getSnippetsAsync() { return await fetchItemsAsync({ fetch: async () => (await client.listContentTypeSnippets().toAllPromise()).data.items, itemType: "snippets", }); }, async getTaxonomiesAsync() { return await fetchItemsAsync({ fetch: async () => (await client.listTaxonomies().toAllPromise()).data.items, itemType: "taxonomies", }); }, async getCustomApps() { return await fetchItemsAsync({ fetch: async () => (await client.listCustomApps().toPromise()).data.items, itemType: "custom apps", }); }, async getSpaces() { return await fetchItemsAsync({ fetch: async () => (await client.listSpaces().toPromise()).data, itemType: "spaces", }); }, async getPreviewUrlConfiguration() { return (await client.getPreviewConfiguration().toPromise()).data; }, }; } async function fetchItemsAsync({ fetch, itemType, returnEmptyArrayOnMapiError, }) { try { const data = await fetch(); console.log(`Fetched '${chalk.yellow(data.length.toString())}' ${itemType}`); return data; } catch (error) { if (!returnEmptyArrayOnMapiError) { throw error; } const errorData = extractErrorData(error); if (errorData.isMapiError) { console.warn(`${chalk.red(`Skip fetching ${itemType}`)}: ${errorData.message}`); return []; } throw error; } } //# sourceMappingURL=management-kontent-fetcher.js.map