All files / src/adapter RequestAdapter.js

77.42% Statements 24/31
38.46% Branches 5/13
84.62% Functions 11/13
78.57% Lines 22/28
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108    8x   8x 8x                   9x       8x 8x         1x                   1x                   1x 1x   1x               1x 1x               1x 1x               1x               1x 1x               1x 1x             1x   1x            
import _ from 'lodash';
function template(str, data) {
  data = data || {};
 
  const matcher = str.match(/{(.+?)}/g);
  Eif(!matcher) return str
  
  matcher.forEach(function(key) {
   str = str.replace(key, data[key.replace('{','').replace('}', '')]);
  });
  return str;
}
 
export default class RequestResolver {
  constructor(config) {
    this.globalOptions = config.globalOptions
  }
 
  resolveRouteParams(model, routeParams) {
    console.log('Resolving Route: ', model, routeParams)
    return template(model.apiPath, routeParams)
  }
 
  create(data, requestOptions) {
    //{params, body, headers}
    return {
      url: requestOptions.apiPath,
      method: 'POST',
      options: { 
        body: JSON.stringify(data) 
      }
    }
  }
 
  update(id, data, requestOptions) {
    return {
      url: `${requestOptions.apiPath}/${id}`,
      method: 'PATCH',
      options: {
        body: JSON.stringify(data)
      }
    }
  }
 
  updateAll(where, data, requestOptions) {
    const params = { where: JSON.stringify(where) }
    const body = JSON.stringify(data)
 
    return {
      url: `${requestOptions.apiPath}/update`,
      method: 'POST',
      options: { params, body }
    }
  }
 
  find(filter, requestOptions) {
    const params = { filter: JSON.stringify(filter) }
    return {
      url: requestOptions.apiPath,
      method: 'GET',
      options: { params }
    }
  }
 
  findById(id, filter, requestOptions) {
    const params = filter ? { filter: JSON.stringify(filter) } : null
    return {
      url: `${requestOptions.apiPath}/${id}`,
      method: 'GET',
      options: { params }
    }
  }
 
  deleteById(id, requestOptions) {
    return {
      url: `${requestOptions.apiPath}/${id}`,
      method: 'DELETE',
      options: { }
    }
  }
 
  count(where, requestOptions) {
    const params = { where: JSON.stringify(where) }
    return {
      url: `${requestOptions.apiPath}/count`,
      method: 'GET',
      options: { params }
    }
  }
 
  custom(name, path, method, options = {}, requestOptions) {
    const _options = { headers: options.headers }
    Iif (options.params) _options.params = _.mapValues(options.params,(value)=>{
      if(_.isObject(value))
      {
        return JSON.stringify(value);
      }
      return value;
    })
    Eif (options.body) _options.body = JSON.stringify(options.body)
 
    return {
      url: `${requestOptions.apiPath}/${path}`,
      method,
      options: _options
    }
  }
}