all files / lib/ finalizeParams.js

29.41% Statements 10/34
23.33% Branches 7/30
33.33% Functions 1/3
29.41% Lines 10/34
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                                                                                                                                                  
var mapCallbacks = require("./mapCallbacks");
 
/**
 * @summary enforces required properties of the request configuration object.
 * @param {object} c - request configuration object
 */
function finalizeParams(c) {
    "use strict";
 
    if (Object.prototype.toString.call(c) !== "[object Object]") {
        throw new TypeError("ajax request expects an object argument. Provided\n" + c + " (" + typeof c + ")");
    }
 
    // url must be string
    if (typeof c.url !== "string") {
        throw new TypeError("ajax request object expects prop 'url' to be a string. Provided\n" + c.url + " (" + typeof c.url + ")");
    }
 
    // timeout can be set
    Iif (c.timeout && typeof c.timeout !== "number") {
        throw new TypeError("ajax request object timeout property must be an integer. Provided\n" + c.timeout + " (" + typeof c.timeout + ")");
    }
 
    // request method must be uppercase string === GET | POST | PUT | DELETE
    Iif (typeof c.method === "string") {
        // enforce proper case
        c.method = c.method.toUpperCase();
 
        if (["GET","POST","PUT","DELETE"].indexOf(c.method) === -1) {
            throw new RangeError("ajax: 'method' must have one of the following values:\nGET\nPOST\nPUT\nDELETE\nProvided: " + c.method);
        }
    } else {
        throw new RangeError("ajax request object expects property 'method' to be one of:\n GET, POST, PUT, DELETE\n Provided:\n" + c.method);
    }
 
    // optional
    if (c.responseType) {
        // enforce proper case
        c.responseType = c.responseType.toLowerCase();
 
        if (["json","text","xml","text/xml","text/html","arraybuffer","blob"].indexOf(c.responseType) === -1) {
            throw new RangeError("ajax: responseType must have one of the following values:\narraybuffer\nblob\njson\ntext\nxml\ntext/xml\ntext/html\n\nProvided: " + c.responseType);
        }
    } else {
        c.responseType = "text";
    }
 
    // force mandatory X-Requested-With header
    if (c.headers) {
        if (Object.prototype.toString.call(c.headers) !== "[object Object]") {
            throw new TypeError("ajax request object expects prop 'headers' to be an object of key:value pairing for header:value\nProvided:\n" + c.headers);
        } else {
            Object.keys(c.headers).forEach(function(key) {
                if (typeof c.headers[key] !== "string") {
                    throw new TypeError("ajax request object 'headers' properties must all be strings.\nProvided " + typeof c.headers[key] + " for " + key);
                }
            });
 
            if (!c.headers["X-Requested-With"]) {
                c.headers["X-Requested-With"] = "XMLHttpRequest";
            }
        }
    } else {
        c.headers = {
            "X-Requested-With": "XMLHttpRequest"
        };
    }
 
    if (c.method === "POST" || c.method === "PUT") {
        if (typeof c.data !== "string") {
            throw new TypeError("ajax request object prop 'data' must be present on POST or PUT requests and must be a string. Provided\n" + c.data + " (" + typeof c.data + ")");
        }
    }
 
    ["onOpen", "onHeaders", "onSuccess", "onFailure"].forEach(function(key) {
        c[key] = mapCallbacks(c[key]);
    });
 
    return c;
}
 
module.exports = finalizeParams;