daily pastebin goal
22%
SHARE
TWEET

Untitled

a guest Apr 27th, 2018 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import DI from "injector";
  2. import Task from "@/utils/task";
  3.  
  4. export default function(DIContainer) {
  5.   this.client = DIContainer.HttpClient;
  6.   this.token = null;
  7.  
  8.   this.setToken = function(token) {
  9.     this.client.defaults.headers["Authorization"] = "Bearer " + token;
  10.     this.token = token;
  11.   };
  12.  
  13.   this.getTokenParams = function() {
  14.     return ["access_token", this.token];
  15.   };
  16.  
  17.   this.get = (path, params = []) => {
  18.     return this.client.get(
  19.       DIContainer.ApiUrlBuilder(path) + DIContainer.ParamsUrlBuilder(params)
  20.     );
  21.   };
  22.  
  23.   this.getForEmbed = function(url) {
  24.     return this.client.get(url);
  25.   };
  26.  
  27.   this.externalGet = function(path, params = []) {
  28.     let externalResource = DIContainer.root + "/" + path;
  29.     return this.client.get(
  30.       externalResource + DIContainer.ParamsUrlBuilder(params)
  31.     );
  32.   };
  33.  
  34.   this.getTask = function(path, params = []) {
  35.     let url =
  36.       DIContainer.ApiUrlBuilder(path) + DIContainer.ParamsUrlBuilder(params);
  37.     return new Task(this.client.get, url);
  38.   };
  39.  
  40.   this.post = (path, body, headers = {}, propagateErrors = false) => {
  41.     // return this.client.post(DIContainer.ApiUrlBuilder(path), body);
  42.     return new Promise((resolve, reject) => {
  43.       this.client
  44.         .post(DIContainer.ApiUrlBuilder(path), body, headers)
  45.         .then(resolve)
  46.         .catch(error => {
  47.           if (propagateErrors) DIContainer.ErrorManager.propagateErrors(error);
  48.           reject(error);
  49.         });
  50.     });
  51.   };
  52.  
  53.   this.put = (path, body) => {
  54.     return this.client.put(DIContainer.ApiUrlBuilder(path), body);
  55.   };
  56.  
  57.   this.delete = (path, params = []) => {
  58.     return this.client.delete(
  59.       DIContainer.ApiUrlBuilder(path) + DIContainer.ParamsUrlBuilder(params)
  60.     );
  61.   };
  62.  
  63.   this.getAndMap = function(path, params = [], decoderModel) {
  64.     return new Promise((resolve, reject) => {
  65.       this.get(path, params)
  66.         .then(response => {
  67.           if (DIContainer.Service.Comp.isArray(response.data)) {
  68.             resolve(
  69.               response.data.map(o =>
  70.                 DIContainer.Service.Decoder.map(decoderModel, o)
  71.               )
  72.             );
  73.           } else {
  74.             resolve(
  75.               DIContainer.Service.Decoder.map(decoderModel, response.data)
  76.             );
  77.           }
  78.         })
  79.         .catch(reason => reject(reason));
  80.     });
  81.   };
RAW Paste Data
Pastebin PRO SPRING Special!
Get 40% OFF Pastebin PRO accounts!
Top