31
31
* https://na1.salesforce.com/ or similar) as a remote site - in the admin
32
32
* console, go to Your Name | Setup | Security Controls | Remote Site Settings
33
33
*/
34
-
34
+
35
35
var forcetk = window . forcetk ;
36
36
37
37
if ( forcetk === undefined ) {
38
38
forcetk = { } ;
39
39
}
40
-
40
+
41
41
if ( 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
+ }
47
47
48
48
/**
49
49
* The Client provides a convenient wrapper for the Force.com REST API,
50
50
* allowing JavaScript in Visualforce pages to use the API via the Ajax
51
51
* 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.
52
116
* @param sessionId a salesforce.com session ID. In a Visualforce page,
53
117
* use '{!$Api.sessionId}' to obtain a session ID.
54
118
* @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.
56
121
*/
57
- forcetk . Client = function ( sessionId , apiVersion , instanceUrl , proxyUrl ) {
122
+ forcetk . Client . prototype . setSessionToken = function ( sessionId , apiVersion , instanceUrl ) {
58
123
this . sessionId = sessionId ;
59
124
this . apiVersion = ( typeof apiVersion === 'undefined' || apiVersion == null )
60
- ? 'v21.0' : apiVersion ;
125
+ ? 'v21.0' : apiVersion ;
61
126
if ( typeof instanceUrl === 'undefined' || instanceUrl == null ) {
62
127
// location.hostname can be of the form 'abc.na1.visual.force.com' or
63
128
// 'na1.salesforce.com'. Split on '.', and take the [1] or [0] element
64
129
// as appropriate
65
130
var elements = location . hostname . split ( "." ) ;
66
131
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" ;
82
133
} else {
83
- // On a server outside VF
84
- this . proxy_url = proxyUrl ;
85
- this . authzHeader = "X-Authorization" ;
134
+ this . instanceUrl = instanceUrl ;
86
135
}
87
136
}
88
137
@@ -94,23 +143,28 @@ if (forcetk.Client === undefined) {
94
143
* @param [method="GET"] HTTP method for call
95
144
* @param [payload=null] payload for POST/PATCH etc
96
145
*/
97
- forcetk . Client . prototype . ajax = function ( path , callback , error , method , payload ) {
146
+ forcetk . Client . prototype . ajax = function ( path , callback , error , method , payload , retry ) {
98
147
var that = this ;
99
- var url = this . instance_url + '/services/data' + path ;
148
+ var url = this . instanceUrl + '/services/data' + path ;
100
149
101
150
$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 ,
105
153
contentType : 'application/json' ,
106
154
processData : false ,
107
- data : ( typeof payload === 'undefined' || payload == null )
108
- ? null : payload ,
155
+ data : payload ,
109
156
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
+ } ,
111
165
dataType : "json" ,
112
166
beforeSend : function ( xhr ) {
113
- if ( that . proxy_url !== null ) {
167
+ if ( that . proxyUrl !== null ) {
114
168
xhr . setRequestHeader ( 'SalesforceProxy-Endpoint' , url ) ;
115
169
}
116
170
xhr . setRequestHeader ( that . authzHeader , "OAuth " + that . sessionId ) ;
@@ -168,7 +222,7 @@ if (forcetk.Client === undefined) {
168
222
* @param [error=null] function to which jqXHR will be passed in case of error
169
223
*/
170
224
forcetk . Client . prototype . describe = function ( objtype , callback , error ) {
171
- this . ajax ( '/' + this . apiVersion + '/sobjects/' + objtype
225
+ this . ajax ( '/' + this . apiVersion + '/sobjects/' + objtype
172
226
+ '/describe/' , callback , error ) ;
173
227
}
174
228
@@ -196,7 +250,7 @@ if (forcetk.Client === undefined) {
196
250
* @param [error=null] function to which jqXHR will be passed in case of error
197
251
*/
198
252
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
200
254
+ '?fields=' + fieldlist , callback , error ) ;
201
255
}
202
256
0 commit comments