proxy-next-example.ts41 lines · main
| 1 | /** |
| 2 | * Example first-party proxy for Next.js App Router (copy into your app). |
| 3 | * |
| 4 | * File: app/api/auth/[...path]/route.ts |
| 5 | * |
| 6 | * Browser talks to same origin /api/auth/* → cookies on YOUR domain. |
| 7 | * This forwards to Briven API briven-engine FDI. |
| 8 | * |
| 9 | * import { brivenEngineProxyTarget } from '@briven/auth/engine'; |
| 10 | * |
| 11 | * Not imported by default — reference only so apps can paste. |
| 12 | */ |
| 13 | |
| 14 | export const NEXT_PROXY_SNIPPET = ` |
| 15 | // app/api/auth/[...path]/route.ts |
| 16 | import { brivenEngineProxyTarget } from '@briven/auth/engine'; |
| 17 | |
| 18 | const TARGET = brivenEngineProxyTarget(process.env.BRIVEN_API_ORIGIN); |
| 19 | |
| 20 | async function proxy(req: Request, ctx: { params: Promise<{ path: string[] }> }) { |
| 21 | const { path } = await ctx.params; |
| 22 | const url = new URL(req.url); |
| 23 | const dest = \`\${TARGET}/\${path.join('/')}\${url.search}\`; |
| 24 | const headers = new Headers(req.headers); |
| 25 | headers.delete('host'); |
| 26 | headers.set('x-briven-engine', 'briven-engine'); |
| 27 | const res = await fetch(dest, { |
| 28 | method: req.method, |
| 29 | headers, |
| 30 | body: req.method === 'GET' || req.method === 'HEAD' ? undefined : await req.arrayBuffer(), |
| 31 | redirect: 'manual', |
| 32 | }); |
| 33 | return new Response(res.body, { status: res.status, headers: res.headers }); |
| 34 | } |
| 35 | |
| 36 | export const GET = proxy; |
| 37 | export const POST = proxy; |
| 38 | export const PUT = proxy; |
| 39 | export const DELETE = proxy; |
| 40 | export const PATCH = proxy; |
| 41 | `.trim(); |