@minesa-org/mini-interaction 0.4.9 → 0.4.10

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.
@@ -15,6 +15,7 @@ export declare class DiscordRestClient {
15
15
  request<T>(path: string, init?: RequestInit & {
16
16
  authenticated?: boolean;
17
17
  }): Promise<T>;
18
+ private createRequestError;
18
19
  createFollowup(interactionToken: string, body: unknown): Promise<unknown>;
19
20
  editOriginal(interactionToken: string, body: unknown): Promise<unknown>;
20
21
  }
@@ -14,18 +14,33 @@ export class DiscordRestClient {
14
14
  let lastError;
15
15
  const { authenticated = true, ...requestInit } = init;
16
16
  for (let attempt = 0; attempt <= this.maxRetries; attempt += 1) {
17
- const response = await this.fetchImpl(`${this.baseUrl}${path}`, {
18
- ...requestInit,
19
- headers: {
20
- ...(authenticated ? { Authorization: `Bot ${this.options.token}` } : {}),
21
- 'Content-Type': 'application/json',
22
- ...(requestInit.headers ?? {}),
23
- },
24
- });
17
+ let response;
18
+ try {
19
+ response = await this.fetchImpl(`${this.baseUrl}${path}`, {
20
+ ...requestInit,
21
+ headers: {
22
+ ...(authenticated ? { Authorization: `Bot ${this.options.token}` } : {}),
23
+ 'Content-Type': 'application/json',
24
+ ...(requestInit.headers ?? {}),
25
+ },
26
+ });
27
+ }
28
+ catch (error) {
29
+ lastError = this.createRequestError(path, requestInit.method, error);
30
+ if (attempt < this.maxRetries) {
31
+ await sleep(150 * (attempt + 1));
32
+ continue;
33
+ }
34
+ break;
35
+ }
25
36
  if (response.status === 429) {
26
- const retryAfter = Number(response.headers.get('retry-after') ?? '1');
27
- await sleep(Math.ceil(retryAfter * 1000));
28
- continue;
37
+ if (attempt < this.maxRetries) {
38
+ const retryAfter = Number(response.headers.get('retry-after') ?? '1');
39
+ await sleep(Math.ceil(retryAfter * 1000));
40
+ continue;
41
+ }
42
+ lastError = new Error(`[DiscordRestClient] ${requestInit.method ?? 'GET'} ${path} failed: 429`);
43
+ break;
29
44
  }
30
45
  if (response.ok) {
31
46
  if (response.status === 204)
@@ -41,6 +56,10 @@ export class DiscordRestClient {
41
56
  }
42
57
  throw lastError instanceof Error ? lastError : new Error('[DiscordRestClient] unknown request failure');
43
58
  }
59
+ createRequestError(path, method, error) {
60
+ const message = error instanceof Error ? error.message : String(error);
61
+ return new Error(`[DiscordRestClient] ${method ?? 'GET'} ${path} failed: ${message}`, { cause: error instanceof Error ? error : undefined });
62
+ }
44
63
  createFollowup(interactionToken, body) {
45
64
  return this.request(`/webhooks/${this.options.applicationId}/${interactionToken}`, {
46
65
  method: 'POST',
@@ -29,6 +29,27 @@ test('editReply and followUp call webhook endpoints', async () => {
29
29
  assert.equal(calls.length, 2);
30
30
  assert.match(calls[0], /messages\/\@original/);
31
31
  });
32
+ test('editReply retries transient transport failures', async () => {
33
+ let attempts = 0;
34
+ const fetchImpl = (async () => {
35
+ attempts += 1;
36
+ if (attempts === 1) {
37
+ throw new TypeError('fetch failed', {
38
+ cause: { code: 'UND_ERR_SOCKET' },
39
+ });
40
+ }
41
+ return new Response(JSON.stringify({ ok: true }), { status: 200 });
42
+ });
43
+ const rest = new DiscordRestClient({
44
+ token: 'x',
45
+ applicationId: 'app',
46
+ fetchImplementation: fetchImpl,
47
+ maxRetries: 1,
48
+ });
49
+ const ctx = new InteractionContext({ interaction, rest });
50
+ await ctx.editReply({ content: 'edit' });
51
+ assert.equal(attempts, 2);
52
+ });
32
53
  test('auto-ack diagnostics callback fires for slow handler', async () => {
33
54
  let message = '';
34
55
  const { rest } = createRest();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@minesa-org/mini-interaction",
3
- "version": "0.4.9",
3
+ "version": "0.4.10",
4
4
  "description": "Mini interaction, connecting your app with Discord via HTTP-interaction (Vercel support).",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",