All files / lib/engine/request put.js

100% Statements 26/26
100% Branches 10/10
100% Functions 4/4
100% Lines 26/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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114    1x                   1x 2x 1x                               1x 1x 1x 1x   1x                                       1x 2x   2x                                     1x   4x 4x 4x         4x 1x         3x   3x 1x   2x     3x 1x         2x         3x    
"use strict";
 
const responseType = require("./../responseType");
 
/**
 * Add headers for particular responseType
 *
 * @param {Number} foundResponseType is number defined as constant in
 *        responseType  module
 * @param {RequestDefinition} request object which contains parameters
 *        to generate HTTP request
 */
exports.addHeaders = function (foundResponseType, request) {
  if (foundResponseType !== responseType.ENTITY_VALUE) {
    request.header("Accept", "application/json");
  }
};
 
/**
 * Append PUT request to batch container
 *
 * @param {agent.batch.Batch} defaultBatch container for HTTP request
 *        definiton based on resource parameter
 * @param {Number} foundResponseType is number defined as constant in
 *        responseType  module
 * @param {EntitySet} resource instance of entitySet definition
 *
 * @return {Promise} promise which is finished when request in batch
 *         is done
 */
exports.batchCall = function (defaultBatch, foundResponseType, resource) {
  const request = resource.defaultRequest;
  const defaultChangeSet = resource.agent.batchManager.defaultChangeSet;
  exports.addHeaders(foundResponseType, request);
 
  return defaultBatch.put(
    request._path,
    request._headers,
    request._payload,
    defaultChangeSet
  );
};
 
/**
 * Send PUT request to server
 *
 * @param {EntitySet} resource instance of entitySet definition
 * @param {Number} foundResponseType is number defined as constant in
 *        responseType  module
 * @param {RequestDefinition} request object which contains parameters
 *        to generate HTTP request
 *
 * @return {Promise} promise which is finished when request in batch
 *         is done
 */
exports.agentCall = function (resource, foundResponseType, request) {
  exports.addHeaders(foundResponseType, request);
 
  return resource.agent.put(
    request._path,
    request._headers,
    foundResponseType === responseType.ENTITY_VALUE
      ? request._payload
      : JSON.stringify(request._payload)
  );
};
 
/**
 * Create agent call or generate batch request (depends
 * on resource settings)
 *
 * @param {Any} body is content which is send to service
 * @param {EntitySet} resource instance of entitySet definition
 *
 * @return {Promise} promise which is finished when request in batch
 *         is done
 */
exports.call = function (body, resource) {
  let responsePromise;
  const defaultBatch = resource.agent.batchManager.defaultBatch;
  const request = resource.defaultRequest;
  const foundResponseType = responseType.determine(
    resource.defaultRequest,
    resource
  );
 
  if (foundResponseType === responseType.PROPERTY_VALUE) {
    throw new Error(
      "Can't create PUT request with $value predicate on single property."
    );
  }
 
  request.calculatePath();
 
  if (foundResponseType === responseType.ENTITY_VALUE) {
    request.payload(body);
  } else {
    request.payload(resource.bodyProperties(body));
  }
 
  if (defaultBatch) {
    responsePromise = resource._handleBatchCall(
      exports.batchCall.bind(null, defaultBatch, foundResponseType, resource),
      defaultBatch
    );
  } else {
    responsePromise = resource._handleAgentCall(
      exports.agentCall.bind(null, resource, foundResponseType, request)
    );
  }
 
  return responsePromise;
};