Advertisement
Guest User

new js api

a guest
Oct 3rd, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var reqwest = require('reqwest');
  2. import Storage from './Storage'
  3. import Core from './Core'
  4.  
  5. class Api {
  6.  
  7.   /*
  8.    * Переменные, основная функция request
  9.    */
  10.  
  11.   http_host = 'http://localhost'+':5000'
  12.  
  13.   async request(url, method='get', data={}) {
  14.     return new Promise(function(resolve, reject){
  15.  
  16.       /*
  17.        * Если есть токен, и мы получаем не токен, то токен не отправляем.
  18.        */
  19.       if (Storage.get('token') && url != '/token') {
  20.         data.token = Storage.get('token')
  21.       }
  22.  
  23.       /*
  24.        * Подготовка данных для отправки через get
  25.        */
  26.       if (method == 'get') {
  27.         let tmp = []
  28.         for (let key in data) {
  29.           tmp.push({name: key, value: data[key]})
  30.         }
  31.         data = tmp
  32.       }
  33.  
  34.       let options = {
  35.         url: this.http_host+url,
  36.         method: method,
  37.         type: 'json',
  38.         data: data,
  39.       }
  40.  
  41.       if (Storage.get('debug')) {
  42.         console.log(url, options)
  43.       }
  44.  
  45.       reqwest(options).then(
  46.         function(data) {
  47.           if (data.status != 'success') {
  48.             console.warn(data.data.code, data.data.message);
  49.             reject(data)
  50.           } else {
  51.             resolve(data)
  52.           }
  53.         },
  54.         function(resp) {
  55.           let data = JSON.parse(resp.responseText)
  56.           if (data.status != 'success') {
  57.             console.warn(data.data.code, data.data.message);
  58.             reject(data)
  59.           } else {
  60.             resolve(data)
  61.           }
  62.         }
  63.       )
  64.     }.bind(this))
  65.   }
  66.  
  67.   /*
  68.    * Логин, регистрация и прочее для юзера
  69.    */
  70.  
  71.   async getUserCurrent() {
  72.     let data = await this.request('/users/current')
  73.     Storage.set('user', data.data.user)
  74.     Core.push('update-user-current')
  75.     return
  76.   }
  77.  
  78.   async login(username, password) {
  79.     let data = await this.request(
  80.       '/token',
  81.       'post',
  82.       {
  83.         username: username,
  84.         password: password
  85.       }
  86.     )
  87.     Storage.set('token', data.data.token);
  88.     await this.getUserCurrent();
  89.  
  90.   }
  91.  
  92.   async register(username, password) {
  93.     let data = await this.request(
  94.       '/users',
  95.       'post',
  96.       {
  97.         username: username,
  98.         password: password
  99.       }
  100.     )
  101.     await this.login(username, password)
  102.   }
  103.  
  104.   /*
  105.    * Посты
  106.    */
  107.  
  108.   async loadPosts() {
  109.     let data = await this.request('/posts')
  110.     return data.data.posts
  111.   }
  112.  
  113.   async loadPost(id) {
  114.     let data = await this.request(`/posts/${id}`)
  115.     return data.data.post
  116.   }
  117.  
  118.   async loadPostComments(id) {
  119.     let data = await this.request(`/posts/${id}/comments`, 'get', {threaded: 1})
  120.     return data.data.comments
  121.   }
  122.  
  123.   async addComment(post_id, parent_id, content) {
  124.     let data = await this.request(
  125.       '/comments',
  126.       'post',
  127.       {
  128.         post: post_id,
  129.         parent: parent_id,
  130.         content: content
  131.       }
  132.     )
  133.     return data.data.Location
  134.   }
  135.  
  136.   async deleteComment(id) {
  137.     await this.request(`/comments/${+id}`, 'delete')
  138.     return
  139.   }
  140.  
  141.   async editComment(id, content) {
  142.     await this.request(`/comments/${id}`, 'patch', {content: content})
  143.     return
  144.   }
  145.  
  146.   async addPost(title, content, preview, blog_id=1) {
  147.     let data = await this.request(
  148.       '/posts',
  149.       'post',
  150.       {
  151.         title: title,
  152.         content: content,
  153.         preview: preview,
  154.         blog: blog_id
  155.       }
  156.     )
  157.     return data.data.Location
  158.   }
  159.  
  160.   async deletePost(id) {
  161.     await this.request('/posts/'+id, 'delete')
  162.     return
  163.   }
  164. }
  165.  
  166. export default new Api()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement