@modyo/cli
Version:
Modyo CLI Command line to expose local development tools
43 lines (42 loc) • 1.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const core_1 = require("@oclif/core");
const debug_1 = tslib_1.__importDefault(require("debug"));
const api_client_1 = tslib_1.__importDefault(require("../utils/api-client"));
const modyo_api_paths_1 = require("./modyo-api-paths");
const debug = (0, debug_1.default)('push/get-widget-id-in-site.ts');
const debugError = (0, debug_1.default)('error::push/get-widget-id-in-site.ts');
async function getWidgetIdInSite(name, { version, accountUrl, siteId, token, }) {
const widgetQueryName = encodeURIComponent(name);
const url = `${accountUrl}/${modyo_api_paths_1.MODYO_API_PATH[version]}/sites/${siteId}/widget_definitions?query=${widgetQueryName}`;
debug(`getting widget from ${url}`);
try {
core_1.ux.action.start('Checking if widget Exist');
const response = await api_client_1.default.get(url, {
headers: {
Authorization: `Bearer ${token}`,
},
});
debug(response.config);
const json = response.data;
if (json.widget_definitions && json.widget_definitions.length > 0) {
const widget = json.widget_definitions
.find((widgetInList) => widgetInList.name === name);
if (widget) {
debug('widget info is :');
debug(widget);
core_1.ux.action.stop(`Widget Exist and the id is ${widget.id}`);
return widget.id;
}
return false;
}
core_1.ux.action.stop('Widget not exist');
return false;
}
catch (error) {
debugError(error);
throw new Error('Unauthorized to get the site\'s widgets');
}
}
exports.default = getWidgetIdInSite;