fix: add better syntax for nextjs proxy

This commit is contained in:
Carl-Gerhard Lindesvärd
2025-11-25 12:10:41 +01:00
parent 6da8267509
commit 828c8c4f91
4 changed files with 11 additions and 10 deletions

View File

@@ -1,5 +1,3 @@
import { createRouteHandler } from '@openpanel/nextjs/server'; import { createRouteHandler } from '@openpanel/nextjs/server';
const routeHandler = createRouteHandler(); export const { GET, POST } = createRouteHandler();
export const GET = routeHandler;
export const POST = routeHandler;

View File

@@ -278,11 +278,7 @@ With `createRouteHandler` you can proxy your events through your server, this wi
```typescript title="/app/api/[...op]/route.ts" ```typescript title="/app/api/[...op]/route.ts"
import { createRouteHandler } from '@openpanel/nextjs/server'; import { createRouteHandler } from '@openpanel/nextjs/server';
const routeHandler = createRouteHandler(); export const { GET, POST } = createRouteHandler();
// Export the same handler for all HTTP methods - it routes internally based on pathname
export const GET = routeHandler;
export const POST = routeHandler;
``` ```
Remember to change the `apiUrl` and `cdnUrl` in the `OpenPanelComponent` to your own server. Remember to change the `apiUrl` and `cdnUrl` in the `OpenPanelComponent` to your own server.

View File

@@ -116,7 +116,7 @@ async function handleScriptProxyRoute(req: Request): Promise<NextResponse> {
function createRouteHandler(options?: RouteHandlerOptions) { function createRouteHandler(options?: RouteHandlerOptions) {
const apiUrl = options?.apiUrl ?? DEFAULT_API_URL; const apiUrl = options?.apiUrl ?? DEFAULT_API_URL;
return async function handler(req: Request): Promise<NextResponse> { const handler = async function handler(req: Request): Promise<NextResponse> {
const url = new URL(req.url); const url = new URL(req.url);
const pathname = url.pathname; const pathname = url.pathname;
const method = req.method; const method = req.method;
@@ -134,6 +134,11 @@ function createRouteHandler(options?: RouteHandlerOptions) {
const apiPath = pathname.substring(apiPathMatch); const apiPath = pathname.substring(apiPathMatch);
return handleApiRoute(req, apiUrl, apiPath); return handleApiRoute(req, apiUrl, apiPath);
}; };
handler.GET = handler;
handler.POST = handler;
return handler;
} }
export { createRouteHandler }; export { createRouteHandler };
@@ -141,3 +146,5 @@ export { createRouteHandler };
// const routeHandler = createRouteHandler(); // const routeHandler = createRouteHandler();
// export const GET = routeHandler; // export const GET = routeHandler;
// export const POST = routeHandler; // export const POST = routeHandler;
// Or
// export const { GET, POST } = createRouteHandler();

View File

@@ -1,6 +1,6 @@
{ {
"name": "@openpanel/nextjs", "name": "@openpanel/nextjs",
"version": "1.1.0-local", "version": "1.1.1-local",
"module": "index.ts", "module": "index.ts",
"scripts": { "scripts": {
"build": "rm -rf dist && tsup", "build": "rm -rf dist && tsup",