Nexysweb / koa-lib

MIT License
0 stars 0 forks source link

Routes.{i18n, ..} #31

Open f4bD3v opened 4 years ago

f4bD3v commented 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);
johnb8005 commented 4 years ago

created after discussion triggered by https://github.com/Nexysweb/koa-lib/pull/30/files

f4bD3v commented 4 years ago
  1. extend koalib config
  2. pass config to App.init() in options
  3. create LibServices from product service section in config
  4. add LibServices to app.context

=> available in middleware

separate LibServices instance can be created in file for easier use in app services

johnb8005 commented 4 years ago

for ref https://github.com/Nexysweb/koa-lib/commit/91b8a5b32c76d5a247a3656804e001681b782160