SPB Git

Repository Public

route.ts 2.9 KB 80 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : Vrai-Prix8 * File             : src/app/api/municipalites/route.ts9 * Module           : app10 * 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// GET /api/municipalites[?q=][&limit=] — les 1 097 municipalités du rôle43// (nom, slug, nombre d'unités, centroïde), de la plus grande à la plus petite.44// `q` filtre sur le début d'un mot du nom, sans égard aux accents ni à la casse.45import { NextRequest } from "next/server";46import { allMunicipalities } from "@/lib/municipalites";47import { slugify } from "@/lib/seo";48import { cache, entier, erreur, json } from "@/lib/api";4950export const dynamic = "force-dynamic";5152export function GET(req: NextRequest) {53  const q = req.nextUrl.searchParams;54  const terme = slugify((q.get("q") ?? "").slice(0, 80));55  const limit = entier(q.get("limit"), 2000, 1, 2000);56  try {57    let liste = allMunicipalities();58    if (terme) {59      // début du slug, ou début d'un mot du slug (« jean » trouve saint-jean-sur-richelieu)60      liste = liste.filter((m) => m.slug.startsWith(terme) || m.slug.includes(`-${terme}`));61    }62    const total = liste.length;63    return json(64      {65        total,66        municipalites: liste.slice(0, limit).map((m) => ({67          nom: m.nom,68          slug: m.slug,69          n: m.n,70          lat: Math.round(m.lat * 1e5) / 1e5,71          lng: Math.round(m.lng * 1e5) / 1e5,72        })),73      },74      cache(3600)75    );76  } catch (err) {77    return erreur(500, (err as Error).message);78  }79}80
Files main
Menu

Theme