Skip to content

Commit

Permalink
Merge pull request #33 from contentstack/feat/DX-502
Browse files Browse the repository at this point in the history
updated personalization url to access in multiple region
  • Loading branch information
shafeeqd959 authored May 9, 2024
2 parents 1514232 + c7c55f0 commit 4e61e45
Show file tree
Hide file tree
Showing 23 changed files with 76 additions and 63 deletions.
9 changes: 7 additions & 2 deletions packages/contentstack-export/src/commands/cm/stacks/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
flags,
ContentstackClient,
FlagInput,
pathValidator
pathValidator,
} from '@contentstack/cli-utilities';
import { ModuleExporter } from '../../../export';
import { setupExportConfig, log, formatError, writeExportMetaFile } from '../../../utils';
Expand Down Expand Up @@ -105,6 +105,7 @@ export default class ExportCommand extends Command {
let exportConfig = await setupExportConfig(flags);
// Note setting host to create cma client
exportConfig.host = this.cmaHost;
exportConfig.region = this.region;
exportDir = exportConfig.data || exportConfig.exportDir;
const managementAPIClient: ContentstackClient = await managementSDKClient(exportConfig);
const moduleExporter = new ModuleExporter(managementAPIClient, exportConfig);
Expand All @@ -113,7 +114,11 @@ export default class ExportCommand extends Command {
writeExportMetaFile(exportConfig);
}
log(exportConfig, `The content of the stack ${exportConfig.apiKey} has been exported successfully!`, 'success');
log(exportConfig, `The log has been stored at '${pathValidator(path.join(exportDir, 'logs', 'export'))}'`, 'success');
log(
exportConfig,
`The log has been stored at '${pathValidator(path.join(exportDir, 'logs', 'export'))}'`,
'success',
);
} catch (error) {
log({ data: exportDir } as ExportConfig, `Failed to export stack content - ${formatError(error)}`, 'error');
log({ data: exportDir } as ExportConfig, `The log has been stored at ${exportDir}`, 'info');
Expand Down
6 changes: 4 additions & 2 deletions packages/contentstack-export/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ const config: DefaultConfig = {
exportVersions: false,
},
personalization: {
baseURL: 'https://personalization-api.contentstack.com',
baseURL: {
NA: 'https://personalization-api.contentstack.com',
},
dirName: 'personalization',
exportOrder: ['attributes', 'audiences', 'events', 'experiences'],
projects: {
Expand Down Expand Up @@ -471,7 +473,7 @@ const config: DefaultConfig = {
stacks: '/stacks/',
},
preserveStackVersion: false,
personalizationEnabled: false,
personalizationEnabled: true,
fetchConcurrency: 5,
writeConcurrency: 5,
developerHubBaseUrl: '',
Expand Down
4 changes: 2 additions & 2 deletions packages/contentstack-export/src/types/default-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ export default interface DefaultConfig {
exportVersions: boolean;
};
personalization: {
dirName: string,
baseURL: string,
dirName: string;
baseURL: Record<string, string>;
} & AnyProperty;
variantEntry: {
dirName: string;
Expand Down
7 changes: 4 additions & 3 deletions packages/contentstack-export/src/types/export-config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Modules } from '.';
import { Modules, Region } from '.';
import DefaultConfig from './default-config';

export default interface ExportConfig extends DefaultConfig {
Expand All @@ -22,12 +22,13 @@ export default interface ExportConfig extends DefaultConfig {
access_token?: string;
authtoken?: string;
'X-User-Agent': string;
organization_uid?: string
organization_uid?: string;
};
access_token?: string;
org_uid?: string;
source_stack?: string;
sourceStackName?:string;
sourceStackName?: string;
region: Region;
}

type branch = {
Expand Down
7 changes: 7 additions & 0 deletions packages/contentstack-export/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ export interface User {
authtoken: string;
}

export interface Region {
name: string;
cma: string;
cda: string;
uiHost: string;
}

export type Modules =
| 'stack'
| 'assets'
Expand Down
10 changes: 3 additions & 7 deletions packages/contentstack-export/src/utils/export-config-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@ const setupConfig = async (exportCmdFlags: any): Promise<ExportConfig> => {

const pattern = /[*$%#<>{}!&?]/g;
if (pattern.test(config.exportDir)) {
cliux.print(
`\nPlease add a directory path without any of the special characters: (*,&,{,},[,],$,%,<,>,?,!)`,
{
color: 'yellow',
},
);
cliux.print(`\nPlease add a directory path without any of the special characters: (*,&,{,},[,],$,%,<,>,?,!)`, {
color: 'yellow',
});
config.exportDir = await askExportDir();
}
config.exportDir = config.exportDir.replace(/['"]/g, '');
Expand Down Expand Up @@ -84,7 +81,6 @@ const setupConfig = async (exportCmdFlags: any): Promise<ExportConfig> => {
if (Array.isArray(config.filteredModules) && config.filteredModules.length > 0) {
config.modules.types = filter(defaultConfig.modules.types, (module) => includes(config.filteredModules, module));
}

return config;
};

Expand Down
7 changes: 5 additions & 2 deletions packages/contentstack-import/src/commands/cm/stacks/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
flags,
FlagInput,
ContentstackClient,
pathValidator
pathValidator,
} from '@contentstack/cli-utilities';

import { ImportConfig } from '../../../types';
Expand Down Expand Up @@ -126,6 +126,7 @@ export default class ImportCommand extends Command {
let importConfig = await setupImportConfig(flags);
// Note setting host to create cma client
importConfig.host = this.cmaHost;
importConfig.region = this.region;
backupDir = importConfig.backupDir;

const managementAPIClient: ContentstackClient = await managementSDKClient(importConfig);
Expand Down Expand Up @@ -156,7 +157,9 @@ export default class ImportCommand extends Command {
log(
{ data: backupDir } as ImportConfig,
`The log has been stored at ${
{ data: backupDir } ? pathValidator(path.join(backupDir || __dirname, 'logs', 'import')) : pathValidator(path.join(__dirname, 'logs'))
{ data: backupDir }
? pathValidator(path.join(backupDir || __dirname, 'logs', 'import'))
: pathValidator(path.join(__dirname, 'logs'))
}`,
'info',
);
Expand Down
7 changes: 4 additions & 3 deletions packages/contentstack-import/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const config: DefaultConfig = {
// locales: ['fr-fr'],
host: 'https://api.contentstack.io/v3',
extensionHost: 'https://app.contentstack.com',
personalizationHost: 'https://personalization-api.contentstack.com',
developerHubUrls: {
'https://api.contentstack.io': 'https://developerhub-api.contentstack.com',
'https://eu-api.contentstack.com': 'https://eu-developerhub-api.contentstack.com',
Expand All @@ -28,7 +27,6 @@ const config: DefaultConfig = {
types: [
'locales',
'environments',
'assets',
'taxonomies',
'extensions',
'marketplace-apps',
Expand All @@ -38,9 +36,9 @@ const config: DefaultConfig = {
'custom-roles',
'workflows',
'entries',
'variant-entries',
'labels',
'webhooks',
'variant-entries',
],
locales: {
dirName: 'locales',
Expand Down Expand Up @@ -153,6 +151,9 @@ const config: DefaultConfig = {
fileName: 'taxonomies.json',
},
personalization: {
baseURL: {
NA: 'https://personalization-api.contentstack.com',
},
importData: true,
dirName: 'personalization',
importOrder: ['projects', 'attributes', 'audiences', 'events', 'experiences'],
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-import/src/types/default-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export default interface DefaultConfig {
versioning: boolean;
host: string;
extensionHost: string;
personalizationHost: string;
developerHubUrls: Record<string, string>;
modules: {
apiConcurrency: number;
Expand Down Expand Up @@ -124,6 +123,7 @@ export default interface DefaultConfig {
dependencies?: Modules[];
};
personalization: {
baseURL: Record<string, string>;
dirName: string;
importData: boolean;
importOrder: string[];
Expand Down
3 changes: 2 additions & 1 deletion packages/contentstack-import/src/types/import-config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Modules } from '.';
import { Modules, Region } from '.';
import DefaultConfig from './default-config';

export interface ExternalConfig {
Expand Down Expand Up @@ -49,6 +49,7 @@ export default interface ImportConfig extends DefaultConfig, ExternalConfig {
skipExisting?: boolean;
skipAudit?: boolean;
stackName?: string;
region: Region;
}

type branch = {
Expand Down
23 changes: 15 additions & 8 deletions packages/contentstack-import/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ export interface PrintOptions {
color?: string;
}

export interface Region {
name: string;
cma: string;
cda: string;
uiHost: string;
}

export interface InquirePayload {
type: string;
name: string;
Expand Down Expand Up @@ -92,7 +99,7 @@ export interface CustomRoleConfig {
customRolesLocalesFileName: string;
}

export interface TaxonomiesConfig{
export interface TaxonomiesConfig {
dirName: string;
fileName: string;
dependencies?: Modules[];
Expand All @@ -101,11 +108,11 @@ export interface TaxonomiesConfig{
export { default as DefaultConfig } from './default-config';
export { default as ImportConfig } from './import-config';

export * from './entries'
export * from './marketplace-app'
export * from './entries';
export * from './marketplace-app';

export type ExtensionType = {
uid: string,
scope: Record<string,unknown>,
title: string
}
export type ExtensionType = {
uid: string;
scope: Record<string, unknown>;
title: string;
};
2 changes: 1 addition & 1 deletion packages/contentstack-variants/src/export/attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default class ExportAttributes extends PersonalizationAdapter<ExportConfi
constructor(readonly exportConfig: ExportConfig) {
super({
config: exportConfig,
baseURL: exportConfig.modules.personalization.baseURL,
baseURL: exportConfig.modules.personalization.baseURL[exportConfig.region.name],
headers: { authtoken: exportConfig.auth_token, 'X-Project-Uid': exportConfig.project_id },
});
this.personalizationConfig = exportConfig.modules.personalization;
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-variants/src/export/audiences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default class ExportAudiences extends PersonalizationAdapter<ExportConfig
constructor(readonly exportConfig: ExportConfig) {
super({
config: exportConfig,
baseURL: exportConfig.modules.personalization.baseURL,
baseURL: exportConfig.modules.personalization.baseURL[exportConfig.region.name],
headers: { authtoken: exportConfig.auth_token, 'X-Project-Uid': exportConfig.project_id },
});
this.personalizationConfig = exportConfig.modules.personalization;
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-variants/src/export/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default class ExportEvents extends PersonalizationAdapter<ExportConfig> {
constructor(readonly exportConfig: ExportConfig) {
super({
config: exportConfig,
baseURL: exportConfig.modules.personalization.baseURL,
baseURL: exportConfig.modules.personalization.baseURL[exportConfig.region.name],
headers: { authtoken: exportConfig.auth_token, 'X-Project-Uid': exportConfig.project_id },
});
this.personalizationConfig = exportConfig.modules.personalization;
Expand Down
20 changes: 9 additions & 11 deletions packages/contentstack-variants/src/export/experiences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default class ExportExperiences extends PersonalizationAdapter<ExportConf
constructor(exportConfig: ExportConfig) {
super({
config: exportConfig,
baseURL: exportConfig.modules.personalization.baseURL,
baseURL: exportConfig.modules.personalization.baseURL[exportConfig.region.name],
headers: { authtoken: exportConfig.auth_token, 'X-Project-Uid': exportConfig.project_id },
});
this.exportConfig = exportConfig;
Expand All @@ -29,25 +29,23 @@ export default class ExportExperiences extends PersonalizationAdapter<ExportConf
// write experiences in to a file
log(this.exportConfig, 'Starting experiences export', 'info');
await fsUtil.makeDirectory(this.experiencesFolderPath);
const experiences: Array<ExperienceStruct> = await this.getExperiences() || [];
const experiences: Array<ExperienceStruct> = (await this.getExperiences()) || [];
if (!experiences || experiences?.length < 1) {
log(this.exportConfig, 'No Experiences found with the give project', 'info');
return;
}
fsUtil.writeFile(path.resolve(this.experiencesFolderPath, 'experiences.json'), experiences);
const experienceToVarianceStrList: Array<string> = [];
const experienceToVariantsStrList: Array<string> = [];
for (let experience of experiences) {
let variants = experience?._cms?.variants ?? {}; // Add nullish coalescing operator
if (variants) {
Object.keys(variants).forEach((variantShortId: string) => {
const experienceToVarianceStr = `${experience.uid}-${variantShortId}-${variants[variantShortId]}`;
experienceToVarianceStrList.push(experienceToVarianceStr);
});
}
let variants = experience?._cms?.variants ?? {};
Object.keys(variants).forEach((variantShortId: string) => {
const experienceToVariantsStr = `${experience.uid}-${variantShortId}-${variants[variantShortId]}`;
experienceToVariantsStrList.push(experienceToVariantsStr);
});
}
fsUtil.writeFile(
path.resolve(this.experiencesFolderPath, 'experiences-variants-ids.json'),
experienceToVarianceStrList,
experienceToVariantsStrList,
);
log(this.exportConfig, 'All the experiences have been exported successfully!', 'success');
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-variants/src/export/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default class ExportProjects extends PersonalizationAdapter<ExportConfig>
constructor(exportConfig: ExportConfig) {
super({
config: exportConfig,
baseURL: exportConfig.modules.personalization.baseURL,
baseURL: exportConfig.modules.personalization.baseURL[exportConfig.region.name],
headers: { authtoken: exportConfig.auth_token, organization_uid: exportConfig.org_uid },
});
this.exportConfig = exportConfig;
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-variants/src/import/attribute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class Attribute extends PersonalizationAdapter<ImportConfig> {
constructor(public readonly config: ImportConfig, private readonly log: LogType = console.log) {
const conf: APIConfig = {
config,
baseURL: config.personalizationHost,
baseURL: config.modules.personalization.baseURL[config.region.name],
headers: { 'X-Project-Uid': config.modules.personalization.project_id, authtoken: config.auth_token },
};
super(Object.assign(config, conf));
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-variants/src/import/audiences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default class Audiences extends PersonalizationAdapter<ImportConfig> {
constructor(public readonly config: ImportConfig, private readonly log: LogType = console.log) {
const conf: APIConfig = {
config,
baseURL: config.personalizationHost,
baseURL: config.modules.personalization.baseURL[config.region.name],
headers: { 'X-Project-Uid': config.modules.personalization.project_id, authtoken: config.auth_token },
};
super(Object.assign(config, conf));
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-variants/src/import/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class Events extends PersonalizationAdapter<ImportConfig> {
constructor(public readonly config: ImportConfig, private readonly log: LogType = console.log) {
const conf: APIConfig = {
config,
baseURL: config.personalizationHost,
baseURL: config.modules.personalization.baseURL[config.region.name],
headers: { 'X-Project-Uid': config.modules.personalization.project_id, authtoken: config.auth_token },
};
super(Object.assign(config, conf));
Expand Down
5 changes: 1 addition & 4 deletions packages/contentstack-variants/src/import/experiences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default class Experiences extends PersonalizationAdapter<ImportConfig> {
constructor(public readonly config: ImportConfig, private readonly log: LogType = console.log) {
const conf: APIConfig = {
config,
baseURL: config.personalizationHost,
baseURL: config.modules.personalization.baseURL[config.region.name],
headers: { 'X-Project-Uid': config.modules.personalization.project_id, authtoken: config.auth_token },
};
super(Object.assign(config, conf));
Expand Down Expand Up @@ -204,6 +204,3 @@ export default class Experiences extends PersonalizationAdapter<ImportConfig> {
}
}
}



2 changes: 1 addition & 1 deletion packages/contentstack-variants/src/import/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default class Project extends PersonalizationAdapter<ImportConfig> {
constructor(public readonly config: ImportConfig, private readonly log: LogType = console.log) {
const conf: APIConfig = {
config,
baseURL: config.personalizationHost,
baseURL: config.modules.personalization.baseURL[config.region.name],
headers: { organization_uid: config.org_uid, authtoken: config.auth_token },
};
super(Object.assign(config, conf));
Expand Down
Loading

0 comments on commit 4e61e45

Please sign in to comment.