Skip to content

Commit

Permalink
testing
Browse files Browse the repository at this point in the history
  • Loading branch information
toddkao committed Dec 23, 2024
1 parent aea9bea commit d20debd
Showing 1 changed file with 32 additions and 25 deletions.
57 changes: 32 additions & 25 deletions examples/nextjs/middleware.ts
Original file line number Diff line number Diff line change
@@ -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
}
// Add other methods (PUT, DELETE, etc.) as needed

0 comments on commit d20debd

Please sign in to comment.