@minesa-org/mini-interaction 0.2.24 → 0.2.25

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.
@@ -190,6 +190,8 @@ export declare class MiniInteraction {
190
190
  private loadComponentsPromise;
191
191
  private registerCommandsPromise;
192
192
  private registerCommandsSignature;
193
+ private vercelWaitUntil;
194
+ private searchedForVercel;
193
195
  /**
194
196
  * Creates a new MiniInteraction client with optional command auto-loading and custom runtime hooks.
195
197
  */
@@ -208,6 +210,10 @@ export declare class MiniInteraction {
208
210
  * Call this periodically to clean up old interaction data.
209
211
  */
210
212
  cleanupExpiredInteractions(): number;
213
+ /**
214
+ * Attempt to find a waitUntil implementation in the environment (e.g. Vercel or Cloudflare).
215
+ */
216
+ private getAutoWaitUntil;
211
217
  private normalizeCommandData;
212
218
  private registerCommand;
213
219
  /**
@@ -286,7 +292,9 @@ export declare class MiniInteraction {
286
292
  * Creates a Node.js style request handler compatible with Express, Next.js API routes,
287
293
  * Vercel serverless functions, and any runtime that expects a `(req, res)` listener.
288
294
  */
289
- createNodeHandler(): InteractionNodeHandler;
295
+ createNodeHandler(options?: {
296
+ waitUntil?: (promise: Promise<void>) => void;
297
+ }): InteractionNodeHandler;
290
298
  /**
291
299
  * Generates a lightweight verification handler that serves an HTML page with an embedded OAuth link.
292
300
  *
@@ -340,7 +348,13 @@ export declare class MiniInteraction {
340
348
  /**
341
349
  * Creates a Fetch API compatible handler for runtimes like Workers or Deno.
342
350
  */
343
- createFetchHandler(): InteractionFetchHandler;
351
+ /**
352
+ * Generates a Fetch-standard request handler compatible with Cloudflare Workers,
353
+ * Bun, Deno, and Next.js Edge Runtime.
354
+ */
355
+ createFetchHandler(options?: {
356
+ waitUntil?: (promise: Promise<void>) => void;
357
+ }): InteractionFetchHandler;
344
358
  /**
345
359
  * Checks if the provided directory path exists on disk.
346
360
  */
@@ -43,6 +43,8 @@ export class MiniInteraction {
43
43
  loadComponentsPromise = null;
44
44
  registerCommandsPromise = null;
45
45
  registerCommandsSignature = null;
46
+ vercelWaitUntil = null;
47
+ searchedForVercel = false;
46
48
  /**
47
49
  * Creates a new MiniInteraction client with optional command auto-loading and custom runtime hooks.
48
50
  */
@@ -129,6 +131,33 @@ export class MiniInteraction {
129
131
  }
130
132
  return cleaned;
131
133
  }
134
+ /**
135
+ * Attempt to find a waitUntil implementation in the environment (e.g. Vercel or Cloudflare).
136
+ */
137
+ async getAutoWaitUntil() {
138
+ if (this.searchedForVercel)
139
+ return this.vercelWaitUntil;
140
+ this.searchedForVercel = true;
141
+ // Try Vercel's @vercel/functions
142
+ try {
143
+ // @ts-ignore - Dynamic import to avoid hard dependency or build errors in non-vercel environments
144
+ const { waitUntil } = await import("@vercel/functions");
145
+ if (typeof waitUntil === "function") {
146
+ this.vercelWaitUntil = waitUntil;
147
+ return waitUntil;
148
+ }
149
+ }
150
+ catch {
151
+ // Ignore if not found
152
+ }
153
+ // Try globalThis.waitUntil (some edge runtimes)
154
+ // @ts-ignore
155
+ if (typeof globalThis.waitUntil === "function") {
156
+ // @ts-ignore
157
+ return globalThis.waitUntil;
158
+ }
159
+ return null;
160
+ }
132
161
  normalizeCommandData(data) {
133
162
  if (typeof data === "object" && data !== null) {
134
163
  return resolveJSONEncodable(data);
@@ -471,7 +500,8 @@ export class MiniInteraction {
471
500
  * Creates a Node.js style request handler compatible with Express, Next.js API routes,
472
501
  * Vercel serverless functions, and any runtime that expects a `(req, res)` listener.
473
502
  */
474
- createNodeHandler() {
503
+ createNodeHandler(options) {
504
+ const { waitUntil } = options ?? {};
475
505
  return (request, response) => {
476
506
  if (request.method !== "POST") {
477
507
  response.statusCode = 405;
@@ -508,6 +538,12 @@ export class MiniInteraction {
508
538
  signature,
509
539
  timestamp,
510
540
  });
541
+ if (result.backgroundWork) {
542
+ const resolvedWaitUntil = waitUntil ?? await this.getAutoWaitUntil();
543
+ if (resolvedWaitUntil) {
544
+ resolvedWaitUntil(result.backgroundWork);
545
+ }
546
+ }
511
547
  response.statusCode = result.status;
512
548
  response.setHeader("content-type", "application/json");
513
549
  response.end(JSON.stringify(result.body));
@@ -756,7 +792,12 @@ export class MiniInteraction {
756
792
  /**
757
793
  * Creates a Fetch API compatible handler for runtimes like Workers or Deno.
758
794
  */
759
- createFetchHandler() {
795
+ /**
796
+ * Generates a Fetch-standard request handler compatible with Cloudflare Workers,
797
+ * Bun, Deno, and Next.js Edge Runtime.
798
+ */
799
+ createFetchHandler(options) {
800
+ const { waitUntil } = options ?? {};
760
801
  return async (request) => {
761
802
  if (request.method !== "POST") {
762
803
  return new Response(JSON.stringify({
@@ -776,6 +817,12 @@ export class MiniInteraction {
776
817
  signature,
777
818
  timestamp,
778
819
  });
820
+ if (result.backgroundWork) {
821
+ const resolvedWaitUntil = waitUntil ?? await this.getAutoWaitUntil();
822
+ if (resolvedWaitUntil) {
823
+ resolvedWaitUntil(result.backgroundWork);
824
+ }
825
+ }
779
826
  return new Response(JSON.stringify(result.body), {
780
827
  status: result.status,
781
828
  headers: { "content-type": "application/json" },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@minesa-org/mini-interaction",
3
- "version": "0.2.24",
3
+ "version": "0.2.25",
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",