@donation-alerts/api
Version:
Interact with Donation Alerts API.
174 lines (173 loc) • 8.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DonationAlertsMerchandiseApi = void 0;
const tslib_1 = require("tslib");
const common_1 = require("@donation-alerts/common");
const shared_utils_1 = require("@stimulcross/shared-utils");
const donation_alerts_merchandise_1 = require("./donation-alerts-merchandise");
const donation_alerts_merchandise_sale_1 = require("./donation-alerts-merchandise-sale");
const create_sha256_signature_from_params_1 = require("../../utils/create-sha256-signature-from-params");
const base_api_1 = require("../base-api");
/**
* 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 base_api_1.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: (0, shared_utils_1.mapOptional)(data.isActive, (v) => (v ? '1' : '0')),
is_percentage: (0, shared_utils_1.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 = (0, create_sha256_signature_from_params_1.createSha256SignatureFromParams)(formData, clientSecret);
const response = await this._apiClient.callApi(user, {
type: 'api',
url: 'merchandise',
method: 'POST',
formBody: { ...formData, signature },
}, rateLimiterOptions);
return new donation_alerts_merchandise_1.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: (0, shared_utils_1.mapOptional)(data.isActive, (v) => (v ? '1' : '0')),
is_percentage: (0, shared_utils_1.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 = (0, create_sha256_signature_from_params_1.createSha256SignatureFromParams)(formData, clientSecret);
const response = await this._apiClient.callApi(user, {
type: 'api',
url: `merchandise/${merchandiseId}`,
method: 'PUT',
formBody: { ...formData, signature },
}, rateLimiterOptions);
return new donation_alerts_merchandise_1.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: (0, shared_utils_1.mapOptional)(data.isActive, (v) => (v ? '1' : '0')),
is_percentage: (0, shared_utils_1.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 = (0, create_sha256_signature_from_params_1.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 donation_alerts_merchandise_1.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 = (0, create_sha256_signature_from_params_1.createSha256SignatureFromParams)(formData, clientSecret);
const response = await this._apiClient.callApi(user, {
type: 'api',
url: 'merchandise_sale',
method: 'POST',
formBody: { ...formData, signature },
}, rateLimiterOptions);
return new donation_alerts_merchandise_sale_1.DonationAlertsMerchandiseSale(response.data);
}
};
exports.DonationAlertsMerchandiseApi = DonationAlertsMerchandiseApi;
exports.DonationAlertsMerchandiseApi = DonationAlertsMerchandiseApi = tslib_1.__decorate([
(0, common_1.ReadDocumentation)('api')
], DonationAlertsMerchandiseApi);