UNPKG

polyv-live-cli

Version:

CLI tool for managing PolyV live streaming services.

119 lines 4.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DonateServiceSdk = void 0; const polyv_live_api_sdk_1 = require("polyv-live-api-sdk"); const errors_1 = require("../utils/errors"); const DEFAULT_DONATE_GIFT_IMAGE = '//s1.videocc.net/default-img/donate/666.png'; class DonateServiceSdk { constructor(authConfig, _serviceConfig) { this.client = new polyv_live_api_sdk_1.PolyVClient(authConfig); this.v4Channel = this.client.v4Channel; } async getDonateConfig(params) { try { const result = await this.v4Channel.getDonate(params); return result; } catch (error) { throw this.wrapError(error, 'getDonateConfig'); } } async updateDonateConfig(params) { try { const donateGiftEnabled = params.donateGiftEnabled ?? 'Y'; const giftDonate = this.normalizeGiftDonateConfig(params.giftDonate ?? this.buildGiftDonateConfig(params.donateAmounts, params.donateEnabled)); await this.v4Channel.updateDonateGift({ channelId: params.channelId, donateGiftEnabled, ...(giftDonate ? { giftDonate } : {}), }); return { code: 200, status: 'success', data: {}, }; } catch (error) { throw this.wrapError(error, 'updateDonateConfig'); } } async listRewardGift(params) { try { const response = await this.v4Channel.listRewardGifts({ channelId: params.channelId, start: params.start, end: params.end, pageNumber: params.pageNumber ?? 1, pageSize: params.pageSize ?? 10, }); return response; } catch (error) { throw this.wrapError(error, 'listRewardGift'); } } async listRewardLikes(params) { try { const response = await this.v4Channel.listRewardLikes({ channelId: params.channelId, pageNumber: params.pageNumber ?? 1, pageSize: params.pageSize ?? 10, ...(params.start !== undefined ? { start: params.start } : {}), ...(params.end !== undefined ? { end: params.end } : {}), }); return response; } catch (error) { throw this.wrapError(error, 'listRewardLikes'); } } buildGiftDonateConfig(amounts, cashEnabled = 'Y') { if (!amounts || amounts.length === 0) { return undefined; } return { payWay: 'CASH', cashPays: amounts.map((amount) => ({ name: String(amount), enabled: cashEnabled, imgType: 'STATIC', img: DEFAULT_DONATE_GIFT_IMAGE, price: amount, })), }; } normalizeGiftDonateConfig(giftDonate) { if (!giftDonate) { return undefined; } const normalizePayItem = (item) => { if (item.img || item.imgType === 'DYNAMIC') { return item; } return { ...item, imgType: item.imgType ?? 'STATIC', img: DEFAULT_DONATE_GIFT_IMAGE, }; }; const normalized = { ...giftDonate, }; if (giftDonate.cashPays) { normalized.cashPays = giftDonate.cashPays.map(normalizePayItem); } if (giftDonate.pointPays) { normalized.pointPays = giftDonate.pointPays.map(normalizePayItem); } return normalized; } wrapError(error, operation) { if (error instanceof errors_1.PolyVError) { return error; } const message = error instanceof Error ? error.message : String(error); return new errors_1.PolyVError(`${operation} failed: ${message}`, 'DONATE_API_ERROR', 1, { originalError: error }); } } exports.DonateServiceSdk = DonateServiceSdk; //# sourceMappingURL=donate-service.js.map