Skip to content

Commit

Permalink
Transform EntityDefinition to support both import and export
Browse files Browse the repository at this point in the history
  • Loading branch information
JiriLojda committed Dec 1, 2023
1 parent 5a581aa commit 641bfcb
Show file tree
Hide file tree
Showing 19 changed files with 82 additions and 55 deletions.
28 changes: 14 additions & 14 deletions src/commands/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ import JSZip from "jszip";
import packageFile from "../../package.json" assert { type: "json" };
import { RegisterCommand } from "../types/yargs.js";
import { serially } from "../utils/requests.js";
import { assetFoldersExportEntity } from "./export/entities/assetFolders.js";
import { assetsExportEntity } from "./export/entities/assets.js";
import { collectionsExportEntity } from "./export/entities/collections.js";
import { contentItemsExportEntity } from "./export/entities/contentItems.js";
import { contentTypesExportEntity } from "./export/entities/contentTypes.js";
import { contentTypesSnippetsExportEntity } from "./export/entities/contentTypesSnippets.js";
import { languagesExportEntity } from "./export/entities/languages.js";
import { languageVariantsExportEntity } from "./export/entities/languageVariants.js";
import { previewUrlsExportEntity } from "./export/entities/previewUrls.js";
import { rolesExportEntity } from "./export/entities/roles.js";
import { spacesExportEntity } from "./export/entities/spaces.js";
import { taxonomiesExportEntity } from "./export/entities/taxonomies.js";
import { workflowsExportEntity } from "./export/entities/workflows.js";
import { EntityDefinition } from "./export/entityDefinition.js";
import { assetFoldersExportEntity } from "./importExportEntities/entities/assetFolders.js";
import { assetsExportEntity } from "./importExportEntities/entities/assets.js";
import { collectionsExportEntity } from "./importExportEntities/entities/collections.js";
import { contentItemsExportEntity } from "./importExportEntities/entities/contentItems.js";
import { contentTypesExportEntity } from "./importExportEntities/entities/contentTypes.js";
import { contentTypesSnippetsExportEntity } from "./importExportEntities/entities/contentTypesSnippets.js";
import { languagesExportEntity } from "./importExportEntities/entities/languages.js";
import { languageVariantsExportEntity } from "./importExportEntities/entities/languageVariants.js";
import { previewUrlsExportEntity } from "./importExportEntities/entities/previewUrls.js";
import { rolesExportEntity } from "./importExportEntities/entities/roles.js";
import { spacesExportEntity } from "./importExportEntities/entities/spaces.js";
import { taxonomiesExportEntity } from "./importExportEntities/entities/taxonomies.js";
import { workflowsExportEntity } from "./importExportEntities/entities/workflows.js";
import { EntityDefinition } from "./importExportEntities/entityDefinition.js";

const zip = new JSZip();

Expand Down
10 changes: 0 additions & 10 deletions src/commands/export/entities/languages.ts

This file was deleted.

9 changes: 0 additions & 9 deletions src/commands/export/entities/previewUrls.ts

This file was deleted.

9 changes: 0 additions & 9 deletions src/commands/export/entities/roles.ts

This file was deleted.

9 changes: 0 additions & 9 deletions src/commands/export/entities/workflows.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ export const assetFoldersExportEntity: EntityDefinition<ReadonlyArray<AssetFolde
name: "assetFolders",
fetchEntities: client => client.listAssetFolders().toPromise().then(res => res.rawData.folders),
serializeEntities: JSON.stringify,
importEntities: () => { throw new Error("Not supported yet.")},
deserializeEntities: () => { throw new Error("Not supported yet.")},
};
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export const assetsExportEntity: EntityDefinition<ReadonlyArray<AssetContracts.I
}
await serially(assets.map(a => () => saveAsset(assetsZip, a)))
},
importEntities: () => { throw new Error("Not supported yet.")},
deserializeEntities: () => { throw new Error("Not supported yet.")},
};

const saveAsset = async (zip: JSZip, asset: AssetContracts.IAssetModelContract) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ export const collectionsExportEntity: EntityDefinition<ReadonlyArray<CollectionC
name: "collections",
fetchEntities: client => client.listCollections().toPromise().then(res => res.rawData.collections),
serializeEntities: collections => JSON.stringify(collections),
importEntities: () => { throw new Error("Not supported yet.")},
deserializeEntities: () => { throw new Error("Not supported yet.")},
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ export const contentItemsExportEntity: EntityDefinition<ReadonlyArray<ContentIte
name: "contentItems",
fetchEntities: client => client.listContentItems().toAllPromise().then(res => res.data.items.map(i => i._raw)),
serializeEntities: collections => JSON.stringify(collections),
};
importEntities: () => { throw new Error("Not supported yet.")},
deserializeEntities: () => { throw new Error("Not supported yet.")},
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ export const contentTypesExportEntity: EntityDefinition<ReadonlyArray<ContentTyp
.toAllPromise()
.then(res => res.data.items.map(t => t._raw)),
serializeEntities: collections => JSON.stringify(collections),
};
importEntities: () => { throw new Error("Not supported yet.")},
deserializeEntities: () => { throw new Error("Not supported yet.")},
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ export const contentTypesSnippetsExportEntity: EntityDefinition<ReadonlyArray<Co
.toAllPromise()
.then(res => res.data.items.map(s => s._raw)),
serializeEntities: collections => JSON.stringify(collections),
};
importEntities: () => { throw new Error("Not supported yet.")},
deserializeEntities: () => { throw new Error("Not supported yet.")},
};
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ export const languageVariantsExportEntity: EntityDefinition<ReadonlyArray<Langua
return variants.flatMap(arr => arr);
},
serializeEntities: collections => JSON.stringify(collections),
};
importEntities: () => { throw new Error("Not supported yet.")},
deserializeEntities: () => { throw new Error("Not supported yet.")},
};
11 changes: 11 additions & 0 deletions src/commands/importExportEntities/entities/languages.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { LanguageContracts } from "@kontent-ai/management-sdk";

import { EntityDefinition } from "../entityDefinition.js";

export const languagesExportEntity: EntityDefinition<ReadonlyArray<LanguageContracts.ILanguageModelContract>> = {
name: "languages",
fetchEntities: client => client.listLanguages().toAllPromise().then(res => res.data.items.map(l => l._raw)),
serializeEntities: collections => JSON.stringify(collections),
importEntities: () => { throw new Error("Not supported yet.")},
deserializeEntities: () => { throw new Error("Not supported yet.")},
};
11 changes: 11 additions & 0 deletions src/commands/importExportEntities/entities/previewUrls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { PreviewContracts } from "@kontent-ai/management-sdk";

import { EntityDefinition } from "../entityDefinition.js";

export const previewUrlsExportEntity: EntityDefinition<PreviewContracts.IPreviewConfigurationContract> = {
name: "previewUrls",
fetchEntities: client => client.getPreviewConfiguration().toPromise().then(res => res.rawData),
serializeEntities: collections => JSON.stringify(collections),
importEntities: () => { throw new Error("Not supported yet.")},
deserializeEntities: () => { throw new Error("Not supported yet.")},
};
11 changes: 11 additions & 0 deletions src/commands/importExportEntities/entities/roles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { RoleContracts } from "@kontent-ai/management-sdk";

import { EntityDefinition } from "../entityDefinition.js";

export const rolesExportEntity: EntityDefinition<ReadonlyArray<RoleContracts.IRoleContract>> = {
name: "roles",
fetchEntities: client => client.listRoles().toPromise().then(res => res.rawData.roles),
serializeEntities: collections => JSON.stringify(collections),
importEntities: () => { throw new Error("Not supported yet.")},
deserializeEntities: () => { throw new Error("Not supported yet.")},
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ export const spacesExportEntity: EntityDefinition<ReadonlyArray<SpaceContracts.I
name: "spaces",
fetchEntities: client => client.listSpaces().toPromise().then(res => res.rawData),
serializeEntities: spaces => JSON.stringify(spaces),
importEntities: () => { throw new Error("Not supported yet.")},
deserializeEntities: () => { throw new Error("Not supported yet.")},
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ export const taxonomiesExportEntity: EntityDefinition<ReadonlyArray<TaxonomyCont
name: "taxonomies",
fetchEntities: client => client.listTaxonomies().toAllPromise().then(res => res.data.items.map(t => t._raw)),
serializeEntities: taxonomies => JSON.stringify(taxonomies),
importEntities: () => { throw new Error("Not supported yet.")},
deserializeEntities: () => { throw new Error("Not supported yet.")},
};
11 changes: 11 additions & 0 deletions src/commands/importExportEntities/entities/workflows.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { WorkflowContracts } from "@kontent-ai/management-sdk";

import { EntityDefinition } from "../entityDefinition.js";

export const workflowsExportEntity: EntityDefinition<WorkflowContracts.IListWorkflowsResponseContract> = {
name: "workflows",
fetchEntities: client => client.listWorkflows().toPromise().then(res => res.rawData),
serializeEntities: collections => JSON.stringify(collections),
importEntities: () => { throw new Error("Not supported yet.")},
deserializeEntities: () => { throw new Error("Not supported yet.")},
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ export type EntityDefinition<T> = Readonly<{
fetchEntities: (client: ManagementClient) => Promise<T>;
serializeEntities: (entities: T) => string;
addOtherFiles?: (loadedEntities: T, zip: JSZip) => Promise<void>;
deserializeEntities: (serialized: string) => T;
importEntities: (client: ManagementClient, entities: T, zip: JSZip) => Promise<void>;
}>;

0 comments on commit 641bfcb

Please sign in to comment.