Skip to content

Commit 2a4236e

Browse files
authored
Merge pull request #363 from Secreto31126/next-middleware
Added NextJS App Router middleware
2 parents 716b10c + 4e673d6 commit 2a4236e

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

src/middleware/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@ export { WhatsAppAPI as VercelMiddleware } from "./vercel.js";
3838
export { WhatsAppAPI as DenoMiddleware } from "./deno.js";
3939
export { WhatsAppAPI as BunMiddleware } from "./bun.js";
4040
export { WhatsAppAPI as SvelteKitMiddleware } from "./sveltekit.js";
41+
export { WhatsAppAPI as NextAppMiddleware } from "./next.js";
4142
export { WhatsAppAPI as WebStandardMiddleware } from "./web-standard.js";
4243
export { WhatsAppAPI as NodeHTTPMiddleware } from "./node-http.js";

src/middleware/next.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { WhatsAppAPI as WebStandardMiddleware } from "./web-standard.js";
2+
3+
/**
4+
* NextJS App Router Endpoints middleware for WhatsAppAPI
5+
*/
6+
export class WhatsAppAPI extends WebStandardMiddleware {
7+
/**
8+
* POST request handler for NextJS App Router
9+
*
10+
* @example
11+
* ```ts
12+
* import { WhatsAppAPI } from 'whatsapp-api-js/middleware/next';
13+
*
14+
* const Whatsapp = new WhatsAppAPI({
15+
* token: "YOUR_TOKEN",
16+
* appSecret: "YOUR_APP_SECRET",
17+
* webhookVerifyToken: "YOUR_WEBHOOK_VERIFY_TOKEN"
18+
* });
19+
*
20+
* export async function POST(request: Request) {
21+
* return new Response(null, {
22+
* status: await Whatsapp.handle_post(request)
23+
* });
24+
* }
25+
* ```
26+
*
27+
* @param req - The request object
28+
* @returns The status code to be sent to the client
29+
*/
30+
async handle_post(req: Request) {
31+
return super.handle_post(req);
32+
}
33+
34+
/**
35+
* GET request handler for NextJS App Router
36+
*
37+
* @example
38+
* ```ts
39+
* import { WhatsAppAPI } from 'whatsapp-api-js/middleware/next';
40+
*
41+
* const Whatsapp = new WhatsAppAPI({
42+
* token: "YOUR_TOKEN",
43+
* appSecret: "YOUR_APP_SECRET",
44+
* webhookVerifyToken: "YOUR_WEBHOOK_VERIFY_TOKEN"
45+
* });
46+
*
47+
* export async function GET(request: Request) {
48+
* try {
49+
* return new Response(Whatsapp.handle_get(request));
50+
* } catch (e) {
51+
* return new Response(null, { status: e as number });
52+
* }
53+
* }
54+
* ```
55+
*
56+
* @param req - The request object
57+
* @returns The challenge string to be sent to the client
58+
* @throws The error code
59+
*/
60+
handle_get(req: Request) {
61+
return super.handle_get(req);
62+
}
63+
}

src/middleware/web-standard.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { isInteger } from "../utils.js";
44
import type { GetParams } from "../types";
55

66
/**
7-
* Web Standard API http server middleware for WhatsAppAPI (deno/bun/SvelteKit/Hono)
7+
* Web Standard API http server middleware for WhatsAppAPI (deno/bun/SvelteKit/NextJS/Hono)
88
*/
99
export class WhatsAppAPI extends WhatsAppAPIMiddleware {
1010
/**

0 commit comments

Comments
 (0)