@kash-88/alerts 1.2.1 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (110) hide show
  1. package/LICENSE +21 -0
  2. package/dist/src/Function/createCustomAlerts.d.ts +16 -0
  3. package/dist/src/Function/createCustomAlerts.js +51 -0
  4. package/dist/src/Function/createMerchandise.d.ts +22 -0
  5. package/dist/src/Function/createMerchandise.js +82 -0
  6. package/dist/src/Function/generateSignature.d.ts +13 -0
  7. package/dist/src/Function/generateSignature.js +18 -0
  8. package/dist/src/Function/getAuthorizeLink.d.ts +13 -0
  9. package/dist/src/Function/getAuthorizeLink.js +27 -0
  10. package/dist/src/Function/getDonationsAlerts.d.ts +11 -0
  11. package/dist/src/Function/getDonationsAlerts.js +30 -0
  12. package/dist/src/Function/getExternal.d.ts +6 -0
  13. package/dist/src/Function/getExternal.js +27 -0
  14. package/dist/src/Function/getOauthToken.d.ts +13 -0
  15. package/dist/src/Function/getOauthToken.js +44 -0
  16. package/dist/src/Function/getPrivateToken.d.ts +10 -0
  17. package/dist/src/Function/getPrivateToken.js +40 -0
  18. package/dist/src/Function/getUser.d.ts +10 -0
  19. package/dist/src/Function/getUser.js +26 -0
  20. package/dist/src/Function/getUserChannel.d.ts +8 -0
  21. package/dist/src/Function/getUserChannel.js +13 -0
  22. package/dist/src/Function/getUserDataFromPromocode.d.ts +11 -0
  23. package/dist/src/Function/getUserDataFromPromocode.js +39 -0
  24. package/dist/src/Function/sendSaleAlert.d.ts +20 -0
  25. package/dist/src/Function/sendSaleAlert.js +81 -0
  26. package/dist/src/Function/updateMerchandise.d.ts +21 -0
  27. package/dist/src/Function/updateMerchandise.js +76 -0
  28. package/dist/src/Function/updateOauthToken.d.ts +13 -0
  29. package/dist/src/Function/updateOauthToken.js +44 -0
  30. package/dist/src/Function/updateOrCreateMerchandise.d.ts +22 -0
  31. package/dist/src/Function/updateOrCreateMerchandise.js +80 -0
  32. package/dist/src/Types/CustomAlerts.d.ts +12 -0
  33. package/dist/src/Types/DonationsAlerts.d.ts +31 -0
  34. package/dist/src/Types/DonationsAlerts.js +1 -0
  35. package/dist/src/Types/Merchandise.d.ts +18 -0
  36. package/dist/src/Types/Merchandise.js +1 -0
  37. package/dist/src/Types/MerchandiseSale.d.ts +14 -0
  38. package/dist/src/Types/MerchandiseSale.js +1 -0
  39. package/dist/src/Types/OAuthScope.d.ts +9 -0
  40. package/dist/src/Types/OAuthScope.js +10 -0
  41. package/dist/src/Types/OAuthToken.d.ts +7 -0
  42. package/dist/src/Types/OAuthToken.js +1 -0
  43. package/dist/src/Types/User.d.ts +9 -0
  44. package/dist/src/Types/User.js +1 -0
  45. package/dist/src/Types/index.d.ts +8 -0
  46. package/dist/src/Types/index.js +2 -0
  47. package/dist/src/WebSocket/CentrifugeClient.d.ts +71 -0
  48. package/dist/src/WebSocket/CentrifugeClient.js +216 -0
  49. package/dist/src/index.d.ts +18 -9
  50. package/dist/src/index.js +18 -9
  51. package/dist/src/utils.d.ts +5 -13
  52. package/dist/src/utils.js +16 -32
  53. package/package.json +22 -18
  54. package/readme.md +502 -81
  55. package/src/Function/createCustomAlerts.ts +65 -0
  56. package/src/Function/createMerchandise.ts +105 -0
  57. package/src/Function/generateSignature.ts +23 -0
  58. package/src/Function/getAuthorizeLink.ts +37 -0
  59. package/src/Function/getDonationsAlerts.ts +37 -0
  60. package/src/Function/getExternal.ts +31 -0
  61. package/src/Function/getOauthToken.ts +54 -0
  62. package/src/Function/getPrivateToken.ts +50 -0
  63. package/src/Function/getUser.ts +32 -0
  64. package/src/Function/getUserChannel.ts +17 -0
  65. package/src/Function/getUserDataFromPromocode.ts +51 -0
  66. package/src/Function/sendSaleAlert.ts +103 -0
  67. package/src/Function/updateMerchandise.ts +98 -0
  68. package/src/Function/updateOauthToken.ts +54 -0
  69. package/src/Function/updateOrCreateMerchandise.ts +103 -0
  70. package/src/Types/CustomAlerts.ts +13 -0
  71. package/src/Types/DonationsAlerts.ts +32 -0
  72. package/src/Types/Merchandise.ts +19 -0
  73. package/src/Types/MerchandiseSale.ts +15 -0
  74. package/src/Types/OAuthScope.ts +10 -0
  75. package/src/Types/OAuthToken.ts +8 -0
  76. package/src/Types/User.ts +10 -0
  77. package/src/Types/index.ts +17 -0
  78. package/src/WebSocket/CentrifugeClient.ts +271 -0
  79. package/src/index.ts +25 -6
  80. package/src/utils.ts +22 -44
  81. package/dist/src/func/getAuthorizeLink.d.ts +0 -18
  82. package/dist/src/func/getAuthorizeLink.js +0 -29
  83. package/dist/src/func/getOauthToken.d.ts +0 -27
  84. package/dist/src/func/getOauthToken.js +0 -42
  85. package/dist/src/func/getPrivateToken.d.ts +0 -29
  86. package/dist/src/func/getPrivateToken.js +0 -46
  87. package/dist/src/func/getUser.d.ts +0 -23
  88. package/dist/src/func/getUser.js +0 -38
  89. package/dist/src/func/getUserChannel.d.ts +0 -19
  90. package/dist/src/func/getUserChannel.js +0 -32
  91. package/dist/src/func/updateAccessToken.d.ts +0 -24
  92. package/dist/src/func/updateAccessToken.js +0 -39
  93. package/dist/src/types.d.ts +0 -33
  94. package/dist/src/ws/CentrifugeClient.d.ts +0 -31
  95. package/dist/src/ws/CentrifugeClient.js +0 -71
  96. package/src/example/getAuthorizeLink.ts +0 -11
  97. package/src/example/getOauthToken.ts +0 -16
  98. package/src/example/getUser.ts +0 -12
  99. package/src/example/getUserChannel.ts +0 -10
  100. package/src/example/updateAccessToken.ts +0 -17
  101. package/src/example/wsExample.ts +0 -50
  102. package/src/func/getAuthorizeLink.ts +0 -33
  103. package/src/func/getOauthToken.ts +0 -46
  104. package/src/func/getPrivateToken.ts +0 -53
  105. package/src/func/getUser.ts +0 -42
  106. package/src/func/getUserChannel.ts +0 -34
  107. package/src/func/updateAccessToken.ts +0 -43
  108. package/src/types.ts +0 -38
  109. package/src/ws/CentrifugeClient.ts +0 -108
  110. /package/dist/src/{types.js → Types/CustomAlerts.js} +0 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Mikhail
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,16 @@
1
+ import { CustomAlerts } from "../Types/index.js";
2
+ /**
3
+ * Send a custom alert to the authorized user.
4
+ *
5
+ * @param {string} access_token - User access token
6
+ * @param {string} external_id - Up to 32 characters long unique alert ID
7
+ * @param {string} header - Up to 255 characters long header text
8
+ * @param {string} message - Up to 300 characters long message text
9
+ * @param {number} is_shown - 0 or 1, determines whether the alert should be displayed
10
+ * @param {string | null} [image_url] - URL to the image file displayed with the alert
11
+ * @param {string | null} [sound_url] - URL to the sound file played when displaying the alert
12
+ *
13
+ * @returns {Promise<CustomAlerts>} Created custom alert data
14
+ * @see {@link https://www.donationalerts.com/apidoc#api_v1__custom_alerts__send_custom_alerts}
15
+ */
16
+ export default function createCustomAlerts(access_token: string, external_id: string, header: string, message: string, is_shown: 0 | 1, image_url?: string | null, sound_url?: string | null): Promise<CustomAlerts>;
@@ -0,0 +1,51 @@
1
+ import axios from "axios";
2
+ import { formatAxiosError } from "../utils.js";
3
+ /**
4
+ * Send a custom alert to the authorized user.
5
+ *
6
+ * @param {string} access_token - User access token
7
+ * @param {string} external_id - Up to 32 characters long unique alert ID
8
+ * @param {string} header - Up to 255 characters long header text
9
+ * @param {string} message - Up to 300 characters long message text
10
+ * @param {number} is_shown - 0 or 1, determines whether the alert should be displayed
11
+ * @param {string | null} [image_url] - URL to the image file displayed with the alert
12
+ * @param {string | null} [sound_url] - URL to the sound file played when displaying the alert
13
+ *
14
+ * @returns {Promise<CustomAlerts>} Created custom alert data
15
+ * @see {@link https://www.donationalerts.com/apidoc#api_v1__custom_alerts__send_custom_alerts}
16
+ */
17
+ export default async function createCustomAlerts(access_token, external_id, header, message, is_shown, image_url, sound_url) {
18
+ if (!access_token || typeof access_token !== "string") {
19
+ throw new Error("access_token must be a non-empty string");
20
+ }
21
+ if (!external_id || typeof external_id !== "string") {
22
+ throw new Error("external_id must be a non-empty string");
23
+ }
24
+ if (typeof header !== "string" || typeof message !== "string") {
25
+ throw new Error("header and message must be strings");
26
+ }
27
+ if (is_shown !== 0 && is_shown !== 1) {
28
+ throw new Error("is_shown must be 0 or 1");
29
+ }
30
+ try {
31
+ const formData = new URLSearchParams();
32
+ formData.append("external_id", external_id);
33
+ formData.append("header", header);
34
+ formData.append("message", message);
35
+ formData.append("is_shown", String(is_shown));
36
+ if (image_url)
37
+ formData.append("image_url", image_url);
38
+ if (sound_url)
39
+ formData.append("sound_url", sound_url);
40
+ const response = await axios.post("https://www.donationalerts.com/api/v1/custom_alert", formData, {
41
+ headers: {
42
+ Authorization: `Bearer ${access_token}`,
43
+ "Content-Type": "application/x-www-form-urlencoded"
44
+ }
45
+ });
46
+ return response.data.data;
47
+ }
48
+ catch (error) {
49
+ throw new Error(formatAxiosError(error));
50
+ }
51
+ }
@@ -0,0 +1,22 @@
1
+ import { Merchandise } from "../Types/index.js";
2
+ /**
3
+ * Create a new merchandise.
4
+ *
5
+ * @param {string} access_token - User access token
6
+ * @param {string} client_secret - API client secret key for signature
7
+ * @param {string} merchant_identifier - Merchant's ID on DonationAlerts
8
+ * @param {string} merchandise_identifier - Up to 16 characters long unique merchandise ID
9
+ * @param {Record<string, string>} title - Object with merchandise titles in different locales (en_US required)
10
+ * @param {string} currency - Currency code (EUR, USD, RUB, BRL, TRY)
11
+ * @param {number} price_user - Revenue added to streamer for each sale
12
+ * @param {number} price_service - Revenue added to DonationAlerts for each sale
13
+ * @param {number} [is_active=0] - 0 or 1, whether merchandise is available for purchase
14
+ * @param {number} [is_percentage=0] - 0 or 1, whether prices are percentages or absolute amounts
15
+ * @param {string} [url] - URL to merchandise's web page
16
+ * @param {string} [img_url] - URL to merchandise's image
17
+ * @param {number} [end_at_ts] - Unix timestamp when merchandise becomes inactive
18
+ *
19
+ * @returns {Promise<Merchandise>} Created merchandise data
20
+ * @see {@link https://www.donationalerts.com/apidoc#api_v1__merchandises__create_merchandise}
21
+ */
22
+ export default function createMerchandise(access_token: string, client_secret: string, merchant_identifier: string, merchandise_identifier: string, title: Record<string, string>, currency: string, price_user: number, price_service: number, is_active?: number, is_percentage?: number, url?: string, img_url?: string, end_at_ts?: number): Promise<Merchandise>;
@@ -0,0 +1,82 @@
1
+ import axios from "axios";
2
+ import generateSignature from "./generateSignature.js";
3
+ import { formatAxiosError } from "../utils.js";
4
+ /**
5
+ * Create a new merchandise.
6
+ *
7
+ * @param {string} access_token - User access token
8
+ * @param {string} client_secret - API client secret key for signature
9
+ * @param {string} merchant_identifier - Merchant's ID on DonationAlerts
10
+ * @param {string} merchandise_identifier - Up to 16 characters long unique merchandise ID
11
+ * @param {Record<string, string>} title - Object with merchandise titles in different locales (en_US required)
12
+ * @param {string} currency - Currency code (EUR, USD, RUB, BRL, TRY)
13
+ * @param {number} price_user - Revenue added to streamer for each sale
14
+ * @param {number} price_service - Revenue added to DonationAlerts for each sale
15
+ * @param {number} [is_active=0] - 0 or 1, whether merchandise is available for purchase
16
+ * @param {number} [is_percentage=0] - 0 or 1, whether prices are percentages or absolute amounts
17
+ * @param {string} [url] - URL to merchandise's web page
18
+ * @param {string} [img_url] - URL to merchandise's image
19
+ * @param {number} [end_at_ts] - Unix timestamp when merchandise becomes inactive
20
+ *
21
+ * @returns {Promise<Merchandise>} Created merchandise data
22
+ * @see {@link https://www.donationalerts.com/apidoc#api_v1__merchandises__create_merchandise}
23
+ */
24
+ export default async function createMerchandise(access_token, client_secret, merchant_identifier, merchandise_identifier, title, currency, price_user, price_service, is_active = 0, is_percentage = 0, url, img_url, end_at_ts) {
25
+ if (!access_token || typeof access_token !== "string") {
26
+ throw new Error("access_token must be a non-empty string");
27
+ }
28
+ if (!client_secret || typeof client_secret !== "string") {
29
+ throw new Error("client_secret must be a non-empty string");
30
+ }
31
+ if (!merchant_identifier || typeof merchant_identifier !== "string") {
32
+ throw new Error("merchant_identifier must be a non-empty string");
33
+ }
34
+ if (!merchandise_identifier || typeof merchandise_identifier !== "string") {
35
+ throw new Error("merchandise_identifier must be a non-empty string");
36
+ }
37
+ if (!title || Object.keys(title).length === 0) {
38
+ throw new Error("title must be a non-empty object");
39
+ }
40
+ if (!currency || typeof currency !== "string") {
41
+ throw new Error("currency must be a non-empty string");
42
+ }
43
+ if (typeof price_user !== "number" || typeof price_service !== "number") {
44
+ throw new Error("price_user and price_service must be numbers");
45
+ }
46
+ try {
47
+ const params = {
48
+ merchant_identifier,
49
+ merchandise_identifier,
50
+ currency,
51
+ price_user,
52
+ price_service,
53
+ is_active,
54
+ is_percentage
55
+ };
56
+ for (const [locale, value] of Object.entries(title)) {
57
+ params[`title[${locale}]`] = value;
58
+ }
59
+ if (url)
60
+ params.url = url;
61
+ if (img_url)
62
+ params.img_url = img_url;
63
+ if (end_at_ts !== undefined)
64
+ params.end_at_ts = end_at_ts;
65
+ const signature = generateSignature(params, client_secret);
66
+ params.signature = signature;
67
+ const formData = new URLSearchParams();
68
+ for (const [key, value] of Object.entries(params)) {
69
+ formData.append(key, String(value));
70
+ }
71
+ const response = await axios.post("https://www.donationalerts.com/api/v1/merchandise", formData, {
72
+ headers: {
73
+ Authorization: `Bearer ${access_token}`,
74
+ "Content-Type": "application/x-www-form-urlencoded"
75
+ }
76
+ });
77
+ return response.data.data;
78
+ }
79
+ catch (error) {
80
+ throw new Error(formatAxiosError(error));
81
+ }
82
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Generate a SHA256 request signature for Merchandise API.
3
+ *
4
+ * The signature is formed from alphabetically sorted parameter values
5
+ * (interpreted as strings) with the API client secret appended to the end.
6
+ *
7
+ * @param {Record<string, string | number>} params - Request parameters
8
+ * @param {string} client_secret - API client secret key
9
+ *
10
+ * @returns {string} SHA256 hex digest signature
11
+ * @see {@link https://www.donationalerts.com/apidoc#introduction__http_api_requests__request_signatures}
12
+ */
13
+ export default function generateSignature(params: Record<string, string | number>, client_secret: string): string;
@@ -0,0 +1,18 @@
1
+ import { createHash } from "crypto";
2
+ /**
3
+ * Generate a SHA256 request signature for Merchandise API.
4
+ *
5
+ * The signature is formed from alphabetically sorted parameter values
6
+ * (interpreted as strings) with the API client secret appended to the end.
7
+ *
8
+ * @param {Record<string, string | number>} params - Request parameters
9
+ * @param {string} client_secret - API client secret key
10
+ *
11
+ * @returns {string} SHA256 hex digest signature
12
+ * @see {@link https://www.donationalerts.com/apidoc#introduction__http_api_requests__request_signatures}
13
+ */
14
+ export default function generateSignature(params, client_secret) {
15
+ const sortedValues = Object.values(params).map(String).sort();
16
+ const valuesString = sortedValues.join("");
17
+ return createHash("sha256").update(valuesString + client_secret).digest("hex");
18
+ }
@@ -0,0 +1,13 @@
1
+ import { OAuthScope } from "../Types/index.js";
2
+ /**
3
+ * Generate OAuth authorization link for DonationAlerts.
4
+ *
5
+ * @param {string} client_id - Your client (application) ID
6
+ * @param {string} redirect_uri - The URL where users will be sent after authorization
7
+ * @param {OAuthScope[]} scopes - Array of access scopes
8
+ * @param {'code' | 'token'} type - Type response oauth token
9
+ *
10
+ * @returns {string} The authorization URL.
11
+ * @see {@link https://www.donationalerts.com/apidoc#authorization__authorization_code__authorization_steps}
12
+ */
13
+ export default function getAuthorizeLink(client_id: string, redirect_uri: string, scopes: OAuthScope[], type: 'code' | 'token'): string;
@@ -0,0 +1,27 @@
1
+ /**
2
+ * Generate OAuth authorization link for DonationAlerts.
3
+ *
4
+ * @param {string} client_id - Your client (application) ID
5
+ * @param {string} redirect_uri - The URL where users will be sent after authorization
6
+ * @param {OAuthScope[]} scopes - Array of access scopes
7
+ * @param {'code' | 'token'} type - Type response oauth token
8
+ *
9
+ * @returns {string} The authorization URL.
10
+ * @see {@link https://www.donationalerts.com/apidoc#authorization__authorization_code__authorization_steps}
11
+ */
12
+ export default function getAuthorizeLink(client_id, redirect_uri, scopes, type) {
13
+ if (!client_id || typeof client_id !== "string") {
14
+ throw new Error("client_id must be a non-empty string");
15
+ }
16
+ if (!redirect_uri || typeof redirect_uri !== "string") {
17
+ throw new Error("redirect_uri must be a non-empty string");
18
+ }
19
+ if (!Array.isArray(scopes) || scopes.length === 0) {
20
+ throw new Error("scopes must be a non-empty array");
21
+ }
22
+ if (type !== "code" && type !== "token") {
23
+ throw new Error("type must be 'code' or 'token'");
24
+ }
25
+ const uniqueScopes = Array.from(new Set(scopes));
26
+ return `https://www.donationalerts.com/oauth/authorize?client_id=${encodeURIComponent(client_id)}&redirect_uri=${encodeURIComponent(redirect_uri)}&response_type=${type}&scope=${uniqueScopes.join("%20")}`;
27
+ }
@@ -0,0 +1,11 @@
1
+ import { DonationsAlerts } from "../Types/index.js";
2
+ /**
3
+ * Fetch user donation alerts list.
4
+ *
5
+ * @param {string} access_token - User access token
6
+ * @param {number | string} [page=1] - Page number for pagination
7
+ *
8
+ * @returns {Promise<DonationsAlerts>} Donation alerts data
9
+ * @see {@link https://www.donationalerts.com/apidoc#api_v1__donations__donation_alerts_list}
10
+ */
11
+ export default function getDonationsAlerts(access_token: string, page?: number | string): Promise<DonationsAlerts>;
@@ -0,0 +1,30 @@
1
+ import axios from "axios";
2
+ import { formatAxiosError } from "../utils.js";
3
+ /**
4
+ * Fetch user donation alerts list.
5
+ *
6
+ * @param {string} access_token - User access token
7
+ * @param {number | string} [page=1] - Page number for pagination
8
+ *
9
+ * @returns {Promise<DonationsAlerts>} Donation alerts data
10
+ * @see {@link https://www.donationalerts.com/apidoc#api_v1__donations__donation_alerts_list}
11
+ */
12
+ export default async function getDonationsAlerts(access_token, page = 1) {
13
+ if (!access_token || typeof access_token !== "string") {
14
+ throw new Error("access_token must be a non-empty string");
15
+ }
16
+ try {
17
+ const response = await axios.get("https://www.donationalerts.com/api/v1/alerts/donations", {
18
+ params: {
19
+ page
20
+ },
21
+ headers: {
22
+ Authorization: `Bearer ${access_token}`
23
+ }
24
+ });
25
+ return response.data;
26
+ }
27
+ catch (error) {
28
+ throw new Error(formatAxiosError(error));
29
+ }
30
+ }
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Generate a random 32-character external ID.
3
+ *
4
+ * @returns {string} 32 random alphanumeric characters
5
+ */
6
+ export default function getExternal(): string;
@@ -0,0 +1,27 @@
1
+ import { randomBytes } from "crypto";
2
+ /**
3
+ * Generate a random 32-character external ID.
4
+ *
5
+ * @returns {string} 32 random alphanumeric characters
6
+ */
7
+ export default function getExternal() {
8
+ const charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
9
+ const randomValues = randomBytes(32);
10
+ // Use rejection sampling to avoid bias
11
+ const charsetLength = charset.length;
12
+ const maxValidValue = 256 - (256 % charsetLength);
13
+ let result = "";
14
+ let index = 0;
15
+ while (index < 32) {
16
+ const randomValue = randomValues[index];
17
+ if (randomValue < maxValidValue) {
18
+ result += charset[randomValue % charsetLength];
19
+ index++;
20
+ }
21
+ else {
22
+ // Rejection: generate a new random value for this position
23
+ randomValues[index] = randomBytes(1)[0];
24
+ }
25
+ }
26
+ return result;
27
+ }
@@ -0,0 +1,13 @@
1
+ import { OAuthToken } from "../Types/index.js";
2
+ /**
3
+ * Exchange Authorization code for OAuth token and Refresh token.
4
+ *
5
+ * @param {string} client_id - Your client (application) ID
6
+ * @param {string} client_secret - Your client (application) secret
7
+ * @param {string} redirect_uri - The URL where users will be sent after authorization
8
+ * @param {string} code - User authorization code
9
+ *
10
+ * @returns {Promise<OAuthToken>} OAuth user token
11
+ * @see {@link https://github.com/kash-ts/alerts-SDK?tab=readme-ov-file#getOauthToken}
12
+ */
13
+ export default function getOauthToken(client_id: string, client_secret: string, redirect_uri: string, code: string): Promise<OAuthToken>;
@@ -0,0 +1,44 @@
1
+ import axios from "axios";
2
+ import { formatAxiosError } from "../utils.js";
3
+ /**
4
+ * Exchange Authorization code for OAuth token and Refresh token.
5
+ *
6
+ * @param {string} client_id - Your client (application) ID
7
+ * @param {string} client_secret - Your client (application) secret
8
+ * @param {string} redirect_uri - The URL where users will be sent after authorization
9
+ * @param {string} code - User authorization code
10
+ *
11
+ * @returns {Promise<OAuthToken>} OAuth user token
12
+ * @see {@link https://github.com/kash-ts/alerts-SDK?tab=readme-ov-file#getOauthToken}
13
+ */
14
+ export default async function getOauthToken(client_id, client_secret, redirect_uri, code) {
15
+ if (!client_id || typeof client_id !== "string") {
16
+ throw new Error("client_id must be a non-empty string");
17
+ }
18
+ if (!client_secret || typeof client_secret !== "string") {
19
+ throw new Error("client_secret must be a non-empty string");
20
+ }
21
+ if (!redirect_uri || typeof redirect_uri !== "string") {
22
+ throw new Error("redirect_uri must be a non-empty string");
23
+ }
24
+ if (!code || typeof code !== "string") {
25
+ throw new Error("code must be a non-empty string");
26
+ }
27
+ try {
28
+ const formData = new URLSearchParams();
29
+ formData.append("grant_type", "authorization_code");
30
+ formData.append("client_id", client_id);
31
+ formData.append("client_secret", client_secret);
32
+ formData.append("redirect_uri", redirect_uri);
33
+ formData.append("code", code);
34
+ const response = await axios.post("https://www.donationalerts.com/oauth/token", formData, {
35
+ headers: {
36
+ "Content-Type": "application/x-www-form-urlencoded"
37
+ }
38
+ });
39
+ return response.data;
40
+ }
41
+ catch (error) {
42
+ throw new Error(formatAxiosError(error));
43
+ }
44
+ }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Get a Private token for subscribing to a DonationAlerts channel via Centrifuge.
3
+ *
4
+ * @param {string} channel - User channel
5
+ * @param {string} client - Centrifugo UUIDv4 client ID
6
+ * @param {string} access_token - User OAuth token
7
+ *
8
+ * @returns {Promise<string>} - Token for channel subscription.
9
+ */
10
+ export default function getPrivateToken(channel: string, client: string, access_token: string): Promise<string>;
@@ -0,0 +1,40 @@
1
+ import axios from "axios";
2
+ import { formatAxiosError } from "../utils.js";
3
+ /**
4
+ * Get a Private token for subscribing to a DonationAlerts channel via Centrifuge.
5
+ *
6
+ * @param {string} channel - User channel
7
+ * @param {string} client - Centrifugo UUIDv4 client ID
8
+ * @param {string} access_token - User OAuth token
9
+ *
10
+ * @returns {Promise<string>} - Token for channel subscription.
11
+ */
12
+ export default async function getPrivateToken(channel, client, access_token) {
13
+ if (!channel || typeof channel !== "string") {
14
+ throw new Error("channel must be a non-empty string");
15
+ }
16
+ if (!client || typeof client !== "string") {
17
+ throw new Error("client must be a non-empty string");
18
+ }
19
+ if (!access_token || typeof access_token !== "string") {
20
+ throw new Error("access_token must be a non-empty string");
21
+ }
22
+ try {
23
+ const response = await axios.post("https://www.donationalerts.com/api/v1/centrifuge/subscribe", {
24
+ channels: [channel],
25
+ client: client
26
+ }, {
27
+ headers: {
28
+ Authorization: `Bearer ${access_token}`,
29
+ "Content-Type": "application/json"
30
+ }
31
+ });
32
+ if (!response.data?.channels?.[0]?.token) {
33
+ throw new Error("Failed to get private token: no channels in response");
34
+ }
35
+ return response.data.channels[0].token;
36
+ }
37
+ catch (error) {
38
+ throw new Error(formatAxiosError(error));
39
+ }
40
+ }
@@ -0,0 +1,10 @@
1
+ import { User } from "../Types/index.js";
2
+ /**
3
+ * Fetch user profile information by OAuth token.
4
+ *
5
+ * @param {string} access_token - User access token
6
+ *
7
+ * @returns {Promise<User>} User data
8
+ * @see {@link https://www.donationalerts.com/apidoc#api_v1__users__user_profile_information}
9
+ */
10
+ export default function getUser(access_token: string): Promise<User>;
@@ -0,0 +1,26 @@
1
+ import axios from "axios";
2
+ import { formatAxiosError } from "../utils.js";
3
+ /**
4
+ * Fetch user profile information by OAuth token.
5
+ *
6
+ * @param {string} access_token - User access token
7
+ *
8
+ * @returns {Promise<User>} User data
9
+ * @see {@link https://www.donationalerts.com/apidoc#api_v1__users__user_profile_information}
10
+ */
11
+ export default async function getUser(access_token) {
12
+ if (!access_token || typeof access_token !== "string") {
13
+ throw new Error("access_token must be a non-empty string");
14
+ }
15
+ try {
16
+ const response = await axios.get("https://www.donationalerts.com/api/v1/user/oauth", {
17
+ headers: {
18
+ Authorization: `Bearer ${access_token}`
19
+ }
20
+ });
21
+ return response.data.data;
22
+ }
23
+ catch (error) {
24
+ throw new Error(formatAxiosError(error));
25
+ }
26
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Get user channel by user id for WebSocket.
3
+ *
4
+ * @param {string | number} id - User ID
5
+ *
6
+ * @returns {string} The donation alert channel identifier in the format "$alerts:donation_{id}".
7
+ */
8
+ export default function getUserChannel(id: string | number): string;
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Get user channel by user id for WebSocket.
3
+ *
4
+ * @param {string | number} id - User ID
5
+ *
6
+ * @returns {string} The donation alert channel identifier in the format "$alerts:donation_{id}".
7
+ */
8
+ export default function getUserChannel(id) {
9
+ if (id === null || id === undefined || id === "") {
10
+ throw new Error("id must be a non-empty string or number");
11
+ }
12
+ return `$alerts:donation_${id}`;
13
+ }
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Get user ID from an advertising promocode.
3
+ *
4
+ * @param {string} access_token - User access token
5
+ * @param {string} client_secret - API client secret key for signature
6
+ * @param {string} promocode - User promocode
7
+ *
8
+ * @returns {Promise<number>} DonationAlerts user ID
9
+ * @see {@link https://www.donationalerts.com/apidoc#api_v1__merchandises__update_or_create_merchandise__get_user_data_from_promocode}
10
+ */
11
+ export default function getUserDataFromPromocode(access_token: string, client_secret: string, promocode: string): Promise<number>;
@@ -0,0 +1,39 @@
1
+ import axios from "axios";
2
+ import generateSignature from "./generateSignature.js";
3
+ import { formatAxiosError } from "../utils.js";
4
+ /**
5
+ * Get user ID from an advertising promocode.
6
+ *
7
+ * @param {string} access_token - User access token
8
+ * @param {string} client_secret - API client secret key for signature
9
+ * @param {string} promocode - User promocode
10
+ *
11
+ * @returns {Promise<number>} DonationAlerts user ID
12
+ * @see {@link https://www.donationalerts.com/apidoc#api_v1__merchandises__update_or_create_merchandise__get_user_data_from_promocode}
13
+ */
14
+ export default async function getUserDataFromPromocode(access_token, client_secret, promocode) {
15
+ if (!access_token || typeof access_token !== "string") {
16
+ throw new Error("access_token must be a non-empty string");
17
+ }
18
+ if (!client_secret || typeof client_secret !== "string") {
19
+ throw new Error("client_secret must be a non-empty string");
20
+ }
21
+ if (!promocode || typeof promocode !== "string") {
22
+ throw new Error("promocode must be a non-empty string");
23
+ }
24
+ try {
25
+ const params = { promocode };
26
+ const signature = generateSignature(params, client_secret);
27
+ params.signature = signature;
28
+ const response = await axios.get("https://www.donationalerts.com/api/v1/merchandise/user", {
29
+ params,
30
+ headers: {
31
+ Authorization: `Bearer ${access_token}`
32
+ }
33
+ });
34
+ return response.data.data.user_id;
35
+ }
36
+ catch (error) {
37
+ throw new Error(formatAxiosError(error));
38
+ }
39
+ }
@@ -0,0 +1,20 @@
1
+ import { MerchandiseSale } from "../Types/index.js";
2
+ /**
3
+ * Send a merchandise sale alert.
4
+ *
5
+ * @param {string} access_token - User access token
6
+ * @param {string} client_secret - API client secret key for signature
7
+ * @param {number} user_id - DonationAlerts user ID to which the sale is referenced
8
+ * @param {string} external_id - Up to 32 characters long unique sale ID
9
+ * @param {string} merchant_identifier - Merchant's ID on DonationAlerts
10
+ * @param {string} merchandise_identifier - Merchant's merchandise ID that was bought
11
+ * @param {number} amount - Grand total of the sale
12
+ * @param {string} currency - Currency code (EUR, USD, RUB, BRL, TRY)
13
+ * @param {number} [bought_amount=1] - Total number of bought items
14
+ * @param {string} [username] - Name of the customer
15
+ * @param {string} [message] - Message sent by the customer
16
+ *
17
+ * @returns {Promise<MerchandiseSale>} Created merchandise sale alert data
18
+ * @see {@link https://www.donationalerts.com/apidoc#api_v1__merchandise_sale_notifications__send_sale_alerts}
19
+ */
20
+ export default function sendSaleAlert(access_token: string, client_secret: string, user_id: number, external_id: string, merchant_identifier: string, merchandise_identifier: string, amount: number, currency: string, bought_amount?: number, username?: string, message?: string): Promise<MerchandiseSale>;