From 550e2f03ad0b60b3c9db3d68dbf014dcb740a2ff Mon Sep 17 00:00:00 2001 From: Indrajith K L Date: Tue, 17 Jan 2017 15:37:25 +0530 Subject: Initial Commit --- src/services.js | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/services.js (limited to 'src/services.js') diff --git a/src/services.js b/src/services.js new file mode 100644 index 0000000..f1f2644 --- /dev/null +++ b/src/services.js @@ -0,0 +1,47 @@ +define(['global'], function(global) { + var _services = { + /* + * @url - String : API End Point + * @requestType - String : The type of the ajax call, possible values are POST, GET, PUT... + * additionalHeaders - JSON Object : Additional headers which can be passed with request. If none empty {} should be passed + * autheticate - boolean : wheather the request requires an authentication. If true token values stored in global.accessToke will be used + * data JSON Object: the paramters/data to send with the request + * shouldStringify - boolean : When set to true the parameter passed will be stingified before sending + * successMethod - function : Callback function when request results in success + * errorMethod - function : Callback function when request results in error + */ + send: function(url, requestType, additionalHeader, authenticate, data, shouldStringify, successMethod, errorMethod) { + var headers = { + "Content-Type": 'application/json' + } + + if (!_.isEmpty(additionalHeader)) { + headers = _.assign(headers, additionalHeader) + } + + if (authenticate) { + headers = _.assign(headers, { + "X-Auth-Token": global.accessToken + }) + } + + if (shouldStringify) { + data = JSON.stringify(data); + } + + $.ajax({ + async: true, + crossDomain: true, + url: url, + type: requestType, + headers: headers, + data: data, + success: successMethod, + error: errorMethod + }) + + + } + } + return _services; +}) -- cgit v1.2.3