@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/src/index.ts CHANGED
@@ -1,19 +1,38 @@
1
1
  import getAuthorizeLink from "@function/getAuthorizeLink.js";
2
2
  import getOauthToken from "@function/getOauthToken.js";
3
3
  import getUser from "@function/getUser.js";
4
- import updateAccessToken from "@function/updateAccessToken.js";
4
+ import updateOauthToken from "@function/updateOauthToken.js";
5
5
  import getUserChannel from "@function/getUserChannel.js";
6
- import { getPrivateToken } from "@function/getPrivateToken.js";
7
- import CentrifugeClient from "@ws/CentrifugeClient.js";
6
+ import getPrivateToken from "@function/getPrivateToken.js";
7
+ import getDonationsAlerts from "@function/getDonationsAlerts.js";
8
+ import getExternal from "@function/getExternal.js";
9
+ import createCustomAlerts from "@function/createCustomAlerts.js";
10
+ import generateSignature from "@function/generateSignature.js";
11
+ import createMerchandise from "@function/createMerchandise.js";
12
+ import updateMerchandise from "@function/updateMerchandise.js";
13
+ import updateOrCreateMerchandise from "@function/updateOrCreateMerchandise.js";
14
+ import getUserDataFromPromocode from "@function/getUserDataFromPromocode.js";
15
+ import sendSaleAlert from "@function/sendSaleAlert.js";
8
16
 
9
- export * from "@type";
17
+ import WebServer from "@ws/CentrifugeClient.js";
18
+ import { OAuthScope } from "@type";
10
19
 
11
20
  export {
12
21
  getAuthorizeLink,
13
22
  getOauthToken,
14
23
  getUser,
15
- updateAccessToken,
24
+ updateOauthToken,
16
25
  getUserChannel,
26
+ getExternal,
17
27
  getPrivateToken,
18
- CentrifugeClient
28
+ getDonationsAlerts,
29
+ createCustomAlerts,
30
+ generateSignature,
31
+ createMerchandise,
32
+ updateMerchandise,
33
+ updateOrCreateMerchandise,
34
+ getUserDataFromPromocode,
35
+ sendSaleAlert,
36
+ WebServer,
37
+ OAuthScope
19
38
  };
package/src/utils.ts CHANGED
@@ -1,44 +1,22 @@
1
- /**
2
- * Checks if a value is a plain object.
3
- * @param {unknown} value The value to check.
4
- * @returns {boolean} True if the value is a plain object, false otherwise.
5
- * @private
6
- */
7
-
8
- function isObject(value: unknown): value is Record<string, unknown> {
9
- return typeof value === "object" && value !== null && !Array.isArray(value);
10
- }
11
-
12
- /**
13
- * Validates that the provided data is an object and contains the required keys.
14
- * For each required key, it also validates that the value is a string or a number.
15
- * @param {unknown} data - The data object to validate.
16
- * @param {string[]} requiredKeys - An array of keys that must be present in the data object.
17
- * @throws {Error} If validation fails.
18
- * @private
19
- */
20
-
21
- export function validateDataObject(data: unknown, requiredKeys: string[]): asserts data is Record<string, string | number | string[]> {
22
- if (!isObject(data)) {
23
- throw new Error("You must provide data as an object.");
24
- }
25
-
26
- for (const key of requiredKeys) {
27
- if (!(key in data) || data[key] === undefined || data[key] === null) {
28
- throw new Error(`You must provide "${key}" in the data object.`);
29
- }
30
-
31
- const value = data[key];
32
-
33
- if (key === "scope") {
34
- if(!Array.isArray(value)) {
35
- throw new Error(`"${key}" must be an array of strings.`);
36
- }
37
- continue;
38
- }
39
-
40
- if (typeof value !== "string" && typeof value !== "number") {
41
- throw new Error(`"${key}" must be a string or a number.`);
42
- }
43
- }
44
- }
1
+ /**
2
+ * Format an axios error into a readable error message.
3
+ *
4
+ * @param {any} error - The error thrown by axios or any other source
5
+ * @returns {string} A formatted error message
6
+ */
7
+
8
+ export function formatAxiosError(error: any): string {
9
+ if (error?.response?.data?.error_description) {
10
+ return error.response.data.error_description;
11
+ }
12
+ if (error?.response?.data?.message) {
13
+ return error.response.data.message;
14
+ }
15
+ if (error?.response?.data?.error) {
16
+ return error.response.data.error;
17
+ }
18
+ if (error?.message) {
19
+ return error.message;
20
+ }
21
+ return String(error);
22
+ }
@@ -1,18 +0,0 @@
1
- import { GetAuthorizeLinkData } from "../types.js";
2
- /**
3
- * Generates an authorization link for the DonationAlerts API.
4
- *
5
- * @example
6
- * import { getAuthorizeLink } from "@kash.88/alerts";
7
- *
8
- * const authLink = getAuthorizeLink({
9
- * client_id: "YOUR_CLIENT_ID",
10
- * scope: ["oauth-user-show"]
11
- * });
12
- * console.log(authLink);
13
- *
14
- * @param {GetAuthorizeLinkData} data - The data for generating the link.
15
- * @returns {string} The authorization URL.
16
- * @see {@link https://www.donationalerts.com/apidoc#authorization__authorization_code__authorization_request}
17
- */
18
- export default function getAuthorizeLink(data: GetAuthorizeLinkData): string;
@@ -1,29 +0,0 @@
1
- import { validateDataObject } from "../utils.js";
2
- /**
3
- * Generates an authorization link for the DonationAlerts API.
4
- *
5
- * @example
6
- * import { getAuthorizeLink } from "@kash.88/alerts";
7
- *
8
- * const authLink = getAuthorizeLink({
9
- * client_id: "YOUR_CLIENT_ID",
10
- * scope: ["oauth-user-show"]
11
- * });
12
- * console.log(authLink);
13
- *
14
- * @param {GetAuthorizeLinkData} data - The data for generating the link.
15
- * @returns {string} The authorization URL.
16
- * @see {@link https://www.donationalerts.com/apidoc#authorization__authorization_code__authorization_request}
17
- */
18
- export default function getAuthorizeLink(data) {
19
- try {
20
- validateDataObject(data, ["client_id", "scope"]);
21
- if (!Array.isArray(data.scope)) {
22
- throw new Error("You must provide \"scope\" as an array in the data object.");
23
- }
24
- return `https://www.donationalerts.com/oauth/authorize?client_id=${data.client_id}&response_type=code&scope=${data.scope.join("%20")}`;
25
- }
26
- catch (error) {
27
- throw new Error(error?.response?.data?.error_description || error?.message || error);
28
- }
29
- }
@@ -1,27 +0,0 @@
1
- import { GetOauthData, OauthToken } from "../types.js";
2
- /**
3
- * Exchanges an authorization code for an access token.
4
- *
5
- * @example
6
- * import { getOauthToken } from "@kash.88/alerts";
7
- *
8
- * // The "code" is received after the user authorizes your application
9
- * // via the link generated by "getAuthorizeLink".
10
- *
11
- * try {
12
- * const tokenData = await getOauthToken({
13
- * client_id: "YOUR_CLIENT_ID",
14
- * client_secret: "YOUR_CLIENT_SECRET",
15
- * code: "AUTHORIZATION_CODE"
16
- * });
17
- *
18
- * console.log(tokenData.access_token);
19
- * } catch (error) {
20
- * console.error("Error getting Oauth token:", error.response.data);
21
- * }
22
- *
23
- * @param {GetOauthData} data - The data for the token request.
24
- * @returns {Promise<OauthToken>} A promise that resolves to the token data from the API.
25
- * @see {@link https://www.donationalerts.com/apidoc#authorization__authorization_code__getting_access_token}
26
- */
27
- export default function getOauthToken(data: GetOauthData): Promise<OauthToken>;
@@ -1,42 +0,0 @@
1
- import axios from "axios";
2
- import { validateDataObject } from "../utils.js";
3
- /**
4
- * Exchanges an authorization code for an access token.
5
- *
6
- * @example
7
- * import { getOauthToken } from "@kash.88/alerts";
8
- *
9
- * // The "code" is received after the user authorizes your application
10
- * // via the link generated by "getAuthorizeLink".
11
- *
12
- * try {
13
- * const tokenData = await getOauthToken({
14
- * client_id: "YOUR_CLIENT_ID",
15
- * client_secret: "YOUR_CLIENT_SECRET",
16
- * code: "AUTHORIZATION_CODE"
17
- * });
18
- *
19
- * console.log(tokenData.access_token);
20
- * } catch (error) {
21
- * console.error("Error getting Oauth token:", error.response.data);
22
- * }
23
- *
24
- * @param {GetOauthData} data - The data for the token request.
25
- * @returns {Promise<OauthToken>} A promise that resolves to the token data from the API.
26
- * @see {@link https://www.donationalerts.com/apidoc#authorization__authorization_code__getting_access_token}
27
- */
28
- export default async function getOauthToken(data) {
29
- try {
30
- validateDataObject(data, ["client_id", "client_secret", "code"]);
31
- const response = await axios.post("https://www.donationalerts.com/oauth/token", {
32
- grant_type: "authorization_code",
33
- client_id: data.client_id,
34
- client_secret: data.client_secret,
35
- code: data.code
36
- });
37
- return response.data;
38
- }
39
- catch (error) {
40
- throw new Error(error?.response?.data?.error_description || error?.message || error);
41
- }
42
- }
@@ -1,29 +0,0 @@
1
- import { GetPrivateToken } from "../types.js";
2
- /**
3
- * Gets a private token for subscribing to a DonationAlerts channel via Centrifuge.
4
- *
5
- * @example
6
- * import { connectPrivateToken } from "../index.js";
7
- *
8
- * // The "channel" can be obtained using the "getUserChannel" function,
9
- * // and the "uuidv4_client_id" is provided during authentication in WebSocket (ws).
10
- *
11
- * try {
12
- * const token = await connectPrivateToken({
13
- * channel: "USER_CHANNEL",
14
- * uuidv4_client_id: "USER_uuid_ID",
15
- * access_token: "USER_ACCESS_TOKEN"
16
- * });
17
- *
18
- * console.log(token);
19
- * } catch (error) {
20
- * console.error("Error getting Oauth token:", error.response.data);
21
- * }
22
- *
23
- * @param {Object} params - Parameters for getting the token.
24
- * @param {string} params.channel - Channel name to subscribe.
25
- * @param {string} params.uuidv4_client_id - UUID v4 client ID.
26
- * @param {string} params.access_token - OAuth access token of the user.
27
- * @returns {Promise<string>} - Token for channel subscription.
28
- */
29
- export declare function getPrivateToken(data: GetPrivateToken): Promise<string>;
@@ -1,46 +0,0 @@
1
- import axios from "axios";
2
- import { validateDataObject } from "../utils.js";
3
- /**
4
- * Gets a private token for subscribing to a DonationAlerts channel via Centrifuge.
5
- *
6
- * @example
7
- * import { connectPrivateToken } from "../index.js";
8
- *
9
- * // The "channel" can be obtained using the "getUserChannel" function,
10
- * // and the "uuidv4_client_id" is provided during authentication in WebSocket (ws).
11
- *
12
- * try {
13
- * const token = await connectPrivateToken({
14
- * channel: "USER_CHANNEL",
15
- * uuidv4_client_id: "USER_uuid_ID",
16
- * access_token: "USER_ACCESS_TOKEN"
17
- * });
18
- *
19
- * console.log(token);
20
- * } catch (error) {
21
- * console.error("Error getting Oauth token:", error.response.data);
22
- * }
23
- *
24
- * @param {Object} params - Parameters for getting the token.
25
- * @param {string} params.channel - Channel name to subscribe.
26
- * @param {string} params.uuidv4_client_id - UUID v4 client ID.
27
- * @param {string} params.access_token - OAuth access token of the user.
28
- * @returns {Promise<string>} - Token for channel subscription.
29
- */
30
- export async function getPrivateToken(data) {
31
- try {
32
- validateDataObject(data, ["channel", "uuidv4_client_id", "access_token"]);
33
- const response = await axios.post("https://www.donationalerts.com/api/v1/centrifuge/subscribe", {
34
- channels: [data.channel],
35
- client: data.uuidv4_client_id
36
- }, {
37
- headers: {
38
- "Authorization": `Bearer ${data.access_token}`
39
- }
40
- });
41
- return response.data.channels[0].token;
42
- }
43
- catch (error) {
44
- throw new Error(error?.response?.data?.error_description || error?.message || error);
45
- }
46
- }
@@ -1,23 +0,0 @@
1
- import { User } from "../types.js";
2
- /**
3
- * Fetches the profile information for the authenticated user.
4
- *
5
- * @example
6
- * import { getUser } from "@kash.88/alerts";
7
- *
8
- * // Get an access_token using getOauthToken or updateAccessToken
9
- *
10
- * try {
11
- * const accessToken = "USER_ACCESS_TOKEN";
12
- * const user = await getUser(accessToken);
13
- *
14
- * console.log(user);
15
- * } catch (error) {
16
- * console.error("Error fetching user:", error.response.data);
17
- * }
18
- *
19
- * @param {string} access_token - The access token for authentication.
20
- * @returns {Promise<User>} A promise that resolves to the user profile data.
21
- * @see {@link https://www.donationalerts.com/apidoc#api_v1__users__user_profile_information}
22
- */
23
- export default function getUser(access_token: string): Promise<User>;
@@ -1,38 +0,0 @@
1
- import axios from "axios";
2
- /**
3
- * Fetches the profile information for the authenticated user.
4
- *
5
- * @example
6
- * import { getUser } from "@kash.88/alerts";
7
- *
8
- * // Get an access_token using getOauthToken or updateAccessToken
9
- *
10
- * try {
11
- * const accessToken = "USER_ACCESS_TOKEN";
12
- * const user = await getUser(accessToken);
13
- *
14
- * console.log(user);
15
- * } catch (error) {
16
- * console.error("Error fetching user:", error.response.data);
17
- * }
18
- *
19
- * @param {string} access_token - The access token for authentication.
20
- * @returns {Promise<User>} A promise that resolves to the user profile data.
21
- * @see {@link https://www.donationalerts.com/apidoc#api_v1__users__user_profile_information}
22
- */
23
- export default async function getUser(access_token) {
24
- try {
25
- if (!access_token) {
26
- throw new Error("You must provide \"access_token\" as a non-empty string.");
27
- }
28
- const response = await axios.get("https://www.donationalerts.com/api/v1/user/oauth", {
29
- headers: {
30
- Authorization: `Bearer ${access_token}`
31
- }
32
- });
33
- return response.data.data;
34
- }
35
- catch (error) {
36
- throw new Error(error?.response?.data?.error_description || error?.message || error);
37
- }
38
- }
@@ -1,19 +0,0 @@
1
- /**
2
- * Fetches the profile information for the authenticated user.
3
- *
4
- * @example
5
- * import { getUserChannel } from "@kash.88/alerts";
6
- *
7
- * // The "id" can be obtained using the "getUser" function,
8
- *
9
- * try {
10
- * const userId = "USER_ID";
11
- * const channelId = getUserChannel(userId);
12
- * } catch (error) {
13
- * console.error("Error generating channel ID:", error.message);
14
- * }
15
- *
16
- * @param {string|number} id - The user ID for which to generate the channel identifier.
17
- * @returns {string} The donation alert channel identifier in the format "$alerts:donation_{id}".
18
- */
19
- export default function getUserChannel(id: string | number): string;
@@ -1,32 +0,0 @@
1
- /**
2
- * Fetches the profile information for the authenticated user.
3
- *
4
- * @example
5
- * import { getUserChannel } from "@kash.88/alerts";
6
- *
7
- * // The "id" can be obtained using the "getUser" function,
8
- *
9
- * try {
10
- * const userId = "USER_ID";
11
- * const channelId = getUserChannel(userId);
12
- * } catch (error) {
13
- * console.error("Error generating channel ID:", error.message);
14
- * }
15
- *
16
- * @param {string|number} id - The user ID for which to generate the channel identifier.
17
- * @returns {string} The donation alert channel identifier in the format "$alerts:donation_{id}".
18
- */
19
- export default function getUserChannel(id) {
20
- try {
21
- if (typeof id !== "string" && typeof id !== "number") {
22
- throw new Error("You must provide \"id\" as a string or number.");
23
- }
24
- if (id === null || id === undefined || id === "") {
25
- throw new Error("You must provide a non-empty \"id\".");
26
- }
27
- return `$alerts:donation_${id}`;
28
- }
29
- catch (error) {
30
- throw new Error(error?.response?.data?.error_description || error?.message || error);
31
- }
32
- }
@@ -1,24 +0,0 @@
1
- import { UpdateTokenData, OauthToken } from "../types.js";
2
- /**
3
- * Refreshes an access token using a refresh token.
4
- *
5
- * @example
6
- * import { updateAccessToken } from "@kash.88/alerts";
7
- *
8
- * try {
9
- * const tokenData = await updateAccessToken({
10
- * client_id: "YOUR_CLIENT_ID",
11
- * client_secret: "YOUR_CLIENT_SECRET",
12
- * refresh_token: "USER_REFRESH_TOKEN"
13
- * });
14
- *
15
- * console.log(tokenData.access_token);
16
- * } catch (error) {
17
- * console.error("Error updating access token:", error.response.data);
18
- * }
19
- *
20
- * @param {UpdateTokenData} data - The data for the token refresh request.
21
- * @returns {Promise<OauthToken>} A promise that resolves to the new token data from the API.
22
- * @see {@link https://www.donationalerts.com/apidoc#authorization__authorization_code__getting_access_token}
23
- */
24
- export default function updateAccessToken(data: UpdateTokenData): Promise<OauthToken>;
@@ -1,39 +0,0 @@
1
- import axios from "axios";
2
- import { validateDataObject } from "../utils.js";
3
- /**
4
- * Refreshes an access token using a refresh token.
5
- *
6
- * @example
7
- * import { updateAccessToken } from "@kash.88/alerts";
8
- *
9
- * try {
10
- * const tokenData = await updateAccessToken({
11
- * client_id: "YOUR_CLIENT_ID",
12
- * client_secret: "YOUR_CLIENT_SECRET",
13
- * refresh_token: "USER_REFRESH_TOKEN"
14
- * });
15
- *
16
- * console.log(tokenData.access_token);
17
- * } catch (error) {
18
- * console.error("Error updating access token:", error.response.data);
19
- * }
20
- *
21
- * @param {UpdateTokenData} data - The data for the token refresh request.
22
- * @returns {Promise<OauthToken>} A promise that resolves to the new token data from the API.
23
- * @see {@link https://www.donationalerts.com/apidoc#authorization__authorization_code__getting_access_token}
24
- */
25
- export default async function updateAccessToken(data) {
26
- try {
27
- validateDataObject(data, ["client_id", "client_secret", "refresh_token"]);
28
- const response = await axios.post("https://www.donationalerts.com/oauth/token", {
29
- grant_type: "refresh_token",
30
- client_id: data.client_id,
31
- client_secret: data.client_secret,
32
- refresh_token: data.refresh_token
33
- });
34
- return response.data;
35
- }
36
- catch (error) {
37
- throw new Error(error?.response?.data?.error_description || error?.message || error);
38
- }
39
- }
@@ -1,33 +0,0 @@
1
- export interface OauthToken {
2
- token_type: string;
3
- expires_in: number;
4
- access_token: string;
5
- refresh_token: string;
6
- }
7
- export interface GetPrivateToken {
8
- channel: string;
9
- uuidv4_client_id: string;
10
- access_token: string;
11
- }
12
- export interface User {
13
- id: number;
14
- code: string;
15
- name: string;
16
- avatar: string;
17
- email: string;
18
- socket_connection_token: string;
19
- }
20
- export interface GetAuthorizeLinkData {
21
- client_id: string | number;
22
- scope: string[];
23
- }
24
- export interface GetOauthData {
25
- client_id: string | number;
26
- client_secret: string;
27
- code: string;
28
- }
29
- export interface UpdateTokenData {
30
- client_id: string | number;
31
- client_secret: string;
32
- refresh_token: string;
33
- }
@@ -1,31 +0,0 @@
1
- import { WebSocket } from "ws";
2
- import TypedEmitter from "typed-emitter";
3
- interface WSClientOptions {
4
- channel: string;
5
- socket_connection_token: string;
6
- access_token: string;
7
- }
8
- interface CentrifugeMessage {
9
- params: Record<string, unknown>;
10
- id: number;
11
- method?: number;
12
- }
13
- type MessageEvents = {
14
- open: () => void;
15
- message: (data: CentrifugeMessage) => void;
16
- close: () => void;
17
- error: (error: Error) => void;
18
- reconnecting: (attempt: number) => void;
19
- reconnect_failed: () => void;
20
- };
21
- declare const CentrifugeClient_base: new () => TypedEmitter<MessageEvents>;
22
- export default class CentrifugeClient extends CentrifugeClient_base {
23
- private options;
24
- private ws;
25
- constructor(options: WSClientOptions);
26
- createConnection(): WebSocket;
27
- confirmConnection(socket_connection_token?: string): void;
28
- connectPrivateToken(channel: string, uuidv4_client_id: string, access_token?: string): Promise<void>;
29
- sendMessage(message: string): void;
30
- }
31
- export {};
@@ -1,71 +0,0 @@
1
- import { getPrivateToken } from "../index.js";
2
- import { WebSocket } from "ws";
3
- import { EventEmitter } from "events";
4
- // --- Configuration ---
5
- const configuration = {
6
- ws: {
7
- url: "wss://centrifugo.donationalerts.com/connection/websocket"
8
- }
9
- };
10
- // --- Class ---
11
- export default class CentrifugeClient extends EventEmitter {
12
- constructor(options) {
13
- super();
14
- this.ws = null;
15
- this.options = options;
16
- this.ws;
17
- }
18
- createConnection() {
19
- if (this.ws) {
20
- return this.ws;
21
- }
22
- try {
23
- this.ws = new WebSocket(configuration.ws.url);
24
- return this.ws;
25
- }
26
- catch (error) {
27
- console.error("[WS] Failed to create connection:", error);
28
- throw error;
29
- }
30
- }
31
- confirmConnection(socket_connection_token = this.options.socket_connection_token) {
32
- const message = {
33
- params: {
34
- token: socket_connection_token
35
- },
36
- id: 1
37
- };
38
- this.sendMessage(JSON.stringify(message));
39
- }
40
- async connectPrivateToken(channel, uuidv4_client_id, access_token = this.options.access_token) {
41
- try {
42
- const token = await getPrivateToken({
43
- channel,
44
- uuidv4_client_id,
45
- access_token
46
- });
47
- const subscribeMessage = {
48
- params: {
49
- channel: channel,
50
- token
51
- },
52
- method: 1,
53
- id: 2
54
- };
55
- this.sendMessage(JSON.stringify(subscribeMessage));
56
- }
57
- catch (error) {
58
- throw new Error(error);
59
- }
60
- }
61
- sendMessage(message) {
62
- if (!this.ws)
63
- return;
64
- try {
65
- this.ws.send(message);
66
- }
67
- catch (error) {
68
- throw new Error(error);
69
- }
70
- }
71
- }
@@ -1,11 +0,0 @@
1
- import { getAuthorizeLink } from "@kash-88/alerts";
2
-
3
- const client_id = "YOUR_CLIENT_ID"; // Get on https://www.donationalerts.com/application/clients
4
- const scope = ["oauth-user-show", "oauth-donation-subscribe", "oauth-donation-index", "oauth-custom_alert-store", "oauth-goal-subscribe", "oauth-poll-subscribe"];
5
-
6
- try {
7
- const link = getAuthorizeLink({ client_id, scope });
8
- console.log("Authorize link:", link);
9
- } catch (error: any) {
10
- console.error("Error:", error.message);
11
- }
@@ -1,16 +0,0 @@
1
- import "dotenv/config";
2
- import { getOauthToken } from "@kash-88/alerts";
3
-
4
- // Get on https://www.donationalerts.com/application/clients
5
- const client_id = "YOUR_CLIENT_ID";
6
- const client_secret = process.env.CLIENT_SECRET!;
7
- const code = "CLIENT_CODE";
8
-
9
- (async () => {
10
- try {
11
- const token = await getOauthToken({ client_id, client_secret, code });
12
- console.log("Oauth token:", token);
13
- } catch (error: any) {
14
- console.error("Error:", error.message);
15
- }
16
- })();
@@ -1,12 +0,0 @@
1
- import { getUser } from "@kash-88/alerts";
2
-
3
- const access_token = 'USER_ACCESS_TOKEN';
4
-
5
- (async () => {
6
- try {
7
- const user = await getUser(access_token);
8
- console.log("User:", user);
9
- } catch (error: any) {
10
- console.error("Error:", error.message);
11
- }
12
- })();