Open f4bD3v opened 4 years ago
import Router from 'koa-router'; import Joi from '@hapi/joi'; import { Middleware } from '../middleware'; !!! import LibServices from '#services/lib'; const router = new Router(); const localeParser = Middleware.Validate.params({ locale: Joi.string() }); router.get('/:locale/dev', localeParser, async ctx => { const { locale } = ctx.params; // ctx.state.response = await LibServices.I18n.get(locale); ctx.state.response = await ctx.context.LibServices.I18n.get(locale); }); router.get('/:locale/serve', localeParser, async ctx => { const { locale } = ctx.params; ctx.state.response = await LibServices.I18n.getFile(locale); ctx.set({'Content-Type': 'application/json'}); }); router.get('/refresh', Middleware.isAdmin, async ctx => { await LibServices.I18n.saveAll(); ctx.ok('refresh.success'); }); export default Middleware.routes(router);
created after discussion triggered by https://github.com/Nexysweb/koa-lib/pull/30/files
=> available in middleware
separate LibServices instance can be created in file for easier use in app services
for ref https://github.com/Nexysweb/koa-lib/commit/91b8a5b32c76d5a247a3656804e001681b782160