|
3 | 3 | * https://github.com/doubleleft/hook-javascript |
4 | 4 | * |
5 | 5 | * @copyright 2015 Doubleleft |
6 | | - * @build 2/11/2015 |
| 6 | + * @build 2/19/2015 |
7 | 7 | */ |
8 | 8 | (function(window) { |
9 | 9 | // |
@@ -9720,20 +9720,22 @@ window.Hook = Hook; |
9720 | 9720 | * @param {Object} options |
9721 | 9721 | * @param {String} options.app_id |
9722 | 9722 | * @param {String} options.key |
9723 | | - * @param {String} options.url default: http://hook.dev |
| 9723 | + * @param {String} options.endpoint default: http://hook.dev |
9724 | 9724 | * |
9725 | 9725 | * @constructor |
9726 | 9726 | */ |
9727 | 9727 |
|
9728 | 9728 | Hook.Client = function(options) { |
9729 | 9729 | if (!options) { options = {}; } |
9730 | | - this.url = options.endpoint || options.url || window.location.origin; |
| 9730 | + this.endpoint = options.endpoint || options.url || window.location.origin; |
9731 | 9731 | this.app_id = options.app_id || options.appId || ""; |
9732 | 9732 | this.key = options.key || ""; |
9733 | 9733 |
|
| 9734 | + this.options = (typeof(options.options) !== "undefined") ? options.options : {}; |
| 9735 | + |
9734 | 9736 | // append last slash if doesn't have it |
9735 | | - if (this.url.lastIndexOf('/') != this.url.length - 1) { |
9736 | | - this.url += "/"; |
| 9737 | + if (this.endpoint.lastIndexOf('/') != this.endpoint.length - 1) { |
| 9738 | + this.endpoint += "/"; |
9737 | 9739 | } |
9738 | 9740 |
|
9739 | 9741 | /** |
@@ -9803,6 +9805,29 @@ Hook.Client.prototype.channel = function(name, options) { |
9803 | 9805 | return new Hook.Channel[options.transport](this, collection, options); |
9804 | 9806 | }; |
9805 | 9807 |
|
| 9808 | +/** |
| 9809 | + * Get remote URL string. |
| 9810 | + * @method url |
| 9811 | + * @param {String} route |
| 9812 | + * @return {String} |
| 9813 | + * |
| 9814 | + * @example Downloading data from a hook route |
| 9815 | + * |
| 9816 | + * location.href = client.url('download', { something: "hey" }) |
| 9817 | + * |
| 9818 | + * @example Using custom hook route for image catpcha |
| 9819 | + * |
| 9820 | + * // Implementing custom route for captcha: https://github.com/doubleleft/hook/wiki/Composer-dependencies |
| 9821 | + * var img = new Image(); |
| 9822 | + * img.src = client.url('captcha'); |
| 9823 | + * |
| 9824 | + */ |
| 9825 | +Hook.Client.prototype.url = function(route, params) { |
| 9826 | + var serializedParams = ""; |
| 9827 | + if (params) { serializedParams = "&" + this.serialize(params); } |
| 9828 | + return this.endpoint + route + this._getCredentialsParams() + serializedParams; |
| 9829 | +}; |
| 9830 | + |
9806 | 9831 | /** |
9807 | 9832 | * Create resource |
9808 | 9833 | * @method post |
@@ -9870,14 +9895,18 @@ Hook.Client.prototype.request = function(segments, method, data) { |
9870 | 9895 | request_headers["Content-Type"] = 'application/json'; // exchange data via JSON to keep basic data types |
9871 | 9896 | } |
9872 | 9897 |
|
| 9898 | + // Use method override? (some web servers doesn't respond to DELETE/PUT requests) |
| 9899 | + if (method !== "GET" && method !== "POST" && this.options.method_override) { |
| 9900 | + request_headers['X-HTTP-Method-Override'] = method; |
| 9901 | + method = "POST"; |
| 9902 | + } |
| 9903 | + |
9873 | 9904 | if (typeof(XDomainRequest) !== "undefined") { |
9874 | 9905 | // XMLHttpRequest#setRequestHeader isn't implemented on Internet Explorer's XDomainRequest |
9875 | | - segments += "?X-App-Id=" + this.app_id + "&X-App-Key=" + this.key + "&r=" + Math.floor(Math.random()*1000); |
9876 | | - var auth_token = this.auth.getToken(); |
9877 | | - if (auth_token) { segments += '&X-Auth-Token=' + auth_token; } |
| 9906 | + segments += this._getCredentialsParams() + "&r=" + Math.floor(Math.random()*1000); |
9878 | 9907 | } |
9879 | 9908 |
|
9880 | | - var xhr = deferred.promise.xhr = uxhr(this.url + segments, payload, { |
| 9909 | + var xhr = deferred.promise.xhr = uxhr(this.endpoint + segments, payload, { |
9881 | 9910 | method: method, |
9882 | 9911 | headers: request_headers, |
9883 | 9912 | sync: synchronous, |
@@ -10024,6 +10053,13 @@ Hook.Client.prototype.getPayload = function(method, data) { |
10024 | 10053 | return payload; |
10025 | 10054 | } |
10026 | 10055 |
|
| 10056 | +Hook.Client.prototype._getCredentialsParams = function() { |
| 10057 | + var params = "?X-App-Id=" + this.app_id + "&X-App-Key=" + this.key; |
| 10058 | + var auth_token = this.auth.getToken(); |
| 10059 | + if (auth_token) { params += '&X-Auth-Token=' + auth_token; } |
| 10060 | + return params; |
| 10061 | +} |
| 10062 | + |
10027 | 10063 | Hook.Client.prototype.serialize = function(obj, prefix) { |
10028 | 10064 | var str = []; |
10029 | 10065 | for (var p in obj) { |
|
0 commit comments