polyv-live-cli
Version:
CLI tool for managing PolyV live streaming services.
214 lines • 7.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createResourceHandlers = createResourceHandlers;
function createResourceHandlers(client) {
return {
channel: createChannelHandler(client),
product: createProductHandler(client),
coupon: createCouponHandler(client),
watchCondition: createWatchConditionHandler(client),
productEnabled: createProductEnabledHandler(client),
couponEnabled: createCouponEnabledHandler(client),
couponChannel: createCouponChannelHandler(client),
};
}
function createChannelHandler(client) {
return {
async create(params, outputConfig) {
const response = await client.channel.createChannelV4(params);
if (outputConfig) {
return mapOutputFields(response, outputConfig);
}
return {
channelId: response.channelId,
channelName: params['name'],
channelPasswd: response.channelPasswd,
userId: response.userId,
};
},
async rollback(resource) {
const channelId = resource['channelId'];
if (!channelId) {
throw new Error('Cannot rollback channel: channelId is missing');
}
const userId = resource['userId'] || resource['createdUserId'] || '';
await client.channel.deleteChannel(String(channelId), userId);
},
};
}
function createWatchConditionHandler(client) {
return {
async create(params, outputConfig) {
const channelId = params['channelId'];
if (!channelId) {
throw new Error('channelId is required for watch condition');
}
const authSettings = params['authSettings'];
if (!authSettings || !Array.isArray(authSettings)) {
throw new Error('authSettings array is required for watch condition');
}
await client.channel.updateWatchCondition({
channelId: String(channelId),
authSettings: authSettings,
});
const result = {
channelId,
authSettings,
success: true,
};
if (outputConfig) {
return mapOutputFields(result, outputConfig);
}
return result;
},
};
}
function createProductHandler(client) {
return {
async create(params, outputConfig) {
if (!params['channelId']) {
throw new Error('channelId is required for product creation');
}
const response = await client.channel.addChannelProduct(params);
if (outputConfig) {
return mapOutputFields(response, outputConfig);
}
return {
productId: response.productId,
name: response.name,
channelId: params['channelId'],
};
},
async rollback(resource) {
const channelId = resource['channelId'];
const productId = resource['productId'];
if (!channelId || !productId) {
throw new Error('Cannot rollback product: channelId or productId is missing');
}
await client.channel.deleteChannelProduct({
channelId,
productId,
});
},
};
}
function createProductEnabledHandler(client) {
return {
async create(params, outputConfig) {
if (!params['channelId']) {
throw new Error('channelId is required for productEnabled');
}
if (!params['enabled']) {
throw new Error('enabled is required for productEnabled');
}
await client.channel.updateChannelProductEnabled({
channelId: String(params['channelId']),
enabled: params['enabled'],
});
const result = {
channelId: params['channelId'],
enabled: params['enabled'],
success: true,
};
if (outputConfig) {
return mapOutputFields(result, outputConfig);
}
return result;
},
};
}
function createCouponEnabledHandler(client) {
return {
async create(params, outputConfig) {
if (!params['channelId']) {
throw new Error('channelId is required for couponEnabled');
}
if (!params['enabled']) {
throw new Error('enabled is required for couponEnabled');
}
await client.v4Channel.updateCouponEnabled({
channelId: String(params['channelId']),
enabled: params['enabled'],
});
const result = {
channelId: params['channelId'],
enabled: params['enabled'],
success: true,
};
if (outputConfig) {
return mapOutputFields(result, outputConfig);
}
return result;
},
};
}
function createCouponHandler(client) {
return {
async create(params, outputConfig) {
const couponId = await client.v4Platform.createCoupon(params);
if (outputConfig) {
return mapOutputFields({ couponId, ...params }, outputConfig);
}
return {
couponId,
name: params['name'],
};
},
async rollback(resource) {
const couponId = resource['couponId'];
if (!couponId) {
throw new Error('Cannot rollback coupon: couponId is missing');
}
await client.v4Platform.deleteCouponsBatch({
couponIds: [couponId],
});
},
};
}
function createCouponChannelHandler(client) {
return {
async create(params, outputConfig) {
if (!params['channelId']) {
throw new Error('channelId is required for couponChannel');
}
if (!params['couponIds'] || !Array.isArray(params['couponIds']) || params['couponIds'].length === 0) {
throw new Error('couponIds array is required for couponChannel');
}
await client.v4Channel.addChannelCoupon({
channelId: String(params['channelId']),
couponIds: params['couponIds'],
});
const result = {
channelId: params['channelId'],
couponIds: params['couponIds'],
success: true,
};
if (outputConfig) {
return mapOutputFields(result, outputConfig);
}
return result;
},
};
}
function mapOutputFields(response, outputConfig) {
const result = {};
for (const [outputField, responsePath] of Object.entries(outputConfig)) {
const value = getNestedValue(response, responsePath);
if (value !== undefined) {
result[outputField] = value;
}
}
return result;
}
function getNestedValue(obj, path) {
const parts = path.split('.');
let value = obj;
for (const part of parts) {
if (value === undefined || value === null) {
return undefined;
}
value = value[part];
}
return value;
}
//# sourceMappingURL=resource-handlers.js.map