refactor(api): extrair montagem e paginação da busca para lib/offSearch

GET /api/search passa de CC 41 para <= 10. Testes de caracterização
(sem rede: fetch e cache JSON temporário) cobrem validação, filtros,
ordenação, paginação, modo cache-only e erros.

Também: parameter property de NutriScanError virou campo explícito e
imports só de tipo usam import type, para o type stripping nativo do
Node; o script de teste não usa mais --experimental-transform-types
(removida no Node 26).
This commit is contained in:
evandro committed 2026-10-10 04:14:48 -03:00
1 parent fc7243e5f8
commit bf688257ef
9 files changed
+752 -357

No files matched your search

+41
View File
@@ -0,0 +1,41 @@
// Resolve o alias "@/" e imports sem extensão do projeto, para que os testes
// importem módulos reais (inclusive Route Handlers do Next.js) com o type
// stripping nativo do Node. Requer Node >= 22.15 (module.registerHooks).
import { existsSync, statSync } from "node:fs";
import { registerHooks } from "node:module";
import { fileURLToPath, pathToFileURL } from "node:url";
const ROOT = new URL("../../", import.meta.url);
const SUFFIXES = ["", ".ts", ".tsx", ".js", "/index.ts", "/index.js"];
function findFile(base) {
for (const suffix of SUFFIXES) {
const candidate = base + suffix;
if (existsSync(candidate) && statSync(candidate).isFile()) return candidate;
}
return null;
}
registerHooks({
resolve(specifier, context, nextResolve) {
if (specifier.startsWith("@/")) {
const found = findFile(fileURLToPath(new URL(specifier.slice(2), ROOT)));
if (found) return nextResolve(pathToFileURL(found).href, context);
}
try {
return nextResolve(specifier, context);
} catch (error) {
const code = error?.code;
if (code !== "ERR_MODULE_NOT_FOUND" && code !== "ERR_UNSUPPORTED_DIR_IMPORT") throw error;
if (specifier.startsWith(".") && context.parentURL) {
const found = findFile(fileURLToPath(new URL(specifier, context.parentURL)));
if (found) return nextResolve(pathToFileURL(found).href, context);
}
// Subcaminho de pacote CJS sem exports, como "next/server".
if (!specifier.startsWith(".") && !specifier.startsWith("node:")) {
return nextResolve(`${specifier}.js`, context);
}
throw error;
}
},
});