3131 * https://na1.salesforce.com/ or similar) as a remote site - in the admin
3232 * console, go to Your Name | Setup | Security Controls | Remote Site Settings
3333 */
34-
34+
3535var forcetk = window . forcetk ;
3636
3737if ( forcetk === undefined ) {
3838 forcetk = { } ;
3939}
40-
40+
4141if ( forcetk . Client === undefined ) {
42-
43- // We use $j rather than $ for jQuery so it works in Visualforce
44- if ( window . $j === undefined ) {
45- $j = $ ;
46- }
42+
43+ // We use $j rather than $ for jQuery so it works in Visualforce
44+ if ( window . $j === undefined ) {
45+ $j = $ ;
46+ }
4747
4848 /**
4949 * The Client provides a convenient wrapper for the Force.com REST API,
5050 * allowing JavaScript in Visualforce pages to use the API via the Ajax
5151 * Proxy.
52+ * @param [clientId=null] 'Consumer Key' in the Remote Access app settings
53+ * @param [loginUrl='https://login.salesforce.com/'] Login endpoint
54+ * @param [proxyUrl=null] Proxy URL. Omit if running on Visualforce or
55+ * PhoneGap etc
56+ * @constructor
57+ */
58+ forcetk . Client = function ( clientId , loginUrl , proxyUrl ) {
59+ this . clientId = clientId ;
60+ this . loginUrl = loginUrl || 'https://login.salesforce.com/' ;
61+ if ( typeof proxyUrl === 'undefined' || proxyUrl === null ) {
62+ if ( location . protocol === 'file:' ) {
63+ // In PhoneGap
64+ this . proxyUrl = null ;
65+ } else {
66+ // In Visualforce
67+ this . proxyUrl = location . protocol + "//" + location . hostname
68+ + "/services/proxy" ;
69+ }
70+ this . authzHeader = "Authorization" ;
71+ } else {
72+ // On a server outside VF
73+ this . proxyUrl = proxyUrl ;
74+ this . authzHeader = "X-Authorization" ;
75+ }
76+ this . refreshToken = null ;
77+ this . sessionId = null ;
78+ this . apiVersion = null ;
79+ this . instanceUrl = null ;
80+ }
81+
82+ /**
83+ * Set a refresh token in the client.
84+ * @param refreshToken an OAuth refresh token
85+ */
86+ forcetk . Client . prototype . setRefreshToken = function ( refreshToken ) {
87+ this . refreshToken = refreshToken ;
88+ }
89+
90+ /**
91+ * Refresh the access token.
92+ * @param callback function to call on success
93+ * @param error function to call on failure
94+ */
95+ forcetk . Client . prototype . refreshAccessToken = function ( callback , error ) {
96+ var that = this ;
97+ var url = this . loginUrl + '/services/oauth2/token' ;
98+ $j . ajax ( {
99+ type : 'POST' ,
100+ url : ( this . proxyUrl !== null ) ? this . proxyUrl : url ,
101+ processData : false ,
102+ data : 'grant_type=refresh_token&client_id=' + this . clientId + '&refresh_token=' + this . refreshToken ,
103+ success : callback ,
104+ error : error ,
105+ dataType : "json" ,
106+ beforeSend : function ( xhr ) {
107+ if ( that . proxyUrl !== null ) {
108+ xhr . setRequestHeader ( 'SalesforceProxy-Endpoint' , url ) ;
109+ }
110+ }
111+ } ) ;
112+ }
113+
114+ /**
115+ * Set a session token and the associated metadata in the client.
52116 * @param sessionId a salesforce.com session ID. In a Visualforce page,
53117 * use '{!$Api.sessionId}' to obtain a session ID.
54118 * @param [apiVersion="21.0"] Force.com API version
55- * @constructor
119+ * @param [instanceUrl] Omit this if running on Visualforce; otherwise
120+ * use the value from the OAuth token.
56121 */
57- forcetk . Client = function ( sessionId , apiVersion , instanceUrl , proxyUrl ) {
122+ forcetk . Client . prototype . setSessionToken = function ( sessionId , apiVersion , instanceUrl ) {
58123 this . sessionId = sessionId ;
59124 this . apiVersion = ( typeof apiVersion === 'undefined' || apiVersion == null )
60- ? 'v21.0' : apiVersion ;
125+ ? 'v21.0' : apiVersion ;
61126 if ( typeof instanceUrl === 'undefined' || instanceUrl == null ) {
62127 // location.hostname can be of the form 'abc.na1.visual.force.com' or
63128 // 'na1.salesforce.com'. Split on '.', and take the [1] or [0] element
64129 // as appropriate
65130 var elements = location . hostname . split ( "." ) ;
66131 var instance = ( elements . length == 3 ) ? elements [ 0 ] : elements [ 1 ] ;
67- this . instance_url = "https://" + instance + ".salesforce.com" ;
68- } else {
69- this . instance_url = instanceUrl ;
70- }
71- if ( typeof proxyUrl === 'undefined' || proxyUrl === null ) {
72- if ( location . protocol === 'file:' ) {
73- // In PhoneGap
74- this . proxy_url = null ;
75- this . authzHeader = null ;
76- } else {
77- // In Visualforce
78- this . proxy_url = location . protocol + "//" + location . hostname
79- + "/services/proxy" ;
80- this . authzHeader = "Authorization" ;
81- }
132+ this . instanceUrl = "https://" + instance + ".salesforce.com" ;
82133 } else {
83- // On a server outside VF
84- this . proxy_url = proxyUrl ;
85- this . authzHeader = "X-Authorization" ;
134+ this . instanceUrl = instanceUrl ;
86135 }
87136 }
88137
@@ -94,23 +143,28 @@ if (forcetk.Client === undefined) {
94143 * @param [method="GET"] HTTP method for call
95144 * @param [payload=null] payload for POST/PATCH etc
96145 */
97- forcetk . Client . prototype . ajax = function ( path , callback , error , method , payload ) {
146+ forcetk . Client . prototype . ajax = function ( path , callback , error , method , payload , retry ) {
98147 var that = this ;
99- var url = this . instance_url + '/services/data' + path ;
148+ var url = this . instanceUrl + '/services/data' + path ;
100149
101150 $j . ajax ( {
102- type : ( typeof method === 'undefined' || method == null )
103- ? "GET" : method ,
104- url : ( this . proxy_url !== null ) ? this . proxy_url : url ,
151+ type : method || "GET" ,
152+ url : ( this . proxyUrl !== null ) ? this . proxyUrl : url ,
105153 contentType : 'application/json' ,
106154 processData : false ,
107- data : ( typeof payload === 'undefined' || payload == null )
108- ? null : payload ,
155+ data : payload ,
109156 success : callback ,
110- error : error ,
157+ error : ( ! this . refreshToken || retry ) ? error : function ( ) {
158+ that . refreshAccessToken ( function ( oauthResponse ) {
159+ that . setSessionToken ( oauthResponse . access_token , null ,
160+ oauthResponse . instance_url ) ;
161+ that . ajax ( path , callback , error , method , payload , true ) ;
162+ } ,
163+ error ) ;
164+ } ,
111165 dataType : "json" ,
112166 beforeSend : function ( xhr ) {
113- if ( that . proxy_url !== null ) {
167+ if ( that . proxyUrl !== null ) {
114168 xhr . setRequestHeader ( 'SalesforceProxy-Endpoint' , url ) ;
115169 }
116170 xhr . setRequestHeader ( that . authzHeader , "OAuth " + that . sessionId ) ;
@@ -168,7 +222,7 @@ if (forcetk.Client === undefined) {
168222 * @param [error=null] function to which jqXHR will be passed in case of error
169223 */
170224 forcetk . Client . prototype . describe = function ( objtype , callback , error ) {
171- this . ajax ( '/' + this . apiVersion + '/sobjects/' + objtype
225+ this . ajax ( '/' + this . apiVersion + '/sobjects/' + objtype
172226 + '/describe/' , callback , error ) ;
173227 }
174228
@@ -196,7 +250,7 @@ if (forcetk.Client === undefined) {
196250 * @param [error=null] function to which jqXHR will be passed in case of error
197251 */
198252 forcetk . Client . prototype . retrieve = function ( objtype , id , fieldlist , callback , error ) {
199- this . ajax ( '/' + this . apiVersion + '/sobjects/' + objtype + '/' + id
253+ this . ajax ( '/' + this . apiVersion + '/sobjects/' + objtype + '/' + id
200254 + '?fields=' + fieldlist , callback , error ) ;
201255 }
202256
0 commit comments