Skip to content

Commit f46535d

Browse files
committed
merge with branch method-override. bump v0.3.0
2 parents 7e5db48 + c6409e9 commit f46535d

File tree

2 files changed

+47
-11
lines changed

2 files changed

+47
-11
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "hook-javascript",
3-
"title": "JavaScript Client for hook",
4-
"description": "JavaScript client to hook",
5-
"version": "0.2.2",
3+
"title": "hook javascript client",
4+
"description": "hook javascript client",
5+
"version": "0.3.0",
66
"homepage": "http://github.com/doubleleft/hook-javascript",
77
"main": "dist/hook.js",
88
"authors": [{

src/core/client.js

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,22 @@
1717
* @param {Object} options
1818
* @param {String} options.app_id
1919
* @param {String} options.key
20-
* @param {String} options.url default: http://hook.dev
20+
* @param {String} options.endpoint default: http://hook.dev
2121
*
2222
* @constructor
2323
*/
2424

2525
Hook.Client = function(options) {
2626
if (!options) { options = {}; }
27-
this.url = options.endpoint || options.url || window.location.origin;
27+
this.endpoint = options.endpoint || options.url || window.location.origin;
2828
this.app_id = options.app_id || options.appId || "";
2929
this.key = options.key || "";
3030

31+
this.options = (typeof(options.options) !== "undefined") ? options.options : {};
32+
3133
// 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 += "/";
3436
}
3537

3638
/**
@@ -100,6 +102,29 @@ Hook.Client.prototype.channel = function(name, options) {
100102
return new Hook.Channel[options.transport](this, collection, options);
101103
};
102104

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+
103128
/**
104129
* Create resource
105130
* @method post
@@ -167,14 +192,18 @@ Hook.Client.prototype.request = function(segments, method, data) {
167192
request_headers["Content-Type"] = 'application/json'; // exchange data via JSON to keep basic data types
168193
}
169194

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+
170201
if (typeof(XDomainRequest) !== "undefined") {
171202
// 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);
175204
}
176205

177-
var xhr = deferred.promise.xhr = uxhr(this.url + segments, payload, {
206+
var xhr = deferred.promise.xhr = uxhr(this.endpoint + segments, payload, {
178207
method: method,
179208
headers: request_headers,
180209
sync: synchronous,
@@ -321,6 +350,13 @@ Hook.Client.prototype.getPayload = function(method, data) {
321350
return payload;
322351
}
323352

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+
324360
Hook.Client.prototype.serialize = function(obj, prefix) {
325361
var str = [];
326362
for (var p in obj) {

0 commit comments

Comments
 (0)