From 21e7ab9ed0b3ced69316c7ef60e6dac769b2fb9a Mon Sep 17 00:00:00 2001 From: Menno Vanderlist Date: Fri, 10 Jul 2015 09:27:11 -0600 Subject: [PATCH 1/9] Replaced escape(soql) and escape(sosl) Fix to support umlaut and other European charaters Replaced with encodeURIComponent(soql) and encodeURIComponent(sosl) --- forcetk.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/forcetk.js b/forcetk.js index eef5ac7..eb1d7f2 100644 --- a/forcetk.js +++ b/forcetk.js @@ -562,7 +562,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) { - return this.ajax('/' + this.apiVersion + '/query?q=' + escape(soql) + return this.ajax('/' + this.apiVersion + '/query?q=' + encodeURIComponent(soql) , callback, error); } @@ -598,7 +598,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) { - return this.ajax('/' + this.apiVersion + '/search?q=' + escape(sosl) + return this.ajax('/' + this.apiVersion + '/search?q=' + encodeURIComponent(sosl) , callback, error); } } From f13596c8aedef867d5aef4978ccf05f275ef948d Mon Sep 17 00:00:00 2001 From: Sean Cuevo Date: Fri, 2 Oct 2015 14:55:29 -0600 Subject: [PATCH 2/9] removing extra new line that would corrupt ms office file attachments --- forcetk.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/forcetk.js b/forcetk.js index eb1d7f2..cc640d7 100644 --- a/forcetk.js +++ b/forcetk.js @@ -286,7 +286,7 @@ if (forcetk.Client === undefined) { + "Content-Disposition: form-data; name=\"" + payloadField + "\"; filename=\"" + filename + "\"\n\n", payload, - "\n\n" + "\n" + "--boundary_" + boundary + "--" ], {type : 'multipart/form-data; boundary=\"boundary_' + boundary + '\"'}); From f05d17c402be989436e42bce16aa5dbaf075f72b Mon Sep 17 00:00:00 2001 From: Sean Cuevo Date: Fri, 2 Oct 2015 14:57:02 -0600 Subject: [PATCH 3/9] adding content type for attachments for IE 11 support --- forcetk.js | 1 + 1 file changed, 1 insertion(+) diff --git a/forcetk.js b/forcetk.js index eb1d7f2..e1865b2 100644 --- a/forcetk.js +++ b/forcetk.js @@ -296,6 +296,7 @@ if (forcetk.Client === undefined) { 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) { request.setRequestHeader('SalesforceProxy-Endpoint', url); } From fb347412c12b482a66e6bccf68a9bf8caad9729c Mon Sep 17 00:00:00 2001 From: Pat Patterson Date: Tue, 6 Oct 2015 11:02:30 -0700 Subject: [PATCH 4/9] Fixed typo in filename parameter. Fixes #74 --- forcetk.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/forcetk.js b/forcetk.js index eef5ac7..ebd6059 100644 --- a/forcetk.js +++ b/forcetk.js @@ -311,7 +311,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, file, callback, error, true); + that.blob(path, fields, filename, file, callback, error, true); }, error); } else { From e12fc025215b5f146f7e5677a2f058c63eeecd61 Mon Sep 17 00:00:00 2001 From: Pat Patterson Date: Wed, 14 Oct 2015 11:25:32 -0700 Subject: [PATCH 5/9] Fixed all JSLint errors/warnings. Fixes #75 --- forcetk.js | 391 ++++++++++++++++++++++++++++------------------------- 1 file changed, 205 insertions(+), 186 deletions(-) diff --git a/forcetk.js b/forcetk.js index 0013562..d568e42 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) { @@ -50,10 +53,11 @@ if (forcetk.Client === undefined) { * PhoneGap etc * @constructor */ - forcetk.Client = function(clientId, loginUrl, proxyUrl) { + forcetk.Client = function (clientId, loginUrl, proxyUrl) { + '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,40 +78,42 @@ 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) { - var that = this; - var url = this.loginUrl + '/services/oauth2/token'; + forcetk.Client.prototype.refreshAccessToken = function (callback, error) { + 'use strict'; + 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); } } }); - } + }; /** * Set a session token and the associated metadata in the client. @@ -117,32 +123,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) { + 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; } - } + }; /* * Low level utility function to call the Salesforce endpoint. @@ -152,41 +158,42 @@ 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) { - var that = this; - var url = (this.visualforce ? '' : this.instanceUrl) + '/services/data' + path; + forcetk.Client.prototype.ajax = function (path, callback, error, method, payload, retry) { + 'use strict'; + 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.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 @@ -200,64 +207,61 @@ 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) { - var that = this; - var url = (this.visualforce ? '' : this.instanceUrl) + path; + forcetk.Client.prototype.getChatterFile = function (path, mimeType, callback, error, retry) { + 'use strict'; + var that = this, + url = (this.visualforce ? '' : this.instanceUrl) + path, + request = new XMLHttpRequest(); - var 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) { - that.refreshAccessToken(function(oauthResponse) { - that.setSessionToken(oauthResponse.access_token, null,oauthResponse.instance_url); + } else if (request.status === 401 && !retry) { + //refresh token in 401 + 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 - function randomString() { - var str = ''; - for (var i = 0; i < 4; i++) { - str += (Math.random().toString(16)+"000000000").substr(2,8); + var randomString = function () { + 'use strict'; + var str = '', + i; + for (i = 0; i < 4; i += 1) { + 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 @@ -270,63 +274,62 @@ 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) { - 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); + 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); 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) { + 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, file, 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, payloadField, payload, 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" @@ -340,12 +343,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, - payloadField, payload, callback, - error, retry) { - return this.blob('/' + this.apiVersion + '/sobjects/' + objtype + '/', - fields, filename, payloadField, payload, callback, error); - } + 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); + }; /* * Update a record with blob data @@ -361,12 +365,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, - payloadField, payload, callback, - error, retry) { - return this.blob('/' + this.apiVersion + '/sobjects/' + objtype + '/' + id + - '?_HttpMethod=PATCH', fields, filename, payloadField, payload, callback, error); - } + 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); + }; /* * Low level utility function to call the Salesforce endpoint specific for Apex REST API. @@ -378,33 +383,34 @@ 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) { - var that = this; - var url = this.instanceUrl + '/services/apexrest' + path; + forcetk.Client.prototype.apexrest = function (path, callback, error, method, payload, paramMap, retry) { + 'use strict'; + 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); } @@ -413,13 +419,15 @@ 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); } }); - } + }; /* * Lists summary information about each Salesforce.com version currently @@ -428,9 +436,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 @@ -438,9 +447,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 @@ -448,9 +458,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. @@ -458,10 +469,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) { - return this.ajax('/' + this.apiVersion + '/sobjects/' + 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 @@ -470,10 +482,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); - } + + '/describe/', callback, error); + }; /* * Creates a new record of the given type. @@ -484,10 +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.create = function(objtype, fields, callback, error) { - return this.ajax('/' + this.apiVersion + '/sobjects/' + objtype + '/' - , callback, error, "POST", JSON.stringify(fields)); - } + 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. @@ -498,16 +512,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.retrieve = function(objtype, id, fieldlist, callback, error) { - if (arguments.length == 4) { + forcetk.Client.prototype.retrieve = function (objtype, id, fieldlist, callback, error) { + 'use strict'; + 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 @@ -521,10 +536,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) { - 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) { + '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. @@ -536,10 +552,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) { - 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) { + '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 @@ -549,10 +566,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) { - return this.ajax('/' + this.apiVersion + '/sobjects/' + objtype + '/' + id - , callback, error, "DELETE"); - } + 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. @@ -561,11 +579,12 @@ 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) { - return this.ajax('/' + this.apiVersion + '/query?q=' + encodeURIComponent(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. *

This should be used if performing a query that retrieves more than can be returned @@ -576,19 +595,18 @@ 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 ); - - if( index > -1 ){ - url = url.substr( index + serviceData.length ); - } else { - //-- leave alone + var serviceData = "services/data", + index = url.indexOf(serviceData); + + if (index > -1) { + url = url.substr(index + serviceData.length); } - - return this.ajax( url, callback, error ); - } + + return this.ajax(url, callback, error); + }; /* * Executes the specified SOSL search. @@ -597,8 +615,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) { - return this.ajax('/' + this.apiVersion + '/search?q=' + encodeURIComponent(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 0c736367d2c7569e421df306d7690249214bfdec Mon Sep 17 00:00:00 2001 From: Pat Patterson Date: Mon, 26 Oct 2015 15:29:41 -0700 Subject: [PATCH 6/9] Handle apexrest payloads correctly. Fixes #49 --- forcetk.js | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/forcetk.js b/forcetk.js index 8964464..7fd2918 100644 --- a/forcetk.js +++ b/forcetk.js @@ -380,7 +380,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 * @param [method="GET"] HTTP method for call - * @param [payload=null] payload for POST/PATCH etc + * @param [payload=null] string or object with payload for POST/PATCH etc or params for GET * @param [paramMap={}] parameters to send as header values for POST/PATCH etc * @param [retry] specifies whether to retry on error */ @@ -389,10 +389,28 @@ if (forcetk.Client === undefined) { var that = this, url = this.instanceUrl + '/services/apexrest' + path; + method = method || "GET"; + + if (method === "GET") { + // Handle proxied query params correctly + if (this.proxyUrl && payload) { + if (typeof payload !== 'string') { + payload = $.param(payload); + } + url += "?" + payload; + payload = null; + } + } else { + // Allow object payload for POST etc + if (payload && typeof payload !== 'string') { + payload = JSON.stringify(payload); + } + } + return $.ajax({ - type: method || "GET", + type: method, async: this.asyncAjax, - url: (this.proxyUrl !== null) ? this.proxyUrl : url, + url: this.proxyUrl || url, contentType: 'application/json', cache: false, processData: false, From cf2e4e4e33d50a295205d76112b9bbb61b255363 Mon Sep 17 00:00:00 2001 From: svc-scm <48930134+svc-scm@users.noreply.github.com> Date: Tue, 12 Oct 2021 04:05:28 -0700 Subject: [PATCH 7/9] Updated/Added CODEOWNERS with ECCN --- CODEOWNERS | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 CODEOWNERS diff --git a/CODEOWNERS b/CODEOWNERS new file mode 100644 index 0000000..522fa4a --- /dev/null +++ b/CODEOWNERS @@ -0,0 +1,2 @@ +# Comment line immediately above ownership line is reserved for related gus information. Please be careful while editing. +#ECCN:Open Source From 52ab8a54c620a4f5b46257e6ce1a3f6cb6b93efa Mon Sep 17 00:00:00 2001 From: Peter Chittum Date: Fri, 14 Oct 2022 21:06:17 +0100 Subject: [PATCH 8/9] adding oss info files --- CODE_OF_CONDUCT.md | 105 +++++++++++++++++++++++++++++++++++++++++++++ LICENSE.txt | 14 ++++++ SECURITY.md | 7 +++ 3 files changed, 126 insertions(+) create mode 100644 CODE_OF_CONDUCT.md create mode 100644 LICENSE.txt create mode 100644 SECURITY.md diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..b4612a7 --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,105 @@ +# Salesforce Open Source Community Code of Conduct + +## About the Code of Conduct + +Equality is a core value at Salesforce. We believe a diverse and inclusive +community fosters innovation and creativity, and are committed to building a +culture where everyone feels included. + +Salesforce open-source projects are committed to providing a friendly, safe, and +welcoming environment for all, regardless of gender identity and expression, +sexual orientation, disability, physical appearance, body size, ethnicity, nationality, +race, age, religion, level of experience, education, socioeconomic status, or +other similar personal characteristics. + +The goal of this code of conduct is to specify a baseline standard of behavior so +that people with different social values and communication styles can work +together effectively, productively, and respectfully in our open source community. +It also establishes a mechanism for reporting issues and resolving conflicts. + +All questions and reports of abusive, harassing, or otherwise unacceptable behavior +in a Salesforce open-source project may be reported by contacting the Salesforce +Open Source Conduct Committee at ossconduct@salesforce.com. + +## Our Pledge + +In the interest of fostering an open and welcoming environment, we as +contributors and maintainers pledge to making participation in our project and +our community a harassment-free experience for everyone, regardless of gender +identity and expression, sexual orientation, disability, physical appearance, +body size, ethnicity, nationality, race, age, religion, level of experience, education, +socioeconomic status, or other similar personal characteristics. + +## Our Standards + +Examples of behavior that contributes to creating a positive environment +include: + +* Using welcoming and inclusive language +* Being respectful of differing viewpoints and experiences +* Gracefully accepting constructive criticism +* Focusing on what is best for the community +* Showing empathy toward other community members + +Examples of unacceptable behavior by participants include: + +* The use of sexualized language or imagery and unwelcome sexual attention or +advances +* Personal attacks, insulting/derogatory comments, or trolling +* Public or private harassment +* Publishing, or threatening to publish, others' private information—such as +a physical or electronic address—without explicit permission +* Other conduct which could reasonably be considered inappropriate in a +professional setting +* Advocating for or encouraging any of the above behaviors + +## Our Responsibilities + +Project maintainers are responsible for clarifying the standards of acceptable +behavior and are expected to take appropriate and fair corrective action in +response to any instances of unacceptable behavior. + +Project maintainers have the right and responsibility to remove, edit, or +reject comments, commits, code, wiki edits, issues, and other contributions +that are not aligned with this Code of Conduct, or to ban temporarily or +permanently any contributor for other behaviors that they deem inappropriate, +threatening, offensive, or harmful. + +## Scope + +This Code of Conduct applies both within project spaces and in public spaces +when an individual is representing the project or its community. Examples of +representing a project or community include using an official project email +address, posting via an official social media account, or acting as an appointed +representative at an online or offline event. Representation of a project may be +further defined and clarified by project maintainers. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be +reported by contacting the Salesforce Open Source Conduct Committee +at ossconduct@salesforce.com. All complaints will be reviewed and investigated +and will result in a response that is deemed necessary and appropriate to the +circumstances. The committee is obligated to maintain confidentiality with +regard to the reporter of an incident. Further details of specific enforcement +policies may be posted separately. + +Project maintainers who do not follow or enforce the Code of Conduct in good +faith may face temporary or permanent repercussions as determined by other +members of the project's leadership and the Salesforce Open Source Conduct +Committee. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][contributor-covenant-home], +version 1.4, available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html. +It includes adaptions and additions from [Go Community Code of Conduct][golang-coc], +[CNCF Code of Conduct][cncf-coc], and [Microsoft Open Source Code of Conduct][microsoft-coc]. + +This Code of Conduct is licensed under the [Creative Commons Attribution 3.0 License][cc-by-3-us]. + +[contributor-covenant-home]: https://www.contributor-covenant.org (https://www.contributor-covenant.org/) +[golang-coc]: https://golang.org/conduct +[cncf-coc]: https://github.com/cncf/foundation/blob/master/code-of-conduct.md +[microsoft-coc]: https://opensource.microsoft.com/codeofconduct/ +[cc-by-3-us]: https://creativecommons.org/licenses/by/3.0/us/ \ No newline at end of file diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..9ba9791 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,14 @@ +BSD 3-Clause License + +Copyright (c) 2022 Salesforce, Inc. +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +3. Neither the name of Salesforce.com nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 0000000..e31774d --- /dev/null +++ b/SECURITY.md @@ -0,0 +1,7 @@ +## Security + +Please report any security issue to [security@salesforce.com](mailto:security@salesforce.com) +as soon as it is discovered. This library limits its runtime dependencies in +order to reduce the total cost of ownership as much as can be, but all consumers +should remain vigilant and have their security stakeholders review all third-party +products (3PP) like this one and their dependencies. \ No newline at end of file From 01974a1415e6546eb35a7aca9e157b0522d20724 Mon Sep 17 00:00:00 2001 From: Philippe Ozil Date: Wed, 10 Jan 2024 14:10:31 +0100 Subject: [PATCH 9/9] Update README.markdown --- README.markdown | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.markdown b/README.markdown index e3c0368..b72c733 100644 --- a/README.markdown +++ b/README.markdown @@ -1,3 +1,7 @@ +> [!WARNING] +> This project is deprecated, use [JSforce](https://jsforce.github.io/) instead. + + Force.com JavaScript REST Toolkit =================================