UNPKG

polyv-live-cli

Version:

CLI tool for managing PolyV live streaming services.

739 lines 33.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ProductServiceSdk = void 0; const errors_1 = require("../utils/errors"); const sdk_1 = require("../sdk"); class ProductServiceSdk { constructor(authConfig, serviceConfig) { this.authConfig = authConfig; this.config = serviceConfig; } async listProducts(request = {}) { try { this.validateListRequest(request); const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); if (request.platform) { const result = await client.v4User.listProducts({ pageNumber: request.page ?? 1, pageSize: request.size ?? 20, }); if (!result?.contents || result.contents.length === 0) { return []; } return result.contents.map((product) => ({ productId: String(product.productId), channelId: '', name: product.name, productType: product.productType, status: product.status, ...(product.price && { price: product.price }), ...(product.realPrice && { realPrice: product.realPrice }), createdAt: product.createdTime ? new Date(product.createdTime) : new Date(0), updatedAt: product.updatedTime ? new Date(product.updatedTime) : new Date(0) })); } if (request.channelId) { const result = await client.channel.listChannelProducts({ channelId: request.channelId, pageNumber: request.page ?? 1, pageSize: request.size ?? 20, }); if (!result?.contents || result.contents.length === 0) { return []; } return result.contents.map((product) => ({ productId: String(product.productId), channelId: String(product.channelId || ''), name: product.name, productType: (product.productType || 'normal'), status: product.status, ...(product.price && { price: product.price }), ...(product.realPrice && { realPrice: product.realPrice }), createdAt: product.createdTime ? new Date(product.createdTime) : new Date(0), updatedAt: product.lastModified ? new Date(product.lastModified) : new Date(0) })); } return []; } catch (error) { throw this.handleError(error, 'listProducts'); } } async addProduct(options) { try { this.validateAddOptions(options); const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); const params = { channelId: options.channelId, name: options.name, status: options.status, linkType: options.linkType, ...(options.productType && { productType: options.productType }), ...(options.cover && { cover: options.cover }), ...(options.link && { link: options.link }), ...(options.pcLink && { pcLink: options.pcLink }), ...(options.mobileLink && { mobileLink: options.mobileLink }), ...(options.wxMiniprogramLink && { wxMiniprogramLink: options.wxMiniprogramLink }), ...(options.wxMiniprogramOriginalId && { wxMiniprogramOriginalId: options.wxMiniprogramOriginalId }), ...(options.mobileAppLink && { mobileAppLink: options.mobileAppLink }), ...(options.androidLink && { androidLink: options.androidLink }), ...(options.iosLink && { iosLink: options.iosLink }), ...(options.params && { params: options.params }), ...(options.productDesc && { productDesc: options.productDesc }), ...(options.features && { features: options.features }), ...(options.btnShow && { btnShow: options.btnShow }), ...(options.yield && { yield: options.yield }), ...(options.originId && { originId: options.originId }), ...(options.strategy && { strategy: options.strategy }), ...(options.productDetail && { productDetail: options.productDetail }), ...(options.ext && { ext: options.ext }), ...(options.tagIds && { tagIds: options.tagIds }), ...(options.priceType && { priceType: options.priceType }), ...(options.realPrice !== undefined && { realPrice: options.realPrice }), ...(options.customPrice && { customPrice: options.customPrice }), ...(options.originalPriceType && { originalPriceType: options.originalPriceType }), ...(options.price !== undefined && { price: options.price }), ...(options.customOrignalPrice && { customOrignalPrice: options.customOrignalPrice }), }; const result = await client.channel.addChannelProduct(params); return { productId: result.productId, name: result.name, channelId: result.channelId, createdTime: result.createdTime }; } catch (error) { throw this.handleError(error, 'addProduct'); } } async updateProduct(options) { try { this.validateUpdateOptions(options); const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); const params = { channelId: options.channelId, productId: options.productId, name: options.name, status: options.status, linkType: options.linkType, ...(options.cover && { cover: options.cover }), ...(options.link && { link: options.link }), ...(options.pcLink && { pcLink: options.pcLink }), ...(options.mobileLink && { mobileLink: options.mobileLink }), ...(options.wxMiniprogramLink && { wxMiniprogramLink: options.wxMiniprogramLink }), ...(options.wxMiniprogramOriginalId && { wxMiniprogramOriginalId: options.wxMiniprogramOriginalId }), ...(options.mobileAppLink && { mobileAppLink: options.mobileAppLink }), ...(options.androidLink && { androidLink: options.androidLink }), ...(options.iosLink && { iosLink: options.iosLink }), ...(options.params && { params: options.params }), ...(options.productDesc && { productDesc: options.productDesc }), ...(options.features && { features: options.features }), ...(options.btnShow && { btnShow: options.btnShow }), ...(options.yield && { yield: options.yield }), ...(options.productDetail && { productDetail: options.productDetail }), ...(options.ext && { ext: options.ext }), ...(options.tagIds && { tagIds: options.tagIds }), ...(options.priceType && { priceType: options.priceType }), ...(options.realPrice !== undefined && { realPrice: options.realPrice }), ...(options.customPrice && { customPrice: options.customPrice }), ...(options.originalPriceType && { originalPriceType: options.originalPriceType }), ...(options.price !== undefined && { price: options.price }), ...(options.customOrignalPrice && { customOrignalPrice: options.customOrignalPrice }), }; return await client.channel.updateChannelProduct(params); } catch (error) { throw this.handleError(error, 'updateProduct'); } } async deleteProduct(options) { try { this.validateDeleteOptions(options); const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return await client.channel.deleteChannelProduct({ channelId: options.channelId, productId: options.productId }); } catch (error) { throw this.handleError(error, 'deleteProduct'); } } async listUserProducts(options = {}) { try { const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return client.v4User.listProducts({ pageNumber: options.page ?? 1, pageSize: options.size ?? 20, ...(options.keyword && { keyword: options.keyword }), ...(options.productType && { productType: options.productType }), }); } catch (error) { throw this.handleError(error, 'listUserProducts'); } } async createUserProduct(options) { try { this.validateUserProductOptions(options, ['name', 'linkType', 'link']); const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return client.v4User.createProduct(this.toUserProductParams(options)); } catch (error) { throw this.handleError(error, 'createUserProduct'); } } async updateUserProduct(options) { try { this.validateUserProductOptions(options, ['productId', 'name', 'linkType', 'link']); const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); await client.v4User.updateProduct(this.toUserProductParams(options)); } catch (error) { throw this.handleError(error, 'updateUserProduct'); } } async deleteUserProduct(options) { try { if (!options.productId || options.productId.trim() === '') { throw new errors_1.PolyVValidationError('productId is required', 'productId', options.productId, 'required'); } const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); await client.v4User.deleteProduct({ productId: options.productId }); } catch (error) { throw this.handleError(error, 'deleteUserProduct'); } } async getChannelProductEnabled(channelId) { try { const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return await client.channel.getChannelProductEnabled(channelId); } catch (error) { throw this.handleError(error, 'getChannelProductEnabled'); } } async updateChannelProductEnabled(options) { try { const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return await client.channel.updateChannelProductEnabled(options); } catch (error) { throw this.handleError(error, 'updateChannelProductEnabled'); } } async batchAddChannelProducts(options) { try { const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return await client.channel.batchAddChannelProducts(options); } catch (error) { throw this.handleError(error, 'batchAddChannelProducts'); } } async batchDeleteChannelProducts(options) { try { const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return await client.channel.batchDeleteChannelProducts(options); } catch (error) { throw this.handleError(error, 'batchDeleteChannelProducts'); } } async batchShelfChannelProducts(options) { try { const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return await client.channel.batchShelfChannelProducts(options); } catch (error) { throw this.handleError(error, 'batchShelfChannelProducts'); } } async shelfChannelProduct(options) { try { const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return await client.channel.shelfChannelProduct(options); } catch (error) { throw this.handleError(error, 'shelfChannelProduct'); } } async sortChannelProduct(options) { try { const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return await client.channel.sortChannelProduct(options); } catch (error) { throw this.handleError(error, 'sortChannelProduct'); } } async pushChannelProduct(options) { try { const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return await client.channel.pushChannelProduct(options); } catch (error) { throw this.handleError(error, 'pushChannelProduct'); } } async cancelPushChannelProduct(options) { try { const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return await client.channel.cancelPushChannelProduct(options); } catch (error) { throw this.handleError(error, 'cancelPushChannelProduct'); } } async referenceProduct(options) { try { const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return await client.channel.referenceProduct(options); } catch (error) { throw this.handleError(error, 'referenceProduct'); } } async listProductTags(options) { try { if (!options.channelId || options.channelId.trim() === '') { throw new errors_1.PolyVValidationError('channelId is required', 'channelId', options.channelId, 'required'); } const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return client.v4User.listProductTags({ channelId: options.channelId, pageNumber: options.page ?? 1, pageSize: options.size ?? 20, ...(options.keyword && { keyword: options.keyword }), }); } catch (error) { throw this.handleError(error, 'listProductTags'); } } async createProductTag(options) { try { if (!options.name || options.name.trim() === '') { throw new errors_1.PolyVValidationError('name is required', 'name', options.name, 'required'); } const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return client.v4User.createProductTag({ name: options.name }); } catch (error) { throw this.handleError(error, 'createProductTag'); } } async updateProductTag(options) { try { if (!options.id || options.id <= 0) { throw new errors_1.PolyVValidationError('id must be a positive integer', 'id', options.id, 'positive_integer'); } if (!options.name || options.name.trim() === '') { throw new errors_1.PolyVValidationError('name is required', 'name', options.name, 'required'); } const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); await client.v4User.updateProductTag({ id: options.id, name: options.name }); } catch (error) { throw this.handleError(error, 'updateProductTag'); } } async deleteProductTag(options) { try { if (!options.id || options.id <= 0) { throw new errors_1.PolyVValidationError('id must be a positive integer', 'id', options.id, 'positive_integer'); } const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); await client.v4User.deleteProductTag({ id: options.id }); } catch (error) { throw this.handleError(error, 'deleteProductTag'); } } async listProductOrders(options) { try { const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return client.v4User.listProductOrders({ pageNumber: options.page ?? 1, pageSize: options.size ?? 20, }); } catch (error) { throw this.handleError(error, 'listProductOrders'); } } async getProductOrder(options) { try { if (!options.orderNo || options.orderNo.trim() === '') { throw new errors_1.PolyVValidationError('orderNo is required', 'orderNo', options.orderNo, 'required'); } const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return client.v4User.getProductOrder({ orderNo: options.orderNo }); } catch (error) { throw this.handleError(error, 'getProductOrder'); } } async batchUpdateProductOrderStatus(options) { try { const orderNos = this.parseStringList(options.orderNos, 'orderNos'); if (orderNos.length > 1000) { throw new errors_1.PolyVValidationError('orderNos cannot exceed 1000 items', 'orderNos', options.orderNos, 'max_items'); } if (!options.status || options.status.trim() === '') { throw new errors_1.PolyVValidationError('status is required', 'status', options.status, 'required'); } const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return client.v4User.batchUpdateOrderStatus({ orderNos, status: options.status, }); } catch (error) { throw this.handleError(error, 'batchUpdateProductOrderStatus'); } } async getProductPushRule(options) { try { this.validateChannelId(options.channelId); const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return client.v4Channel.getProductPushRule({ channelId: options.channelId }); } catch (error) { throw this.handleError(error, 'getProductPushRule'); } } async updateProductPushRule(options) { try { this.validateChannelId(options.channelId); const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); await client.v4Channel.updateProductPushRule(this.compact({ channelId: options.channelId, productExplainEnabled: options.productExplainEnabled, productExplainingAutoPushAndSticky: options.productExplainingAutoPushAndSticky, productListSortType: options.productListSortType, productTagSortType: options.productTagSortType, productPushRule: options.productPushRule, productHotEffectEnabled: options.productHotEffectEnabled, normalProductHotEffectTips: options.normalProductHotEffectTips, jobProductHotEffectTips: options.jobProductHotEffectTips, financeProductHotEffectTips: options.financeProductHotEffectTips, outLinkProductRedirectEnabled: options.outLinkProductRedirectEnabled, productTagSortOrderIds: options.productTagSortOrderIds, })); } catch (error) { throw this.handleError(error, 'updateProductPushRule'); } } async listChannelProductTags(options) { try { this.validateChannelId(options.channelId); const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return client.v4Channel.productTagList({ channelId: options.channelId, pageNumber: options.page ?? 1, pageSize: options.size ?? 20, }); } catch (error) { throw this.handleError(error, 'listChannelProductTags'); } } async createChannelProductTag(options) { try { this.validateChannelId(options.channelId); if (!options.name || options.name.trim() === '') { throw new errors_1.PolyVValidationError('name is required', 'name', options.name, 'required'); } const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return client.v4Channel.productTagCreate({ channelId: options.channelId, name: options.name, }); } catch (error) { throw this.handleError(error, 'createChannelProductTag'); } } async updateChannelProductTag(options) { try { this.validateChannelId(options.channelId); this.validatePositiveNumber(options.id, 'id'); if (!options.name || options.name.trim() === '') { throw new errors_1.PolyVValidationError('name is required', 'name', options.name, 'required'); } const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); await client.v4Channel.productTagUpdate({ channelId: options.channelId, id: options.id, name: options.name, }); } catch (error) { throw this.handleError(error, 'updateChannelProductTag'); } } async deleteChannelProductTag(options) { try { this.validateChannelId(options.channelId); this.validatePositiveNumber(options.id, 'id'); const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); await client.v4Channel.productTagDelete({ channelId: options.channelId, id: options.id, }); } catch (error) { throw this.handleError(error, 'deleteChannelProductTag'); } } async listProductStats(options) { try { this.validateChannelId(options.channelId); const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return client.v4Channel.listProductStats(this.compact({ channelId: options.channelId, productId: options.productId, productName: options.productName, sessionId: options.sessionId, pageNumber: options.page ?? 1, pageSize: options.size ?? 20, })); } catch (error) { throw this.handleError(error, 'listProductStats'); } } async getProductStatsSummary(options) { try { this.validateChannelId(options.channelId); const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); return client.v4Channel.getProductStatsSummary(this.compact({ channelId: options.channelId, sessionId: options.sessionId, })); } catch (error) { throw this.handleError(error, 'getProductStatsSummary'); } } async sortChannelProductRank(options) { try { this.validateChannelId(options.channelId); this.validatePositiveNumber(options.productId, 'productId'); this.validatePositiveNumber(options.rank, 'rank'); const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); await client.v4Channel.sortChannelProductRank({ channelId: options.channelId, productId: options.productId, rank: options.rank, }); } catch (error) { throw this.handleError(error, 'sortChannelProductRank'); } } async toppingChannelProduct(options) { try { this.validateChannelId(options.channelId); this.validatePositiveNumber(options.productId, 'productId'); const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); await client.v4Channel.toppingChannelProduct({ channelId: options.channelId, productId: options.productId, }); } catch (error) { throw this.handleError(error, 'toppingChannelProduct'); } } async untoppingChannelProduct(options) { try { this.validateChannelId(options.channelId); this.validatePositiveNumber(options.productId, 'productId'); const client = (0, sdk_1.createSdkClient)(this.authConfig, this.config.baseUrl); await client.v4Channel.untoppingChannelProduct({ channelId: options.channelId, productId: options.productId, }); } catch (error) { throw this.handleError(error, 'untoppingChannelProduct'); } } validateListRequest(request) { const errors = []; if (request.page !== undefined) { if (typeof request.page !== 'number' || !Number.isInteger(request.page) || request.page < 1) { errors.push('page must be a positive integer (minimum 1)'); } } if (request.size !== undefined) { if (typeof request.size !== 'number' || !Number.isInteger(request.size) || request.size < 1 || request.size > 100) { errors.push('size must be an integer between 1 and 100'); } } if (request.channelId !== undefined && (typeof request.channelId !== 'string' || request.channelId.trim().length === 0)) { errors.push('channelId must be a non-empty string'); } if (errors.length > 0) { throw new errors_1.PolyVValidationError(`Product list request validation failed: ${errors.join(', ')}`, 'request', request, 'validation_failed'); } } validateAddOptions(options) { const errors = []; if (!options.channelId || options.channelId.trim() === '') { errors.push('channelId is required'); } if (!options.name || options.name.trim() === '') { errors.push('name is required'); } if (options.status === undefined) { errors.push('status is required (1 for on-shelf, 2 for off-shelf)'); } else if (![1, 2].includes(options.status)) { errors.push('status must be 1 (on-shelf) or 2 (off-shelf)'); } if (options.linkType === undefined) { errors.push('linkType is required (10 for universal, 11 for multi-platform)'); } else if (![10, 11].includes(options.linkType)) { errors.push('linkType must be 10 (universal) or 11 (multi-platform)'); } if (options.linkType === 10 && (!options.link || options.link.trim() === '')) { errors.push('link is required when linkType is 10'); } if (errors.length > 0) { throw new errors_1.PolyVValidationError(`Product add options validation failed: ${errors.join(', ')}`, 'options', options, 'validation_failed'); } } validateUpdateOptions(options) { const errors = []; if (!options.channelId || options.channelId.trim() === '') { errors.push('channelId is required'); } if (options.productId === undefined) { errors.push('productId is required'); } if (options.status !== undefined && ![1, 2].includes(options.status)) { errors.push('status must be 1 or 2'); } if (options.linkType !== undefined && ![10, 11].includes(options.linkType)) { errors.push('linkType must be 10 or 11'); } if (errors.length > 0) { throw new errors_1.PolyVValidationError(`Product update options validation failed: ${errors.join(', ')}`, 'options', options, 'validation_failed'); } } validateDeleteOptions(options) { const errors = []; if (!options.channelId || options.channelId.trim() === '') { errors.push('channelId is required'); } if (options.productId === undefined) { errors.push('productId is required'); } if (errors.length > 0) { throw new errors_1.PolyVValidationError(`Product delete options validation failed: ${errors.join(', ')}`, 'options', options, 'validation_failed'); } } validateUserProductOptions(options, requiredFields) { const values = options; const errors = []; for (const field of requiredFields) { const value = values[field]; if (value === undefined || value === null || value === '') { errors.push(`${field} is required`); } } if (values['linkType'] !== undefined && ![10, 11, 12].includes(Number(values['linkType']))) { errors.push('linkType must be 10, 11, or 12'); } if (errors.length > 0) { throw new errors_1.PolyVValidationError(`User product options validation failed: ${errors.join(', ')}`, 'options', options, 'validation_failed'); } } toUserProductParams(options) { return this.compact({ ...('productId' in options ? { productId: options.productId } : {}), name: options.name, linkType: options.linkType, link: options.link, cover: options.cover, productType: options.productType, pcLink: options.pcLink, mobileLink: options.mobileLink, wxMiniprogramOriginalId: options.wxMiniprogramOriginalId, wxMiniprogramLink: options.wxMiniprogramLink, mobileAppLink: options.mobileAppLink, iosLink: options.iosLink, androidLink: options.androidLink, otherLink: options.otherLink, features: options.features, tagIds: options.tagIds ? this.parseNumberList(options.tagIds, 'tagIds') : undefined, btnShow: options.btnShow, productDesc: options.productDesc, productDetail: options.productDetail, ext: options.ext, priceType: options.priceType, realPrice: options.realPrice, customPrice: options.customPrice, originalPriceType: options.originalPriceType, price: options.price, customOrignalPrice: options.customOrignalPrice, }); } parseStringList(value, fieldName) { const list = value.split(',').map(item => item.trim()).filter(Boolean); if (list.length === 0) { throw new errors_1.PolyVValidationError(`${fieldName} must not be empty`, fieldName, value, 'not_empty'); } return Array.from(new Set(list)); } parseNumberList(value, fieldName) { return this.parseStringList(value, fieldName).map(item => { const parsed = Number(item); if (!Number.isInteger(parsed) || parsed <= 0) { throw new errors_1.PolyVValidationError(`${fieldName} must contain positive integers`, fieldName, value, 'positive_integer_list'); } return parsed; }); } validateChannelId(channelId) { if (!channelId || channelId.trim() === '') { throw new errors_1.PolyVValidationError('channelId is required', 'channelId', channelId, 'required'); } } validatePositiveNumber(value, fieldName) { if (!Number.isInteger(value) || value <= 0) { throw new errors_1.PolyVValidationError(`${fieldName} must be a positive integer`, fieldName, value, 'positive_integer'); } } compact(params) { return Object.fromEntries(Object.entries(params).filter(([, value]) => value !== undefined && value !== '')); } handleError(error, operation) { if (this.config.debug) { console.error(`[ProductServiceSdk] Error in ${operation}:`, error); } if (error instanceof errors_1.PolyVError || error instanceof errors_1.PolyVAPIError || error instanceof errors_1.PolyVValidationError) { return error; } if (error instanceof Error) { const anyError = error; if (anyError.polyvCode || anyError.code) { return new errors_1.PolyVAPIError(error.message, anyError.code || 'API_ERROR', anyError.status || 500, { polyvCode: anyError.polyvCode, polyvMessage: anyError.polyvMessage || error.message, }); } return new errors_1.PolyVError(`Failed to ${operation}: ${error.message}`, 'PRODUCT_SERVICE_ERROR', 500, { originalError: error.message }); } return new errors_1.PolyVError(`Failed to ${operation}: Unknown error`, 'UNKNOWN_ERROR', 500, { originalError: String(error) }); } } exports.ProductServiceSdk = ProductServiceSdk; //# sourceMappingURL=product.service.sdk.js.map