UNPKG

@donation-alerts/api

Version:
171 lines (170 loc) 8.09 kB
import { __decorate } from "tslib"; import { ReadDocumentation, } from '@donation-alerts/common'; import { mapOptional } from '@stimulcross/shared-utils'; import { DonationAlertsMerchandise } from "./donation-alerts-merchandise.mjs"; import { DonationAlertsMerchandiseSale, } from "./donation-alerts-merchandise-sale.mjs"; import { createSha256SignatureFromParams } from "../../utils/create-sha256-signature-from-params.mjs"; import { BaseApi } from "../base-api.mjs"; /** * API for managing DonationAlerts' merchandise sales and items. * * @remarks * The `DonationAlertsMerchandiseApi` provides methods to create, update, and manage merchandise items, * as well as to notify DonationAlerts of new sales. * Merchants can use this API to integrate sales systems with DonationAlerts' platform, enabling * streamers to sell merchandise with flexible revenue sharing. Access is granted upon request. * For details, refer to the official documentation. * * @see https://www.donationalerts.com/apidoc#advertisement__merchandises */ let DonationAlertsMerchandiseApi = class DonationAlertsMerchandiseApi extends BaseApi { /** * Creates a new merchandise item. * * @param user The ID of the authorized user. * @param clientSecret The application client secret associated with the authorized application. * @param data The details of the merchandise item to create. * @param rateLimiterOptions Optional rate limiter settings. * * @returns An instance of {@link DonationAlertsMerchandise} representing the created item. * * @throws {@link HttpError} if the response status code is not in the 200-299 range. * @throws {@link UnregisteredUserError} if the user is not registered in the authentication provider. */ async createMerchandise(user, clientSecret, data, rateLimiterOptions) { const formData = { merchant_identifier: data.merchantIdentifier, merchandise_identifier: data.merchandiseIdentifier, title: data.title, is_active: mapOptional(data.isActive, (v) => (v ? '1' : '0')), is_percentage: mapOptional(data.isPercentage, (v) => (v ? '1' : '0')), currency: data.currency, price_user: data.priceUser, price_service: data.priceService, url: data.url, img_url: data.imageUrl, end_at_ts: data.endTimestamp, }; const signature = createSha256SignatureFromParams(formData, clientSecret); const response = await this._apiClient.callApi(user, { type: 'api', url: 'merchandise', method: 'POST', formBody: { ...formData, signature }, }, rateLimiterOptions); return new DonationAlertsMerchandise(response.data); } /** * Updates an existing merchandise item. * * @param user The ID of the authorized user. * @param clientSecret The application client secret associated with the authorized application. * @param merchandiseId The ID of the merchandise to update. * @param data The modified data for the merchandise item. * @param rateLimiterOptions Optional rate limiter settings. * * @returns An updated instance of `DonationAlertsMerchandise`. * * @throws {@link HttpError} if the response status code is not in the 200-299 range. * @throws {@link UnregisteredUserError} if the user is not registered in the authentication provider. */ async updateMerchandise(user, clientSecret, merchandiseId, data, rateLimiterOptions) { const formData = { merchant_identifier: data.merchantIdentifier, merchandise_identifier: data.merchandiseIdentifier, title: data.title, is_active: mapOptional(data.isActive, (v) => (v ? '1' : '0')), is_percentage: mapOptional(data.isPercentage, (v) => (v ? '1' : '0')), currency: data.currency, price_user: data.priceUser, price_service: data.priceService, url: data.url, img_url: data.imageUrl, end_at_ts: data.endTimestamp, }; const signature = createSha256SignatureFromParams(formData, clientSecret); const response = await this._apiClient.callApi(user, { type: 'api', url: `merchandise/${merchandiseId}`, method: 'PUT', formBody: { ...formData, signature }, }, rateLimiterOptions); return new DonationAlertsMerchandise(response.data); } /** * Creates or updates a merchandise item. * * @remarks * A versatile method that either updates an existing merchandise item or creates a new one if it doesn't * already exist. * * @param user The ID of the user to use the access token of. * @param clientSecret The application client secret. * This secret must correspond to the application that the user authenticated. * @param data The merchandise data to create or update. * @param rateLimiterOptions Optional rate limiter configuration. * * @returns An instance of {@link DonationAlertsMerchandise} representing the created or updated merchandise item. * * @throws {@link HttpError} if the response status code falls outside the 200–299 range. * @throws {@link UnregisteredUserError} if the specified user is not registered with the authentication provider. */ async createOrUpdateMerchandise(user, clientSecret, data, rateLimiterOptions) { const formData = { title: data.title, is_active: mapOptional(data.isActive, (v) => (v ? '1' : '0')), is_percentage: mapOptional(data.isPercentage, (v) => (v ? '1' : '0')), currency: data.currency, price_user: data.priceUser, price_service: data.priceService, url: data.url, img_url: data.imageUrl, end_at_ts: data.endTimestamp, }; const signature = createSha256SignatureFromParams(formData, clientSecret); const response = await this._apiClient.callApi(user, { type: 'api', url: `merchandise/${data.merchantIdentifier}/${data.merchandiseIdentifier}`, method: 'POST', formBody: { ...formData, signature }, }, rateLimiterOptions); return new DonationAlertsMerchandise(response.data); } /** * Sends a sale alert to DonationAlerts. * * @param user The ID of the authorized user. * @param clientSecret The application client secret associated with the authorized application. * @param data The sale alert data to send. * @param rateLimiterOptions Optional rate limiter settings. * * @returns An instance of {@link DonationAlertsMerchandiseSale} representing the sale alert. * * @throws {@link HttpError} if the response status code is not in the 200-299 range. * @throws {@link UnregisteredUserError} if the user is not registered in the authentication provider. */ async sendSaleAlert(user, clientSecret, data, rateLimiterOptions) { const formData = { external_id: data.externalId, merchant_identifier: data.merchantIdentifier, merchandise_identifier: data.merchandiseIdentifier, amount: data.amount, currency: data.currency, boughtAmount: data.boughtAmount, username: data.username, message: data.message, }; const signature = createSha256SignatureFromParams(formData, clientSecret); const response = await this._apiClient.callApi(user, { type: 'api', url: 'merchandise_sale', method: 'POST', formBody: { ...formData, signature }, }, rateLimiterOptions); return new DonationAlertsMerchandiseSale(response.data); } }; DonationAlertsMerchandiseApi = __decorate([ ReadDocumentation('api') ], DonationAlertsMerchandiseApi); export { DonationAlertsMerchandiseApi };