import request from 'request';
import { SiteConf, getDate } from '../../src/base/';

const options = {
  hostname: `${ SiteConf.PostsApi}`,
  port: 80,
  path: '/',
  method: 'GET',
  ciphers: 'DES-CBC3-SHA'
};

export const postsApiHandler = (req, res, next)  => {
  request(options, (error, response, body) => {
    if (error) {
      console.log(error);
      res.status(500).json(error);
      next();
    } else {
      const data = JSON.parse(body);
      const pagination = data.meta.pagination;
      const posts = PostList(data.posts);
      const result = { posts, pagination };
      res.json(result);
      next();
    }
  });
}; 

export const PostList = (posts) => {
  return posts.map((post) => {
    const reg = new RegExp(`^(.+?)${ SiteConf.postOpeningSplitChar }`);
    const result = reg.exec(post.html);
    if (result) post.opening = result[1];
    else {
      let i = 0;
      let max = SiteConf.postOpeningChars;
      const words = post.html.split(' ');
      post.opening = '';
      for (i; i <= max ; i++) {
        post.opening += `${words[i]} `;
      }
      post.opening += '...</p>';
  
    }
    post.html = null;
    post.markdown = null;
    post.published_at = getDate(post.published_at);
    return post;
  }
  );
};
@@ -82,135 +82,8 @@
 ';%0A%0A
-const options = %7B%0A  hostname: %60$%7B SiteConf.PostsApi%7D%60,%0A  port: 80,%0A  path: '/',%0A  method: 'GET',%0A  ciphers: 'DES-CBC3-SHA'%0A%7D;%0A%0A
 expo
@@ -146,15 +146,32 @@
 est(
-options
+%60$%7B SiteConf.PostsApi %7D%60
 , (e
@@ -275,30 +275,16 @@
 error);%0A
-      next();%0A
     %7D el
@@ -486,22 +486,8 @@
 t);%0A
-      next();%0A
     

clean api posts