Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : Vrai-Prix8 * File : src/lib/api.ts9 * Module : lib10 * Repository : https://git.spboucher.ai/vrai-prix11 * Created : 2026-11-2612 * Last Modified : 2026-11-2613 *14 * Author : Simon-Pierre Boucher15 * Position : Professor of Finance16 * Institution : Université du Québec en Outaouais (UQO)17 * Organization : SP Boucher18 * Website : https://spboucher.ai19 * Git : https://git.spboucher.ai20 *21 * Preferred Contact: contact@spboucher.ai22 * Development : dev@spboucher.ai23 * Support : support@spboucher.ai24 * Security/Admin : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142// Aides communes des routes /api/** : réponses JSON homogènes, erreurs, routes43// retirées. Les en-têtes CORS (lecture ouverte) et X-Robots-Tag sont posés44// pour tout /api/** par next.config.ts.45import { NextResponse } from "next/server";4647/** Cache HTTP public : `maxAge` secondes, puis servi périmé pendant la revalidation. */48export function cache(maxAge: number, swr = maxAge * 4): Record<string, string> {49 return { "Cache-Control": `public, max-age=${maxAge}, stale-while-revalidate=${swr}` };50}5152export const SANS_CACHE: Record<string, string> = { "Cache-Control": "no-store" };5354export function json<T>(data: T, headers: Record<string, string> = SANS_CACHE, status = 200): NextResponse {55 return NextResponse.json(data, { status, headers });56}5758/** Erreur JSON : `{ error, code }`. */59export function erreur(status: number, message: string, code?: string): NextResponse {60 return NextResponse.json({ error: message, code: code ?? String(status) }, { status, headers: SANS_CACHE });61}6263/**64 * Route retirée dans la v2 (comptes, connexion, favoris serveur, agent du65 * groupe) : 410 Gone, pour que clients et robots cessent de l'appeler.66 */67export function retire(quoi: string): NextResponse {68 return NextResponse.json(69 {70 error: `${quoi} : retiré dans Vrai-Prix v2. Aucun compte : les favoris et réglages restent sur l'appareil.`,71 code: "gone",72 },73 { status: 410, headers: SANS_CACHE }74 );75}7677/** Page HTML retirée (ancienne adresse publique sans équivalent). */78export function pageRetiree(titre: string): Response {79 const html = `<!doctype html><html lang="fr-CA"><head><meta charset="utf-8"><meta name="robots" content="noindex"><meta name="viewport" content="width=device-width,initial-scale=1"><title>${titre} — Vrai-Prix</title><style>body{font:16px/1.5 system-ui,sans-serif;max-width:34rem;margin:18vh auto;padding:0 1.25rem;color:#141814;background:#f5f3ee}a{color:inherit}</style></head><body><h1 style="font-size:1.4rem">${titre}</h1><p>Cette page n'existe plus. Vrai-Prix fonctionne sans compte : vos favoris et réglages restent sur votre appareil.</p><p><a href="/">Retour à l'accueil</a> · <a href="/favoris">Mes favoris</a></p></body></html>`;80 return new Response(html, { status: 410, headers: { "Content-Type": "text/html; charset=utf-8", "Cache-Control": "no-store" } });81}8283/** Lit `bbox=ouest,sud,est,nord` (degrés). Renvoie null si absent ou invalide. */84export function lireBbox(v: string | null): { west: number; south: number; east: number; north: number } | null {85 if (!v) return null;86 const p = v.split(",").map((x) => Number(x));87 if (p.length !== 4 || p.some((x) => !Number.isFinite(x))) return null;88 const [west, south, east, north] = p;89 if (west >= east || south >= north) return null;90 if (south < -90 || north > 90 || west < -180 || east > 180) return null;91 return { west, south, east, north };92}9394export function entier(v: string | null, defaut: number, min: number, max: number): number {95 const n = v == null || v === "" ? NaN : Number(v);96 if (!Number.isFinite(n)) return defaut;97 return Math.min(max, Math.max(min, Math.floor(n)));98}99