predikt/oracle/web/proxy.ts
caelum0x 2b68bde5d5 Predikt: full Polymarket-style prediction market — web + mobile + on-chain + bots
Working tree is now the canonical repo. Off-chain play-money + trustless on-chain
(FPMM AMM + CLOB via UMA settlement), AI market factory, creator/social/distribution,
mobile shell + push, one-command demo + deploy-kit. Secrets excluded (see ENV-AND-KEYS.md).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-06 23:29:50 +03:00

74 lines
1.9 KiB
TypeScript

import { PROD_CONFIG } from 'common/envs/prod'
import { NextResponse, type NextRequest } from 'next/server'
export async function proxy(req: NextRequest) {
const url = req.nextUrl
// Handle play parameter removal for all requests
if (url.searchParams.has('play')) {
const playValue = url.searchParams.get('play')
url.searchParams.delete('play')
if (playValue === 'false') {
// Redirect to path with --cash suffix and no query parameters
const newUrl = new URL(url.pathname + '--cash', url.origin)
return NextResponse.redirect(newUrl, 308)
} else {
return NextResponse.redirect(url, 308)
}
}
// Only run API proxy logic for API requests
if (url.pathname.startsWith('/api/')) {
const path = req.nextUrl.pathname.replace('/api/', '')
if (pathsToSkip.includes(path)) {
return NextResponse.next()
}
return new Response('Permanent Redirect', {
status: 308,
headers: {
location: getProxiedRequestUrl(req, path),
},
})
}
// For non-API requests, just continue normally
return NextResponse.next()
}
export const config = {
matcher: [
// API proxy
'/api/v0/:path*',
// Contract pages - be specific about the format
// This matches /username/contract-slug but not / or /browse etc
'/([^/]+)/([^/]+)',
// Embed pages
'/embed/([^/]+)/([^/]+)',
],
}
const pathsToSkip = ['v0/deployment-id', 'v0/revalidate']
function getProxiedRequestUrl(req: NextRequest, path: string) {
const baseUrl = getApiUrl(path)
const [_prefix, qs] = req.url!.split('?', 2)
if (qs) {
return baseUrl + '?' + qs
} else {
return baseUrl
}
}
// copied from common/src/utils/api. TODO the right thing
function getApiUrl(path: string) {
if (process.env.NEXT_PUBLIC_API_URL) {
return `http://${process.env.NEXT_PUBLIC_API_URL}/${path}`
} else {
const { apiEndpoint } = PROD_CONFIG
return `https://${apiEndpoint}/${path}`
}
}