|
| 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 | +} |
0 commit comments