Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Taxonomy | Replace http calls with SDK calls #1095

Merged
merged 10 commits into from
Oct 12, 2023
40 changes: 20 additions & 20 deletions package-lock.json

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

Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ class ExportToCsvCommand extends Command {
case 'taxonomies': {
let stack;
let stackAPIClient;
let taxUID;
if (managementTokenAlias) {
const { stackDetails, apiClient } = await this.getAliasDetails(managementTokenAlias, stackName);
managementAPIClient = apiClient;
Expand All @@ -224,7 +223,7 @@ class ExportToCsvCommand extends Command {
}

stackAPIClient = this.getStackClient(managementAPIClient, stack);
await this.createTaxonomyAndTermCsvFile(stackName, stack, taxonomyUID);
await this.createTaxonomyAndTermCsvFile(stackAPIClient, stackName, stack, taxonomyUID);
break;
}
}
Expand All @@ -242,8 +241,8 @@ class ExportToCsvCommand extends Command {
getStackClient(managementAPIClient, stack) {
const stackInit = {
api_key: stack.apiKey,
branch_uid: stack.branch_uid,
};
if(stack?.branch_uid) stackInit['branch_uid'] = stack.branch_uid;
if (stack.token) {
return managementAPIClient.stack({
...stackInit,
Expand Down Expand Up @@ -360,20 +359,19 @@ class ExportToCsvCommand extends Command {
* @param {object} stack
* @param {string} taxUID
*/
async createTaxonomyAndTermCsvFile(stackName, stack, taxUID) {
const { cma } = configHandler.get('region') || {};
async createTaxonomyAndTermCsvFile(stackAPIClient, stackName, stack, taxUID) {
const payload = {
baseUrl: `${cma}/v3/taxonomies`,
apiKey: stack.apiKey,
mgToken: stack?.token,
stackAPIClient,
type: '',
limit: config.limit || 100
};
//check whether the taxonomy is valid or not
let taxonomies = [];
if (taxUID) {
const taxonomy = await util.getTaxonomy(payload, taxUID);
payload['taxonomyUID'] = taxUID;
const taxonomy = await util.getTaxonomy(payload);
taxonomies.push(taxonomy);
} else {
payload['url'] = payload.baseUrl;
taxonomies = await util.getAllTaxonomies(payload);
}

Expand All @@ -389,7 +387,7 @@ class ExportToCsvCommand extends Command {
const taxonomy = taxonomies[index];
const taxonomyUID = taxonomy?.uid;
if (taxonomyUID) {
payload['url'] = `${payload.baseUrl}/${taxonomyUID}/terms`;
payload['taxonomyUID'] = taxonomyUID;
const terms = await util.getAllTermsOfTaxonomy(payload);
const formattedTermsData = util.formatTermsOfTaxonomyData(terms, taxonomyUID);
const taxonomyName = taxonomy?.name ? taxonomy.name : '';
Expand Down
134 changes: 74 additions & 60 deletions packages/contentstack-export-to-csv/src/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ const debug = require('debug')('export-to-csv');
const checkboxPlus = require('inquirer-checkbox-plus-prompt');

const config = require('./config.js');
const { cliux, configHandler, HttpClient, messageHandler } = require('@contentstack/cli-utilities');
const {
cliux,
configHandler,
HttpClient,
messageHandler,
managementSDKClient,
ContentstackClient,
} = require('@contentstack/cli-utilities');

const directory = './data';
const delimeter = os.platform() === 'win32' ? '\\' : '/';
Expand Down Expand Up @@ -687,41 +694,43 @@ function wait(time) {
* fetch all taxonomies in the provided stack
* @param {object} payload
* @param {number} skip
* @param {number} limit
* @param {array} taxonomies
* @returns
*/
async function getAllTaxonomies(payload, skip = 0, limit = 100, taxonomies = []) {
const response = await apiRequestHandler(payload, skip, limit);
async function getAllTaxonomies(payload, skip = 0, taxonomies = []) {
payload['type'] = 'taxonomies';
const response = await taxonomySDKHandler(payload, skip);
if (response) {
skip += config.limit || 100;
skip += payload.limit;
//TODO - replace response.taxonomies with items
taxonomies = [...taxonomies, ...response.taxonomies];
if (skip >= response?.count) {
return taxonomies;
} else {
return getAllTaxonomies(payload, skip, limit, taxonomies);
return getAllTaxonomies(payload, skip, taxonomies);
}
}
return taxonomies;
}

/**
* fetch terms of related taxonomy
* fetch taxonomy related terms
* @param {object} payload
* @param {number} skip
* @param {number} limit
* @param {array} terms
* @returns
*/
async function getAllTermsOfTaxonomy(payload, skip = 0, limit = 100, terms = []) {
const response = await apiRequestHandler(payload, skip, limit);
if (response) {
skip += config.limit || 100;
terms = [...terms, ...response.terms];
if (skip >= response?.count) {
async function getAllTermsOfTaxonomy(payload, skip = 0, terms = []) {
payload['type'] = 'terms';
const { items, count } = await taxonomySDKHandler(payload, skip);
if (items) {
skip += payload.limit;
terms = [...terms, ...items];
if (skip >= count) {
return terms;
} else {
return getAllTermsOfTaxonomy(payload, skip, limit, terms);
return getAllTermsOfTaxonomy(payload, skip, terms);
}
}
return terms;
Expand All @@ -733,50 +742,54 @@ async function getAllTermsOfTaxonomy(payload, skip = 0, limit = 100, terms = [])
* @param {string} taxonomyUID
* @returns
*/
async function getTaxonomy(payload, taxonomyUID) {
payload['url'] = `${payload.baseUrl}/${taxonomyUID}`;
const resp = await apiRequestHandler(payload);
return resp?.taxonomy || '';
}

async function apiRequestHandler(payload, skip, limit) {
const headers = {
api_key: payload.apiKey,
'Content-Type': 'application/json',
};

if (payload?.mgToken) headers['authorization'] = payload.mgToken;
else headers['authToken'] = configHandler.get('authtoken');

const params = {
include_count: true,
skip: 0,
limit: 30,
};

if (skip >= 0) params['skip'] = skip;
if (limit >= 0) params['limit'] = limit;

return await new HttpClient()
.headers(headers)
.queryParams(params)
.get(payload.url)
.then((res) => {
//NOTE - temporary code for handling api errors response
const { status, data } = res;
if ([200, 201, 202].includes(status)) return data;
else {
let errorMsg;
if ([500, 503, 502].includes(status)) errorMsg = data?.message || data;
else errorMsg = data?.error_message;
if (errorMsg === undefined) {
errorMsg = Object.values(data?.errors) && flat(Object.values(data.errors));
}
cliux.print(`Error: ${errorMsg}`, { color: 'red' });
process.exit(1);
}
})
.catch((err) => handleErrorMsg(err));
async function getTaxonomy(payload) {
payload['type'] = 'taxonomy';
const resp = await taxonomySDKHandler(payload);
return resp;
}

/**
* taxonomy & term sdk handler
* @async
* @method
* @param payload
* @param skip
* @param limit
* @returns {*} Promise<any>
*/
async function taxonomySDKHandler(payload, skip) {
const { stackAPIClient, taxonomyUID, type } = payload;

const queryParams = { include_count: true, limit: payload.limit };
if (skip >= 0) queryParams['skip'] = skip || 0;

switch (type) {
case 'taxonomies':
//TODO - replace count with find
return await stackAPIClient
.taxonomy()
.query(queryParams)
.count()
.then((data) => data)
.catch((err) => handleErrorMsg(err));
case 'taxonomy':
return await stackAPIClient
.taxonomy(taxonomyUID)
.fetch()
.then((data) => data)
.catch((err) => handleErrorMsg(err));
case 'terms':
queryParams['depth'] = 0;
return await stackAPIClient
.taxonomy(taxonomyUID)
.terms()
.query(queryParams)
.find()
.then((data) => data)
.catch((err) => handleErrorMsg(err));
default:
handleErrorMsg({ errorMessage: 'Invalid module!' });
}
}

/**
Expand Down Expand Up @@ -821,7 +834,8 @@ function handleErrorMsg(err) {
if (err?.errorMessage) {
cliux.print(`Error: ${err.errorMessage}`, { color: 'red' });
} else if (err?.message) {
cliux.print(`Error: ${err.message}`, { color: 'red' });
const errorMsg = err?.errors?.taxonomy || err?.errors?.term || err?.message;
cliux.print(`Error: ${errorMsg}`, { color: 'red' });
} else {
console.log(err);
cliux.print(`Error: ${messageHandler.parse('CLI_EXPORT_CSV_API_FAILED')}`, { color: 'red' });
Expand Down Expand Up @@ -860,5 +874,5 @@ module.exports = {
formatTaxonomiesData,
formatTermsOfTaxonomyData,
getTaxonomy,
getStacks,
getStacks
};
Loading