@nerimity/nerimity.js 1.11.1 → 1.12.1
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/.eslintrc.js +9 -9
- package/build/RawData.d.ts +1 -0
- package/build/RawData.d.ts.map +1 -1
- package/build/RawData.js.map +1 -1
- package/build/Webhooks.d.ts +73 -0
- package/build/Webhooks.d.ts.map +1 -0
- package/build/Webhooks.js +122 -0
- package/build/Webhooks.js.map +1 -0
- package/build/index.d.ts +1 -0
- package/build/index.d.ts.map +1 -1
- package/build/index.js +3 -1
- package/build/index.js.map +1 -1
- package/examples/webooks.js +10 -0
- package/package.json +38 -37
- package/src/RawData.ts +1 -0
- package/src/Webhooks.ts +148 -0
- package/src/index.ts +1 -0
package/.eslintrc.js
CHANGED
|
@@ -4,18 +4,18 @@ module.exports = {
|
|
|
4
4
|
es2021: true,
|
|
5
5
|
node: true,
|
|
6
6
|
},
|
|
7
|
-
extends: [
|
|
7
|
+
extends: ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
|
|
8
8
|
overrides: [],
|
|
9
|
-
parser:
|
|
9
|
+
parser: "@typescript-eslint/parser",
|
|
10
10
|
parserOptions: {
|
|
11
|
-
ecmaVersion:
|
|
12
|
-
sourceType:
|
|
11
|
+
ecmaVersion: "latest",
|
|
12
|
+
sourceType: "module",
|
|
13
13
|
},
|
|
14
|
-
plugins: [
|
|
14
|
+
plugins: ["@typescript-eslint"],
|
|
15
15
|
rules: {
|
|
16
|
-
indent: [
|
|
17
|
-
|
|
18
|
-
quotes: [
|
|
19
|
-
semi: [
|
|
16
|
+
indent: ["error", 2],
|
|
17
|
+
"linebreak-style": ["error", "windows"],
|
|
18
|
+
quotes: ["error", "multi"],
|
|
19
|
+
semi: ["error", "always"],
|
|
20
20
|
},
|
|
21
21
|
};
|
package/build/RawData.d.ts
CHANGED
package/build/RawData.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RawData.d.ts","sourceRoot":"","sources":["../src/RawData.ts"],"names":[],"mappings":"AACA,MAAM,WAAW,oBAAoB;IACnC,IAAI,EAAE,OAAO,CAAC;IACd,OAAO,EAAE,SAAS,EAAE,CAAC;IACrB,aAAa,EAAE,eAAe,EAAE,CAAC;IACjC,eAAe,EAAE,GAAG,EAAE,CAAC;IACvB,QAAQ,EAAE,UAAU,EAAE,CAAC;IACvB,WAAW,EAAE,GAAG,EAAE,CAAC;IACnB,SAAS,EAAE,GAAG,EAAE,CAAC;IACjB,OAAO,EAAE,GAAG,EAAE,CAAC;IACf,KAAK,EAAE,GAAG,EAAE,CAAC;IACb,wBAAwB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAClD;AAED,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;
|
|
1
|
+
{"version":3,"file":"RawData.d.ts","sourceRoot":"","sources":["../src/RawData.ts"],"names":[],"mappings":"AACA,MAAM,WAAW,oBAAoB;IACnC,IAAI,EAAE,OAAO,CAAC;IACd,OAAO,EAAE,SAAS,EAAE,CAAC;IACrB,aAAa,EAAE,eAAe,EAAE,CAAC;IACjC,eAAe,EAAE,GAAG,EAAE,CAAC;IACvB,QAAQ,EAAE,UAAU,EAAE,CAAC;IACvB,WAAW,EAAE,GAAG,EAAE,CAAC;IACnB,SAAS,EAAE,GAAG,EAAE,CAAC;IACjB,OAAO,EAAE,GAAG,EAAE,CAAC;IACf,KAAK,EAAE,GAAG,EAAE,CAAC;IACb,wBAAwB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAClD;AAED,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;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,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,WAAW,CAAC,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC;IACzB,OAAO,EAAE,OAAO,CAAC;IACjB,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,SAAS,EAAE,OAAO,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE;QAAE,EAAE,EAAE,MAAM,CAAA;KAAE,EAAE,CAAC;IAC1B,OAAO,EAAE;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE;YAAE,EAAE,EAAE,MAAM,CAAC;YAAC,QAAQ,EAAE,MAAM,CAAA;SAAE,CAAA;KAAE,EAAE,CAAC;IACvE,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,MAAM,EAAE;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC;IAC/D,KAAK,EAAE,MAAM,CAAC;IACd,YAAY,EAAE,GAAG,CAAC;IAElB,IAAI,CAAC,EAAE,GAAG,CAAC;CACZ"}
|
package/build/RawData.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RawData.js","sourceRoot":"","sources":["../src/RawData.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"RawData.js","sourceRoot":"","sources":["../src/RawData.ts"],"names":[],"mappings":";;;AA2DA,IAAY,WAMX;AAND,WAAY,WAAW;IACrB,mDAAW,CAAA;IACX,2DAAe,CAAA;IACf,6DAAgB,CAAA;IAChB,uDAAa,CAAA;IACb,qDAAY,CAAA;AACd,CAAC,EANW,WAAW,2BAAX,WAAW,QAMtB;AA8BD,IAAY,WAIX;AAJD,WAAY,WAAW;IACrB,mDAAW,CAAA;IACX,2DAAe,CAAA;IACf,qDAAY,CAAA;AACd,CAAC,EAJW,WAAW,2BAAX,WAAW,QAItB"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options for sending a webhook message.
|
|
3
|
+
*/
|
|
4
|
+
export interface WebhookOptions {
|
|
5
|
+
/** Message content. */
|
|
6
|
+
content?: string;
|
|
7
|
+
/** Username to display. */
|
|
8
|
+
username?: string;
|
|
9
|
+
/** Avatar URL to display. */
|
|
10
|
+
avatarUrl?: string;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Nerimity.js implementation for sending Nerimity webhooks.
|
|
14
|
+
*/
|
|
15
|
+
export declare class WebhookBuilder {
|
|
16
|
+
private channelId;
|
|
17
|
+
private token;
|
|
18
|
+
private options;
|
|
19
|
+
/**
|
|
20
|
+
* Creates a new WebhookBuilder instance.
|
|
21
|
+
*
|
|
22
|
+
* @param webhook - The webhook identifier. Can be:
|
|
23
|
+
* - Full URL: `"https://nerimity.com/api/webhooks/{channelId}/{token}"`
|
|
24
|
+
* @param options - Optional webhook message options (content, username, avatarUrl).
|
|
25
|
+
*
|
|
26
|
+
* @throws If the webhook format is invalid or missing required fields.
|
|
27
|
+
*/
|
|
28
|
+
constructor(webhook: string | {
|
|
29
|
+
channelId: string;
|
|
30
|
+
token: string;
|
|
31
|
+
}, options?: WebhookOptions);
|
|
32
|
+
/**
|
|
33
|
+
* Parse webhook string format and extract channelId + token.
|
|
34
|
+
*
|
|
35
|
+
* @param webhook - Webhook string in one of the accepted formats.
|
|
36
|
+
* @throws If the string is not a valid webhook format.
|
|
37
|
+
*/
|
|
38
|
+
private parseWebhookString;
|
|
39
|
+
/**
|
|
40
|
+
* Validate message options (content, username, avatarUrl).
|
|
41
|
+
*
|
|
42
|
+
* @param options - The options object to validate.
|
|
43
|
+
* @throws If an option type is invalid.
|
|
44
|
+
*/
|
|
45
|
+
private validateOptions;
|
|
46
|
+
/**
|
|
47
|
+
* Set the username override for the webhook.
|
|
48
|
+
*
|
|
49
|
+
* @param username - The custom username.
|
|
50
|
+
* @returns The current WebhookBuilder instance (for chaining).
|
|
51
|
+
*
|
|
52
|
+
* @throws If username is not a string.
|
|
53
|
+
*/
|
|
54
|
+
setUsername(username: string): this;
|
|
55
|
+
/**
|
|
56
|
+
* Set the avatar image for the webhook.
|
|
57
|
+
*
|
|
58
|
+
* @param avatarUrl - The avatar image URL.
|
|
59
|
+
* @returns The current WebhookBuilder instance (for chaining).
|
|
60
|
+
*
|
|
61
|
+
* @throws If avatarUrl is not a string.
|
|
62
|
+
*/
|
|
63
|
+
setAvatar(avatarUrl: string): this;
|
|
64
|
+
/**
|
|
65
|
+
* Sends the webhook request to Nerimity's API.
|
|
66
|
+
*
|
|
67
|
+
* @returns A Promise resolving with the JSON response from the API.
|
|
68
|
+
*
|
|
69
|
+
* @throws If the request fails, is unauthorized, or returns invalid JSON.
|
|
70
|
+
*/
|
|
71
|
+
send(content: string): Promise<any>;
|
|
72
|
+
}
|
|
73
|
+
//# sourceMappingURL=Webhooks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Webhooks.d.ts","sourceRoot":"","sources":["../src/Webhooks.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,cAAc;IAC3B,uBAAuB;IACvB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,2BAA2B;IAC3B,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,6BAA6B;IAC7B,SAAS,CAAC,EAAE,MAAM,CAAC;CACtB;AAED;;GAEG;AACH,qBAAa,cAAc;IACvB,OAAO,CAAC,SAAS,CAAc;IAC/B,OAAO,CAAC,KAAK,CAAc;IAC3B,OAAO,CAAC,OAAO,CAAiB;IAEhC;;;;;;;;OAQG;gBAEC,OAAO,EAAE,MAAM,GAAG;QAAE,SAAS,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,EACtD,OAAO,GAAE,cAAmB;IAgBhC;;;;;OAKG;IACH,OAAO,CAAC,kBAAkB;IAc1B;;;;;OAKG;IACH,OAAO,CAAC,eAAe;IAOvB;;;;;;;OAOG;IACI,WAAW,CAAC,QAAQ,EAAE,MAAM;IAOnC;;;;;;;OAOG;IACI,SAAS,CAAC,SAAS,EAAE,MAAM;IAOlC;;;;;;OAMG;IACU,IAAI,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC;CA8BnD"}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WebhookBuilder = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Nerimity.js implementation for sending Nerimity webhooks.
|
|
6
|
+
*/
|
|
7
|
+
class WebhookBuilder {
|
|
8
|
+
/**
|
|
9
|
+
* Creates a new WebhookBuilder instance.
|
|
10
|
+
*
|
|
11
|
+
* @param webhook - The webhook identifier. Can be:
|
|
12
|
+
* - Full URL: `"https://nerimity.com/api/webhooks/{channelId}/{token}"`
|
|
13
|
+
* @param options - Optional webhook message options (content, username, avatarUrl).
|
|
14
|
+
*
|
|
15
|
+
* @throws If the webhook format is invalid or missing required fields.
|
|
16
|
+
*/
|
|
17
|
+
constructor(webhook, options = {}) {
|
|
18
|
+
this.channelId = "";
|
|
19
|
+
this.token = "";
|
|
20
|
+
this.options = options;
|
|
21
|
+
if (typeof webhook === "object") {
|
|
22
|
+
if (!webhook.channelId || !webhook.token)
|
|
23
|
+
throw new Error("Webhook object must have channelId and token");
|
|
24
|
+
this.channelId = webhook.channelId;
|
|
25
|
+
this.token = webhook.token;
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
this.parseWebhookString(webhook);
|
|
29
|
+
}
|
|
30
|
+
this.validateOptions(this.options);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Parse webhook string format and extract channelId + token.
|
|
34
|
+
*
|
|
35
|
+
* @param webhook - Webhook string in one of the accepted formats.
|
|
36
|
+
* @throws If the string is not a valid webhook format.
|
|
37
|
+
*/
|
|
38
|
+
parseWebhookString(webhook) {
|
|
39
|
+
if (!webhook || typeof webhook !== "string")
|
|
40
|
+
throw new Error("Webhook must be a non-empty string");
|
|
41
|
+
if (webhook.startsWith("http")) {
|
|
42
|
+
const match = webhook.match(/\/webhooks\/(\d+)\/(.+)$/);
|
|
43
|
+
if (!match)
|
|
44
|
+
throw new Error(`Invalid webhook URL: ${webhook}`);
|
|
45
|
+
this.channelId = match[1];
|
|
46
|
+
this.token = match[2];
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
throw new Error(`Unrecognized webhook format: ${webhook}`);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Validate message options (content, username, avatarUrl).
|
|
54
|
+
*
|
|
55
|
+
* @param options - The options object to validate.
|
|
56
|
+
* @throws If an option type is invalid.
|
|
57
|
+
*/
|
|
58
|
+
validateOptions(options) {
|
|
59
|
+
if (options.username && typeof options.username !== "string")
|
|
60
|
+
throw new Error("Webhook option 'username' must be a string");
|
|
61
|
+
if (options.avatarUrl && typeof options.avatarUrl !== "string")
|
|
62
|
+
throw new Error("Webhook option 'avatarUrl' must be a string");
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Set the username override for the webhook.
|
|
66
|
+
*
|
|
67
|
+
* @param username - The custom username.
|
|
68
|
+
* @returns The current WebhookBuilder instance (for chaining).
|
|
69
|
+
*
|
|
70
|
+
* @throws If username is not a string.
|
|
71
|
+
*/
|
|
72
|
+
setUsername(username) {
|
|
73
|
+
if (typeof username !== "string")
|
|
74
|
+
throw new Error("Username must be a string");
|
|
75
|
+
this.options.username = username;
|
|
76
|
+
return this;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Set the avatar image for the webhook.
|
|
80
|
+
*
|
|
81
|
+
* @param avatarUrl - The avatar image URL.
|
|
82
|
+
* @returns The current WebhookBuilder instance (for chaining).
|
|
83
|
+
*
|
|
84
|
+
* @throws If avatarUrl is not a string.
|
|
85
|
+
*/
|
|
86
|
+
setAvatar(avatarUrl) {
|
|
87
|
+
if (typeof avatarUrl !== "string")
|
|
88
|
+
throw new Error("Avatar URL must be a string");
|
|
89
|
+
this.options.avatarUrl = avatarUrl;
|
|
90
|
+
return this;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Sends the webhook request to Nerimity's API.
|
|
94
|
+
*
|
|
95
|
+
* @returns A Promise resolving with the JSON response from the API.
|
|
96
|
+
*
|
|
97
|
+
* @throws If the request fails, is unauthorized, or returns invalid JSON.
|
|
98
|
+
*/
|
|
99
|
+
async send(content) {
|
|
100
|
+
const url = `https://nerimity.com/api/webhooks/${this.channelId}/${this.token}`;
|
|
101
|
+
try {
|
|
102
|
+
const res = await fetch(url, {
|
|
103
|
+
method: "POST",
|
|
104
|
+
headers: { "Content-Type": "application/json" },
|
|
105
|
+
body: JSON.stringify({ content, ...this.options }),
|
|
106
|
+
});
|
|
107
|
+
if (!res.ok) {
|
|
108
|
+
const body = await res.text().catch(() => "");
|
|
109
|
+
throw new Error(`HTTP ${res.status} ${res.statusText}${body ? ` - ${body}` : ""}`);
|
|
110
|
+
}
|
|
111
|
+
const json = await res.json().catch(() => {
|
|
112
|
+
throw new Error("Webhook returned invalid JSON");
|
|
113
|
+
});
|
|
114
|
+
return json;
|
|
115
|
+
}
|
|
116
|
+
catch (err) {
|
|
117
|
+
throw new Error(`Failed to send webhook: ${err.message}`);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
exports.WebhookBuilder = WebhookBuilder;
|
|
122
|
+
//# sourceMappingURL=Webhooks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Webhooks.js","sourceRoot":"","sources":["../src/Webhooks.ts"],"names":[],"mappings":";;;AAYA;;GAEG;AACH,MAAa,cAAc;IAKvB;;;;;;;;OAQG;IACH,YACI,O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EAAE,CAAC,CAAC;gBAC9C,MAAM,IAAI,KAAK,CACX,QAAQ,GAAG,CAAC,MAAM,IAAI,GAAG,CAAC,UAAU,GAChC,IAAI,CAAC,CAAC,CAAC,MAAM,IAAI,EAAE,CAAC,CAAC,CAAC,EAC1B,EAAE,CACL,CAAC;YACN,CAAC;YAED,MAAM,IAAI,GAAG,MAAM,GAAG,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE;gBACrC,MAAM,IAAI,KAAK,CAAC,+BAA+B,CAAC,CAAC;YACrD,CAAC,CAAC,CAAC;YAEH,OAAO,IAAI,CAAC;QAChB,CAAC;QAAC,OAAO,GAAQ,EAAE,CAAC;YAChB,MAAM,IAAI,KAAK,CACX,2BAA2B,GAAG,CAAC,OAAO,EAAE,CAC3C,CAAC;QACN,CAAC;IACL,CAAC;CACJ;AApID,wCAoIC"}
|
package/build/index.d.ts
CHANGED
package/build/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAC1C,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAC1C,OAAO,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AACrD,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC"}
|
package/build/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RPCClient = exports.AttachmentBuilder = exports.Events = exports.Client = void 0;
|
|
3
|
+
exports.WebhookBuilder = exports.RPCClient = exports.AttachmentBuilder = exports.Events = exports.Client = void 0;
|
|
4
4
|
var Client_1 = require("./Client");
|
|
5
5
|
Object.defineProperty(exports, "Client", { enumerable: true, get: function () { return Client_1.Client; } });
|
|
6
6
|
Object.defineProperty(exports, "Events", { enumerable: true, get: function () { return Client_1.Events; } });
|
|
@@ -8,4 +8,6 @@ var Attachment_1 = require("./Attachment");
|
|
|
8
8
|
Object.defineProperty(exports, "AttachmentBuilder", { enumerable: true, get: function () { return Attachment_1.AttachmentBuilder; } });
|
|
9
9
|
var RPCClient_1 = require("./RPCClient");
|
|
10
10
|
Object.defineProperty(exports, "RPCClient", { enumerable: true, get: function () { return RPCClient_1.RPCClient; } });
|
|
11
|
+
var Webhooks_1 = require("./Webhooks");
|
|
12
|
+
Object.defineProperty(exports, "WebhookBuilder", { enumerable: true, get: function () { return Webhooks_1.WebhookBuilder; } });
|
|
11
13
|
//# sourceMappingURL=index.js.map
|
package/build/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,mCAA0C;AAAjC,gGAAA,MAAM,OAAA;AAAE,gGAAA,MAAM,OAAA;AACvB,2CAAiD;AAAxC,+GAAA,iBAAiB,OAAA;AAC1B,yCAAqD;AAA5C,sGAAA,SAAS,OAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,mCAA0C;AAAjC,gGAAA,MAAM,OAAA;AAAE,gGAAA,MAAM,OAAA;AACvB,2CAAiD;AAAxC,+GAAA,iBAAiB,OAAA;AAC1B,yCAAqD;AAA5C,sGAAA,SAAS,OAAA;AAClB,uCAA4C;AAAnC,0GAAA,cAAc,OAAA"}
|
package/package.json
CHANGED
|
@@ -1,37 +1,38 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@nerimity/nerimity.js",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "",
|
|
5
|
-
"main": "build/index.js",
|
|
6
|
-
"
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
"typescript": "^
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
"
|
|
21
|
-
"node
|
|
22
|
-
"
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
"
|
|
36
|
-
}
|
|
37
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@nerimity/nerimity.js",
|
|
3
|
+
"version": "1.12.1",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "build/index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "tsc",
|
|
8
|
+
"prepublishOnly": "tsc",
|
|
9
|
+
"dev": "tsc && node tests/test.js"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [],
|
|
12
|
+
"author": "SupertigerDev",
|
|
13
|
+
"license": "ISC",
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"@types/node-fetch": "^2.6.12",
|
|
16
|
+
"@types/ws": "^8.5.12",
|
|
17
|
+
"@typescript-eslint/eslint-plugin": "^7.0.1",
|
|
18
|
+
"@typescript-eslint/parser": "^7.0.1",
|
|
19
|
+
"eslint": "^8.56.0",
|
|
20
|
+
"nodemon": "^3.0.3",
|
|
21
|
+
"ts-node": "^10.9.2",
|
|
22
|
+
"typescript": "^5.3.3"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"eventemitter3": "^5.0.1",
|
|
26
|
+
"node-fetch": "^2.7.0",
|
|
27
|
+
"socket.io-client": "^4.7.4",
|
|
28
|
+
"ws": "^8.18.0"
|
|
29
|
+
},
|
|
30
|
+
"repository": {
|
|
31
|
+
"type": "git",
|
|
32
|
+
"url": "git+https://github.com/Nerimity/nerimity.js.git"
|
|
33
|
+
},
|
|
34
|
+
"bugs": {
|
|
35
|
+
"url": "https://github.com/Nerimity/nerimity.js/issues"
|
|
36
|
+
},
|
|
37
|
+
"homepage": "https://github.com/Nerimity/nerimity.js#readme"
|
|
38
|
+
}
|
package/src/RawData.ts
CHANGED
package/src/Webhooks.ts
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Options for sending a webhook message.
|
|
3
|
+
*/
|
|
4
|
+
export interface WebhookOptions {
|
|
5
|
+
/** Message content. */
|
|
6
|
+
content?: string;
|
|
7
|
+
/** Username to display. */
|
|
8
|
+
username?: string;
|
|
9
|
+
/** Avatar URL to display. */
|
|
10
|
+
avatarUrl?: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Nerimity.js implementation for sending Nerimity webhooks.
|
|
15
|
+
*/
|
|
16
|
+
export class WebhookBuilder {
|
|
17
|
+
private channelId: string = "";
|
|
18
|
+
private token: string = "";
|
|
19
|
+
private options: WebhookOptions;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Creates a new WebhookBuilder instance.
|
|
23
|
+
*
|
|
24
|
+
* @param webhook - The webhook identifier. Can be:
|
|
25
|
+
* - Full URL: `"https://nerimity.com/api/webhooks/{channelId}/{token}"`
|
|
26
|
+
* @param options - Optional webhook message options (content, username, avatarUrl).
|
|
27
|
+
*
|
|
28
|
+
* @throws If the webhook format is invalid or missing required fields.
|
|
29
|
+
*/
|
|
30
|
+
constructor(
|
|
31
|
+
webhook: string | { channelId: string; token: string },
|
|
32
|
+
options: WebhookOptions = {}
|
|
33
|
+
) {
|
|
34
|
+
this.options = options;
|
|
35
|
+
|
|
36
|
+
if (typeof webhook === "object") {
|
|
37
|
+
if (!webhook.channelId || !webhook.token)
|
|
38
|
+
throw new Error("Webhook object must have channelId and token");
|
|
39
|
+
this.channelId = webhook.channelId;
|
|
40
|
+
this.token = webhook.token;
|
|
41
|
+
} else {
|
|
42
|
+
this.parseWebhookString(webhook);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
this.validateOptions(this.options);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Parse webhook string format and extract channelId + token.
|
|
50
|
+
*
|
|
51
|
+
* @param webhook - Webhook string in one of the accepted formats.
|
|
52
|
+
* @throws If the string is not a valid webhook format.
|
|
53
|
+
*/
|
|
54
|
+
private parseWebhookString(webhook: string) {
|
|
55
|
+
if (!webhook || typeof webhook !== "string")
|
|
56
|
+
throw new Error("Webhook must be a non-empty string");
|
|
57
|
+
|
|
58
|
+
if (webhook.startsWith("http")) {
|
|
59
|
+
const match = webhook.match(/\/webhooks\/(\d+)\/(.+)$/);
|
|
60
|
+
if (!match) throw new Error(`Invalid webhook URL: ${webhook}`);
|
|
61
|
+
this.channelId = match[1];
|
|
62
|
+
this.token = match[2];
|
|
63
|
+
} else {
|
|
64
|
+
throw new Error(`Unrecognized webhook format: ${webhook}`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Validate message options (content, username, avatarUrl).
|
|
70
|
+
*
|
|
71
|
+
* @param options - The options object to validate.
|
|
72
|
+
* @throws If an option type is invalid.
|
|
73
|
+
*/
|
|
74
|
+
private validateOptions(options: WebhookOptions) {
|
|
75
|
+
if (options.username && typeof options.username !== "string")
|
|
76
|
+
throw new Error("Webhook option 'username' must be a string");
|
|
77
|
+
if (options.avatarUrl && typeof options.avatarUrl !== "string")
|
|
78
|
+
throw new Error("Webhook option 'avatarUrl' must be a string");
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Set the username override for the webhook.
|
|
83
|
+
*
|
|
84
|
+
* @param username - The custom username.
|
|
85
|
+
* @returns The current WebhookBuilder instance (for chaining).
|
|
86
|
+
*
|
|
87
|
+
* @throws If username is not a string.
|
|
88
|
+
*/
|
|
89
|
+
public setUsername(username: string) {
|
|
90
|
+
if (typeof username !== "string")
|
|
91
|
+
throw new Error("Username must be a string");
|
|
92
|
+
this.options.username = username;
|
|
93
|
+
return this;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Set the avatar image for the webhook.
|
|
98
|
+
*
|
|
99
|
+
* @param avatarUrl - The avatar image URL.
|
|
100
|
+
* @returns The current WebhookBuilder instance (for chaining).
|
|
101
|
+
*
|
|
102
|
+
* @throws If avatarUrl is not a string.
|
|
103
|
+
*/
|
|
104
|
+
public setAvatar(avatarUrl: string) {
|
|
105
|
+
if (typeof avatarUrl !== "string")
|
|
106
|
+
throw new Error("Avatar URL must be a string");
|
|
107
|
+
this.options.avatarUrl = avatarUrl;
|
|
108
|
+
return this;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Sends the webhook request to Nerimity's API.
|
|
113
|
+
*
|
|
114
|
+
* @returns A Promise resolving with the JSON response from the API.
|
|
115
|
+
*
|
|
116
|
+
* @throws If the request fails, is unauthorized, or returns invalid JSON.
|
|
117
|
+
*/
|
|
118
|
+
public async send(content: string): Promise<any> {
|
|
119
|
+
const url = `https://nerimity.com/api/webhooks/${this.channelId}/${this.token}`;
|
|
120
|
+
|
|
121
|
+
try {
|
|
122
|
+
const res = await fetch(url, {
|
|
123
|
+
method: "POST",
|
|
124
|
+
headers: { "Content-Type": "application/json" },
|
|
125
|
+
body: JSON.stringify({content, ...this.options}),
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
if (!res.ok) {
|
|
129
|
+
const body = await res.text().catch(() => "");
|
|
130
|
+
throw new Error(
|
|
131
|
+
`HTTP ${res.status} ${res.statusText}${
|
|
132
|
+
body ? ` - ${body}` : ""
|
|
133
|
+
}`
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const json = await res.json().catch(() => {
|
|
138
|
+
throw new Error("Webhook returned invalid JSON");
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
return json;
|
|
142
|
+
} catch (err: any) {
|
|
143
|
+
throw new Error(
|
|
144
|
+
`Failed to send webhook: ${err.message}`
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
package/src/index.ts
CHANGED