@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
@@ -1,10 +0,0 @@
1
- import { getUserChannel } from "@kash-88/alerts";
2
-
3
- const user_id = "USER_ID";
4
-
5
- try {
6
- const channel = getUserChannel(user_id);
7
- console.log("User channel:", channel);
8
- } catch (error: any) {
9
- console.error("Error:", error.message);
10
- }
@@ -1,17 +0,0 @@
1
- import "dotenv/config";
2
- import { updateAccessToken } 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
-
8
- const refresh_token = "USER_REFRESH_TOKEN";
9
-
10
- (async () => {
11
- try {
12
- const token = await updateAccessToken({ client_id, client_secret, refresh_token });
13
- console.log("Updated token:", token);
14
- } catch (error: any) {
15
- console.error("Error:", error.message);
16
- }
17
- })();
@@ -1,50 +0,0 @@
1
- import "dotenv/config";
2
-
3
- import { getUser, getUserChannel, CentrifugeClient } from "@kash-88/alerts";
4
-
5
- const access_token = process.env.ACCESS_TOKEN!;
6
- let isConnectToPrivate = false;
7
-
8
- async function main() {
9
- try {
10
- const user = await getUser(access_token);
11
- const channel = getUserChannel(user.id);
12
- const socket_connection_token = String(process.env.SOCKET_CONNECTION_TOKEN);
13
-
14
- const client = new CentrifugeClient({
15
- channel,
16
- socket_connection_token,
17
- access_token
18
- });
19
-
20
- const ws = client.createConnection();
21
-
22
- ws.on("open", async () => {
23
- console.log("WebSocket соединение открыто");
24
- client.confirmConnection(socket_connection_token);
25
- });
26
-
27
- ws.on("message", (message) => {
28
- const srt = message.toString("utf8");
29
- const json = JSON.parse(srt);
30
-
31
- if(json.id = 1 && !isConnectToPrivate) {
32
- isConnectToPrivate = true; return client.connectPrivateToken(channel, json.result.client);
33
- }
34
-
35
- console.log(srt);
36
- });
37
-
38
- ws.on("close", () => {
39
- console.log("WebSocket соединение закрыто");
40
- });
41
-
42
- ws.on("error", (err) => {
43
- console.error("Ошибка WebSocket:", err);
44
- });
45
- } catch (error: any) {
46
- console.error("Ошибка:", error.message);
47
- }
48
- }
49
-
50
- main();
@@ -1,33 +0,0 @@
1
- import { validateDataObject } from "@utils";
2
- import { GetAuthorizeLinkData } from "@type";
3
-
4
- /**
5
- * Generates an authorization link for the DonationAlerts API.
6
- *
7
- * @example
8
- * import { getAuthorizeLink } from "@kash.88/alerts";
9
- *
10
- * const authLink = getAuthorizeLink({
11
- * client_id: "YOUR_CLIENT_ID",
12
- * scope: ["oauth-user-show"]
13
- * });
14
- * console.log(authLink);
15
- *
16
- * @param {GetAuthorizeLinkData} data - The data for generating the link.
17
- * @returns {string} The authorization URL.
18
- * @see {@link https://www.donationalerts.com/apidoc#authorization__authorization_code__authorization_request}
19
- */
20
-
21
- export default function getAuthorizeLink(data: GetAuthorizeLinkData): string {
22
- try {
23
- validateDataObject(data, ["client_id", "scope"]);
24
-
25
- if (!Array.isArray(data.scope)) {
26
- throw new Error("You must provide \"scope\" as an array in the data object.");
27
- }
28
-
29
- return `https://www.donationalerts.com/oauth/authorize?client_id=${data.client_id}&response_type=code&scope=${data.scope.join("%20")}`;
30
- } catch (error: any) {
31
- throw new Error(error?.response?.data?.error_description || error?.message || error);
32
- }
33
- }
@@ -1,46 +0,0 @@
1
- import axios from "axios";
2
- import { validateDataObject } from "@utils";
3
- import { GetOauthData, OauthToken } from "@type";
4
-
5
- /**
6
- * Exchanges an authorization code for an access token.
7
- *
8
- * @example
9
- * import { getOauthToken } from "@kash.88/alerts";
10
- *
11
- * // The "code" is received after the user authorizes your application
12
- * // via the link generated by "getAuthorizeLink".
13
- *
14
- * try {
15
- * const tokenData = await getOauthToken({
16
- * client_id: "YOUR_CLIENT_ID",
17
- * client_secret: "YOUR_CLIENT_SECRET",
18
- * code: "AUTHORIZATION_CODE"
19
- * });
20
- *
21
- * console.log(tokenData.access_token);
22
- * } catch (error) {
23
- * console.error("Error getting Oauth token:", error.response.data);
24
- * }
25
- *
26
- * @param {GetOauthData} data - The data for the token request.
27
- * @returns {Promise<OauthToken>} A promise that resolves to the token data from the API.
28
- * @see {@link https://www.donationalerts.com/apidoc#authorization__authorization_code__getting_access_token}
29
- */
30
-
31
- export default async function getOauthToken(data: GetOauthData): Promise<OauthToken> {
32
- try {
33
- validateDataObject(data, ["client_id", "client_secret", "code"]);
34
-
35
- const response = await axios.post<OauthToken>("https://www.donationalerts.com/oauth/token", {
36
- grant_type: "authorization_code",
37
- client_id: data.client_id,
38
- client_secret: data.client_secret,
39
- code: data.code
40
- });
41
-
42
- return response.data;
43
- } catch (error: any) {
44
- throw new Error(error?.response?.data?.error_description || error?.message || error);
45
- }
46
- }
@@ -1,53 +0,0 @@
1
- import axios from "axios";
2
- import { GetPrivateToken } from "@type";
3
- import { validateDataObject } from "@utils";
4
-
5
- /**
6
- * Gets a private token for subscribing to a DonationAlerts channel via Centrifuge.
7
- *
8
- * @example
9
- * import { connectPrivateToken } from "@kash-88/alerts";
10
- *
11
- * // The "channel" can be obtained using the "getUserChannel" function,
12
- * // and the "uuidv4_client_id" is provided during authentication in WebSocket (ws).
13
- *
14
- * try {
15
- * const token = await connectPrivateToken({
16
- * channel: "USER_CHANNEL",
17
- * uuidv4_client_id: "USER_uuid_ID",
18
- * access_token: "USER_ACCESS_TOKEN"
19
- * });
20
- *
21
- * console.log(token);
22
- * } catch (error) {
23
- * console.error("Error getting Oauth token:", error.response.data);
24
- * }
25
- *
26
- * @param {Object} params - Parameters for getting the token.
27
- * @param {string} params.channel - Channel name to subscribe.
28
- * @param {string} params.uuidv4_client_id - UUID v4 client ID.
29
- * @param {string} params.access_token - OAuth access token of the user.
30
- * @returns {Promise<string>} - Token for channel subscription.
31
- */
32
-
33
- export async function getPrivateToken(data: GetPrivateToken): Promise<string> {
34
- try {
35
- validateDataObject(data, ["channel", "uuidv4_client_id", "access_token"]);
36
-
37
- const response = await axios.post(
38
- "https://www.donationalerts.com/api/v1/centrifuge/subscribe",
39
- {
40
- channels: [data.channel],
41
- client: data.uuidv4_client_id
42
- },
43
- {
44
- headers: {
45
- "Authorization": `Bearer ${data.access_token}`
46
- }
47
- }
48
- );
49
- return response.data.channels[0].token;
50
- } catch (error: any) {
51
- throw new Error(error?.response?.data?.error_description || error?.message || error);
52
- }
53
- }
@@ -1,42 +0,0 @@
1
- import axios from "axios";
2
- import { User } from "@type";
3
-
4
- /**
5
- * Fetches the profile information for the authenticated user.
6
- *
7
- * @example
8
- * import { getUser } from "@kash.88/alerts";
9
- *
10
- * // Get an access_token using getOauthToken or updateAccessToken
11
- *
12
- * try {
13
- * const accessToken = "USER_ACCESS_TOKEN";
14
- * const user = await getUser(accessToken);
15
- *
16
- * console.log(user);
17
- * } catch (error) {
18
- * console.error("Error fetching user:", error.response.data);
19
- * }
20
- *
21
- * @param {string} access_token - The access token for authentication.
22
- * @returns {Promise<User>} A promise that resolves to the user profile data.
23
- * @see {@link https://www.donationalerts.com/apidoc#api_v1__users__user_profile_information}
24
- */
25
-
26
- export default async function getUser(access_token: string): Promise<User> {
27
- try {
28
- if (!access_token) {
29
- throw new Error("You must provide \"access_token\" as a non-empty string.");
30
- }
31
-
32
- const response = await axios.get<{ data: User }>("https://www.donationalerts.com/api/v1/user/oauth", {
33
- headers: {
34
- Authorization: `Bearer ${access_token}`
35
- }
36
- });
37
-
38
- return response.data.data;
39
- } catch (error: any) {
40
- throw new Error(error?.response?.data?.error_description || error?.message || error);
41
- }
42
- }
@@ -1,34 +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
-
20
- export default function getUserChannel(id: string | number): string {
21
- try {
22
- if (typeof id !== "string" && typeof id !== "number") {
23
- throw new Error("You must provide \"id\" as a string or number.");
24
- }
25
-
26
- if (id === null || id === undefined || id === "") {
27
- throw new Error("You must provide a non-empty \"id\".");
28
- }
29
-
30
- return `$alerts:donation_${id}`;
31
- } catch (error: any) {
32
- throw new Error(error?.response?.data?.error_description || error?.message || error);
33
- }
34
- }
@@ -1,43 +0,0 @@
1
- import axios from "axios";
2
- import { validateDataObject } from "@utils";
3
- import { UpdateTokenData, OauthToken } from "@type";
4
-
5
- /**
6
- * Refreshes an access token using a refresh token.
7
- *
8
- * @example
9
- * import { updateAccessToken } from "@kash.88/alerts";
10
- *
11
- * try {
12
- * const tokenData = await updateAccessToken({
13
- * client_id: "YOUR_CLIENT_ID",
14
- * client_secret: "YOUR_CLIENT_SECRET",
15
- * refresh_token: "USER_REFRESH_TOKEN"
16
- * });
17
- *
18
- * console.log(tokenData.access_token);
19
- * } catch (error) {
20
- * console.error("Error updating access token:", error.response.data);
21
- * }
22
- *
23
- * @param {UpdateTokenData} data - The data for the token refresh request.
24
- * @returns {Promise<OauthToken>} A promise that resolves to the new token data from the API.
25
- * @see {@link https://www.donationalerts.com/apidoc#authorization__authorization_code__getting_access_token}
26
- */
27
-
28
- export default async function updateAccessToken(data: UpdateTokenData): Promise<OauthToken> {
29
- try {
30
- validateDataObject(data, ["client_id", "client_secret", "refresh_token"]);
31
-
32
- const response = await axios.post<OauthToken>("https://www.donationalerts.com/oauth/token", {
33
- grant_type: "refresh_token",
34
- client_id: data.client_id,
35
- client_secret: data.client_secret,
36
- refresh_token: data.refresh_token
37
- });
38
-
39
- return response.data;
40
- } catch (error: any) {
41
- throw new Error(error?.response?.data?.error_description || error?.message || error);
42
- }
43
- }
package/src/types.ts DELETED
@@ -1,38 +0,0 @@
1
- export interface OauthToken {
2
- token_type: string;
3
- expires_in: number;
4
- access_token: string;
5
- refresh_token: string;
6
- }
7
-
8
- export interface GetPrivateToken {
9
- channel: string;
10
- uuidv4_client_id: string;
11
- access_token: string;
12
- }
13
-
14
- export interface User {
15
- id: number;
16
- code: string;
17
- name: string;
18
- avatar: string;
19
- email: string;
20
- socket_connection_token: string;
21
- }
22
-
23
- export interface GetAuthorizeLinkData {
24
- client_id: string | number;
25
- scope: string[];
26
- }
27
-
28
- export interface GetOauthData {
29
- client_id: string | number;
30
- client_secret: string;
31
- code: string;
32
- }
33
-
34
- export interface UpdateTokenData {
35
- client_id: string | number;
36
- client_secret: string;
37
- refresh_token: string;
38
- }
@@ -1,108 +0,0 @@
1
- import { getPrivateToken } from "@kash-88/alerts";
2
- import { WebSocket } from "ws";
3
- import { EventEmitter } from "events";
4
- import TypedEmitter from "typed-emitter";
5
-
6
- // --- Interfaces ---
7
- interface CentrifugeConfiguration {
8
- ws: {
9
- url: string;
10
- };
11
- }
12
-
13
- interface WSClientOptions {
14
- channel: string;
15
- socket_connection_token: string;
16
- access_token: string;
17
- }
18
-
19
- interface CentrifugeMessage {
20
- params: Record<string, unknown>;
21
- id: number;
22
- method?: number;
23
- }
24
-
25
- // --- Configuration ---
26
- const configuration: CentrifugeConfiguration = {
27
- ws: {
28
- url: "wss://centrifugo.donationalerts.com/connection/websocket"
29
- }
30
- };
31
-
32
- type MessageEvents = {
33
- open: () => void;
34
- message: (data: CentrifugeMessage) => void;
35
- close: () => void;
36
- error: (error: Error) => void;
37
- reconnecting: (attempt: number) => void;
38
- reconnect_failed: () => void;
39
- }
40
-
41
- // --- Class ---
42
- export default class CentrifugeClient extends (EventEmitter as new () => TypedEmitter<MessageEvents>) {
43
- private options: WSClientOptions;
44
- private ws: WebSocket | null = null;
45
-
46
- constructor(options: WSClientOptions) {
47
- super();
48
- this.options = options; this.ws;
49
- }
50
-
51
- public createConnection(): WebSocket {
52
- if (this.ws) {
53
- return this.ws;
54
- }
55
-
56
- try {
57
- this.ws = new WebSocket(configuration.ws.url);
58
-
59
- return this.ws;
60
- } catch (error) {
61
- console.error("[WS] Failed to create connection:", error);
62
- throw error;
63
- }
64
- }
65
-
66
- public confirmConnection(socket_connection_token: string = this.options.socket_connection_token): void {
67
- const message: CentrifugeMessage = {
68
- params: {
69
- token: socket_connection_token
70
- },
71
- id: 1
72
- };
73
- this.sendMessage(JSON.stringify(message));
74
- }
75
-
76
- public async connectPrivateToken(channel: string, uuidv4_client_id: string, access_token: string = this.options.access_token): Promise<void> {
77
- try {
78
- const token = await getPrivateToken({
79
- channel,
80
- uuidv4_client_id,
81
- access_token
82
- });
83
-
84
- const subscribeMessage: CentrifugeMessage = {
85
- params: {
86
- channel: channel,
87
- token
88
- },
89
- method: 1,
90
- id: 2
91
- };
92
-
93
- this.sendMessage(JSON.stringify(subscribeMessage));
94
- } catch (error: any) {
95
- throw new Error(error);
96
- }
97
- }
98
-
99
- public sendMessage(message: string): void {
100
- if(!this.ws) return;
101
-
102
- try {
103
- this.ws.send(message);
104
- } catch (error: any) {
105
- throw new Error(error);
106
- }
107
- }
108
- }
File without changes