Skip to content

Commit 828c8c4

Browse files
committed
fix: add better syntax for nextjs proxy
1 parent 6da8267 commit 828c8c4

File tree

4 files changed

+11
-10
lines changed

4 files changed

+11
-10
lines changed
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
import { createRouteHandler } from '@openpanel/nextjs/server';
22

3-
const routeHandler = createRouteHandler();
4-
export const GET = routeHandler;
5-
export const POST = routeHandler;
3+
export const { GET, POST } = createRouteHandler();

apps/public/content/docs/(tracking)/sdks/nextjs.mdx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,7 @@ With `createRouteHandler` you can proxy your events through your server, this wi
278278
```typescript title="/app/api/[...op]/route.ts"
279279
import { createRouteHandler } from '@openpanel/nextjs/server';
280280

281-
const routeHandler = createRouteHandler();
282-
283-
// Export the same handler for all HTTP methods - it routes internally based on pathname
284-
export const GET = routeHandler;
285-
export const POST = routeHandler;
281+
export const { GET, POST } = createRouteHandler();
286282
```
287283

288284
Remember to change the `apiUrl` and `cdnUrl` in the `OpenPanelComponent` to your own server.

packages/sdks/nextjs/createNextRouteHandler.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ async function handleScriptProxyRoute(req: Request): Promise<NextResponse> {
116116
function createRouteHandler(options?: RouteHandlerOptions) {
117117
const apiUrl = options?.apiUrl ?? DEFAULT_API_URL;
118118

119-
return async function handler(req: Request): Promise<NextResponse> {
119+
const handler = async function handler(req: Request): Promise<NextResponse> {
120120
const url = new URL(req.url);
121121
const pathname = url.pathname;
122122
const method = req.method;
@@ -134,10 +134,17 @@ function createRouteHandler(options?: RouteHandlerOptions) {
134134
const apiPath = pathname.substring(apiPathMatch);
135135
return handleApiRoute(req, apiUrl, apiPath);
136136
};
137+
138+
handler.GET = handler;
139+
handler.POST = handler;
140+
141+
return handler;
137142
}
138143

139144
export { createRouteHandler };
140145

141146
// const routeHandler = createRouteHandler();
142147
// export const GET = routeHandler;
143148
// export const POST = routeHandler;
149+
// Or
150+
// export const { GET, POST } = createRouteHandler();

packages/sdks/nextjs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@openpanel/nextjs",
3-
"version": "1.1.0-local",
3+
"version": "1.1.1-local",
44
"module": "index.ts",
55
"scripts": {
66
"build": "rm -rf dist && tsup",

0 commit comments

Comments
 (0)