From ddee017df9f3b1c59ed607fe8173d076e8f859b3 Mon Sep 17 00:00:00 2001 From: Matt Goldspink Date: Wed, 21 Oct 2015 10:27:21 -0700 Subject: [PATCH 01/11] Derrick's fixes --- forcetk.js | 411 ++++++++++++++++++++++++++--------------------------- 1 file changed, 200 insertions(+), 211 deletions(-) diff --git a/forcetk.js b/forcetk.js index 8964464..2f56e1a 100644 --- a/forcetk.js +++ b/forcetk.js @@ -32,9 +32,6 @@ * console, go to Your Name | Setup | Security Controls | Remote Site Settings */ -/*jslint browser: true*/ -/*global alert, Blob, $, jQuery*/ - var forcetk = window.forcetk; if (forcetk === undefined) { @@ -51,26 +48,34 @@ if (forcetk.Client === undefined) { * @param [loginUrl='https://login.salesforce.com/'] Login endpoint * @param [proxyUrl=null] Proxy URL. Omit if running on Visualforce or * PhoneGap etc + * @param [communityInd=null] Indicator if forcetk is used from Community * @constructor */ - forcetk.Client = function (clientId, loginUrl, proxyUrl) { - 'use strict'; + forcetk.Client = function(clientId, loginUrl, proxyUrl, communityInd) { this.clientId = clientId; this.loginUrl = loginUrl || 'https://login.salesforce.com/'; - if (proxyUrl === undefined || proxyUrl === null) { + if (typeof proxyUrl === 'undefined' || proxyUrl === null) { if (location.protocol === 'file:' || location.protocol === 'ms-appx:') { // In PhoneGap this.proxyUrl = null; } else { // In Visualforce - still need proxyUrl for Apex REST methods - this.proxyUrl = location.protocol + "//" + location.hostname - + "/services/proxy"; - } - this.authzHeader = "Authorization"; + this.proxyUrl = location.protocol + "//" + location.hostname; + + // Vlocity Card Team added pathname to handle community path because in the scenario of community, + // forcetk proxy is set to community url, and if the url has path, the proxy url has to contain that. + // Otherwise, you will get a 503 error + if (typeof communityInd !== 'undefined' && communityInd !== null && communityInd) { + this.proxyUrl += location.pathname + "services/proxy"; + } else { + this.proxyUrl += "/services/proxy"; + } + } + this.authzHeader = "Authorization"; // Vlocity Card Team: this is for VF } else { // On a server outside VF this.proxyUrl = proxyUrl; - this.authzHeader = "X-Authorization"; + this.authzHeader = "X-Authorization"; // Vlocity Card Team: if you ever use this for VF, you will get a 401 unauthorized - INVALID SESSION error } this.refreshToken = null; this.sessionId = null; @@ -78,42 +83,40 @@ if (forcetk.Client === undefined) { this.visualforce = false; this.instanceUrl = null; this.asyncAjax = true; - }; + } /** * Set a refresh token in the client. * @param refreshToken an OAuth refresh token */ - forcetk.Client.prototype.setRefreshToken = function (refreshToken) { - 'use strict'; + forcetk.Client.prototype.setRefreshToken = function(refreshToken) { this.refreshToken = refreshToken; - }; + } /** * Refresh the access token. * @param callback function to call on success * @param error function to call on failure */ - forcetk.Client.prototype.refreshAccessToken = function (callback, error) { - 'use strict'; - var that = this, - url = this.loginUrl + '/services/oauth2/token'; + forcetk.Client.prototype.refreshAccessToken = function(callback, error) { + var that = this; + var url = this.loginUrl + '/services/oauth2/token'; return $.ajax({ type: 'POST', - url: (this.proxyUrl !== null && !this.visualforce) ? this.proxyUrl : url, + url: (this.proxyUrl !== null && ! this.visualforce) ? this.proxyUrl: url, cache: false, processData: false, data: 'grant_type=refresh_token&client_id=' + this.clientId + '&refresh_token=' + this.refreshToken, success: callback, error: error, dataType: "json", - beforeSend: function (xhr) { - if (that.proxyUrl !== null && !this.visualforce) { + beforeSend: function(xhr) { + if (that.proxyUrl !== null && ! this.visualforce) { xhr.setRequestHeader('SalesforceProxy-Endpoint', url); } } }); - }; + } /** * Set a session token and the associated metadata in the client. @@ -123,32 +126,32 @@ if (forcetk.Client === undefined) { * @param [instanceUrl] Omit this if running on Visualforce; otherwise * use the value from the OAuth token. */ - forcetk.Client.prototype.setSessionToken = function (sessionId, apiVersion, instanceUrl) { - 'use strict'; + forcetk.Client.prototype.setSessionToken = function(sessionId, apiVersion, instanceUrl) { this.sessionId = sessionId; - this.apiVersion = (apiVersion === undefined || apiVersion === null) - ? 'v29.0' : apiVersion; - if (instanceUrl === undefined || instanceUrl === null) { + this.apiVersion = (typeof apiVersion === 'undefined' || apiVersion === null) + ? 'v29.0': apiVersion; + if (typeof instanceUrl === 'undefined' || instanceUrl == null) { this.visualforce = true; // location.hostname can be of the form 'abc.na1.visual.force.com', // 'na1.salesforce.com' or 'abc.my.salesforce.com' (custom domains). // Split on '.', and take the [1] or [0] element as appropriate - var elements = location.hostname.split("."), - instance = null; - if (elements.length === 4 && elements[1] === 'my') { + var elements = location.hostname.split("."); + + var instance = null; + if(elements.length == 4 && elements[1] === 'my') { instance = elements[0] + '.' + elements[1]; - } else if (elements.length === 3) { + } else if(elements.length == 3){ instance = elements[0]; } else { instance = elements[1]; } - + this.instanceUrl = "https://" + instance + ".salesforce.com"; } else { this.instanceUrl = instanceUrl; } - }; + } /* * Low level utility function to call the Salesforce endpoint. @@ -158,42 +161,41 @@ if (forcetk.Client === undefined) { * @param [method="GET"] HTTP method for call * @param [payload=null] payload for POST/PATCH etc */ - forcetk.Client.prototype.ajax = function (path, callback, error, method, payload, retry) { - 'use strict'; - var that = this, - url = (this.visualforce ? '' : this.instanceUrl) + '/services/data' + path; + forcetk.Client.prototype.ajax = function(path, callback, error, method, payload, retry) { + var that = this; + var url = (this.visualforce ? '' : this.instanceUrl) + '/services/data' + path; return $.ajax({ type: method || "GET", async: this.asyncAjax, - url: (this.proxyUrl !== null && !this.visualforce) ? this.proxyUrl : url, - contentType: method === "DELETE" ? null : 'application/json', + url: (this.proxyUrl !== null && ! this.visualforce) ? this.proxyUrl: url, + contentType: method == "DELETE" ? null : 'application/json', cache: false, processData: false, data: payload, success: callback, - error: (!this.refreshToken || retry) ? error : function (jqXHR, textStatus, errorThrown) { + error: (!this.refreshToken || retry ) ? error : function(jqXHR, textStatus, errorThrown) { if (jqXHR.status === 401) { - that.refreshAccessToken(function (oauthResponse) { + that.refreshAccessToken(function(oauthResponse) { that.setSessionToken(oauthResponse.access_token, null, - oauthResponse.instance_url); + oauthResponse.instance_url); that.ajax(path, callback, error, method, payload, true); }, - error); + error); } else { error(jqXHR, textStatus, errorThrown); } }, dataType: "json", - beforeSend: function (xhr) { - if (that.proxyUrl !== null && !that.visualforce) { + beforeSend: function(xhr) { + if (that.proxyUrl !== null && ! that.visualforce) { xhr.setRequestHeader('SalesforceProxy-Endpoint', url); } xhr.setRequestHeader(that.authzHeader, "Bearer " + that.sessionId); xhr.setRequestHeader('X-User-Agent', 'salesforce-toolkit-rest-javascript/' + that.apiVersion); } }); - }; + } /** * Utility function to query the Chatter API and download a file @@ -207,61 +209,64 @@ if (forcetk.Client === undefined) { * @param [error=null] function to which request will be passed in case of error * @param retry true if we've already tried refresh token flow once */ - forcetk.Client.prototype.getChatterFile = function (path, mimeType, callback, error, retry) { - 'use strict'; - var that = this, - url = (this.visualforce ? '' : this.instanceUrl) + path, - request = new XMLHttpRequest(); + forcetk.Client.prototype.getChatterFile = function(path, mimeType, callback, error, retry) { + var that = this; + var url = (this.visualforce ? '' : this.instanceUrl) + path; - request.open("GET", (this.proxyUrl !== null && !this.visualforce) ? this.proxyUrl : url, true); + var request = new XMLHttpRequest(); + + request.open("GET", (this.proxyUrl !== null && ! this.visualforce) ? this.proxyUrl: url, true); request.responseType = "arraybuffer"; - + request.setRequestHeader(this.authzHeader, "Bearer " + this.sessionId); request.setRequestHeader('X-User-Agent', 'salesforce-toolkit-rest-javascript/' + this.apiVersion); - if (this.proxyUrl !== null && !this.visualforce) { + if (this.proxyUrl !== null && ! this.visualforce) { request.setRequestHeader('SalesforceProxy-Endpoint', url); } - - request.onreadystatechange = function () { + + request.onreadystatechange = function() { // continue if the process is completed - if (request.readyState === 4) { + if (request.readyState == 4) { // continue only if HTTP status is "OK" - if (request.status === 200) { + if (request.status == 200) { try { // retrieve the response callback(request.response); - } catch (e) { + } + catch(e) { // display error message alert("Error reading the response: " + e.toString()); } - } else if (request.status === 401 && !retry) { - //refresh token in 401 - that.refreshAccessToken(function (oauthResponse) { - that.setSessionToken(oauthResponse.access_token, null, oauthResponse.instance_url); + } + //refresh token in 401 + else if(request.status == 401 && !retry) { + that.refreshAccessToken(function(oauthResponse) { + that.setSessionToken(oauthResponse.access_token, null,oauthResponse.instance_url); that.getChatterFile(path, mimeType, callback, error, true); - }, error); - } else { + }, + error); + } + else { // display status message - error(request, request.statusText, request.response); + error(request,request.statusText,request.response); } - } - }; + } + + } request.send(); - - }; - + + } + // Local utility to create a random string for multipart boundary - var randomString = function () { - 'use strict'; - var str = '', - i; - for (i = 0; i < 4; i += 1) { - str += (Math.random().toString(16) + "000000000").substr(2, 8); + function randomString() { + var str = ''; + for (var i = 0; i < 4; i++) { + str += (Math.random().toString(16)+"000000000").substr(2,8); } return str; - }; - + } + /* Low level function to create/update records with blob data * @param path resource path relative to /services/data * @param fields an object containing initial field names and values for @@ -274,63 +279,63 @@ if (forcetk.Client === undefined) { * @param [error=null] function to which response will be passed in case of error * @param retry true if we've already tried refresh token flow once */ - forcetk.Client.prototype.blob = function (path, fields, filename, payloadField, payload, callback, error, retry) { - 'use strict'; - var that = this, - url = (this.visualforce ? '' : this.instanceUrl) + '/services/data' + path, - boundary = randomString(), - blob = new Blob([ - "--boundary_" + boundary + '\n' - + "Content-Disposition: form-data; name=\"entity_content\";" + "\n" - + "Content-Type: application/json" + "\n\n" - + JSON.stringify(fields) - + "\n\n" - + "--boundary_" + boundary + "\n" - + "Content-Type: application/octet-stream" + "\n" - + "Content-Disposition: form-data; name=\"" + payloadField - + "\"; filename=\"" + filename + "\"\n\n", - payload, - "\n\n" - + "--boundary_" + boundary + "--" - ], {type : 'multipart/form-data; boundary=\"boundary_' + boundary + '\"'}), - request = new XMLHttpRequest(); - - request.open("POST", (this.proxyUrl !== null && !this.visualforce) ? this.proxyUrl : url, this.asyncAjax); + forcetk.Client.prototype.blob = function(path, fields, filename, payloadField, payload, callback, error, retry) { + var that = this; + var url = (this.visualforce ? '' : this.instanceUrl) + '/services/data' + path; + var boundary = randomString(); + + var blob = new Blob([ + "--boundary_" + boundary + '\n' + + "Content-Disposition: form-data; name=\"entity_content\";" + "\n" + + "Content-Type: application/json" + "\n\n" + + JSON.stringify(fields) + + "\n\n" + + "--boundary_" + boundary + "\n" + + "Content-Type: application/octet-stream" + "\n" + + "Content-Disposition: form-data; name=\"" + payloadField + + "\"; filename=\"" + filename + "\"\n\n", + payload, + "\n\n" + + "--boundary_" + boundary + "--" + ], {type : 'multipart/form-data; boundary=\"boundary_' + boundary + '\"'}); + + var request = new XMLHttpRequest(); + request.open("POST", (this.proxyUrl !== null && ! this.visualforce) ? this.proxyUrl: url, this.asyncAjax); request.setRequestHeader('Accept', 'application/json'); request.setRequestHeader(this.authzHeader, "Bearer " + this.sessionId); request.setRequestHeader('X-User-Agent', 'salesforce-toolkit-rest-javascript/' + this.apiVersion); - request.setRequestHeader('Content-Type', 'multipart/form-data; boundary=\"boundary_' + boundary + '\"'); - if (this.proxyUrl !== null && !this.visualforce) { + if (this.proxyUrl !== null && ! this.visualforce) { request.setRequestHeader('SalesforceProxy-Endpoint', url); } - + if (this.asyncAjax) { - request.onreadystatechange = function () { + request.onreadystatechange = function() { // continue if the process is completed - if (request.readyState === 4) { + if (request.readyState == 4) { // continue only if HTTP status is good if (request.status >= 200 && request.status < 300) { // retrieve the response callback(request.response ? JSON.parse(request.response) : null); - } else if (request.status === 401 && !retry) { - that.refreshAccessToken(function (oauthResponse) { - that.setSessionToken(oauthResponse.access_token, null, oauthResponse.instance_url); - that.blob(path, fields, filename, payloadField, payload, callback, error, true); - }, error); + } else if(request.status == 401 && !retry) { + that.refreshAccessToken(function(oauthResponse) { + that.setSessionToken(oauthResponse.access_token, null,oauthResponse.instance_url); + that.blob(path, fields, fileName, file, callback, error, true); + }, + error); } else { // return status message error(request, request.statusText, request.response); } - } - }; + } + } } - + request.send(blob); - + return this.asyncAjax ? null : JSON.parse(request.response); - }; - + } + /* * Create a record with blob data * @param objtype object type; e.g. "ContentVersion" @@ -344,13 +349,12 @@ if (forcetk.Client === undefined) { * @param [error=null] function to which response will be passed in case of error * @param retry true if we've already tried refresh token flow once */ - forcetk.Client.prototype.createBlob = function (objtype, fields, filename, - payloadField, payload, callback, - error, retry) { - 'use strict'; - return this.blob('/' + this.apiVersion + '/sobjects/' + objtype + '/', - fields, filename, payloadField, payload, callback, error, retry); - }; + forcetk.Client.prototype.createBlob = function(objtype, fields, filename, + payloadField, payload, callback, + error, retry) { + return this.blob('/' + this.apiVersion + '/sobjects/' + objtype + '/', + fields, filename, payloadField, payload, callback, error); + } /* * Update a record with blob data @@ -366,13 +370,12 @@ if (forcetk.Client === undefined) { * @param [error=null] function to which response will be passed in case of error * @param retry true if we've already tried refresh token flow once */ - forcetk.Client.prototype.updateBlob = function (objtype, id, fields, filename, - payloadField, payload, callback, - error, retry) { - 'use strict'; - return this.blob('/' + this.apiVersion + '/sobjects/' + objtype + '/' + id + - '?_HttpMethod=PATCH', fields, filename, payloadField, payload, callback, error, retry); - }; + forcetk.Client.prototype.updateBlob = function(objtype, id, fields, filename, + payloadField, payload, callback, + error, retry) { + return this.blob('/' + this.apiVersion + '/sobjects/' + objtype + '/' + id + + '?_HttpMethod=PATCH', fields, filename, payloadField, payload, callback, error); + } /* * Low level utility function to call the Salesforce endpoint specific for Apex REST API. @@ -384,34 +387,33 @@ if (forcetk.Client === undefined) { * @param [paramMap={}] parameters to send as header values for POST/PATCH etc * @param [retry] specifies whether to retry on error */ - forcetk.Client.prototype.apexrest = function (path, callback, error, method, payload, paramMap, retry) { - 'use strict'; - var that = this, - url = this.instanceUrl + '/services/apexrest' + path; + forcetk.Client.prototype.apexrest = function(path, callback, error, method, payload, paramMap, retry) { + var that = this; + var url = this.instanceUrl + '/services/apexrest' + path; return $.ajax({ type: method || "GET", async: this.asyncAjax, - url: (this.proxyUrl !== null) ? this.proxyUrl : url, + url: (this.proxyUrl !== null) ? this.proxyUrl: url, contentType: 'application/json', cache: false, processData: false, data: payload, success: callback, - error: (!this.refreshToken || retry) ? error : function (jqXHR, textStatus, errorThrown) { + error: (!this.refreshToken || retry ) ? error : function(jqXHR, textStatus, errorThrown) { if (jqXHR.status === 401) { - that.refreshAccessToken(function (oauthResponse) { + that.refreshAccessToken(function(oauthResponse) { that.setSessionToken(oauthResponse.access_token, null, - oauthResponse.instance_url); + oauthResponse.instance_url); that.apexrest(path, callback, error, method, payload, paramMap, true); - }, error); + }, + error); } else { error(jqXHR, textStatus, errorThrown); } }, dataType: "json", - beforeSend: function (xhr) { - var paramName; + beforeSend: function(xhr) { if (that.proxyUrl !== null) { xhr.setRequestHeader('SalesforceProxy-Endpoint', url); } @@ -420,15 +422,13 @@ if (forcetk.Client === undefined) { paramMap = {}; } for (paramName in paramMap) { - if (paramMap.hasOwnProperty(paramName)) { - xhr.setRequestHeader(paramName, paramMap[paramName]); - } + xhr.setRequestHeader(paramName, paramMap[paramName]); } xhr.setRequestHeader(that.authzHeader, "Bearer " + that.sessionId); xhr.setRequestHeader('X-User-Agent', 'salesforce-toolkit-rest-javascript/' + that.apiVersion); } }); - }; + } /* * Lists summary information about each Salesforce.com version currently @@ -437,10 +437,9 @@ if (forcetk.Client === undefined) { * @param callback function to which response will be passed * @param [error=null] function to which jqXHR will be passed in case of error */ - forcetk.Client.prototype.versions = function (callback, error) { - 'use strict'; + forcetk.Client.prototype.versions = function(callback, error) { return this.ajax('/', callback, error); - }; + } /* * Lists available resources for the client's API version, including @@ -448,10 +447,9 @@ if (forcetk.Client === undefined) { * @param callback function to which response will be passed * @param [error=null] function to which jqXHR will be passed in case of error */ - forcetk.Client.prototype.resources = function (callback, error) { - 'use strict'; + forcetk.Client.prototype.resources = function(callback, error) { return this.ajax('/' + this.apiVersion + '/', callback, error); - }; + } /* * Lists the available objects and their metadata for your organization's @@ -459,10 +457,9 @@ if (forcetk.Client === undefined) { * @param callback function to which response will be passed * @param [error=null] function to which jqXHR will be passed in case of error */ - forcetk.Client.prototype.describeGlobal = function (callback, error) { - 'use strict'; + forcetk.Client.prototype.describeGlobal = function(callback, error) { return this.ajax('/' + this.apiVersion + '/sobjects/', callback, error); - }; + } /* * Describes the individual metadata for the specified object. @@ -470,11 +467,10 @@ if (forcetk.Client === undefined) { * @param callback function to which response will be passed * @param [error=null] function to which jqXHR will be passed in case of error */ - forcetk.Client.prototype.metadata = function (objtype, callback, error) { - 'use strict'; - return this.ajax('/' + this.apiVersion + '/sobjects/' + objtype + '/', - callback, error); - }; + forcetk.Client.prototype.metadata = function(objtype, callback, error) { + return this.ajax('/' + this.apiVersion + '/sobjects/' + objtype + '/' + , callback, error); + } /* * Completely describes the individual metadata at all levels for the @@ -483,11 +479,10 @@ if (forcetk.Client === undefined) { * @param callback function to which response will be passed * @param [error=null] function to which jqXHR will be passed in case of error */ - forcetk.Client.prototype.describe = function (objtype, callback, error) { - 'use strict'; + forcetk.Client.prototype.describe = function(objtype, callback, error) { return this.ajax('/' + this.apiVersion + '/sobjects/' + objtype - + '/describe/', callback, error); - }; + + '/describe/', callback, error); + } /* * Creates a new record of the given type. @@ -498,11 +493,10 @@ if (forcetk.Client === undefined) { * @param callback function to which response will be passed * @param [error=null] function to which jqXHR will be passed in case of error */ - forcetk.Client.prototype.create = function (objtype, fields, callback, error) { - 'use strict'; - return this.ajax('/' + this.apiVersion + '/sobjects/' + objtype + '/', - callback, error, "POST", JSON.stringify(fields)); - }; + forcetk.Client.prototype.create = function(objtype, fields, callback, error) { + return this.ajax('/' + this.apiVersion + '/sobjects/' + objtype + '/' + , callback, error, "POST", JSON.stringify(fields)); + } /* * Retrieves field values for a record of the given type. @@ -513,17 +507,16 @@ if (forcetk.Client === undefined) { * @param callback function to which response will be passed * @param [error=null] function to which jqXHR will be passed in case of error */ - forcetk.Client.prototype.retrieve = function (objtype, id, fieldlist, callback, error) { - 'use strict'; - if (arguments.length === 4) { + forcetk.Client.prototype.retrieve = function(objtype, id, fieldlist, callback, error) { + if (arguments.length == 4) { error = callback; callback = fieldlist; fieldlist = null; } var fields = fieldlist ? '?fields=' + fieldlist : ''; return this.ajax('/' + this.apiVersion + '/sobjects/' + objtype + '/' + id - + fields, callback, error); - }; + + fields, callback, error); + } /* * Upsert - creates or updates record of the given type, based on the @@ -537,11 +530,10 @@ if (forcetk.Client === undefined) { * @param callback function to which response will be passed * @param [error=null] function to which jqXHR will be passed in case of error */ - forcetk.Client.prototype.upsert = function (objtype, externalIdField, externalId, fields, callback, error) { - 'use strict'; - return this.ajax('/' + this.apiVersion + '/sobjects/' + objtype + '/' + externalIdField + '/' + externalId - + '?_HttpMethod=PATCH', callback, error, "POST", JSON.stringify(fields)); - }; + forcetk.Client.prototype.upsert = function(objtype, externalIdField, externalId, fields, callback, error) { + return this.ajax('/' + this.apiVersion + '/sobjects/' + objtype + '/' + externalIdField + '/' + externalId + + '?_HttpMethod=PATCH', callback, error, "POST", JSON.stringify(fields)); + } /* * Updates field values on a record of the given type. @@ -553,11 +545,10 @@ if (forcetk.Client === undefined) { * @param callback function to which response will be passed * @param [error=null] function to which jqXHR will be passed in case of error */ - forcetk.Client.prototype.update = function (objtype, id, fields, callback, error) { - 'use strict'; - return this.ajax('/' + this.apiVersion + '/sobjects/' + objtype + '/' + id - + '?_HttpMethod=PATCH', callback, error, "POST", JSON.stringify(fields)); - }; + forcetk.Client.prototype.update = function(objtype, id, fields, callback, error) { + return this.ajax('/' + this.apiVersion + '/sobjects/' + objtype + '/' + id + + '?_HttpMethod=PATCH', callback, error, "POST", JSON.stringify(fields)); + } /* * Deletes a record of the given type. Unfortunately, 'delete' is a @@ -567,11 +558,10 @@ if (forcetk.Client === undefined) { * @param callback function to which response will be passed * @param [error=null] function to which jqXHR will be passed in case of error */ - forcetk.Client.prototype.del = function (objtype, id, callback, error) { - 'use strict'; - return this.ajax('/' + this.apiVersion + '/sobjects/' + objtype + '/' + id, - callback, error, "DELETE"); - }; + forcetk.Client.prototype.del = function(objtype, id, callback, error) { + return this.ajax('/' + this.apiVersion + '/sobjects/' + objtype + '/' + id + , callback, error, "DELETE"); + } /* * Executes the specified SOQL query. @@ -580,12 +570,11 @@ if (forcetk.Client === undefined) { * @param callback function to which response will be passed * @param [error=null] function to which jqXHR will be passed in case of error */ - forcetk.Client.prototype.query = function (soql, callback, error) { - 'use strict'; - return this.ajax('/' + this.apiVersion + '/query?q=' + encodeURIComponent(soql), - callback, error); - }; - + forcetk.Client.prototype.query = function(soql, callback, error) { + return this.ajax('/' + this.apiVersion + '/query?q=' + encodeURIComponent(soql) + , callback, error); + } + /* * Queries the next set of records based on pagination. *

This should be used if performing a query that retrieves more than can be returned @@ -596,18 +585,19 @@ if (forcetk.Client === undefined) { * @param callback function to which response will be passed * @param [error=null] function to which jqXHR will be passed in case of error */ - forcetk.Client.prototype.queryMore = function (url, callback, error) { - 'use strict'; + forcetk.Client.prototype.queryMore = function( url, callback, error ){ //-- ajax call adds on services/data to the url call, so only send the url after - var serviceData = "services/data", - index = url.indexOf(serviceData); - - if (index > -1) { - url = url.substr(index + serviceData.length); + var serviceData = "services/data"; + var index = url.indexOf( serviceData ); + + if( index > -1 ){ + url = url.substr( index + serviceData.length ); + } else { + //-- leave alone } - - return this.ajax(url, callback, error); - }; + + return this.ajax( url, callback, error ); + } /* * Executes the specified SOSL search. @@ -616,9 +606,8 @@ if (forcetk.Client === undefined) { * @param callback function to which response will be passed * @param [error=null] function to which jqXHR will be passed in case of error */ - forcetk.Client.prototype.search = function (sosl, callback, error) { - 'use strict'; - return this.ajax('/' + this.apiVersion + '/search?q=' + encodeURIComponent(sosl), - callback, error); - }; + forcetk.Client.prototype.search = function(sosl, callback, error) { + return this.ajax('/' + this.apiVersion + '/search?q=' + encodeURIComponent(sosl) + , callback, error); + } } From f55e88820905e3c78745f10bf09feeb9f2e5f706 Mon Sep 17 00:00:00 2001 From: Matt Goldspink Date: Wed, 21 Oct 2015 10:31:44 -0700 Subject: [PATCH 02/11] Adding back in use strict --- forcetk.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/forcetk.js b/forcetk.js index 2f56e1a..652edaa 100644 --- a/forcetk.js +++ b/forcetk.js @@ -32,6 +32,9 @@ * console, go to Your Name | Setup | Security Controls | Remote Site Settings */ +/*jslint browser: true*/ +/*global alert, Blob, $, jQuery*/ + var forcetk = window.forcetk; if (forcetk === undefined) { @@ -52,6 +55,7 @@ if (forcetk.Client === undefined) { * @constructor */ forcetk.Client = function(clientId, loginUrl, proxyUrl, communityInd) { + 'use strict'; this.clientId = clientId; this.loginUrl = loginUrl || 'https://login.salesforce.com/'; if (typeof proxyUrl === 'undefined' || proxyUrl === null) { @@ -90,6 +94,7 @@ if (forcetk.Client === undefined) { * @param refreshToken an OAuth refresh token */ forcetk.Client.prototype.setRefreshToken = function(refreshToken) { + 'use strict'; this.refreshToken = refreshToken; } @@ -99,6 +104,7 @@ if (forcetk.Client === undefined) { * @param error function to call on failure */ forcetk.Client.prototype.refreshAccessToken = function(callback, error) { + 'use strict'; var that = this; var url = this.loginUrl + '/services/oauth2/token'; return $.ajax({ @@ -127,6 +133,7 @@ if (forcetk.Client === undefined) { * use the value from the OAuth token. */ forcetk.Client.prototype.setSessionToken = function(sessionId, apiVersion, instanceUrl) { + 'use strict'; this.sessionId = sessionId; this.apiVersion = (typeof apiVersion === 'undefined' || apiVersion === null) ? 'v29.0': apiVersion; @@ -162,6 +169,7 @@ if (forcetk.Client === undefined) { * @param [payload=null] payload for POST/PATCH etc */ forcetk.Client.prototype.ajax = function(path, callback, error, method, payload, retry) { + 'use strict'; var that = this; var url = (this.visualforce ? '' : this.instanceUrl) + '/services/data' + path; @@ -210,6 +218,7 @@ if (forcetk.Client === undefined) { * @param retry true if we've already tried refresh token flow once */ forcetk.Client.prototype.getChatterFile = function(path, mimeType, callback, error, retry) { + 'use strict'; var that = this; var url = (this.visualforce ? '' : this.instanceUrl) + path; @@ -280,6 +289,7 @@ if (forcetk.Client === undefined) { * @param retry true if we've already tried refresh token flow once */ forcetk.Client.prototype.blob = function(path, fields, filename, payloadField, payload, callback, error, retry) { + 'use strict'; var that = this; var url = (this.visualforce ? '' : this.instanceUrl) + '/services/data' + path; var boundary = randomString(); @@ -352,6 +362,7 @@ if (forcetk.Client === undefined) { forcetk.Client.prototype.createBlob = function(objtype, fields, filename, payloadField, payload, callback, error, retry) { + 'use strict'; return this.blob('/' + this.apiVersion + '/sobjects/' + objtype + '/', fields, filename, payloadField, payload, callback, error); } @@ -373,6 +384,7 @@ if (forcetk.Client === undefined) { forcetk.Client.prototype.updateBlob = function(objtype, id, fields, filename, payloadField, payload, callback, error, retry) { + 'use strict'; return this.blob('/' + this.apiVersion + '/sobjects/' + objtype + '/' + id + '?_HttpMethod=PATCH', fields, filename, payloadField, payload, callback, error); } @@ -388,6 +400,7 @@ if (forcetk.Client === undefined) { * @param [retry] specifies whether to retry on error */ forcetk.Client.prototype.apexrest = function(path, callback, error, method, payload, paramMap, retry) { + 'use strict'; var that = this; var url = this.instanceUrl + '/services/apexrest' + path; @@ -438,6 +451,7 @@ if (forcetk.Client === undefined) { * @param [error=null] function to which jqXHR will be passed in case of error */ forcetk.Client.prototype.versions = function(callback, error) { + 'use strict'; return this.ajax('/', callback, error); } @@ -448,6 +462,7 @@ if (forcetk.Client === undefined) { * @param [error=null] function to which jqXHR will be passed in case of error */ forcetk.Client.prototype.resources = function(callback, error) { + 'use strict'; return this.ajax('/' + this.apiVersion + '/', callback, error); } @@ -458,6 +473,7 @@ if (forcetk.Client === undefined) { * @param [error=null] function to which jqXHR will be passed in case of error */ forcetk.Client.prototype.describeGlobal = function(callback, error) { + 'use strict'; return this.ajax('/' + this.apiVersion + '/sobjects/', callback, error); } @@ -468,6 +484,7 @@ if (forcetk.Client === undefined) { * @param [error=null] function to which jqXHR will be passed in case of error */ forcetk.Client.prototype.metadata = function(objtype, callback, error) { + 'use strict'; return this.ajax('/' + this.apiVersion + '/sobjects/' + objtype + '/' , callback, error); } @@ -480,6 +497,7 @@ if (forcetk.Client === undefined) { * @param [error=null] function to which jqXHR will be passed in case of error */ forcetk.Client.prototype.describe = function(objtype, callback, error) { + 'use strict'; return this.ajax('/' + this.apiVersion + '/sobjects/' + objtype + '/describe/', callback, error); } @@ -494,6 +512,7 @@ if (forcetk.Client === undefined) { * @param [error=null] function to which jqXHR will be passed in case of error */ forcetk.Client.prototype.create = function(objtype, fields, callback, error) { + 'use strict'; return this.ajax('/' + this.apiVersion + '/sobjects/' + objtype + '/' , callback, error, "POST", JSON.stringify(fields)); } @@ -508,6 +527,7 @@ if (forcetk.Client === undefined) { * @param [error=null] function to which jqXHR will be passed in case of error */ forcetk.Client.prototype.retrieve = function(objtype, id, fieldlist, callback, error) { + 'use strict'; if (arguments.length == 4) { error = callback; callback = fieldlist; @@ -531,6 +551,7 @@ if (forcetk.Client === undefined) { * @param [error=null] function to which jqXHR will be passed in case of error */ forcetk.Client.prototype.upsert = function(objtype, externalIdField, externalId, fields, callback, error) { + 'use strict'; return this.ajax('/' + this.apiVersion + '/sobjects/' + objtype + '/' + externalIdField + '/' + externalId + '?_HttpMethod=PATCH', callback, error, "POST", JSON.stringify(fields)); } @@ -546,6 +567,7 @@ if (forcetk.Client === undefined) { * @param [error=null] function to which jqXHR will be passed in case of error */ forcetk.Client.prototype.update = function(objtype, id, fields, callback, error) { + 'use strict'; return this.ajax('/' + this.apiVersion + '/sobjects/' + objtype + '/' + id + '?_HttpMethod=PATCH', callback, error, "POST", JSON.stringify(fields)); } @@ -559,6 +581,7 @@ if (forcetk.Client === undefined) { * @param [error=null] function to which jqXHR will be passed in case of error */ forcetk.Client.prototype.del = function(objtype, id, callback, error) { + 'use strict'; return this.ajax('/' + this.apiVersion + '/sobjects/' + objtype + '/' + id , callback, error, "DELETE"); } @@ -571,6 +594,7 @@ if (forcetk.Client === undefined) { * @param [error=null] function to which jqXHR will be passed in case of error */ forcetk.Client.prototype.query = function(soql, callback, error) { + 'use strict'; return this.ajax('/' + this.apiVersion + '/query?q=' + encodeURIComponent(soql) , callback, error); } @@ -586,6 +610,7 @@ if (forcetk.Client === undefined) { * @param [error=null] function to which jqXHR will be passed in case of error */ forcetk.Client.prototype.queryMore = function( url, callback, error ){ + 'use strict'; //-- ajax call adds on services/data to the url call, so only send the url after var serviceData = "services/data"; var index = url.indexOf( serviceData ); @@ -607,6 +632,7 @@ if (forcetk.Client === undefined) { * @param [error=null] function to which jqXHR will be passed in case of error */ forcetk.Client.prototype.search = function(sosl, callback, error) { + 'use strict'; return this.ajax('/' + this.apiVersion + '/search?q=' + encodeURIComponent(sosl) , callback, error); } From 7d35a8ad69a2d0823734b37015df920b1f4c54ff Mon Sep 17 00:00:00 2001 From: Matt Goldspink Date: Wed, 21 Oct 2015 10:35:56 -0700 Subject: [PATCH 03/11] More formatting changes --- forcetk.js | 95 +++++++++++++++++++++++++++--------------------------- 1 file changed, 48 insertions(+), 47 deletions(-) diff --git a/forcetk.js b/forcetk.js index 652edaa..0b8705f 100644 --- a/forcetk.js +++ b/forcetk.js @@ -54,7 +54,7 @@ if (forcetk.Client === undefined) { * @param [communityInd=null] Indicator if forcetk is used from Community * @constructor */ - forcetk.Client = function(clientId, loginUrl, proxyUrl, communityInd) { + forcetk.Client = function (clientId, loginUrl, proxyUrl, communityInd) { 'use strict'; this.clientId = clientId; this.loginUrl = loginUrl || 'https://login.salesforce.com/'; @@ -87,23 +87,23 @@ if (forcetk.Client === undefined) { this.visualforce = false; this.instanceUrl = null; this.asyncAjax = true; - } + }; /** * Set a refresh token in the client. * @param refreshToken an OAuth refresh token */ - forcetk.Client.prototype.setRefreshToken = function(refreshToken) { + forcetk.Client.prototype.setRefreshToken = function (refreshToken) { 'use strict'; this.refreshToken = refreshToken; - } + }; /** * Refresh the access token. * @param callback function to call on success * @param error function to call on failure */ - forcetk.Client.prototype.refreshAccessToken = function(callback, error) { + forcetk.Client.prototype.refreshAccessToken = function (callback, error) { 'use strict'; var that = this; var url = this.loginUrl + '/services/oauth2/token'; @@ -122,7 +122,7 @@ if (forcetk.Client === undefined) { } } }); - } + }; /** * Set a session token and the associated metadata in the client. @@ -132,7 +132,7 @@ if (forcetk.Client === undefined) { * @param [instanceUrl] Omit this if running on Visualforce; otherwise * use the value from the OAuth token. */ - forcetk.Client.prototype.setSessionToken = function(sessionId, apiVersion, instanceUrl) { + forcetk.Client.prototype.setSessionToken = function (sessionId, apiVersion, instanceUrl) { 'use strict'; this.sessionId = sessionId; this.apiVersion = (typeof apiVersion === 'undefined' || apiVersion === null) @@ -158,7 +158,7 @@ if (forcetk.Client === undefined) { } else { this.instanceUrl = instanceUrl; } - } + }; /* * Low level utility function to call the Salesforce endpoint. @@ -168,7 +168,7 @@ if (forcetk.Client === undefined) { * @param [method="GET"] HTTP method for call * @param [payload=null] payload for POST/PATCH etc */ - forcetk.Client.prototype.ajax = function(path, callback, error, method, payload, retry) { + forcetk.Client.prototype.ajax = function (path, callback, error, method, payload, retry) { 'use strict'; var that = this; var url = (this.visualforce ? '' : this.instanceUrl) + '/services/data' + path; @@ -203,7 +203,7 @@ if (forcetk.Client === undefined) { xhr.setRequestHeader('X-User-Agent', 'salesforce-toolkit-rest-javascript/' + that.apiVersion); } }); - } + }; /** * Utility function to query the Chatter API and download a file @@ -217,7 +217,7 @@ if (forcetk.Client === undefined) { * @param [error=null] function to which request will be passed in case of error * @param retry true if we've already tried refresh token flow once */ - forcetk.Client.prototype.getChatterFile = function(path, mimeType, callback, error, retry) { + forcetk.Client.prototype.getChatterFile = function (path, mimeType, callback, error, retry) { 'use strict'; var that = this; var url = (this.visualforce ? '' : this.instanceUrl) + path; @@ -265,10 +265,11 @@ if (forcetk.Client === undefined) { request.send(); - } + }; // Local utility to create a random string for multipart boundary - function randomString() { + var randomString = function () { + 'use strict'; var str = ''; for (var i = 0; i < 4; i++) { str += (Math.random().toString(16)+"000000000").substr(2,8); @@ -288,7 +289,7 @@ if (forcetk.Client === undefined) { * @param [error=null] function to which response will be passed in case of error * @param retry true if we've already tried refresh token flow once */ - forcetk.Client.prototype.blob = function(path, fields, filename, payloadField, payload, callback, error, retry) { + forcetk.Client.prototype.blob = function (path, fields, filename, payloadField, payload, callback, error, retry) { 'use strict'; var that = this; var url = (this.visualforce ? '' : this.instanceUrl) + '/services/data' + path; @@ -344,7 +345,7 @@ if (forcetk.Client === undefined) { request.send(blob); return this.asyncAjax ? null : JSON.parse(request.response); - } + }; /* * Create a record with blob data @@ -359,13 +360,13 @@ if (forcetk.Client === undefined) { * @param [error=null] function to which response will be passed in case of error * @param retry true if we've already tried refresh token flow once */ - forcetk.Client.prototype.createBlob = function(objtype, fields, filename, + forcetk.Client.prototype.createBlob = function (objtype, fields, filename, payloadField, payload, callback, error, retry) { 'use strict'; return this.blob('/' + this.apiVersion + '/sobjects/' + objtype + '/', fields, filename, payloadField, payload, callback, error); - } + }; /* * Update a record with blob data @@ -381,13 +382,13 @@ if (forcetk.Client === undefined) { * @param [error=null] function to which response will be passed in case of error * @param retry true if we've already tried refresh token flow once */ - forcetk.Client.prototype.updateBlob = function(objtype, id, fields, filename, + forcetk.Client.prototype.updateBlob = function (objtype, id, fields, filename, payloadField, payload, callback, error, retry) { 'use strict'; return this.blob('/' + this.apiVersion + '/sobjects/' + objtype + '/' + id + '?_HttpMethod=PATCH', fields, filename, payloadField, payload, callback, error); - } + }; /* * Low level utility function to call the Salesforce endpoint specific for Apex REST API. @@ -399,7 +400,7 @@ if (forcetk.Client === undefined) { * @param [paramMap={}] parameters to send as header values for POST/PATCH etc * @param [retry] specifies whether to retry on error */ - forcetk.Client.prototype.apexrest = function(path, callback, error, method, payload, paramMap, retry) { + forcetk.Client.prototype.apexrest = function (path, callback, error, method, payload, paramMap, retry) { 'use strict'; var that = this; var url = this.instanceUrl + '/services/apexrest' + path; @@ -441,7 +442,7 @@ if (forcetk.Client === undefined) { xhr.setRequestHeader('X-User-Agent', 'salesforce-toolkit-rest-javascript/' + that.apiVersion); } }); - } + }; /* * Lists summary information about each Salesforce.com version currently @@ -450,10 +451,10 @@ if (forcetk.Client === undefined) { * @param callback function to which response will be passed * @param [error=null] function to which jqXHR will be passed in case of error */ - forcetk.Client.prototype.versions = function(callback, error) { + forcetk.Client.prototype.versions = function (callback, error) { 'use strict'; return this.ajax('/', callback, error); - } + }; /* * Lists available resources for the client's API version, including @@ -461,10 +462,10 @@ if (forcetk.Client === undefined) { * @param callback function to which response will be passed * @param [error=null] function to which jqXHR will be passed in case of error */ - forcetk.Client.prototype.resources = function(callback, error) { + forcetk.Client.prototype.resources = function (callback, error) { 'use strict'; return this.ajax('/' + this.apiVersion + '/', callback, error); - } + }; /* * Lists the available objects and their metadata for your organization's @@ -472,10 +473,10 @@ if (forcetk.Client === undefined) { * @param callback function to which response will be passed * @param [error=null] function to which jqXHR will be passed in case of error */ - forcetk.Client.prototype.describeGlobal = function(callback, error) { + forcetk.Client.prototype.describeGlobal = function (callback, error) { 'use strict'; return this.ajax('/' + this.apiVersion + '/sobjects/', callback, error); - } + }; /* * Describes the individual metadata for the specified object. @@ -483,11 +484,11 @@ if (forcetk.Client === undefined) { * @param callback function to which response will be passed * @param [error=null] function to which jqXHR will be passed in case of error */ - forcetk.Client.prototype.metadata = function(objtype, callback, error) { + forcetk.Client.prototype.metadata = function (objtype, callback, error) { 'use strict'; return this.ajax('/' + this.apiVersion + '/sobjects/' + objtype + '/' , callback, error); - } + }; /* * Completely describes the individual metadata at all levels for the @@ -496,11 +497,11 @@ if (forcetk.Client === undefined) { * @param callback function to which response will be passed * @param [error=null] function to which jqXHR will be passed in case of error */ - forcetk.Client.prototype.describe = function(objtype, callback, error) { + forcetk.Client.prototype.describe = function (objtype, callback, error) { 'use strict'; return this.ajax('/' + this.apiVersion + '/sobjects/' + objtype + '/describe/', callback, error); - } + }; /* * Creates a new record of the given type. @@ -511,11 +512,11 @@ if (forcetk.Client === undefined) { * @param callback function to which response will be passed * @param [error=null] function to which jqXHR will be passed in case of error */ - forcetk.Client.prototype.create = function(objtype, fields, callback, error) { + forcetk.Client.prototype.create = function (objtype, fields, callback, error) { 'use strict'; return this.ajax('/' + this.apiVersion + '/sobjects/' + objtype + '/' , callback, error, "POST", JSON.stringify(fields)); - } + }; /* * Retrieves field values for a record of the given type. @@ -526,7 +527,7 @@ if (forcetk.Client === undefined) { * @param callback function to which response will be passed * @param [error=null] function to which jqXHR will be passed in case of error */ - forcetk.Client.prototype.retrieve = function(objtype, id, fieldlist, callback, error) { + forcetk.Client.prototype.retrieve = function (objtype, id, fieldlist, callback, error) { 'use strict'; if (arguments.length == 4) { error = callback; @@ -536,7 +537,7 @@ if (forcetk.Client === undefined) { var fields = fieldlist ? '?fields=' + fieldlist : ''; return this.ajax('/' + this.apiVersion + '/sobjects/' + objtype + '/' + id + fields, callback, error); - } + }; /* * Upsert - creates or updates record of the given type, based on the @@ -550,11 +551,11 @@ if (forcetk.Client === undefined) { * @param callback function to which response will be passed * @param [error=null] function to which jqXHR will be passed in case of error */ - forcetk.Client.prototype.upsert = function(objtype, externalIdField, externalId, fields, callback, error) { + forcetk.Client.prototype.upsert = function (objtype, externalIdField, externalId, fields, callback, error) { 'use strict'; return this.ajax('/' + this.apiVersion + '/sobjects/' + objtype + '/' + externalIdField + '/' + externalId + '?_HttpMethod=PATCH', callback, error, "POST", JSON.stringify(fields)); - } + }; /* * Updates field values on a record of the given type. @@ -566,11 +567,11 @@ if (forcetk.Client === undefined) { * @param callback function to which response will be passed * @param [error=null] function to which jqXHR will be passed in case of error */ - forcetk.Client.prototype.update = function(objtype, id, fields, callback, error) { + forcetk.Client.prototype.update = function (objtype, id, fields, callback, error) { 'use strict'; return this.ajax('/' + this.apiVersion + '/sobjects/' + objtype + '/' + id + '?_HttpMethod=PATCH', callback, error, "POST", JSON.stringify(fields)); - } + }; /* * Deletes a record of the given type. Unfortunately, 'delete' is a @@ -580,11 +581,11 @@ if (forcetk.Client === undefined) { * @param callback function to which response will be passed * @param [error=null] function to which jqXHR will be passed in case of error */ - forcetk.Client.prototype.del = function(objtype, id, callback, error) { + forcetk.Client.prototype.del = function (objtype, id, callback, error) { 'use strict'; return this.ajax('/' + this.apiVersion + '/sobjects/' + objtype + '/' + id , callback, error, "DELETE"); - } + }; /* * Executes the specified SOQL query. @@ -593,11 +594,11 @@ if (forcetk.Client === undefined) { * @param callback function to which response will be passed * @param [error=null] function to which jqXHR will be passed in case of error */ - forcetk.Client.prototype.query = function(soql, callback, error) { + forcetk.Client.prototype.query = function (soql, callback, error) { 'use strict'; return this.ajax('/' + this.apiVersion + '/query?q=' + encodeURIComponent(soql) , callback, error); - } + }; /* * Queries the next set of records based on pagination. @@ -609,7 +610,7 @@ if (forcetk.Client === undefined) { * @param callback function to which response will be passed * @param [error=null] function to which jqXHR will be passed in case of error */ - forcetk.Client.prototype.queryMore = function( url, callback, error ){ + forcetk.Client.prototype.queryMore = function ( url, callback, error ){ 'use strict'; //-- ajax call adds on services/data to the url call, so only send the url after var serviceData = "services/data"; @@ -622,7 +623,7 @@ if (forcetk.Client === undefined) { } return this.ajax( url, callback, error ); - } + }; /* * Executes the specified SOSL search. @@ -631,9 +632,9 @@ if (forcetk.Client === undefined) { * @param callback function to which response will be passed * @param [error=null] function to which jqXHR will be passed in case of error */ - forcetk.Client.prototype.search = function(sosl, callback, error) { + forcetk.Client.prototype.search = function (sosl, callback, error) { 'use strict'; return this.ajax('/' + this.apiVersion + '/search?q=' + encodeURIComponent(sosl) , callback, error); - } + }; } From 97ffdbecda2462e58083f6fd5d37bb301dd4e564 Mon Sep 17 00:00:00 2001 From: Matt Goldspink Date: Wed, 21 Oct 2015 10:50:57 -0700 Subject: [PATCH 04/11] Formatting changes to get back in sync with latest --- forcetk.js | 263 ++++++++++++++++++++++++++--------------------------- 1 file changed, 128 insertions(+), 135 deletions(-) diff --git a/forcetk.js b/forcetk.js index 0b8705f..7a11a47 100644 --- a/forcetk.js +++ b/forcetk.js @@ -105,19 +105,19 @@ if (forcetk.Client === undefined) { */ forcetk.Client.prototype.refreshAccessToken = function (callback, error) { 'use strict'; - var that = this; - var url = this.loginUrl + '/services/oauth2/token'; + var that = this, + url = this.loginUrl + '/services/oauth2/token'; return $.ajax({ type: 'POST', - url: (this.proxyUrl !== null && ! this.visualforce) ? this.proxyUrl: url, + url: (this.proxyUrl !== null && !this.visualforce) ? this.proxyUrl : url, cache: false, processData: false, data: 'grant_type=refresh_token&client_id=' + this.clientId + '&refresh_token=' + this.refreshToken, success: callback, error: error, dataType: "json", - beforeSend: function(xhr) { - if (that.proxyUrl !== null && ! this.visualforce) { + beforeSend: function (xhr) { + if (that.proxyUrl !== null && !this.visualforce) { xhr.setRequestHeader('SalesforceProxy-Endpoint', url); } } @@ -135,25 +135,24 @@ if (forcetk.Client === undefined) { forcetk.Client.prototype.setSessionToken = function (sessionId, apiVersion, instanceUrl) { 'use strict'; this.sessionId = sessionId; - this.apiVersion = (typeof apiVersion === 'undefined' || apiVersion === null) - ? 'v29.0': apiVersion; - if (typeof instanceUrl === 'undefined' || instanceUrl == null) { + this.apiVersion = (apiVersion === undefined || apiVersion === null) + ? 'v29.0' : apiVersion; + if (instanceUrl === undefined || instanceUrl === null) { this.visualforce = true; // location.hostname can be of the form 'abc.na1.visual.force.com', // 'na1.salesforce.com' or 'abc.my.salesforce.com' (custom domains). // Split on '.', and take the [1] or [0] element as appropriate - var elements = location.hostname.split("."); - - var instance = null; - if(elements.length == 4 && elements[1] === 'my') { + var elements = location.hostname.split("."), + instance = null; + if (elements.length === 4 && elements[1] === 'my') { instance = elements[0] + '.' + elements[1]; - } else if(elements.length == 3){ + } else if (elements.length === 3){ instance = elements[0]; } else { instance = elements[1]; } - + this.instanceUrl = "https://" + instance + ".salesforce.com"; } else { this.instanceUrl = instanceUrl; @@ -170,33 +169,33 @@ if (forcetk.Client === undefined) { */ forcetk.Client.prototype.ajax = function (path, callback, error, method, payload, retry) { 'use strict'; - var that = this; - var url = (this.visualforce ? '' : this.instanceUrl) + '/services/data' + path; + var that = this, + url = (this.visualforce ? '' : this.instanceUrl) + '/services/data' + path; return $.ajax({ type: method || "GET", async: this.asyncAjax, - url: (this.proxyUrl !== null && ! this.visualforce) ? this.proxyUrl: url, - contentType: method == "DELETE" ? null : 'application/json', + url: (this.proxyUrl !== null && !this.visualforce) ? this.proxyUrl : url, + contentType: method === "DELETE" ? null : 'application/json', cache: false, processData: false, data: payload, success: callback, - error: (!this.refreshToken || retry ) ? error : function(jqXHR, textStatus, errorThrown) { + error: (!this.refreshToken || retry) ? error : function (jqXHR, textStatus, errorThrown) { if (jqXHR.status === 401) { that.refreshAccessToken(function(oauthResponse) { that.setSessionToken(oauthResponse.access_token, null, - oauthResponse.instance_url); + oauthResponse.instance_url); that.ajax(path, callback, error, method, payload, true); }, - error); + error); } else { error(jqXHR, textStatus, errorThrown); } }, dataType: "json", - beforeSend: function(xhr) { - if (that.proxyUrl !== null && ! that.visualforce) { + beforeSend: function (xhr) { + if (that.proxyUrl !== null && !that.visualforce) { xhr.setRequestHeader('SalesforceProxy-Endpoint', url); } xhr.setRequestHeader(that.authzHeader, "Bearer " + that.sessionId); @@ -219,64 +218,59 @@ if (forcetk.Client === undefined) { */ forcetk.Client.prototype.getChatterFile = function (path, mimeType, callback, error, retry) { 'use strict'; - var that = this; - var url = (this.visualforce ? '' : this.instanceUrl) + path; - - var request = new XMLHttpRequest(); + var that = this, + url = (this.visualforce ? '' : this.instanceUrl) + path, + request = new XMLHttpRequest(); - request.open("GET", (this.proxyUrl !== null && ! this.visualforce) ? this.proxyUrl: url, true); + request.open("GET", (this.proxyUrl !== null && !this.visualforce) ? this.proxyUrl : url, true); request.responseType = "arraybuffer"; - + request.setRequestHeader(this.authzHeader, "Bearer " + this.sessionId); request.setRequestHeader('X-User-Agent', 'salesforce-toolkit-rest-javascript/' + this.apiVersion); - if (this.proxyUrl !== null && ! this.visualforce) { + if (this.proxyUrl !== null && !this.visualforce) { request.setRequestHeader('SalesforceProxy-Endpoint', url); } - - request.onreadystatechange = function() { + + request.onreadystatechange = function () { // continue if the process is completed - if (request.readyState == 4) { + if (request.readyState === 4) { // continue only if HTTP status is "OK" - if (request.status == 200) { + if (request.status === 200) { try { // retrieve the response callback(request.response); - } - catch(e) { + } catch (e) { // display error message alert("Error reading the response: " + e.toString()); } - } - //refresh token in 401 - else if(request.status == 401 && !retry) { + } else if (request.status === 401 && !retry) { + //refresh token in 401 that.refreshAccessToken(function(oauthResponse) { - that.setSessionToken(oauthResponse.access_token, null,oauthResponse.instance_url); + that.setSessionToken(oauthResponse.access_token, null, oauthResponse.instance_url); that.getChatterFile(path, mimeType, callback, error, true); - }, - error); - } - else { + }, error); + } else { // display status message - error(request,request.statusText,request.response); + error(request, request.statusText, request.response); } - } - + } } request.send(); - + }; - + // Local utility to create a random string for multipart boundary var randomString = function () { 'use strict'; - var str = ''; - for (var i = 0; i < 4; i++) { - str += (Math.random().toString(16)+"000000000").substr(2,8); + var str = '', + i; + for (i = 0; i < 4; i++) { + str += (Math.random().toString(16) + "000000000").substr(2, 8); } return str; - } - + }; + /* Low level function to create/update records with blob data * @param path resource path relative to /services/data * @param fields an object containing initial field names and values for @@ -289,64 +283,63 @@ if (forcetk.Client === undefined) { * @param [error=null] function to which response will be passed in case of error * @param retry true if we've already tried refresh token flow once */ - forcetk.Client.prototype.blob = function (path, fields, filename, payloadField, payload, callback, error, retry) { + forcetk.Client.prototype.blob = function (path, fields, filename, payloadField, payload, callback, error, retry) { 'use strict'; - var that = this; - var url = (this.visualforce ? '' : this.instanceUrl) + '/services/data' + path; - var boundary = randomString(); - - var blob = new Blob([ - "--boundary_" + boundary + '\n' - + "Content-Disposition: form-data; name=\"entity_content\";" + "\n" - + "Content-Type: application/json" + "\n\n" - + JSON.stringify(fields) - + "\n\n" - + "--boundary_" + boundary + "\n" - + "Content-Type: application/octet-stream" + "\n" - + "Content-Disposition: form-data; name=\"" + payloadField - + "\"; filename=\"" + filename + "\"\n\n", - payload, - "\n\n" - + "--boundary_" + boundary + "--" - ], {type : 'multipart/form-data; boundary=\"boundary_' + boundary + '\"'}); - - var request = new XMLHttpRequest(); - request.open("POST", (this.proxyUrl !== null && ! this.visualforce) ? this.proxyUrl: url, this.asyncAjax); + var that = this, + url = (this.visualforce ? '' : this.instanceUrl) + '/services/data' + path, + boundary = randomString(), + blob = new Blob([ + "--boundary_" + boundary + '\n' + + "Content-Disposition: form-data; name=\"entity_content\";" + "\n" + + "Content-Type: application/json" + "\n\n" + + JSON.stringify(fields) + + "\n\n" + + "--boundary_" + boundary + "\n" + + "Content-Type: application/octet-stream" + "\n" + + "Content-Disposition: form-data; name=\"" + payloadField + + "\"; filename=\"" + filename + "\"\n\n", + payload, + "\n\n" + + "--boundary_" + boundary + "--" + ], {type : 'multipart/form-data; boundary=\"boundary_' + boundary + '\"'}), + request = new XMLHttpRequest(); + + request.open("POST", (this.proxyUrl !== null && !this.visualforce) ? this.proxyUrl : url, this.asyncAjax); request.setRequestHeader('Accept', 'application/json'); request.setRequestHeader(this.authzHeader, "Bearer " + this.sessionId); request.setRequestHeader('X-User-Agent', 'salesforce-toolkit-rest-javascript/' + this.apiVersion); - if (this.proxyUrl !== null && ! this.visualforce) { + request.setRequestHeader('Content-Type', 'multipart/form-data; boundary=\"boundary_' + boundary + '\"'); + if (this.proxyUrl !== null && !this.visualforce) { request.setRequestHeader('SalesforceProxy-Endpoint', url); } - + if (this.asyncAjax) { - request.onreadystatechange = function() { + request.onreadystatechange = function () { // continue if the process is completed - if (request.readyState == 4) { + if (request.readyState === 4) { // continue only if HTTP status is good if (request.status >= 200 && request.status < 300) { // retrieve the response callback(request.response ? JSON.parse(request.response) : null); - } else if(request.status == 401 && !retry) { - that.refreshAccessToken(function(oauthResponse) { - that.setSessionToken(oauthResponse.access_token, null,oauthResponse.instance_url); + } else if (request.status == 401 && !retry) { + that.refreshAccessToken(function (oauthResponse) { + that.setSessionToken(oauthResponse.access_token, null, oauthResponse.instance_url); that.blob(path, fields, fileName, file, callback, error, true); - }, - error); + }, error); } else { // return status message error(request, request.statusText, request.response); } - } + } } } - + request.send(blob); - + return this.asyncAjax ? null : JSON.parse(request.response); }; - + /* * Create a record with blob data * @param objtype object type; e.g. "ContentVersion" @@ -360,12 +353,12 @@ if (forcetk.Client === undefined) { * @param [error=null] function to which response will be passed in case of error * @param retry true if we've already tried refresh token flow once */ - forcetk.Client.prototype.createBlob = function (objtype, fields, filename, - payloadField, payload, callback, - error, retry) { + forcetk.Client.prototype.createBlob = function (objtype, fields, filename, + payloadField, payload, callback, + error, retry) { 'use strict'; - return this.blob('/' + this.apiVersion + '/sobjects/' + objtype + '/', - fields, filename, payloadField, payload, callback, error); + return this.blob('/' + this.apiVersion + '/sobjects/' + objtype + '/', + fields, filename, payloadField, payload, callback, error, retry); }; /* @@ -382,12 +375,12 @@ if (forcetk.Client === undefined) { * @param [error=null] function to which response will be passed in case of error * @param retry true if we've already tried refresh token flow once */ - forcetk.Client.prototype.updateBlob = function (objtype, id, fields, filename, - payloadField, payload, callback, - error, retry) { + forcetk.Client.prototype.updateBlob = function (objtype, id, fields, filename, + payloadField, payload, callback, + error, retry) { 'use strict'; - return this.blob('/' + this.apiVersion + '/sobjects/' + objtype + '/' + id + - '?_HttpMethod=PATCH', fields, filename, payloadField, payload, callback, error); + return this.blob('/' + this.apiVersion + '/sobjects/' + objtype + '/' + id + + '?_HttpMethod=PATCH', fields, filename, payloadField, payload, callback, error, retry); }; /* @@ -402,32 +395,32 @@ if (forcetk.Client === undefined) { */ forcetk.Client.prototype.apexrest = function (path, callback, error, method, payload, paramMap, retry) { 'use strict'; - var that = this; - var url = this.instanceUrl + '/services/apexrest' + path; + var that = this, + url = this.instanceUrl + '/services/apexrest' + path; return $.ajax({ type: method || "GET", async: this.asyncAjax, - url: (this.proxyUrl !== null) ? this.proxyUrl: url, + url: (this.proxyUrl !== null) ? this.proxyUrl : url, contentType: 'application/json', cache: false, processData: false, data: payload, success: callback, - error: (!this.refreshToken || retry ) ? error : function(jqXHR, textStatus, errorThrown) { + error: (!this.refreshToken || retry) ? error : function (jqXHR, textStatus, errorThrown) { if (jqXHR.status === 401) { - that.refreshAccessToken(function(oauthResponse) { + that.refreshAccessToken(function (oauthResponse) { that.setSessionToken(oauthResponse.access_token, null, - oauthResponse.instance_url); + oauthResponse.instance_url); that.apexrest(path, callback, error, method, payload, paramMap, true); - }, - error); + }, error); } else { error(jqXHR, textStatus, errorThrown); } }, dataType: "json", - beforeSend: function(xhr) { + beforeSend: function (xhr) { + var paramName; if (that.proxyUrl !== null) { xhr.setRequestHeader('SalesforceProxy-Endpoint', url); } @@ -436,7 +429,9 @@ if (forcetk.Client === undefined) { paramMap = {}; } for (paramName in paramMap) { - xhr.setRequestHeader(paramName, paramMap[paramName]); + if (paramMap.hasOWnProperty(paramName)) { + xhr.setRequestHeader(paramName, paramMap[paramName]); + } } xhr.setRequestHeader(that.authzHeader, "Bearer " + that.sessionId); xhr.setRequestHeader('X-User-Agent', 'salesforce-toolkit-rest-javascript/' + that.apiVersion); @@ -486,8 +481,8 @@ if (forcetk.Client === undefined) { */ forcetk.Client.prototype.metadata = function (objtype, callback, error) { 'use strict'; - return this.ajax('/' + this.apiVersion + '/sobjects/' + objtype + '/' - , callback, error); + return this.ajax('/' + this.apiVersion + '/sobjects/' + objtype + '/', + callback, error); }; /* @@ -500,7 +495,7 @@ if (forcetk.Client === undefined) { forcetk.Client.prototype.describe = function (objtype, callback, error) { 'use strict'; return this.ajax('/' + this.apiVersion + '/sobjects/' + objtype - + '/describe/', callback, error); + + '/describe/', callback, error); }; /* @@ -514,8 +509,8 @@ if (forcetk.Client === undefined) { */ forcetk.Client.prototype.create = function (objtype, fields, callback, error) { 'use strict'; - return this.ajax('/' + this.apiVersion + '/sobjects/' + objtype + '/' - , callback, error, "POST", JSON.stringify(fields)); + return this.ajax('/' + this.apiVersion + '/sobjects/' + objtype + '/', + callback, error, "POST", JSON.stringify(fields)); }; /* @@ -529,14 +524,14 @@ if (forcetk.Client === undefined) { */ forcetk.Client.prototype.retrieve = function (objtype, id, fieldlist, callback, error) { 'use strict'; - if (arguments.length == 4) { + if (arguments.length === 4) { error = callback; callback = fieldlist; fieldlist = null; } var fields = fieldlist ? '?fields=' + fieldlist : ''; return this.ajax('/' + this.apiVersion + '/sobjects/' + objtype + '/' + id - + fields, callback, error); + + fields, callback, error); }; /* @@ -553,8 +548,8 @@ if (forcetk.Client === undefined) { */ forcetk.Client.prototype.upsert = function (objtype, externalIdField, externalId, fields, callback, error) { 'use strict'; - return this.ajax('/' + this.apiVersion + '/sobjects/' + objtype + '/' + externalIdField + '/' + externalId - + '?_HttpMethod=PATCH', callback, error, "POST", JSON.stringify(fields)); + return this.ajax('/' + this.apiVersion + '/sobjects/' + objtype + '/' + externalIdField + '/' + externalId + + '?_HttpMethod=PATCH', callback, error, "POST", JSON.stringify(fields)); }; /* @@ -569,8 +564,8 @@ if (forcetk.Client === undefined) { */ forcetk.Client.prototype.update = function (objtype, id, fields, callback, error) { 'use strict'; - return this.ajax('/' + this.apiVersion + '/sobjects/' + objtype + '/' + id - + '?_HttpMethod=PATCH', callback, error, "POST", JSON.stringify(fields)); + return this.ajax('/' + this.apiVersion + '/sobjects/' + objtype + '/' + id + + '?_HttpMethod=PATCH', callback, error, "POST", JSON.stringify(fields)); }; /* @@ -583,8 +578,8 @@ if (forcetk.Client === undefined) { */ forcetk.Client.prototype.del = function (objtype, id, callback, error) { 'use strict'; - return this.ajax('/' + this.apiVersion + '/sobjects/' + objtype + '/' + id - , callback, error, "DELETE"); + return this.ajax('/' + this.apiVersion + '/sobjects/' + objtype + '/' + id, + callback, error, "DELETE"); }; /* @@ -596,10 +591,10 @@ if (forcetk.Client === undefined) { */ forcetk.Client.prototype.query = function (soql, callback, error) { 'use strict'; - return this.ajax('/' + this.apiVersion + '/query?q=' + encodeURIComponent(soql) - , callback, error); + return this.ajax('/' + this.apiVersion + '/query?q=' + encodeURIComponent(soql), + callback, error); }; - + /* * Queries the next set of records based on pagination. *

This should be used if performing a query that retrieves more than can be returned @@ -610,19 +605,17 @@ if (forcetk.Client === undefined) { * @param callback function to which response will be passed * @param [error=null] function to which jqXHR will be passed in case of error */ - forcetk.Client.prototype.queryMore = function ( url, callback, error ){ + forcetk.Client.prototype.queryMore = function (url, callback, error) { 'use strict'; //-- ajax call adds on services/data to the url call, so only send the url after - var serviceData = "services/data"; - var index = url.indexOf( serviceData ); + var serviceData = "services/data", + index = url.indexOf(serviceData); - if( index > -1 ){ - url = url.substr( index + serviceData.length ); - } else { - //-- leave alone + if( index > -1) { + url = url.substr(index + serviceData.length); } - return this.ajax( url, callback, error ); + return this.ajax(url, callback, error); }; /* @@ -634,7 +627,7 @@ if (forcetk.Client === undefined) { */ forcetk.Client.prototype.search = function (sosl, callback, error) { 'use strict'; - return this.ajax('/' + this.apiVersion + '/search?q=' + encodeURIComponent(sosl) - , callback, error); + return this.ajax('/' + this.apiVersion + '/search?q=' + encodeURIComponent(sosl), + callback, error); }; } From a5c5a80286d5dbfd924b04eabe5a97492b6abd69 Mon Sep 17 00:00:00 2001 From: Matt Goldspink Date: Wed, 21 Oct 2015 10:55:20 -0700 Subject: [PATCH 05/11] More small code formatting changes --- forcetk.js | 56 +++++++++++++++++++++++++++--------------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/forcetk.js b/forcetk.js index 7a11a47..431d76b 100644 --- a/forcetk.js +++ b/forcetk.js @@ -58,7 +58,7 @@ if (forcetk.Client === undefined) { 'use strict'; this.clientId = clientId; this.loginUrl = loginUrl || 'https://login.salesforce.com/'; - if (typeof proxyUrl === 'undefined' || proxyUrl === null) { + if (proxyUrl === undefined || proxyUrl === null) { if (location.protocol === 'file:' || location.protocol === 'ms-appx:') { // In PhoneGap this.proxyUrl = null; @@ -74,7 +74,7 @@ if (forcetk.Client === undefined) { } else { this.proxyUrl += "/services/proxy"; } - } + } this.authzHeader = "Authorization"; // Vlocity Card Team: this is for VF } else { // On a server outside VF @@ -147,7 +147,7 @@ if (forcetk.Client === undefined) { instance = null; if (elements.length === 4 && elements[1] === 'my') { instance = elements[0] + '.' + elements[1]; - } else if (elements.length === 3){ + } else if (elements.length === 3) { instance = elements[0]; } else { instance = elements[1]; @@ -183,7 +183,7 @@ if (forcetk.Client === undefined) { success: callback, error: (!this.refreshToken || retry) ? error : function (jqXHR, textStatus, errorThrown) { if (jqXHR.status === 401) { - that.refreshAccessToken(function(oauthResponse) { + that.refreshAccessToken(function (oauthResponse) { that.setSessionToken(oauthResponse.access_token, null, oauthResponse.instance_url); that.ajax(path, callback, error, method, payload, true); @@ -221,8 +221,8 @@ if (forcetk.Client === undefined) { var that = this, url = (this.visualforce ? '' : this.instanceUrl) + path, request = new XMLHttpRequest(); - - request.open("GET", (this.proxyUrl !== null && !this.visualforce) ? this.proxyUrl : url, true); + + request.open("GET", (this.proxyUrl !== null && !this.visualforce) ? this.proxyUrl : url, true); request.responseType = "arraybuffer"; request.setRequestHeader(this.authzHeader, "Bearer " + this.sessionId); @@ -245,7 +245,7 @@ if (forcetk.Client === undefined) { } } else if (request.status === 401 && !retry) { //refresh token in 401 - that.refreshAccessToken(function(oauthResponse) { + that.refreshAccessToken(function (oauthResponse) { that.setSessionToken(oauthResponse.access_token, null, oauthResponse.instance_url); that.getChatterFile(path, mimeType, callback, error, true); }, error); @@ -254,7 +254,7 @@ if (forcetk.Client === undefined) { error(request, request.statusText, request.response); } } - } + }; request.send(); @@ -265,7 +265,7 @@ if (forcetk.Client === undefined) { 'use strict'; var str = '', i; - for (i = 0; i < 4; i++) { + for (i = 0; i < 4; i += 1) { str += (Math.random().toString(16) + "000000000").substr(2, 8); } return str; @@ -289,21 +289,21 @@ if (forcetk.Client === undefined) { url = (this.visualforce ? '' : this.instanceUrl) + '/services/data' + path, boundary = randomString(), blob = new Blob([ - "--boundary_" + boundary + '\n' - + "Content-Disposition: form-data; name=\"entity_content\";" + "\n" - + "Content-Type: application/json" + "\n\n" - + JSON.stringify(fields) - + "\n\n" - + "--boundary_" + boundary + "\n" - + "Content-Type: application/octet-stream" + "\n" - + "Content-Disposition: form-data; name=\"" + payloadField - + "\"; filename=\"" + filename + "\"\n\n", + "--boundary_" + boundary + '\n' + + "Content-Disposition: form-data; name=\"entity_content\";" + "\n" + + "Content-Type: application/json" + "\n\n" + + JSON.stringify(fields) + + "\n\n" + + "--boundary_" + boundary + "\n" + + "Content-Type: application/octet-stream" + "\n" + + "Content-Disposition: form-data; name=\"" + payloadField + + "\"; filename=\"" + filename + "\"\n\n", payload, - "\n\n" - + "--boundary_" + boundary + "--" + "\n\n" + + "--boundary_" + boundary + "--" ], {type : 'multipart/form-data; boundary=\"boundary_' + boundary + '\"'}), request = new XMLHttpRequest(); - + request.open("POST", (this.proxyUrl !== null && !this.visualforce) ? this.proxyUrl : url, this.asyncAjax); request.setRequestHeader('Accept', 'application/json'); @@ -322,17 +322,17 @@ if (forcetk.Client === undefined) { if (request.status >= 200 && request.status < 300) { // retrieve the response callback(request.response ? JSON.parse(request.response) : null); - } else if (request.status == 401 && !retry) { + } else if (request.status === 401 && !retry) { that.refreshAccessToken(function (oauthResponse) { that.setSessionToken(oauthResponse.access_token, null, oauthResponse.instance_url); - that.blob(path, fields, fileName, file, callback, error, true); + that.blob(path, fields, fileName, payloadField, payload, callback, error, true); }, error); } else { // return status message error(request, request.statusText, request.response); } } - } + }; } request.send(blob); @@ -429,7 +429,7 @@ if (forcetk.Client === undefined) { paramMap = {}; } for (paramName in paramMap) { - if (paramMap.hasOWnProperty(paramName)) { + if (paramMap.hasOwnProperty(paramName)) { xhr.setRequestHeader(paramName, paramMap[paramName]); } } @@ -610,11 +610,11 @@ if (forcetk.Client === undefined) { //-- ajax call adds on services/data to the url call, so only send the url after var serviceData = "services/data", index = url.indexOf(serviceData); - - if( index > -1) { + + if (index > -1) { url = url.substr(index + serviceData.length); } - + return this.ajax(url, callback, error); }; From bdfdaa016da31c9c48059c60572719951406e819 Mon Sep 17 00:00:00 2001 From: Matt Goldspink Date: Wed, 21 Oct 2015 10:55:46 -0700 Subject: [PATCH 06/11] Update forcetk.js --- forcetk.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/forcetk.js b/forcetk.js index 431d76b..f238bd6 100644 --- a/forcetk.js +++ b/forcetk.js @@ -325,7 +325,7 @@ if (forcetk.Client === undefined) { } else if (request.status === 401 && !retry) { that.refreshAccessToken(function (oauthResponse) { that.setSessionToken(oauthResponse.access_token, null, oauthResponse.instance_url); - that.blob(path, fields, fileName, payloadField, payload, callback, error, true); + that.blob(path, fields, filename, payloadField, payload, callback, error, true); }, error); } else { // return status message From eaba0bb0dbbb293198b8904911195640706f306f Mon Sep 17 00:00:00 2001 From: Matt Goldspink Date: Wed, 21 Oct 2015 11:12:19 -0700 Subject: [PATCH 07/11] Simplifying code change --- forcetk.js | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/forcetk.js b/forcetk.js index f238bd6..1d2091f 100644 --- a/forcetk.js +++ b/forcetk.js @@ -64,22 +64,14 @@ if (forcetk.Client === undefined) { this.proxyUrl = null; } else { // In Visualforce - still need proxyUrl for Apex REST methods - this.proxyUrl = location.protocol + "//" + location.hostname; - - // Vlocity Card Team added pathname to handle community path because in the scenario of community, - // forcetk proxy is set to community url, and if the url has path, the proxy url has to contain that. - // Otherwise, you will get a 503 error - if (typeof communityInd !== 'undefined' && communityInd !== null && communityInd) { - this.proxyUrl += location.pathname + "services/proxy"; - } else { - this.proxyUrl += "/services/proxy"; - } + this.proxyUrl = location.protocol + "//" + location.hostname + + (communityInd ? location.pathname : "") + "services/proxy"; } - this.authzHeader = "Authorization"; // Vlocity Card Team: this is for VF + this.authzHeader = "Authorization"; } else { // On a server outside VF this.proxyUrl = proxyUrl; - this.authzHeader = "X-Authorization"; // Vlocity Card Team: if you ever use this for VF, you will get a 401 unauthorized - INVALID SESSION error + this.authzHeader = "X-Authorization"; } this.refreshToken = null; this.sessionId = null; From c3b54bf972599040d82eec7b0e0d1cc29b0d2149 Mon Sep 17 00:00:00 2001 From: Matt Goldspink Date: Wed, 21 Oct 2015 11:24:28 -0700 Subject: [PATCH 08/11] Add missing "/" --- forcetk.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/forcetk.js b/forcetk.js index 1d2091f..cbfc6ca 100644 --- a/forcetk.js +++ b/forcetk.js @@ -65,7 +65,7 @@ if (forcetk.Client === undefined) { } else { // In Visualforce - still need proxyUrl for Apex REST methods this.proxyUrl = location.protocol + "//" + location.hostname - + (communityInd ? location.pathname : "") + "services/proxy"; + + (communityInd ? location.pathname : "") + "/services/proxy"; } this.authzHeader = "Authorization"; } else { From b6f86e086466e719b4dc5e73ed22da604d3aa9b4 Mon Sep 17 00:00:00 2001 From: Matt Goldspink Date: Wed, 21 Oct 2015 11:31:37 -0700 Subject: [PATCH 09/11] Update forcetk.js Handle trailing "/" on community pathname and only do replace if communityInd is true --- forcetk.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/forcetk.js b/forcetk.js index cbfc6ca..642b75f 100644 --- a/forcetk.js +++ b/forcetk.js @@ -65,7 +65,7 @@ if (forcetk.Client === undefined) { } else { // In Visualforce - still need proxyUrl for Apex REST methods this.proxyUrl = location.protocol + "//" + location.hostname - + (communityInd ? location.pathname : "") + "/services/proxy"; + + (communityInd === true ? location.pathname.replace(/\/$/, "") : "") + "/services/proxy"; } this.authzHeader = "Authorization"; } else { From 95237e6869a8704e40f0e0a20d7074f5198f25fe Mon Sep 17 00:00:00 2001 From: Matt Goldspink Date: Mon, 2 Nov 2015 18:10:08 -0800 Subject: [PATCH 10/11] Remove Apex prefix when in communities --- forcetk.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/forcetk.js b/forcetk.js index 642b75f..c531e8e 100644 --- a/forcetk.js +++ b/forcetk.js @@ -65,7 +65,8 @@ if (forcetk.Client === undefined) { } else { // In Visualforce - still need proxyUrl for Apex REST methods this.proxyUrl = location.protocol + "//" + location.hostname - + (communityInd === true ? location.pathname.replace(/\/$/, "") : "") + "/services/proxy"; + + (communityInd === true ? location.pathname.replace(/apex\/\w+/, "").replace(/\/$/, "") : "") + + "/services/proxy"; } this.authzHeader = "Authorization"; } else { From 9eab77bd57299eb9f258e3f8923489a9aa17b6f0 Mon Sep 17 00:00:00 2001 From: Matt Goldspink Date: Thu, 14 Jun 2018 08:59:46 +0100 Subject: [PATCH 11/11] Create package.json --- package.json | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 package.json diff --git a/package.json b/package.json new file mode 100644 index 0000000..1d25847 --- /dev/null +++ b/package.json @@ -0,0 +1,23 @@ +{ + "name": "ForceTK", + "version": "1.0.0", + "description": "ForceTK - a minimal Force.com REST API for JavaScript apps", + "main": "forcetk.js", + "directories": { + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/mattgoldspink/Force.com-JavaScript-REST-Toolkit.git" + }, + "keywords": [ + ], + "author": "OSREC ", + "license": "BSD3", + "bugs": { + "url": "https://github.com/mattgoldspink/Force.com-JavaScript-REST-Toolkit/issues" + }, + "homepage": "https://github.com/mattgoldspink/Force.com-JavaScript-REST-Toolkit#readme" +}