PronounsPage/server/api/nouns/index.get.ts
Valentyne Stigloher 10180aa6a3 (refactor) use #shared alias instead of ~~/shared
the #shared alias used by Nuxt cannot be easily disabled and to prevent breackage with jiti, we make use of it
2025-08-17 18:56:02 +02:00

43 lines
1.6 KiB
TypeScript

import type { Noun } from '#shared/classes.ts';
import { genders, numeri, resolveFirstDeclension } from '#shared/nouns.ts';
import type { NounsData } from '#shared/nouns.ts';
import { getLocale, loadConfig, loadNounsData } from '~~/server/data.ts';
import { buildNoun, getNounEntries } from '~~/server/nouns.ts';
const downgradeToV1 = (noun: Noun, nounsData: NounsData) => {
const downgradedNoun: Record<string, unknown> = {
id: noun.id,
sourcesData: noun.sourcesData,
categories: noun.categories.join('|'),
sources: noun.sources.join(','),
};
for (const gender of genders) {
for (const numerus of numeri) {
const wordSpellings = noun.words[gender]?.[numerus]
?.map((word) => resolveFirstDeclension(word, numerus, nounsData)) ?? [];
downgradedNoun[gender + (numerus === 'singular' ? '' : 'Pl')] = wordSpellings.join('|');
}
}
return downgradedNoun;
};
export default defineEventHandler(async (event) => {
const locale = getLocale(event);
checkIsConfigEnabledOr404(await loadConfig(locale), 'nouns');
const query = getQuery(event);
const { isGranted } = await useAuthentication(event);
const db = useDatabase();
const nouns = await getNounEntries(db, isGranted, locale);
if (query.version !== '2') {
const config = await loadConfig(locale);
const nounsData = await loadNounsData(locale);
return nouns
.map((nounRaw) => buildNoun(nounRaw, config, nounsData))
.map((noun) => downgradeToV1(noun, nounsData));
} else {
return nouns;
}
});