Files
jaengseung-made/app/api/lotto/_nas.ts
gahusb 040866292e fix: 로또 API 504 타임아웃 및 Application error 수정
- _nas.ts: AbortSignal timeout 10s → 25s (NAS 무거운 연산 대응)
- stats/performance, report/latest, report/history: maxDuration = 60 추가 (Vercel 함수 타임아웃 연장)
- ReportTab: 에러 응답({error:"NAS_TIMEOUT"}) 받을 시 렌더 전 차단, confidence_factors null guard 추가
- PurchaseTab: API 에러 응답 감지 후 조용히 빈 상태 유지
- PatternTab: 에러 응답 감지 후 에러 메시지 표시

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-20 01:55:48 +09:00

87 lines
3.2 KiB
TypeScript

import { NextResponse } from 'next/server';
import { createClient } from '@/lib/supabase/server';
const LOTTO_PRODUCT_IDS = ['lotto_gold', 'lotto_platinum', 'lotto_diamond', 'lotto_annual'];
function nasHeaders() {
const h: Record<string, string> = {};
if (process.env.NAS_LOTTO_API_KEY) h['Authorization'] = `Bearer ${process.env.NAS_LOTTO_API_KEY}`;
return h;
}
function nasBase() {
const base = process.env.NAS_LOTTO_API_URL;
if (!base) throw new Error('NAS_URL_NOT_CONFIGURED');
return base;
}
export async function nasGet(path: string, timeoutMs = 25000): Promise<unknown> {
const res = await fetch(`${nasBase()}${path}`, {
headers: nasHeaders(), signal: AbortSignal.timeout(timeoutMs),
});
if (!res.ok) throw new Error(`NAS_${res.status}`);
return res.json();
}
export async function nasPost(path: string, body: unknown): Promise<unknown> {
const res = await fetch(`${nasBase()}${path}`, {
method: 'POST',
headers: { ...nasHeaders(), 'Content-Type': 'application/json' },
body: JSON.stringify(body),
signal: AbortSignal.timeout(25000),
});
if (!res.ok) throw new Error(`NAS_${res.status}`);
return res.json();
}
export async function nasPut(path: string, body: unknown): Promise<unknown> {
const res = await fetch(`${nasBase()}${path}`, {
method: 'PUT',
headers: { ...nasHeaders(), 'Content-Type': 'application/json' },
body: JSON.stringify(body),
signal: AbortSignal.timeout(25000),
});
if (!res.ok) throw new Error(`NAS_${res.status}`);
return res.json();
}
export async function nasDelete(path: string): Promise<unknown> {
const res = await fetch(`${nasBase()}${path}`, {
method: 'DELETE', headers: nasHeaders(), signal: AbortSignal.timeout(25000),
});
if (!res.ok) throw new Error(`NAS_${res.status}`);
return res.json();
}
export interface AuthResult { userId: string; plan: string; }
export async function requireSubscription(): Promise<AuthResult | NextResponse> {
const supabase = await createClient();
const { data: { user }, error } = await supabase.auth.getUser();
if (error || !user) return NextResponse.json({ error: 'UNAUTHORIZED' }, { status: 401 });
const { data: sub } = await supabase
.from('subscriptions').select('product_id')
.eq('user_id', user.id).eq('status', 'active')
.in('product_id', LOTTO_PRODUCT_IDS).maybeSingle();
if (sub) return { userId: user.id, plan: sub.product_id };
const ago31 = new Date(Date.now() - 31 * 24 * 60 * 60 * 1000).toISOString();
const { data: order } = await supabase
.from('orders').select('product_id')
.eq('user_id', user.id).eq('status', 'paid')
.in('product_id', LOTTO_PRODUCT_IDS)
.gte('created_at', ago31)
.order('created_at', { ascending: false }).limit(1).maybeSingle();
if (order) return { userId: user.id, plan: order.product_id };
return NextResponse.json({ error: 'NOT_SUBSCRIBED' }, { status: 403 });
}
export function handleNasError(err: unknown): NextResponse {
const e = err as { name?: string };
if (e?.name === 'TimeoutError') return NextResponse.json({ error: 'NAS_TIMEOUT' }, { status: 504 });
console.error('[NAS]', err);
return NextResponse.json({ error: 'INTERNAL_ERROR' }, { status: 500 });
}