@teever/ez-hook 0.3.5
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/LICENSE +21 -0
- package/README.md +147 -0
- package/dist/classes/Embed.d.ts +205 -0
- package/dist/classes/Embed.d.ts.map +1 -0
- package/dist/classes/Embed.js +315 -0
- package/dist/classes/Embed.js.map +1 -0
- package/dist/classes/Errors.d.ts +59 -0
- package/dist/classes/Errors.d.ts.map +1 -0
- package/dist/classes/Errors.js +75 -0
- package/dist/classes/Errors.js.map +1 -0
- package/dist/classes/RequestClient.d.ts +38 -0
- package/dist/classes/RequestClient.d.ts.map +1 -0
- package/dist/classes/RequestClient.js +117 -0
- package/dist/classes/RequestClient.js.map +1 -0
- package/dist/classes/Webhook.d.ts +136 -0
- package/dist/classes/Webhook.d.ts.map +1 -0
- package/dist/classes/Webhook.js +208 -0
- package/dist/classes/Webhook.js.map +1 -0
- package/dist/classes/index.d.ts +5 -0
- package/dist/classes/index.d.ts.map +1 -0
- package/dist/classes/index.js +5 -0
- package/dist/classes/index.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/types/FileAttachment.d.ts +19 -0
- package/dist/types/FileAttachment.d.ts.map +1 -0
- package/dist/types/FileAttachment.js +2 -0
- package/dist/types/FileAttachment.js.map +1 -0
- package/dist/types/IAttachment.d.ts +39 -0
- package/dist/types/IAttachment.d.ts.map +1 -0
- package/dist/types/IAttachment.js +2 -0
- package/dist/types/IAttachment.js.map +1 -0
- package/dist/types/IAuthor.d.ts +21 -0
- package/dist/types/IAuthor.d.ts.map +1 -0
- package/dist/types/IAuthor.js +2 -0
- package/dist/types/IAuthor.js.map +1 -0
- package/dist/types/IEmbed.d.ts +66 -0
- package/dist/types/IEmbed.d.ts.map +1 -0
- package/dist/types/IEmbed.js +2 -0
- package/dist/types/IEmbed.js.map +1 -0
- package/dist/types/IField.d.ts +17 -0
- package/dist/types/IField.d.ts.map +1 -0
- package/dist/types/IField.js +2 -0
- package/dist/types/IField.js.map +1 -0
- package/dist/types/IFooter.d.ts +17 -0
- package/dist/types/IFooter.d.ts.map +1 -0
- package/dist/types/IFooter.js +2 -0
- package/dist/types/IFooter.js.map +1 -0
- package/dist/types/IImage.d.ts +21 -0
- package/dist/types/IImage.d.ts.map +1 -0
- package/dist/types/IImage.js +2 -0
- package/dist/types/IImage.js.map +1 -0
- package/dist/types/IProvider.d.ts +11 -0
- package/dist/types/IProvider.d.ts.map +1 -0
- package/dist/types/IProvider.js +2 -0
- package/dist/types/IProvider.js.map +1 -0
- package/dist/types/IThumbnail.d.ts +21 -0
- package/dist/types/IThumbnail.d.ts.map +1 -0
- package/dist/types/IThumbnail.js +2 -0
- package/dist/types/IThumbnail.js.map +1 -0
- package/dist/types/IUser.d.ts +67 -0
- package/dist/types/IUser.d.ts.map +1 -0
- package/dist/types/IUser.js +2 -0
- package/dist/types/IUser.js.map +1 -0
- package/dist/types/IVideo.d.ts +15 -0
- package/dist/types/IVideo.d.ts.map +1 -0
- package/dist/types/IVideo.js +2 -0
- package/dist/types/IVideo.js.map +1 -0
- package/dist/types/IWebhook.d.ts +30 -0
- package/dist/types/IWebhook.d.ts.map +1 -0
- package/dist/types/IWebhook.js +2 -0
- package/dist/types/IWebhook.js.map +1 -0
- package/dist/types/IWebhookParameter.d.ts +16 -0
- package/dist/types/IWebhookParameter.d.ts.map +1 -0
- package/dist/types/IWebhookParameter.js +2 -0
- package/dist/types/IWebhookParameter.js.map +1 -0
- package/dist/types/IWebhookResponse.d.ts +62 -0
- package/dist/types/IWebhookResponse.d.ts.map +1 -0
- package/dist/types/IWebhookResponse.js +2 -0
- package/dist/types/IWebhookResponse.js.map +1 -0
- package/dist/types/WebhookResponse.d.ts +38 -0
- package/dist/types/WebhookResponse.d.ts.map +1 -0
- package/dist/types/WebhookResponse.js +2 -0
- package/dist/types/WebhookResponse.js.map +1 -0
- package/dist/types/index.d.ts +16 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +2 -0
- package/dist/types/index.js.map +1 -0
- package/package.json +61 -0
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base error class for ez-hook library
|
|
3
|
+
*/
|
|
4
|
+
export declare class EzHookError extends Error {
|
|
5
|
+
constructor(message: string);
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Error thrown when webhook validation fails
|
|
9
|
+
* @example
|
|
10
|
+
* ```typescript
|
|
11
|
+
* try {
|
|
12
|
+
* webhook.setContent('a'.repeat(2001)); // Too long
|
|
13
|
+
* } catch (error) {
|
|
14
|
+
* if (error instanceof ValidationError) {
|
|
15
|
+
* console.log(`Validation failed for field: ${error.field}`);
|
|
16
|
+
* }
|
|
17
|
+
* }
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export declare class ValidationError extends EzHookError {
|
|
21
|
+
readonly field?: string | undefined;
|
|
22
|
+
readonly maxLength?: number | undefined;
|
|
23
|
+
readonly actualLength?: number | undefined;
|
|
24
|
+
constructor(message: string, field?: string | undefined, maxLength?: number | undefined, actualLength?: number | undefined);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Error thrown when webhook HTTP request fails
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* try {
|
|
31
|
+
* await webhook.send();
|
|
32
|
+
* } catch (error) {
|
|
33
|
+
* if (error instanceof WebhookError) {
|
|
34
|
+
* console.log(`Request failed with status: ${error.statusCode}`);
|
|
35
|
+
* if (error.retryAfter) {
|
|
36
|
+
* console.log(`Retry after: ${error.retryAfter}ms`);
|
|
37
|
+
* }
|
|
38
|
+
* }
|
|
39
|
+
* }
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export declare class WebhookError extends EzHookError {
|
|
43
|
+
readonly statusCode?: number | undefined;
|
|
44
|
+
readonly retryAfter?: number | undefined;
|
|
45
|
+
constructor(message: string, statusCode?: number | undefined, retryAfter?: number | undefined);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Error thrown when rate limit is exceeded
|
|
49
|
+
*/
|
|
50
|
+
export declare class RateLimitError extends WebhookError {
|
|
51
|
+
constructor(retryAfter?: number);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Error thrown when webhook URL is invalid or unreachable
|
|
55
|
+
*/
|
|
56
|
+
export declare class WebhookNotFoundError extends WebhookError {
|
|
57
|
+
constructor();
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=Errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Errors.d.ts","sourceRoot":"","sources":["../../src/classes/Errors.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,qBAAa,WAAY,SAAQ,KAAK;gBACzB,OAAO,EAAE,MAAM;CAI3B;AAED;;;;;;;;;;;;GAYG;AACH,qBAAa,eAAgB,SAAQ,WAAW;aAG9B,KAAK,CAAC,EAAE,MAAM;aACd,SAAS,CAAC,EAAE,MAAM;aAClB,YAAY,CAAC,EAAE,MAAM;gBAHrC,OAAO,EAAE,MAAM,EACC,KAAK,CAAC,EAAE,MAAM,YAAA,EACd,SAAS,CAAC,EAAE,MAAM,YAAA,EAClB,YAAY,CAAC,EAAE,MAAM,YAAA;CAItC;AAED;;;;;;;;;;;;;;;GAeG;AACH,qBAAa,YAAa,SAAQ,WAAW;aAG3B,UAAU,CAAC,EAAE,MAAM;aACnB,UAAU,CAAC,EAAE,MAAM;gBAFnC,OAAO,EAAE,MAAM,EACC,UAAU,CAAC,EAAE,MAAM,YAAA,EACnB,UAAU,CAAC,EAAE,MAAM,YAAA;CAIpC;AAED;;GAEG;AACH,qBAAa,cAAe,SAAQ,YAAY;gBACnC,UAAU,CAAC,EAAE,MAAM;CAG/B;AAED;;GAEG;AACH,qBAAa,oBAAqB,SAAQ,YAAY;;CAIrD"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base error class for ez-hook library
|
|
3
|
+
*/
|
|
4
|
+
export class EzHookError extends Error {
|
|
5
|
+
constructor(message) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = this.constructor.name;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Error thrown when webhook validation fails
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* try {
|
|
15
|
+
* webhook.setContent('a'.repeat(2001)); // Too long
|
|
16
|
+
* } catch (error) {
|
|
17
|
+
* if (error instanceof ValidationError) {
|
|
18
|
+
* console.log(`Validation failed for field: ${error.field}`);
|
|
19
|
+
* }
|
|
20
|
+
* }
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export class ValidationError extends EzHookError {
|
|
24
|
+
field;
|
|
25
|
+
maxLength;
|
|
26
|
+
actualLength;
|
|
27
|
+
constructor(message, field, maxLength, actualLength) {
|
|
28
|
+
super(message);
|
|
29
|
+
this.field = field;
|
|
30
|
+
this.maxLength = maxLength;
|
|
31
|
+
this.actualLength = actualLength;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Error thrown when webhook HTTP request fails
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* try {
|
|
39
|
+
* await webhook.send();
|
|
40
|
+
* } catch (error) {
|
|
41
|
+
* if (error instanceof WebhookError) {
|
|
42
|
+
* console.log(`Request failed with status: ${error.statusCode}`);
|
|
43
|
+
* if (error.retryAfter) {
|
|
44
|
+
* console.log(`Retry after: ${error.retryAfter}ms`);
|
|
45
|
+
* }
|
|
46
|
+
* }
|
|
47
|
+
* }
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export class WebhookError extends EzHookError {
|
|
51
|
+
statusCode;
|
|
52
|
+
retryAfter;
|
|
53
|
+
constructor(message, statusCode, retryAfter) {
|
|
54
|
+
super(message);
|
|
55
|
+
this.statusCode = statusCode;
|
|
56
|
+
this.retryAfter = retryAfter;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Error thrown when rate limit is exceeded
|
|
61
|
+
*/
|
|
62
|
+
export class RateLimitError extends WebhookError {
|
|
63
|
+
constructor(retryAfter) {
|
|
64
|
+
super('Rate limit exceeded', 429, retryAfter);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Error thrown when webhook URL is invalid or unreachable
|
|
69
|
+
*/
|
|
70
|
+
export class WebhookNotFoundError extends WebhookError {
|
|
71
|
+
constructor() {
|
|
72
|
+
super('Webhook not found or invalid URL', 404);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
//# sourceMappingURL=Errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Errors.js","sourceRoot":"","sources":["../../src/classes/Errors.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,OAAO,WAAY,SAAQ,KAAK;IACrC,YAAY,OAAe;QAC1B,KAAK,CAAC,OAAO,CAAC,CAAA;QACd,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI,CAAA;IAClC,CAAC;CACD;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,OAAO,eAAgB,SAAQ,WAAW;IAG9B;IACA;IACA;IAJjB,YACC,OAAe,EACC,KAAc,EACd,SAAkB,EAClB,YAAqB;QAErC,KAAK,CAAC,OAAO,CAAC,CAAA;QAJE,UAAK,GAAL,KAAK,CAAS;QACd,cAAS,GAAT,SAAS,CAAS;QAClB,iBAAY,GAAZ,YAAY,CAAS;IAGtC,CAAC;CACD;AAED;;;;;;;;;;;;;;;GAeG;AACH,MAAM,OAAO,YAAa,SAAQ,WAAW;IAG3B;IACA;IAHjB,YACC,OAAe,EACC,UAAmB,EACnB,UAAmB;QAEnC,KAAK,CAAC,OAAO,CAAC,CAAA;QAHE,eAAU,GAAV,UAAU,CAAS;QACnB,eAAU,GAAV,UAAU,CAAS;IAGpC,CAAC;CACD;AAED;;GAEG;AACH,MAAM,OAAO,cAAe,SAAQ,YAAY;IAC/C,YAAY,UAAmB;QAC9B,KAAK,CAAC,qBAAqB,EAAE,GAAG,EAAE,UAAU,CAAC,CAAA;IAC9C,CAAC;CACD;AAED;;GAEG;AACH,MAAM,OAAO,oBAAqB,SAAQ,YAAY;IACrD;QACC,KAAK,CAAC,kCAAkC,EAAE,GAAG,CAAC,CAAA;IAC/C,CAAC;CACD"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Delays the execution of the code for a specified number of seconds.
|
|
3
|
+
* @param second - The number of seconds to delay.
|
|
4
|
+
* @returns A Promise that resolves after the specified number of seconds.
|
|
5
|
+
*/
|
|
6
|
+
export declare function delay(second: number): Promise<void>;
|
|
7
|
+
export interface RetryConfig {
|
|
8
|
+
maxRetries: number;
|
|
9
|
+
baseDelay: number;
|
|
10
|
+
maxDelay: number;
|
|
11
|
+
}
|
|
12
|
+
export interface RequestResult {
|
|
13
|
+
ok: boolean;
|
|
14
|
+
status: number;
|
|
15
|
+
retryAfter?: number;
|
|
16
|
+
bodyText?: string;
|
|
17
|
+
error?: string;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Class for handling HTTP requests with customizable headers, body, and parameters.
|
|
21
|
+
*
|
|
22
|
+
* @class
|
|
23
|
+
* @internal
|
|
24
|
+
* @category Internal
|
|
25
|
+
*/
|
|
26
|
+
export declare class RequestClient {
|
|
27
|
+
private static DEFAULT_RETRY_CONFIG;
|
|
28
|
+
webhookUrl: string;
|
|
29
|
+
private retryCount;
|
|
30
|
+
retryConfig: RetryConfig;
|
|
31
|
+
constructor(webhookUrl: string, retryConfig?: Partial<RetryConfig>);
|
|
32
|
+
private calculateBackoff;
|
|
33
|
+
private shouldRetry;
|
|
34
|
+
send(method: 'GET' | 'POST' | 'PATCH', data: unknown, init?: Pick<RequestInit, 'signal' | 'headers'> & {
|
|
35
|
+
timeoutMs?: number;
|
|
36
|
+
}): Promise<RequestResult>;
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=RequestClient.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RequestClient.d.ts","sourceRoot":"","sources":["../../src/classes/RequestClient.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,wBAAgB,KAAK,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAInD;AAED,MAAM,WAAW,WAAW;IAC3B,UAAU,EAAE,MAAM,CAAA;IAClB,SAAS,EAAE,MAAM,CAAA;IACjB,QAAQ,EAAE,MAAM,CAAA;CAChB;AACD,MAAM,WAAW,aAAa;IAC7B,EAAE,EAAE,OAAO,CAAA;IACX,MAAM,EAAE,MAAM,CAAA;IACd,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,KAAK,CAAC,EAAE,MAAM,CAAA;CACd;AAED;;;;;;GAMG;AACH,qBAAa,aAAa;IACzB,OAAO,CAAC,MAAM,CAAC,oBAAoB,CAIlC;IAEM,UAAU,EAAE,MAAM,CAAA;IACzB,OAAO,CAAC,UAAU,CAAI;IACf,WAAW,EAAE,WAAW,CAAA;gBAEnB,UAAU,EAAE,MAAM,EAAE,WAAW,CAAC,EAAE,OAAO,CAAC,WAAW,CAAC;IAKlE,OAAO,CAAC,gBAAgB;IAYxB,OAAO,CAAC,WAAW;IAQN,IAAI,CAChB,MAAM,EAAE,KAAK,GAAG,MAAM,GAAG,OAAO,EAChC,IAAI,EAAE,OAAO,EACb,IAAI,CAAC,EAAE,IAAI,CAAC,WAAW,EAAE,QAAQ,GAAG,SAAS,CAAC,GAAG;QAAE,SAAS,CAAC,EAAE,MAAM,CAAA;KAAE,GACrE,OAAO,CAAC,aAAa,CAAC;CAmFzB"}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Delays the execution of the code for a specified number of seconds.
|
|
3
|
+
* @param second - The number of seconds to delay.
|
|
4
|
+
* @returns A Promise that resolves after the specified number of seconds.
|
|
5
|
+
*/
|
|
6
|
+
export function delay(second) {
|
|
7
|
+
return new Promise((resolve) => {
|
|
8
|
+
setTimeout(resolve, second * 1000);
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Class for handling HTTP requests with customizable headers, body, and parameters.
|
|
13
|
+
*
|
|
14
|
+
* @class
|
|
15
|
+
* @internal
|
|
16
|
+
* @category Internal
|
|
17
|
+
*/
|
|
18
|
+
export class RequestClient {
|
|
19
|
+
static DEFAULT_RETRY_CONFIG = {
|
|
20
|
+
maxRetries: 3,
|
|
21
|
+
baseDelay: 1000, // 1 second
|
|
22
|
+
maxDelay: 60000 // 60 seconds
|
|
23
|
+
};
|
|
24
|
+
webhookUrl;
|
|
25
|
+
retryCount = 0;
|
|
26
|
+
retryConfig;
|
|
27
|
+
constructor(webhookUrl, retryConfig) {
|
|
28
|
+
this.webhookUrl = webhookUrl;
|
|
29
|
+
this.retryConfig = { ...RequestClient.DEFAULT_RETRY_CONFIG, ...retryConfig };
|
|
30
|
+
}
|
|
31
|
+
calculateBackoff() {
|
|
32
|
+
// Exponential backoff with jitter
|
|
33
|
+
const exponential = Math.min(this.retryConfig.maxDelay, this.retryConfig.baseDelay * 2 ** this.retryCount);
|
|
34
|
+
// Add random jitter +/- 20%
|
|
35
|
+
const jitter = exponential * 0.2 * (Math.random() - 0.5);
|
|
36
|
+
const withJitter = exponential + jitter;
|
|
37
|
+
return Math.max(0, withJitter);
|
|
38
|
+
}
|
|
39
|
+
shouldRetry(status) {
|
|
40
|
+
// Retry on rate limits and server errors
|
|
41
|
+
return (this.retryCount < this.retryConfig.maxRetries &&
|
|
42
|
+
(status === 429 || status === 0 || (status >= 500 && status < 600)));
|
|
43
|
+
}
|
|
44
|
+
async send(method, data, init) {
|
|
45
|
+
const { signal, headers = {}, timeoutMs } = init ?? {};
|
|
46
|
+
const controller = timeoutMs ? new AbortController() : undefined;
|
|
47
|
+
const timeout = timeoutMs
|
|
48
|
+
? setTimeout(() => controller?.abort(), timeoutMs)
|
|
49
|
+
: undefined;
|
|
50
|
+
try {
|
|
51
|
+
const response = await fetch(this.webhookUrl, {
|
|
52
|
+
method,
|
|
53
|
+
headers: {
|
|
54
|
+
'Content-Type': 'application/json',
|
|
55
|
+
...headers
|
|
56
|
+
},
|
|
57
|
+
body: method === 'GET' ? undefined : JSON.stringify(data),
|
|
58
|
+
signal: controller?.signal ?? signal
|
|
59
|
+
});
|
|
60
|
+
const text = await response.text();
|
|
61
|
+
const retryAfterHeader = response.headers.get('retry-after') ??
|
|
62
|
+
response.headers.get('x-ratelimit-reset-after') ??
|
|
63
|
+
undefined;
|
|
64
|
+
const retryAfterSeconds = retryAfterHeader
|
|
65
|
+
? Number.parseFloat(retryAfterHeader)
|
|
66
|
+
: undefined;
|
|
67
|
+
const retryAfterMs = Number.isFinite(retryAfterSeconds) && retryAfterSeconds
|
|
68
|
+
? retryAfterSeconds * 1000
|
|
69
|
+
: undefined;
|
|
70
|
+
if (response.ok) {
|
|
71
|
+
this.retryCount = 0;
|
|
72
|
+
return {
|
|
73
|
+
ok: true,
|
|
74
|
+
status: response.status,
|
|
75
|
+
retryAfter: retryAfterMs,
|
|
76
|
+
bodyText: text || undefined
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
if (this.shouldRetry(response.status)) {
|
|
80
|
+
this.retryCount++;
|
|
81
|
+
const delayMs = response.status === 429
|
|
82
|
+
? (retryAfterMs ?? this.calculateBackoff())
|
|
83
|
+
: this.calculateBackoff();
|
|
84
|
+
await delay(delayMs / 1000);
|
|
85
|
+
return this.send(method, data, init);
|
|
86
|
+
}
|
|
87
|
+
const errorResult = {
|
|
88
|
+
ok: false,
|
|
89
|
+
status: response.status,
|
|
90
|
+
retryAfter: retryAfterMs,
|
|
91
|
+
bodyText: text || undefined,
|
|
92
|
+
error: text || response.statusText || `HTTP ${response.status}`
|
|
93
|
+
};
|
|
94
|
+
this.retryCount = 0;
|
|
95
|
+
return errorResult;
|
|
96
|
+
}
|
|
97
|
+
catch (_error) {
|
|
98
|
+
const message = _error instanceof Error ? _error.message : _error ? String(_error) : '';
|
|
99
|
+
if (this.shouldRetry(0)) {
|
|
100
|
+
this.retryCount++;
|
|
101
|
+
await delay(this.calculateBackoff() / 1000);
|
|
102
|
+
return this.send(method, data, init);
|
|
103
|
+
}
|
|
104
|
+
this.retryCount = 0;
|
|
105
|
+
return {
|
|
106
|
+
ok: false,
|
|
107
|
+
status: 0,
|
|
108
|
+
error: message || 'Network error'
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
finally {
|
|
112
|
+
if (timeout)
|
|
113
|
+
clearTimeout(timeout);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
//# sourceMappingURL=RequestClient.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RequestClient.js","sourceRoot":"","sources":["../../src/classes/RequestClient.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,UAAU,KAAK,CAAC,MAAc;IACnC,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;QAC9B,UAAU,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC,CAAA;IACnC,CAAC,CAAC,CAAA;AACH,CAAC;AAeD;;;;;;GAMG;AACH,MAAM,OAAO,aAAa;IACjB,MAAM,CAAC,oBAAoB,GAAgB;QAClD,UAAU,EAAE,CAAC;QACb,SAAS,EAAE,IAAI,EAAE,WAAW;QAC5B,QAAQ,EAAE,KAAK,CAAC,aAAa;KAC7B,CAAA;IAEM,UAAU,CAAQ;IACjB,UAAU,GAAG,CAAC,CAAA;IACf,WAAW,CAAa;IAE/B,YAAY,UAAkB,EAAE,WAAkC;QACjE,IAAI,CAAC,UAAU,GAAG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AA;QACF,CAAC;gBAAS,CAAC;YACV,IAAI,OAAO;gBAAE,YAAY,CAAC,OAAO,CAAC,CAAA;QACnC,CAAC;IACF,CAAC"}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import type { IEmbed, IWebhook, IWebhookParameter, WebhookFile } from '../types';
|
|
2
|
+
import type { Embed } from './Embed';
|
|
3
|
+
import { RequestClient, type RequestResult, type RetryConfig } from './RequestClient';
|
|
4
|
+
/**
|
|
5
|
+
* Webhook class
|
|
6
|
+
*
|
|
7
|
+
* ```ts
|
|
8
|
+
* import { Webhook } from '@teever/ez-hook'
|
|
9
|
+
* const webhook = new Webhook('https://discord.com/api/webhooks/1234567890/abcdefghijklmnopqrstuvwxyz')
|
|
10
|
+
* webhook.setUsername('username')
|
|
11
|
+
* webhook.setContent('Hello world!')
|
|
12
|
+
*
|
|
13
|
+
* const success = await webhook.send()
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export declare class Webhook {
|
|
17
|
+
protected client: RequestClient;
|
|
18
|
+
/**
|
|
19
|
+
* Webhook username override.
|
|
20
|
+
*/
|
|
21
|
+
protected username?: string;
|
|
22
|
+
/**
|
|
23
|
+
* Webhook avatar override.
|
|
24
|
+
*/
|
|
25
|
+
protected avatar_url?: string;
|
|
26
|
+
/**
|
|
27
|
+
* Whether or not this notification should be read as text to speech.
|
|
28
|
+
*/
|
|
29
|
+
protected tts?: boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Message contents.
|
|
32
|
+
* Max 2000 characters
|
|
33
|
+
*/
|
|
34
|
+
protected content?: string;
|
|
35
|
+
/**
|
|
36
|
+
* Contents of a file being sent.
|
|
37
|
+
*/
|
|
38
|
+
protected file?: WebhookFile;
|
|
39
|
+
/**
|
|
40
|
+
* Embedded "rich" content.
|
|
41
|
+
*/
|
|
42
|
+
protected embeds?: Array<IEmbed>;
|
|
43
|
+
/**
|
|
44
|
+
*
|
|
45
|
+
* @param webhookUrl
|
|
46
|
+
*/
|
|
47
|
+
constructor(webhookUrl: string, retryConfig?: Partial<RetryConfig>);
|
|
48
|
+
/**
|
|
49
|
+
* Set Webhook username override.
|
|
50
|
+
*
|
|
51
|
+
* @param username string
|
|
52
|
+
* @returns Embed
|
|
53
|
+
*/
|
|
54
|
+
setUsername(username: string): Webhook;
|
|
55
|
+
/**
|
|
56
|
+
* Set Webhook avatar override.
|
|
57
|
+
*
|
|
58
|
+
* @param url string
|
|
59
|
+
* @returns Embed
|
|
60
|
+
*/
|
|
61
|
+
setAvatarUrl(url: string): Webhook;
|
|
62
|
+
/**
|
|
63
|
+
* Set Whether or not this notification should be read as text to speech.
|
|
64
|
+
*
|
|
65
|
+
* @param flag boolean
|
|
66
|
+
* @returns Embed
|
|
67
|
+
*/
|
|
68
|
+
setTTS(flag: boolean): Webhook;
|
|
69
|
+
/**
|
|
70
|
+
* Set Message contents.
|
|
71
|
+
* Max 2000 characters
|
|
72
|
+
*
|
|
73
|
+
* @param content string
|
|
74
|
+
* @returns Embed
|
|
75
|
+
*/
|
|
76
|
+
setContent(content: string): Webhook;
|
|
77
|
+
/**
|
|
78
|
+
* Add Embedded "rich" content.
|
|
79
|
+
* Max 10 embeds
|
|
80
|
+
*
|
|
81
|
+
* @param embed string
|
|
82
|
+
* @returns Embed
|
|
83
|
+
*/
|
|
84
|
+
addEmbed(embed: Embed): Webhook;
|
|
85
|
+
/**
|
|
86
|
+
* Set Contents of a file being sent.
|
|
87
|
+
*
|
|
88
|
+
* @param file Attachment | string
|
|
89
|
+
* @returns Embed
|
|
90
|
+
*/
|
|
91
|
+
setFile(file: WebhookFile): Webhook;
|
|
92
|
+
/**
|
|
93
|
+
* Validate the webhook
|
|
94
|
+
*
|
|
95
|
+
* @returns void
|
|
96
|
+
*/
|
|
97
|
+
validate(): void;
|
|
98
|
+
/**
|
|
99
|
+
* Send webhook
|
|
100
|
+
*
|
|
101
|
+
* @returns Promise<boolean>
|
|
102
|
+
*/
|
|
103
|
+
send(init?: Parameters<RequestClient['send']>[2]): Promise<RequestResult>;
|
|
104
|
+
/**
|
|
105
|
+
* Modify the current webhook.
|
|
106
|
+
*
|
|
107
|
+
* @param options IWebHookParameter
|
|
108
|
+
* @returns Promise<IWebhookResponse>
|
|
109
|
+
*/
|
|
110
|
+
modify(options: IWebhookParameter, init?: Parameters<RequestClient['send']>[2]): Promise<RequestResult>;
|
|
111
|
+
/**
|
|
112
|
+
* Get the current webhook.
|
|
113
|
+
*
|
|
114
|
+
* @returns Promise<IWebhookResponse>
|
|
115
|
+
*/
|
|
116
|
+
get(init?: Parameters<RequestClient['send']>[2]): Promise<RequestResult>;
|
|
117
|
+
/**
|
|
118
|
+
* Check whether or not the current webhook is valid.
|
|
119
|
+
*
|
|
120
|
+
* @returns Promise<boolean>
|
|
121
|
+
*/
|
|
122
|
+
isValid(): Promise<boolean>;
|
|
123
|
+
/**
|
|
124
|
+
* Generate parameter ready object
|
|
125
|
+
*
|
|
126
|
+
* @returns IWebhook
|
|
127
|
+
*/
|
|
128
|
+
toObject(): IWebhook;
|
|
129
|
+
/**
|
|
130
|
+
* Get the current retry configuration.
|
|
131
|
+
*
|
|
132
|
+
* @returns RetryConfig
|
|
133
|
+
*/
|
|
134
|
+
getRetryConfig(): RetryConfig;
|
|
135
|
+
}
|
|
136
|
+
//# sourceMappingURL=Webhook.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Webhook.d.ts","sourceRoot":"","sources":["../../src/classes/Webhook.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAEX,MAAM,EACN,QAAQ,EACR,iBAAiB,EACjB,WAAW,EACX,MAAM,UAAU,CAAA;AACjB,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAEpC,OAAO,EACN,aAAa,EACb,KAAK,aAAa,EAClB,KAAK,WAAW,EAChB,MAAM,iBAAiB,CAAA;AAExB;;;;;;;;;;;GAWG;AAEH,qBAAa,OAAO;IACnB,SAAS,CAAC,MAAM,EAAE,aA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AC;IAIzB;;;;OAIG;IACU,GAAG,CACf,IAAI,CAAC,EAAE,UAAU,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,GACzC,OAAO,CAAC,aAAa,CAAC;IAIzB;;;;OAIG;IACU,OAAO,IAAI,OAAO,CAAC,OAAO,CAAC;IAMxC;;;;OAIG;IACI,QAAQ,IAAI,QAAQ;IAW3B;;;;OAIG;IACI,cAAc,IAAI,WAAW;CAGpC"}
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import { ValidationError } from './Errors';
|
|
2
|
+
import { RequestClient } from './RequestClient';
|
|
3
|
+
/**
|
|
4
|
+
* Webhook class
|
|
5
|
+
*
|
|
6
|
+
* ```ts
|
|
7
|
+
* import { Webhook } from '@teever/ez-hook'
|
|
8
|
+
* const webhook = new Webhook('https://discord.com/api/webhooks/1234567890/abcdefghijklmnopqrstuvwxyz')
|
|
9
|
+
* webhook.setUsername('username')
|
|
10
|
+
* webhook.setContent('Hello world!')
|
|
11
|
+
*
|
|
12
|
+
* const success = await webhook.send()
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export class Webhook {
|
|
16
|
+
client;
|
|
17
|
+
/**
|
|
18
|
+
* Webhook username override.
|
|
19
|
+
*/
|
|
20
|
+
username;
|
|
21
|
+
/**
|
|
22
|
+
* Webhook avatar override.
|
|
23
|
+
*/
|
|
24
|
+
avatar_url;
|
|
25
|
+
/**
|
|
26
|
+
* Whether or not this notification should be read as text to speech.
|
|
27
|
+
*/
|
|
28
|
+
tts;
|
|
29
|
+
/**
|
|
30
|
+
* Message contents.
|
|
31
|
+
* Max 2000 characters
|
|
32
|
+
*/
|
|
33
|
+
content;
|
|
34
|
+
/**
|
|
35
|
+
* Contents of a file being sent.
|
|
36
|
+
*/
|
|
37
|
+
file;
|
|
38
|
+
/**
|
|
39
|
+
* Embedded "rich" content.
|
|
40
|
+
*/
|
|
41
|
+
embeds;
|
|
42
|
+
/**
|
|
43
|
+
*
|
|
44
|
+
* @param webhookUrl
|
|
45
|
+
*/
|
|
46
|
+
constructor(webhookUrl, retryConfig) {
|
|
47
|
+
this.client = new RequestClient(webhookUrl, retryConfig);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Set Webhook username override.
|
|
51
|
+
*
|
|
52
|
+
* @param username string
|
|
53
|
+
* @returns Embed
|
|
54
|
+
*/
|
|
55
|
+
setUsername(username) {
|
|
56
|
+
this.username = username;
|
|
57
|
+
return this;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Set Webhook avatar override.
|
|
61
|
+
*
|
|
62
|
+
* @param url string
|
|
63
|
+
* @returns Embed
|
|
64
|
+
*/
|
|
65
|
+
setAvatarUrl(url) {
|
|
66
|
+
this.avatar_url = url;
|
|
67
|
+
return this;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Set Whether or not this notification should be read as text to speech.
|
|
71
|
+
*
|
|
72
|
+
* @param flag boolean
|
|
73
|
+
* @returns Embed
|
|
74
|
+
*/
|
|
75
|
+
setTTS(flag) {
|
|
76
|
+
this.tts = flag;
|
|
77
|
+
return this;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Set Message contents.
|
|
81
|
+
* Max 2000 characters
|
|
82
|
+
*
|
|
83
|
+
* @param content string
|
|
84
|
+
* @returns Embed
|
|
85
|
+
*/
|
|
86
|
+
setContent(content) {
|
|
87
|
+
if (content.length > 2000) {
|
|
88
|
+
throw new ValidationError('Content length exceeds 2000 characters', 'content', 2000, content.length);
|
|
89
|
+
}
|
|
90
|
+
this.content = content;
|
|
91
|
+
return this;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Add Embedded "rich" content.
|
|
95
|
+
* Max 10 embeds
|
|
96
|
+
*
|
|
97
|
+
* @param embed string
|
|
98
|
+
* @returns Embed
|
|
99
|
+
*/
|
|
100
|
+
addEmbed(embed) {
|
|
101
|
+
if (typeof this.embeds === 'undefined') {
|
|
102
|
+
this.embeds = [embed.toObject()];
|
|
103
|
+
}
|
|
104
|
+
else if (this.embeds.length < 10) {
|
|
105
|
+
this.embeds.push(embed.toObject());
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
throw new ValidationError('Embeds length exceeds 10', 'embeds', 10);
|
|
109
|
+
}
|
|
110
|
+
return this;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Set Contents of a file being sent.
|
|
114
|
+
*
|
|
115
|
+
* @param file Attachment | string
|
|
116
|
+
* @returns Embed
|
|
117
|
+
*/
|
|
118
|
+
setFile(file) {
|
|
119
|
+
this.file = file;
|
|
120
|
+
return this;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Validate the webhook
|
|
124
|
+
*
|
|
125
|
+
* @returns void
|
|
126
|
+
*/
|
|
127
|
+
validate() {
|
|
128
|
+
if ((this.content?.length ?? 0) > 2000) {
|
|
129
|
+
throw new ValidationError('Content length exceeds 2000 characters', 'content', 2000, this.content?.length);
|
|
130
|
+
}
|
|
131
|
+
if ((this.embeds?.length ?? 0) > 10) {
|
|
132
|
+
throw new ValidationError('Embeds length exceeds 10', 'embeds', 10);
|
|
133
|
+
}
|
|
134
|
+
for (const embed of this.embeds ?? []) {
|
|
135
|
+
if ((embed.fields?.length ?? 0) > 25) {
|
|
136
|
+
throw new ValidationError('Fields length exceeds 25', 'fields', 25);
|
|
137
|
+
}
|
|
138
|
+
for (const field of embed.fields ?? []) {
|
|
139
|
+
if ((field.name?.length ?? 0) > 255) {
|
|
140
|
+
throw new ValidationError('Field name length exceeds 255 characters', 'field.name', 255, field.name?.length);
|
|
141
|
+
}
|
|
142
|
+
if ((field.value?.length ?? 0) > 1024) {
|
|
143
|
+
throw new ValidationError('Field value length exceeds 1024 characters', 'field.value', 1024, field.value?.length);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Send webhook
|
|
150
|
+
*
|
|
151
|
+
* @returns Promise<boolean>
|
|
152
|
+
*/
|
|
153
|
+
async send(init) {
|
|
154
|
+
this.validate();
|
|
155
|
+
return this.client.send('POST', this.toObject(), init);
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Modify the current webhook.
|
|
159
|
+
*
|
|
160
|
+
* @param options IWebHookParameter
|
|
161
|
+
* @returns Promise<IWebhookResponse>
|
|
162
|
+
*/
|
|
163
|
+
async modify(options, init) {
|
|
164
|
+
return this.client.send('PATCH', options, init);
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Get the current webhook.
|
|
168
|
+
*
|
|
169
|
+
* @returns Promise<IWebhookResponse>
|
|
170
|
+
*/
|
|
171
|
+
async get(init) {
|
|
172
|
+
return this.client.send('GET', '', init);
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Check whether or not the current webhook is valid.
|
|
176
|
+
*
|
|
177
|
+
* @returns Promise<boolean>
|
|
178
|
+
*/
|
|
179
|
+
async isValid() {
|
|
180
|
+
return fetch(this.client.webhookUrl)
|
|
181
|
+
.then(() => true)
|
|
182
|
+
.catch(() => false);
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Generate parameter ready object
|
|
186
|
+
*
|
|
187
|
+
* @returns IWebhook
|
|
188
|
+
*/
|
|
189
|
+
toObject() {
|
|
190
|
+
return {
|
|
191
|
+
username: this.username,
|
|
192
|
+
avatar_url: this.avatar_url,
|
|
193
|
+
tts: this.tts,
|
|
194
|
+
content: this.content,
|
|
195
|
+
file: this.file,
|
|
196
|
+
embeds: this.embeds
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Get the current retry configuration.
|
|
201
|
+
*
|
|
202
|
+
* @returns RetryConfig
|
|
203
|
+
*/
|
|
204
|
+
getRetryConfig() {
|
|
205
|
+
return this.client.retryConfig;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
//# sourceMappingURL=Webhook.js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