From d20debd1ea308d3e98573ee02a7d623289b60013 Mon Sep 17 00:00:00 2001 From: Todd Kao Date: Sun, 22 Dec 2024 19:25:50 -0500 Subject: [PATCH] testing --- examples/nextjs/middleware.ts | 57 ++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 25 deletions(-) diff --git a/examples/nextjs/middleware.ts b/examples/nextjs/middleware.ts index 2d8588f7..8d8931a1 100644 --- a/examples/nextjs/middleware.ts +++ b/examples/nextjs/middleware.ts @@ -1,28 +1,35 @@ -import { NextResponse } from 'next/server' -import type { NextRequest } from 'next/server' - -export function middleware(request: NextRequest) { - // Only handle requests to /api/skip/* - if (!request.nextUrl.pathname.startsWith('/api/skip/')) { - return NextResponse.next() - } - - // Transform the request URL from /api/skip/* to go.skip.build/api/skip/* - const skipUrl = new URL(request.nextUrl.pathname.replace('/api/skip/', '/api/skip/'), 'https://go.skip.build') - skipUrl.search = request.nextUrl.search // Preserve query parameters - - // Forward the request to Skip API - return NextResponse.rewrite(skipUrl, { - request: { - // Forward headers - headers: new Headers({ - 'Content-Type': 'application/json', - // Add any other required headers - }) - } +import { NextRequest } from 'next/server' + +export async function GET(request: NextRequest, { params }: { params: { path?: string[] } }) { + const path = params.path?.join('/') || '' + const { searchParams } = new URL(request.url) + + const skipUrl = `https://go.skip.build/api/skip/${path}${searchParams.toString() ? '?' + searchParams.toString() : ''}` + + const response = await fetch(skipUrl, { + headers: { + 'Content-Type': 'application/json', + }, + }) + + return response +} + +export async function POST(request: NextRequest, { params }: { params: { path?: string[] } }) { + const path = params.path?.join('/') || '' + const { searchParams } = new URL(request.url) + + const skipUrl = `https://go.skip.build/api/skip/${path}${searchParams.toString() ? '?' + searchParams.toString() : ''}` + + const response = await fetch(skipUrl, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(await request.json()), }) + + return response } -export const config = { - matcher: '/api/skip/:path*', // Only match /api/skip routes -} \ No newline at end of file +// Add other methods (PUT, DELETE, etc.) as needed \ No newline at end of file