23aa78d3da
- GET /api/sheets/tabs : liste les onglets Google Sheets disponibles - readSheetProducts(tab?) et fetchPendingProducts(set, tab?) acceptent un filtre - /api/sync/preview?tab=xxx et /api/creation/preview?tab=xxx filtrent sur un onglet - Composant FamilySelector : grille de boutons chargeant les onglets dynamiquement - Page Sync : étape 0 de sélection famille avant l'analyse - Page Création : étape famille avant analyse, badge de famille sélectionnée Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
32 lines
1.2 KiB
TypeScript
32 lines
1.2 KiB
TypeScript
import { NextResponse } from 'next/server'
|
|
import { getServerSession } from 'next-auth'
|
|
import { authOptions } from '@/lib/auth'
|
|
|
|
export async function GET() {
|
|
const session = await getServerSession(authOptions)
|
|
if (!session) return NextResponse.json({ error: 'Non authentifié' }, { status: 401 })
|
|
|
|
const apiKey = process.env.GOOGLE_API_KEY
|
|
const sheetId = process.env.GOOGLE_SHEETS_ID
|
|
if (!apiKey || !sheetId) {
|
|
return NextResponse.json({ error: 'GOOGLE_API_KEY et GOOGLE_SHEETS_ID requis' }, { status: 500 })
|
|
}
|
|
|
|
try {
|
|
const url = `https://sheets.googleapis.com/v4/spreadsheets/${sheetId}?key=${apiKey}&fields=sheets.properties.title`
|
|
const res = await fetch(url, { cache: 'no-store' })
|
|
if (!res.ok) {
|
|
const err = await res.text()
|
|
throw new Error(`Google Sheets API error: ${res.status} — ${err}`)
|
|
}
|
|
const data = await res.json()
|
|
const tabs: string[] = (data.sheets as Array<{ properties: { title: string } }>).map(
|
|
(s) => s.properties.title,
|
|
)
|
|
return NextResponse.json({ tabs })
|
|
} catch (err) {
|
|
const message = err instanceof Error ? err.message : 'Erreur inconnue'
|
|
return NextResponse.json({ error: message }, { status: 500 })
|
|
}
|
|
}
|