Skip to content

Commit

Permalink
Merge pull request #25 from contentstack/fix/variant-entry-fix
Browse files Browse the repository at this point in the history
Fix: Project id header added in variant entry
  • Loading branch information
antonyagustine authored Apr 24, 2024
2 parents 17878ea + 061a447 commit 514edb7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
11 changes: 9 additions & 2 deletions packages/contentstack-export/src/export/modules/entries.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as path from 'path';
import { ContentstackClient, FsUtility } from '@contentstack/cli-utilities';
import { LogType, Export, ExportProjects } from '@contentstack/cli-variants';
import VariantEntries from '@contentstack/cli-variants/lib/export/variant-entries';

import BaseClass, { ApiOptions } from './base-class';
import { log, formatError, fsUtil } from '../../utils';
Expand All @@ -20,7 +21,7 @@ export default class EntriesExport extends BaseClass {
batchLimit?: number;
exportVersions: boolean;
};
private variantEntries;
private variantEntries!: VariantEntries;
private entriesDirPath: string;
private localesFilePath: string;
private schemaFilePath: string;
Expand All @@ -47,7 +48,6 @@ export default class EntriesExport extends BaseClass {
'schema.json',
);
this.projectInstance = new ExportProjects(this.exportConfig);
this.variantEntries = new Export.VariantEntries(this.exportConfig, log as LogType);
}

async start() {
Expand All @@ -62,12 +62,19 @@ export default class EntriesExport extends BaseClass {

// NOTE Check if variant is enabled in specific stack
if (this.exportConfig.personalizationEnabled) {
let project_id;
try {
const project = await this.projectInstance.projects({ connectedStackApiKey: this.exportConfig.apiKey });

if (project && project[0]?.uid) {
project_id = project[0].uid;
this.exportVariantEntry = true;
}

this.variantEntries = new Export.VariantEntries(
Object.assign(this.exportConfig, { project_id }),
log as LogType,
);
} catch (_error) {}
}

Expand Down
8 changes: 7 additions & 1 deletion packages/contentstack-variants/src/export/variant-entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ export default class VariantEntries extends VariantAdapter<VariantHttpClient<Exp
httpClient: true,
baseURL: config.host,
Adapter: VariantHttpClient<ExportConfig>,
headers: { organization_uid: config.org_uid, authtoken: config.auth_token },
headers: {
api_key: config.apiKey,
branch: config.branchName,
authtoken: config.auth_token,
organization_uid: config.org_uid,
'X-Project-Uid': config.project_id,
},
};
super(Object.assign(config, conf));
this.entriesDirPath = resolve(config.data, config.branchName || '', config.modules.entries.dirName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ import messages from '../messages';
import { AdapterHelper } from './adapter-helper';

export class VariantHttpClient<C> extends AdapterHelper<C, HttpClient> implements VariantInterface<C, HttpClient> {
public baseURL: string
constructor(config: APIConfig, options?: HttpClientOptions) {
super(config, options);
const baseURL = config.baseURL?.includes('http') ? `${config.baseURL}/v3` : `https://${config.baseURL}/v3`;
this.apiClient.baseUrl(baseURL);
this.baseURL = config.baseURL?.includes('http') ? `${config.baseURL}/v3` : `https://${config.baseURL}/v3`;
this.apiClient.baseUrl(this.baseURL);
}

async variantEntry(options: VariantOptions) {
Expand Down
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 514edb7

Please sign in to comment.