polyv-live-cli
Version:
CLI tool for managing PolyV live streaming services.
73 lines • 3.41 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CustomFieldHandler = void 0;
const base_handler_1 = require("./base.handler");
const errors_1 = require("../utils/errors");
const api_command_1 = require("../utils/api-command");
const custom_field_service_1 = require("../services/custom-field-service");
class CustomFieldHandler extends base_handler_1.BaseHandler {
constructor(authConfig, serviceConfig) {
super();
this.service = new custom_field_service_1.CustomFieldServiceSdk(authConfig, serviceConfig);
}
async list(options) {
this.show(await this.service.listCustomFields(), options.output);
}
async add(options) {
this.requireFields(options, ['customFieldId', 'customFieldName', 'customFieldType']);
await (0, api_command_1.confirmWrite)(options.force, `Create custom field ${options.customFieldId}?`);
this.show({
success: true,
result: await this.service.addCustomField({
customFieldId: options.customFieldId,
customFieldName: options.customFieldName,
customFieldType: options.customFieldType,
}),
}, options.output);
}
async saveValues(options) {
const values = this.buildValues(options);
await (0, api_command_1.confirmWrite)(options.force, `Save ${values.length} custom field viewer value(s)?`);
this.show({ success: true, result: await this.service.addCustomFieldValue(values) }, options.output);
}
buildValues(options) {
if (options.values) {
const parsed = (0, api_command_1.parseJsonArray)(options.values);
if (parsed.length === 0) {
throw new errors_1.PolyVValidationError('values must not be empty', 'values', options.values, 'not_empty');
}
return parsed.map((item, index) => {
if (!item || typeof item !== 'object' || Array.isArray(item)) {
throw new errors_1.PolyVValidationError(`values[${index}] must be an object`, 'values', options.values, 'object_array');
}
const value = item;
this.requireFields(value, ['viewerId', 'customFieldId', 'customFieldValue']);
return {
viewerId: String(value['viewerId']),
customFieldId: String(value['customFieldId']),
customFieldValue: String(value['customFieldValue']),
};
});
}
this.requireFields(options, ['viewerId', 'customFieldId', 'customFieldValue']);
return [{
viewerId: options.viewerId,
customFieldId: options.customFieldId,
customFieldValue: options.customFieldValue,
}];
}
requireFields(options, fields) {
const missing = fields.filter((field) => {
const value = options[field];
return value === undefined || value === null || value === '';
});
if (missing.length > 0) {
throw new errors_1.PolyVValidationError(`Missing required option(s): ${missing.join(', ')}`, 'options', options, 'required');
}
}
show(data, output = 'table') {
this.displayData(data, output);
}
}
exports.CustomFieldHandler = CustomFieldHandler;
//# sourceMappingURL=custom-field.handler.js.map