All files / src/api/connector index.js

61.54% Statements 16/26
0% Branches 0/5
66.67% Functions 6/9
61.54% Lines 16/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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          3x   3x 1x   1x                             1x     3x                                                                           4x 4x     1x 1x     1x   1x 1x 1x       2x 2x      
import { assign, isFunction } from 'lodash';
import * as internalTypes from './internal';
import * as externalTypes from './external';
import { logger } from '../../utility';
 
const connectorTypes = assign({}, internalTypes, externalTypes);
 
const parseSourceData = (connector, source, options, data) => {
  const collection = data[source.name];
 
  const result = {
    connector: {
      id: connector.id,
      name: connector.name,
      type: connector.type.name,
    },
    id: source.id,
    name: source.name,
    model: source.model,
    schema: source.schema,
    meta: source.meta,
    items: collection.items,
    pagination: collection.pagination,
  };
 
  return result;
};
 
const parseChangeSourceData = (connector, source, options, data) => {
  const result = {
    connector: {
      id: connector.id,
      name: connector.name,
      type: connector.type.name,
    },
    id: source.id,
    name: source.name,
    model: source.model,
    meta: source.meta,
    schema: source.schema,
    data,
  };
 
  return result;
};
 
export default {
  /*
    Options object should have `action` and `payload` properties.
    Action is used to differentiate methods on connector backend API.
    */
  changeSourceData(connector, source, options = {}) {
    const connectorType = this.getConnectorType(connector.type);
 
    if (!isFunction(connectorType.changeSourceData)) {
      const message = `Changing source data is not supported on ${connector.name} connector`;
      logger.error(message);
      return Promise.reject(message);
    }
 
    return connectorType.changeSourceData(connector, source, options).then((data) => {
      const result = parseChangeSourceData(connector, source, options, data);
      return result;
    });
  },
  getConnectorType(connector) {
    const type = connectorTypes[connector.type];
    return type;
  },
  getSources(connector, options = {}) {
    const connectorType = this.getConnectorType(connector.type);
    return connectorType.getSources(connector, options);
  },
  getSourceData(connector, source, options = {}) {
    const connectorType = this.getConnectorType(connector.type);
 
    return connectorType.getSourceData(connector, source, options).then((data) => {
      const result = parseSourceData(connector, source, options, data);
      return result;
    });
  },
  getSourceSchema(connector, source, options) {
    const connectorType = this.getConnectorType(connector.type);
    return connectorType.getSourceSchema(connector, source, options);
  },
};