@contentstack/cli-variants
Version:
Variants plugin
106 lines (105 loc) • 4.46 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AdapterHelper = void 0;
const pick_1 = __importDefault(require("lodash/pick"));
const cli_utilities_1 = require("@contentstack/cli-utilities");
const messages_1 = __importStar(require("../messages"));
class AdapterHelper {
constructor(adapterConfig, options) {
this.adapterConfig = adapterConfig;
this.$t = messages_1.$t;
this.messages = messages_1.default;
this.config = adapterConfig.config;
delete this.adapterConfig.config;
const pickConfig = [
'url',
'auth',
'method',
'baseURL',
'headers',
'adapter',
'httpAgent',
'httpsAgent',
'responseType',
];
this.apiClient = new cli_utilities_1.HttpClient((0, pick_1.default)(adapterConfig, pickConfig), options);
if (adapterConfig.cmaConfig) {
this.cmaAPIClient = new cli_utilities_1.HttpClient((0, pick_1.default)(adapterConfig.cmaConfig, pickConfig), options);
}
}
/**
* The function constructs a query string from a given object by encoding its key-value pairs.
* @param query - It looks like you have a function `constructQuery` that takes a query object as a
* parameter. The function loops through the keys in the query object and constructs a query string
* based on the key-value pairs.
* @returns The `constructQuery` function returns a string that represents the query parameters
* constructed from the input `query` object. If the `query` object is empty (has no keys), the
* function does not return anything (void).
*/
constructQuery(query) {
if (Object.keys(query).length) {
let queryParam = '';
for (const key in query) {
if (Object.prototype.hasOwnProperty.call(query, key)) {
const element = query[key];
switch (typeof element) {
case 'boolean':
queryParam = queryParam.concat(key);
break;
default:
queryParam = queryParam.concat(`&${key}=${encodeURIComponent(element)}`);
break;
}
}
}
return queryParam;
}
}
/**
* The `delay` function in TypeScript returns a Promise that resolves after a specified number of
* milliseconds.
* @param {number} ms - The `ms` parameter represents the number of milliseconds for which the delay
* should occur before the `Promise` is resolved.
* @returns A Promise that resolves after the specified delay in milliseconds.
*/
delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms <= 0 ? 0 : ms));
}
}
exports.AdapterHelper = AdapterHelper;