UNPKG

@contentstack/cli-variants

Version:

Variants plugin

130 lines (129 loc) 5.99 kB
"use strict"; 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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; 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) { var _a, _b, _c, _d; 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); } if (adapterConfig.delayMs) { if (this.apiClient) { (_b = (_a = this.apiClient.interceptors) === null || _a === void 0 ? void 0 : _a.request) === null || _b === void 0 ? void 0 : _b.use((requestConfig) => __awaiter(this, void 0, void 0, function* () { yield this.delay(adapterConfig.delayMs); return requestConfig; })); } if (this.cmaAPIClient) { (_d = (_c = this.cmaAPIClient.interceptors) === null || _c === void 0 ? void 0 : _c.request) === null || _d === void 0 ? void 0 : _d.use((requestConfig) => __awaiter(this, void 0, void 0, function* () { yield this.delay(adapterConfig.delayMs); return requestConfig; })); } } } /** * 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;