@kash-88/alerts 1.0.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.
- package/dist/example/getAuthorizeLink.d.ts +1 -0
- package/dist/example/getAuthorizeLink.js +13 -0
- package/dist/example/getOauthToken.d.ts +1 -0
- package/dist/example/getOauthToken.js +16 -0
- package/dist/example/getUser.d.ts +1 -0
- package/dist/example/getUser.js +14 -0
- package/dist/example/getUserChannel.d.ts +1 -0
- package/dist/example/getUserChannel.js +12 -0
- package/dist/example/updateAccessToken.d.ts +1 -0
- package/dist/example/updateAccessToken.js +16 -0
- package/dist/example/wsExample.d.ts +1 -0
- package/dist/example/wsExample.js +42 -0
- package/dist/src/example/getAuthorizeLink.d.ts +1 -0
- package/dist/src/example/getAuthorizeLink.js +13 -0
- package/dist/src/example/getOauthToken.d.ts +1 -0
- package/dist/src/example/getOauthToken.js +16 -0
- package/dist/src/example/getUser.d.ts +1 -0
- package/dist/src/example/getUser.js +14 -0
- package/dist/src/example/getUserChannel.d.ts +1 -0
- package/dist/src/example/getUserChannel.js +12 -0
- package/dist/src/example/updateAccessToken.d.ts +1 -0
- package/dist/src/example/updateAccessToken.js +16 -0
- package/dist/src/example/wsExample.d.ts +1 -0
- package/dist/src/example/wsExample.js +42 -0
- package/dist/src/func/getAuthorizeLink.d.ts +18 -0
- package/dist/src/func/getAuthorizeLink.js +29 -0
- package/dist/src/func/getOauthToken.d.ts +27 -0
- package/dist/src/func/getOauthToken.js +42 -0
- package/dist/src/func/getPrivateToken.d.ts +29 -0
- package/dist/src/func/getPrivateToken.js +46 -0
- package/dist/src/func/getUser.d.ts +23 -0
- package/dist/src/func/getUser.js +38 -0
- package/dist/src/func/getUserChannel.d.ts +19 -0
- package/dist/src/func/getUserChannel.js +32 -0
- package/dist/src/func/updateAccessToken.d.ts +24 -0
- package/dist/src/func/updateAccessToken.js +39 -0
- package/dist/src/index.d.ts +9 -0
- package/dist/src/index.js +9 -0
- package/dist/src/types.d.ts +33 -0
- package/dist/src/types.js +1 -0
- package/dist/src/utils.d.ts +21 -0
- package/dist/src/utils.js +48 -0
- package/dist/src/ws/CentrifugeClient.d.ts +31 -0
- package/dist/src/ws/CentrifugeClient.js +71 -0
- package/package.json +59 -0
- package/readme.md +82 -0
- package/src/example/getAuthorizeLink.ts +15 -0
- package/src/example/getOauthToken.ts +18 -0
- package/src/example/getUser.ts +16 -0
- package/src/example/getUserChannel.ts +14 -0
- package/src/example/updateAccessToken.ts +18 -0
- package/src/example/wsExample.ts +52 -0
- package/src/func/getAuthorizeLink.ts +33 -0
- package/src/func/getOauthToken.ts +46 -0
- package/src/func/getPrivateToken.ts +53 -0
- package/src/func/getUser.ts +42 -0
- package/src/func/getUserChannel.ts +34 -0
- package/src/func/updateAccessToken.ts +43 -0
- package/src/index.ts +19 -0
- package/src/types.ts +38 -0
- package/src/utils.ts +56 -0
- package/src/ws/CentrifugeClient.ts +108 -0
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import axios from "axios";
|
|
2
|
+
import { validateDataObject } from "../utils";
|
|
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
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import getAuthorizeLink from "./func/getAuthorizeLink";
|
|
2
|
+
import getOauthToken from "./func/getOauthToken";
|
|
3
|
+
import getUser from "./func/getUser";
|
|
4
|
+
import updateAccessToken from "./func/updateAccessToken";
|
|
5
|
+
import getUserChannel from "./func/getUserChannel";
|
|
6
|
+
import { getPrivateToken } from "./func/getPrivateToken";
|
|
7
|
+
import CentrifugeClient from "./ws/CentrifugeClient";
|
|
8
|
+
export * from "./types";
|
|
9
|
+
export { getAuthorizeLink, getOauthToken, getUser, updateAccessToken, getUserChannel, getPrivateToken, CentrifugeClient };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import getAuthorizeLink from "./func/getAuthorizeLink";
|
|
2
|
+
import getOauthToken from "./func/getOauthToken";
|
|
3
|
+
import getUser from "./func/getUser";
|
|
4
|
+
import updateAccessToken from "./func/updateAccessToken";
|
|
5
|
+
import getUserChannel from "./func/getUserChannel";
|
|
6
|
+
import { getPrivateToken } from "./func/getPrivateToken";
|
|
7
|
+
import CentrifugeClient from "./ws/CentrifugeClient";
|
|
8
|
+
export * from "./types";
|
|
9
|
+
export { getAuthorizeLink, getOauthToken, getUser, updateAccessToken, getUserChannel, getPrivateToken, CentrifugeClient };
|
|
@@ -0,0 +1,33 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,21 @@
|
|
|
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
|
+
* Validates that the provided data is an object and contains the required keys.
|
|
9
|
+
* For each required key, it also validates that the value is a string or a number.
|
|
10
|
+
* @param {unknown} data - The data object to validate.
|
|
11
|
+
* @param {string[]} requiredKeys - An array of keys that must be present in the data object.
|
|
12
|
+
* @throws {Error} If validation fails.
|
|
13
|
+
* @private
|
|
14
|
+
*/
|
|
15
|
+
export declare function validateDataObject(data: unknown, requiredKeys: string[]): asserts data is Record<string, string | number | string[]>;
|
|
16
|
+
/**
|
|
17
|
+
* Checks if required environment variables are present.
|
|
18
|
+
* Throws an error with a clear message if any variable is missing.
|
|
19
|
+
* @param {string[]} keys - List of required environment variable names.
|
|
20
|
+
*/
|
|
21
|
+
export declare function checkEnv(keys: string[]): void;
|
|
@@ -0,0 +1,48 @@
|
|
|
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
|
+
function isObject(value) {
|
|
8
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Validates that the provided data is an object and contains the required keys.
|
|
12
|
+
* For each required key, it also validates that the value is a string or a number.
|
|
13
|
+
* @param {unknown} data - The data object to validate.
|
|
14
|
+
* @param {string[]} requiredKeys - An array of keys that must be present in the data object.
|
|
15
|
+
* @throws {Error} If validation fails.
|
|
16
|
+
* @private
|
|
17
|
+
*/
|
|
18
|
+
export function validateDataObject(data, requiredKeys) {
|
|
19
|
+
if (!isObject(data)) {
|
|
20
|
+
throw new Error("You must provide data as an object.");
|
|
21
|
+
}
|
|
22
|
+
for (const key of requiredKeys) {
|
|
23
|
+
if (!(key in data) || data[key] === undefined || data[key] === null) {
|
|
24
|
+
throw new Error(`You must provide "${key}" in the data object.`);
|
|
25
|
+
}
|
|
26
|
+
const value = data[key];
|
|
27
|
+
if (key === "scope") {
|
|
28
|
+
if (!Array.isArray(value)) {
|
|
29
|
+
throw new Error(`"${key}" must be an array of strings.`);
|
|
30
|
+
}
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
if (typeof value !== "string" && typeof value !== "number") {
|
|
34
|
+
throw new Error(`"${key}" must be a string or a number.`);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Checks if required environment variables are present.
|
|
40
|
+
* Throws an error with a clear message if any variable is missing.
|
|
41
|
+
* @param {string[]} keys - List of required environment variable names.
|
|
42
|
+
*/
|
|
43
|
+
export function checkEnv(keys) {
|
|
44
|
+
const missing = keys.filter((key) => !process.env[key]);
|
|
45
|
+
if (missing.length > 0) {
|
|
46
|
+
throw new Error(`Missing required environment variables: ${missing.join(", ")}`);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
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 {};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { getPrivateToken } from "../index";
|
|
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
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kash-88/alerts",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "DonationAlerts API for Node.js",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"DonationAlerts",
|
|
8
|
+
"DonationAlerts API",
|
|
9
|
+
"DonationAlerts SDK"
|
|
10
|
+
],
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"registry": "https://npm.pkg.github.com/",
|
|
13
|
+
"access": "public"
|
|
14
|
+
},
|
|
15
|
+
"homepage": "https://github.com/kash-88/alerts-SDK#readme",
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/kash-88/alerts-SDK/issues"
|
|
18
|
+
},
|
|
19
|
+
"main": "dist/src/index.js",
|
|
20
|
+
"types": "dist/src/index.d.ts",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"import": "./dist/src/index.js",
|
|
24
|
+
"require": "./dist/src/index.js"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"dist",
|
|
29
|
+
"src",
|
|
30
|
+
"ws"
|
|
31
|
+
],
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git+https://github.com/kash-88/alerts-SDK.git"
|
|
35
|
+
},
|
|
36
|
+
"license": "ISC",
|
|
37
|
+
"author": "kash.88",
|
|
38
|
+
"scripts": {
|
|
39
|
+
"publish:github": "npm publish",
|
|
40
|
+
"build": "tsc && tsc-alias",
|
|
41
|
+
"prepublishOnly": "npm run build",
|
|
42
|
+
"publish:npm-laster": "npm publish --registry=https://registry.npmjs.org/ --tag latest",
|
|
43
|
+
"publish:npm-dev": "npm publish --registry=https://registry.npmjs.org/ --tag Dev",
|
|
44
|
+
"publish:npm-beta": "npm publish --registry=https://registry.npmjs.org/ --tag Beta"
|
|
45
|
+
},
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"axios": "^1.10.0",
|
|
48
|
+
"typed-emitter": "^2.1.0",
|
|
49
|
+
"ws": "^8.17.0"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@types/node": "^20.12.12",
|
|
53
|
+
"@types/ws": "^8.5.10",
|
|
54
|
+
"ts-node": "^10.9.2",
|
|
55
|
+
"tsc-alias": "^1.8.16",
|
|
56
|
+
"tsconfig-paths": "^4.2.0",
|
|
57
|
+
"typescript": "^5.4.5"
|
|
58
|
+
}
|
|
59
|
+
}
|
package/readme.md
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# DonationAlerts API
|
|
2
|
+
A convenient library for interacting with the DonationAlerts API!
|
|
3
|
+
|
|
4
|
+
## Installation
|
|
5
|
+
```bash
|
|
6
|
+
$ npm install @kash-88/alerts
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## All methods
|
|
10
|
+
|
|
11
|
+
## (Sync) getAuthorizeLink
|
|
12
|
+
* Generates an authorization link for DonationAlerts OAuth.
|
|
13
|
+
* Param: `{ client_id: string, scope: string[] }`
|
|
14
|
+
* Api page: https://www.donationalerts.com/apidoc#authorization__authorization_code__authorization_request
|
|
15
|
+
* Endpoint: https://www.donationalerts.com/oauth/authorize
|
|
16
|
+
* Example:
|
|
17
|
+
```js
|
|
18
|
+
import { getAuthorizeLink } from '@kash-88/alerts';
|
|
19
|
+
|
|
20
|
+
const link = getAuthorizeLink({
|
|
21
|
+
client_id: 'YOUR_CLIENT_ID',
|
|
22
|
+
scope: ['oauth-user-show'] // https://www.donationalerts.com/apidoc#authorization__scopes
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
console.log(link);
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
## (Async) getOauthToken
|
|
31
|
+
* Exchanges authorization code for access_token and refresh_token.
|
|
32
|
+
* Param: `{ client_id: string, client_secret: string, code: string }`
|
|
33
|
+
* Api page: https://www.donationalerts.com/apidoc#authorization__authorization_code__getting_access_token
|
|
34
|
+
* Endpoint: https://www.donationalerts.com/oauth/token
|
|
35
|
+
* Example:
|
|
36
|
+
```js
|
|
37
|
+
import { getOauthToken } from '@kash-88/alerts';
|
|
38
|
+
|
|
39
|
+
const tokenData = await getOauthToken({
|
|
40
|
+
client_id: 'YOUR_CLIENT_ID',
|
|
41
|
+
client_secret: 'YOUR_CLIENT_SECRET',
|
|
42
|
+
code: 'AUTHORIZATION_CODE'
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
console.log(tokenData);
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
## (Async) updateAccessToken
|
|
51
|
+
* Refreshes access_token using refresh_token.
|
|
52
|
+
* Param: `{ client_id: string|number, client_secret: string, refresh_token: string }`
|
|
53
|
+
* Api page: https://www.donationalerts.com/apidoc#authorization__authorization_code__refreshing_access_tokens
|
|
54
|
+
* Endpoint: https://www.donationalerts.com/oauth/token
|
|
55
|
+
* Example:
|
|
56
|
+
```js
|
|
57
|
+
import { updateAccessToken } from '@kash-88/alerts';
|
|
58
|
+
|
|
59
|
+
const tokenData = await updateAccessToken({
|
|
60
|
+
client_id: 'YOUR_CLIENT_ID',
|
|
61
|
+
client_secret: 'YOUR_CLIENT_SECRET',
|
|
62
|
+
refresh_token: 'USER_REFRESH_TOKEN'
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
console.log(tokenData.access_token);
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
---
|
|
69
|
+
|
|
70
|
+
## (Async) getUser
|
|
71
|
+
* Fetches user profile information by access_token.
|
|
72
|
+
* Param: `access_token: string`
|
|
73
|
+
* Api page: https://www.donationalerts.com/apidoc#api_v1__users
|
|
74
|
+
* Endpoint: https://www.donationalerts.com/api/v1/user/oauth
|
|
75
|
+
* Example:
|
|
76
|
+
```js
|
|
77
|
+
import { getUser } from '@kash-88/alerts';
|
|
78
|
+
|
|
79
|
+
const user = await getUser('USER_ACCESS_TOKEN');
|
|
80
|
+
|
|
81
|
+
console.log(user);
|
|
82
|
+
```
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import "dotenv/config";
|
|
2
|
+
import { getAuthorizeLink } from "@kash-88/alerts";
|
|
3
|
+
import { checkEnv } from "@utils";
|
|
4
|
+
|
|
5
|
+
checkEnv(["CLIENT_ID", "SCOPE"]);
|
|
6
|
+
|
|
7
|
+
const client_id = process.env.CLIENT_ID!;
|
|
8
|
+
const scope = process.env.SCOPE ? process.env.SCOPE.split(",") : ["oauth-user-show"];
|
|
9
|
+
|
|
10
|
+
try {
|
|
11
|
+
const link = getAuthorizeLink({ client_id, scope });
|
|
12
|
+
console.log("Authorize link:", link);
|
|
13
|
+
} catch (error: any) {
|
|
14
|
+
console.error("Error:", error.message);
|
|
15
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import "dotenv/config";
|
|
2
|
+
import { getOauthToken } from "@kash-88/alerts";
|
|
3
|
+
import { checkEnv } from "@utils";
|
|
4
|
+
|
|
5
|
+
checkEnv(["CLIENT_ID", "CLIENT_SECRET", "CODE"]);
|
|
6
|
+
|
|
7
|
+
const client_id = process.env.CLIENT_ID!;
|
|
8
|
+
const client_secret = process.env.CLIENT_SECRET!;
|
|
9
|
+
const code = process.env.CODE!;
|
|
10
|
+
|
|
11
|
+
(async () => {
|
|
12
|
+
try {
|
|
13
|
+
const token = await getOauthToken({ client_id, client_secret, code });
|
|
14
|
+
console.log("Oauth token:", token);
|
|
15
|
+
} catch (error: any) {
|
|
16
|
+
console.error("Error:", error.message);
|
|
17
|
+
}
|
|
18
|
+
})();
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import "dotenv/config";
|
|
2
|
+
import { getUser } from "@kash-88/alerts";
|
|
3
|
+
import { checkEnv } from "@utils";
|
|
4
|
+
|
|
5
|
+
checkEnv(["ACCESS_TOKEN"]);
|
|
6
|
+
|
|
7
|
+
const access_token = process.env.ACCESS_TOKEN!;
|
|
8
|
+
|
|
9
|
+
(async () => {
|
|
10
|
+
try {
|
|
11
|
+
const user = await getUser(access_token);
|
|
12
|
+
console.log("User:", user);
|
|
13
|
+
} catch (error: any) {
|
|
14
|
+
console.error("Error:", error.message);
|
|
15
|
+
}
|
|
16
|
+
})();
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import "dotenv/config";
|
|
2
|
+
import { getUserChannel } from "@kash-88/alerts";
|
|
3
|
+
import { checkEnv } from "@utils";
|
|
4
|
+
|
|
5
|
+
checkEnv(["CLIENT_ID"]);
|
|
6
|
+
|
|
7
|
+
const id = process.env.CLIENT_ID!;
|
|
8
|
+
|
|
9
|
+
try {
|
|
10
|
+
const channel = getUserChannel(id);
|
|
11
|
+
console.log("User channel:", channel);
|
|
12
|
+
} catch (error: any) {
|
|
13
|
+
console.error("Error:", error.message);
|
|
14
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import "dotenv/config";
|
|
2
|
+
import { updateAccessToken } from "@kash-88/alerts";
|
|
3
|
+
import { checkEnv } from "@utils";
|
|
4
|
+
|
|
5
|
+
checkEnv(["CLIENT_ID", "CLIENT_SECRET", "REFRESH_TOKEN"]);
|
|
6
|
+
|
|
7
|
+
const client_id = process.env.CLIENT_ID!;
|
|
8
|
+
const client_secret = process.env.CLIENT_SECRET!;
|
|
9
|
+
const refresh_token = process.env.REFRESH_TOKEN!;
|
|
10
|
+
|
|
11
|
+
(async () => {
|
|
12
|
+
try {
|
|
13
|
+
const token = await updateAccessToken({ client_id, client_secret, refresh_token });
|
|
14
|
+
console.log("Updated token:", token);
|
|
15
|
+
} catch (error: any) {
|
|
16
|
+
console.error("Error:", error.message);
|
|
17
|
+
}
|
|
18
|
+
})();
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import "dotenv/config";
|
|
2
|
+
|
|
3
|
+
import { getUser, getUserChannel, CentrifugeClient } from "@kash-88/alerts";
|
|
4
|
+
import { checkEnv } from "@utils";
|
|
5
|
+
|
|
6
|
+
checkEnv(["CLIENT_ID", "ACCESS_TOKEN", "SOCKET_CONNECTION_TOKEN"]);
|
|
7
|
+
const access_token = process.env.ACCESS_TOKEN!;
|
|
8
|
+
let isConnectToPrivate = false;
|
|
9
|
+
|
|
10
|
+
async function main() {
|
|
11
|
+
try {
|
|
12
|
+
const user = await getUser(access_token);
|
|
13
|
+
const channel = getUserChannel(user.id);
|
|
14
|
+
const socket_connection_token = String(process.env.SOCKET_CONNECTION_TOKEN);
|
|
15
|
+
|
|
16
|
+
const client = new CentrifugeClient({
|
|
17
|
+
channel,
|
|
18
|
+
socket_connection_token,
|
|
19
|
+
access_token
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
const ws = client.createConnection();
|
|
23
|
+
|
|
24
|
+
ws.on("open", async () => {
|
|
25
|
+
console.log("WebSocket соединение открыто");
|
|
26
|
+
client.confirmConnection(socket_connection_token);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
ws.on("message", (message) => {
|
|
30
|
+
const srt = message.toString("utf8");
|
|
31
|
+
const json = JSON.parse(srt);
|
|
32
|
+
|
|
33
|
+
if(json.id = 1 && !isConnectToPrivate) {
|
|
34
|
+
isConnectToPrivate = true; return client.connectPrivateToken(channel, json.result.client);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
console.log(srt);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
ws.on("close", () => {
|
|
41
|
+
console.log("WebSocket соединение закрыто");
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
ws.on("error", (err) => {
|
|
45
|
+
console.error("Ошибка WebSocket:", err);
|
|
46
|
+
});
|
|
47
|
+
} catch (error: any) {
|
|
48
|
+
console.error("Ошибка:", error.message);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
main();
|
|
@@ -0,0 +1,33 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
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
|
+
}
|