|
17 | 17 | * @param {Object} options |
18 | 18 | * @param {String} options.app_id |
19 | 19 | * @param {String} options.key |
20 | | - * @param {String} options.url default: http://hook.dev |
| 20 | + * @param {String} options.endpoint default: http://hook.dev |
21 | 21 | * |
22 | 22 | * @constructor |
23 | 23 | */ |
24 | 24 |
|
25 | 25 | Hook.Client = function(options) { |
26 | 26 | if (!options) { options = {}; } |
27 | | - this.url = options.endpoint || options.url || window.location.origin; |
| 27 | + this.endpoint = options.endpoint || options.url || window.location.origin; |
28 | 28 | this.app_id = options.app_id || options.appId || ""; |
29 | 29 | this.key = options.key || ""; |
30 | 30 |
|
| 31 | + this.options = (typeof(options.options) !== "undefined") ? options.options : {}; |
| 32 | + |
31 | 33 | // append last slash if doesn't have it |
32 | | - if (this.url.lastIndexOf('/') != this.url.length - 1) { |
33 | | - this.url += "/"; |
| 34 | + if (this.endpoint.lastIndexOf('/') != this.endpoint.length - 1) { |
| 35 | + this.endpoint += "/"; |
34 | 36 | } |
35 | 37 |
|
36 | 38 | /** |
@@ -100,6 +102,29 @@ Hook.Client.prototype.channel = function(name, options) { |
100 | 102 | return new Hook.Channel[options.transport](this, collection, options); |
101 | 103 | }; |
102 | 104 |
|
| 105 | +/** |
| 106 | + * Get remote URL string. |
| 107 | + * @method url |
| 108 | + * @param {String} route |
| 109 | + * @return {String} |
| 110 | + * |
| 111 | + * @example Downloading data from a hook route |
| 112 | + * |
| 113 | + * location.href = client.url('download', { something: "hey" }) |
| 114 | + * |
| 115 | + * @example Using custom hook route for image catpcha |
| 116 | + * |
| 117 | + * // Implementing custom route for captcha: https://github.com/doubleleft/hook/wiki/Composer-dependencies |
| 118 | + * var img = new Image(); |
| 119 | + * img.src = client.url('captcha'); |
| 120 | + * |
| 121 | + */ |
| 122 | +Hook.Client.prototype.url = function(route, params) { |
| 123 | + var serializedParams = ""; |
| 124 | + if (params) { serializedParams = "&" + this.serialize(params); } |
| 125 | + return this.endpoint + route + this._getCredentialsParams() + serializedParams; |
| 126 | +}; |
| 127 | + |
103 | 128 | /** |
104 | 129 | * Create resource |
105 | 130 | * @method post |
@@ -167,14 +192,18 @@ Hook.Client.prototype.request = function(segments, method, data) { |
167 | 192 | request_headers["Content-Type"] = 'application/json'; // exchange data via JSON to keep basic data types |
168 | 193 | } |
169 | 194 |
|
| 195 | + // Use method override? (some web servers doesn't respond to DELETE/PUT requests) |
| 196 | + if (method !== "GET" && method !== "POST" && this.options.method_override) { |
| 197 | + request_headers['X-HTTP-Method-Override'] = method; |
| 198 | + method = "POST"; |
| 199 | + } |
| 200 | + |
170 | 201 | if (typeof(XDomainRequest) !== "undefined") { |
171 | 202 | // XMLHttpRequest#setRequestHeader isn't implemented on Internet Explorer's XDomainRequest |
172 | | - segments += "?X-App-Id=" + this.app_id + "&X-App-Key=" + this.key + "&r=" + Math.floor(Math.random()*1000); |
173 | | - var auth_token = this.auth.getToken(); |
174 | | - if (auth_token) { segments += '&X-Auth-Token=' + auth_token; } |
| 203 | + segments += this._getCredentialsParams() + "&r=" + Math.floor(Math.random()*1000); |
175 | 204 | } |
176 | 205 |
|
177 | | - var xhr = deferred.promise.xhr = uxhr(this.url + segments, payload, { |
| 206 | + var xhr = deferred.promise.xhr = uxhr(this.endpoint + segments, payload, { |
178 | 207 | method: method, |
179 | 208 | headers: request_headers, |
180 | 209 | sync: synchronous, |
@@ -321,6 +350,13 @@ Hook.Client.prototype.getPayload = function(method, data) { |
321 | 350 | return payload; |
322 | 351 | } |
323 | 352 |
|
| 353 | +Hook.Client.prototype._getCredentialsParams = function() { |
| 354 | + var params = "?X-App-Id=" + this.app_id + "&X-App-Key=" + this.key; |
| 355 | + var auth_token = this.auth.getToken(); |
| 356 | + if (auth_token) { params += '&X-Auth-Token=' + auth_token; } |
| 357 | + return params; |
| 358 | +} |
| 359 | + |
324 | 360 | Hook.Client.prototype.serialize = function(obj, prefix) { |
325 | 361 | var str = []; |
326 | 362 | for (var p in obj) { |
|
0 commit comments