diff --git a/api/source/index.ts b/api/source/index.ts index 50a9bc390..1a84d45a4 100644 --- a/api/source/index.ts +++ b/api/source/index.ts @@ -13,6 +13,7 @@ import { redisCacheMiddleware } from './redis-cache.js' import { docRoutes } from './route/doc.js' import { openapiRoutes } from './route/openapi.js' import Sentry, { requestHandler, tracingMiddleWare } from './sentry.js' +import { getUnitKey } from './units.js' type State = Koa.DefaultState type Context = Koa.DefaultContext @@ -42,7 +43,7 @@ app.use(cors()) router.use('/api/v1', docRoutes(), openapiRoutes(openapi)) -const apiRoutes = publicodesAPI(new Engine(rules)) +const apiRoutes = publicodesAPI(new Engine(rules, { getUnitKey })) router.use( '/api/v1', @@ -65,4 +66,4 @@ const server = app.listen(port, function () { console.log('listening on port:', port) }) -export { server, app } +export { app, server } diff --git a/api/source/units.ts b/api/source/units.ts new file mode 100644 index 000000000..e99c75380 --- /dev/null +++ b/api/source/units.ts @@ -0,0 +1,25 @@ +// units from site/source/locales/units.yaml: +const units = Object.entries({ + heure_plural: 'heures', + jour_plural: 'jours', + 'jour ouvré_plural': 'jours ouvrés', + semaine_plural: 'semaine', + trimestre_plural: 'trimestres', + 'trimestre validé_plural': 'trimestres validés', + an_plural: 'ans', + employé_plural: 'employés', + point_plural: 'points', + mois_plural: 'mois', + manifestation_plural: 'manifestations', + 'titre-restaurant_plural': 'titres-restaurant', + part_plural: 'parts', + enfant_plural: 'enfants', +}) + +export const getUnitKey = (unit: string): string => { + const key = units + .find(([, trans]) => trans === unit)?.[0] + .replace(/_plural$/, '') + + return key || unit +}