Skip to content

Commit

Permalink
feat: properties for adding a query string to the urls
Browse files Browse the repository at this point in the history
  • Loading branch information
grappendorf committed Sep 23, 2015
1 parent be641cc commit d2f242b
Show file tree
Hide file tree
Showing 5 changed files with 352 additions and 116 deletions.
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,54 @@ Properties

Use this URL instead of the default one for "PUT /resources/:id/action" requests.

* **queryString**

- *type:* string

A query string (without the '?') that is appended to the general url.

* **indexQueryString**

- *type:* string

A query string (without the '?') that is appended to the indexUrl.

* **showQueryString**

- *type:* string

A query string (without the '?') that is appended to the showUrl.

* **newQueryString**

- *type:* string

A query string (without the '?') that is appended to the newUrl.

* **createQueryString**

- *type:* string

A query string (without the '?') that is appended to the createUrl.

* **updateQueryString**

- *type:* string

A query string (without the '?') that is appended to the updateUrl.

* **destroyQueryString**

- *type:* string

A query string (without the '?') that is appended to the destroyUrl.

* **memberQueryString**

- *type:* string

A query string (without the '?') that is appended to the memberUrl.

* **headers**

- *type:* object
Expand Down
163 changes: 102 additions & 61 deletions grapp-rest-resource.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,30 @@
memberUrl: {
type: String
},
queryString: {
type: String
},
indexQueryString: {
type: String
},
showQueryString: {
type: String
},
newQueryString: {
type: String
},
createQueryString: {
type: String
},
updateQueryString: {
type: String
},
destroyQueryString: {
type: String
},
memberQueryString: {
type: String
},
headers: {
type: Object,
value: function() {
Expand All @@ -65,76 +89,93 @@
}
},
ready: function() {
var self;
self = this;
return this.resource = {
index: function() {
return new Promise(function(resolve, reject) {
return self._sendRequest('GET', self.indexUrl).then(function(request) {
return self._handleResponse(request.response, request.xhr.status, resolve);
}, function() {
return self._handleError(self.request.xhr.response, self.request.xhr.status, reject);
index: (function(_this) {
return function() {
return new Promise(function(resolve, reject) {
return _this._sendRequest('GET', _this.indexUrl, _this.indexQueryString).then(function(request) {
return _this._handleResponse(request.response, request.xhr.status, resolve);
}, function() {
return _this._handleError(_this.request.xhr.response, _this.request.xhr.status, reject);
});
});
});
},
show: function(id) {
return new Promise(function(resolve, reject) {
return self._sendRequest('GET', self.showUrl, id).then(function(request) {
return self._handleResponse(request.response, request.xhr.status, resolve);
}, function() {
return self._handleError(self.request.xhr.response, self.request.xhr.status, reject);
};
})(this),
show: (function(_this) {
return function(id) {
return new Promise(function(resolve, reject) {
return _this._sendRequest('GET', _this.showUrl, _this.showQueryString, id).then(function(request) {
return _this._handleResponse(request.response, request.xhr.status, resolve);
}, function() {
return _this._handleError(_this.request.xhr.response, _this.request.xhr.status, reject);
});
});
});
},
"new": function() {
return new Promise(function(resolve, reject) {
return self._sendRequest('GET', self.newUrl, null, 'new').then(function(request) {
return self._handleResponse(request.response, request.xhr.status, resolve);
}, function() {
return self._handleError(self.request.xhr.response, self.request.xhr.status, reject);
};
})(this),
"new": (function(_this) {
return function() {
return new Promise(function(resolve, reject) {
return _this._sendRequest('GET', _this.newUrl, _this.newQueryString, null, 'new').then(function(request) {
return _this._handleResponse(request.response, request.xhr.status, resolve);
}, function() {
return _this._handleError(_this.request.xhr.response, _this.request.xhr.status, reject);
});
});
});
},
create: function(data) {
return new Promise(function(resolve, reject) {
return self._sendRequest('POST', self.createUrl, null, data).then(function(request) {
return self._handleResponse(request.response, request.xhr.status, resolve);
}, function() {
return self._handleError(self.request.xhr.response, self.request.xhr.status, reject);
};
})(this),
create: (function(_this) {
return function(data) {
return new Promise(function(resolve, reject) {
return _this._sendRequest('POST', _this.createUrl, _this.createQueryString, null, data).then(function(request) {
return _this._handleResponse(request.response, request.xhr.status, resolve);
}, function() {
return _this._handleError(_this.request.xhr.response, _this.request.xhr.status, reject);
});
});
});
},
update: function(id, data) {
return new Promise(function(resolve, reject) {
return self._sendRequest('PUT', self.updateUrl, id, data).then(function(request) {
return self._handleResponse(request.response, request.xhr.status, resolve);
}, function() {
return self._handleError(self.request.xhr.response, self.request.xhr.status, reject);
};
})(this),
update: (function(_this) {
return function(id, data) {
return new Promise(function(resolve, reject) {
return _this._sendRequest('PUT', _this.updateUrl, _this.updateQueryString, id, data).then(function(request) {
return _this._handleResponse(request.response, request.xhr.status, resolve);
}, function() {
return _this._handleError(_this.request.xhr.response, _this.request.xhr.status, reject);
});
});
});
},
destroy: function(id) {
return new Promise(function(resolve, reject) {
return self._sendRequest('DELETE', self.destroyUrl, id).then(function(request) {
return self._handleResponse(request.response, request.xhr.status, resolve);
}, function() {
return self._handleError(self.request.xhr.response, self.request.xhr.status, reject);
};
})(this),
destroy: (function(_this) {
return function(id) {
return new Promise(function(resolve, reject) {
return _this._sendRequest('DELETE', _this.destroyUrl, _this.destroyQueryString, id).then(function(request) {
return _this._handleResponse(request.response, request.xhr.status, resolve);
}, function() {
return _this._handleError(_this.request.xhr.response, _this.request.xhr.status, reject);
});
});
});
},
memberAction: function(id, action) {
return new Promise(function(resolve, reject) {
return self._sendRequest('PUT', self.memberUrl, id, action).then(function(request) {
return self._handleResponse(request.response, request.xhr.status, resolve);
}, function() {
return self._handleError(self.request.xhr.response, self.request.xhr.status, reject);
};
})(this),
memberAction: (function(_this) {
return function(id, action) {
console.log(action);
return new Promise(function(resolve, reject) {
return _this._sendRequest('PUT', _this.memberUrl, _this.memberQueryString, id, null, action).then(function(request) {
return _this._handleResponse(request.response, request.xhr.status, resolve);
}, function() {
return _this._handleError(_this.request.xhr.response, _this.request.xhr.status, reject);
});
});
});
}
};
})(this)
};
},
_prepareUrl: function(url, params, id, action) {
_prepareUrl: function(url, queryString, params, id, action) {
var name, value;
console.log(action);
if (queryString) {
url += '?' + queryString;
}
if (typeof params === 'string') {
params = JSON.parse(params);
}
Expand Down Expand Up @@ -167,7 +208,7 @@
}
return h;
},
_sendRequest: function(method, url, id, data, action) {
_sendRequest: function(method, url, queryString, id, data, action) {
if (id == null) {
id = null;
}
Expand All @@ -180,7 +221,7 @@
this.request = this._createRequestElement();
return this.request.send({
method: method,
url: this._prepareUrl(url || this.url, this.params, id, action),
url: this._prepareUrl(url || this.url, queryString || this.queryString, this.params, id, action),
headers: this._prepareHeaders(),
body: (data ? JSON.stringify(data) : void 0)
});
Expand Down
117 changes: 63 additions & 54 deletions src/grapp-rest-resource.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -13,64 +13,73 @@ Polymer
updateUrl: {type: String}
destroyUrl: {type: String}
memberUrl: {type: String}
queryString: {type: String}
indexQueryString: {type: String}
showQueryString: {type: String}
newQueryString: {type: String}
createQueryString: {type: String}
updateQueryString: {type: String}
destroyQueryString: {type: String}
memberQueryString: {type: String}
headers: {type: Object, value: -> {}}
token: {type: String},
request: {type: Object}

ready: ->
self = @

@resource =
index: ->
new Promise (resolve, reject) ->
self._sendRequest('GET', self.indexUrl).then (request) ->
self._handleResponse request.response, request.xhr.status, resolve
, ->
self._handleError self.request.xhr.response, self.request.xhr.status, reject

show: (id) ->
new Promise (resolve, reject) ->
self._sendRequest('GET', self.showUrl, id).then (request) ->
self._handleResponse request.response, request.xhr.status, resolve
, ->
self._handleError self.request.xhr.response, self.request.xhr.status, reject

new: () ->
new Promise (resolve, reject) ->
self._sendRequest('GET', self.newUrl, null, 'new').then (request) ->
self._handleResponse request.response, request.xhr.status, resolve
, ->
self._handleError self.request.xhr.response, self.request.xhr.status, reject

create: (data) ->
new Promise (resolve, reject) ->
self._sendRequest('POST', self.createUrl, null, data).then (request) ->
self._handleResponse request.response, request.xhr.status, resolve
, ->
self._handleError self.request.xhr.response, self.request.xhr.status, reject

update: (id, data) ->
new Promise (resolve, reject) ->
self._sendRequest('PUT', self.updateUrl, id, data).then (request) ->
self._handleResponse request.response, request.xhr.status, resolve
, ->
self._handleError self.request.xhr.response, self.request.xhr.status, reject

destroy: (id) ->
new Promise (resolve, reject) ->
self._sendRequest('DELETE', self.destroyUrl, id).then (request) ->
self._handleResponse request.response, request.xhr.status, resolve
, ->
self._handleError self.request.xhr.response, self.request.xhr.status, reject

memberAction: (id, action) ->
new Promise (resolve, reject) ->
self._sendRequest('PUT', self.memberUrl, id, action).then (request) ->
self._handleResponse request.response, request.xhr.status, resolve
, ->
self._handleError self.request.xhr.response, self.request.xhr.status, reject

_prepareUrl: (url, params, id, action) ->
index: =>
new Promise (resolve, reject) =>
@_sendRequest('GET', @indexUrl, @indexQueryString).then (request) =>
@_handleResponse request.response, request.xhr.status, resolve
, =>
@_handleError @request.xhr.response, @request.xhr.status, reject

show: (id) =>
new Promise (resolve, reject) =>
@_sendRequest('GET', @showUrl, @showQueryString, id).then (request) =>
@_handleResponse request.response, request.xhr.status, resolve
, =>
@_handleError @request.xhr.response, @request.xhr.status, reject

new: () =>
new Promise (resolve, reject) =>
@_sendRequest('GET', @newUrl, @newQueryString, null, 'new').then (request) =>
@_handleResponse request.response, request.xhr.status, resolve
, =>
@_handleError @request.xhr.response, @request.xhr.status, reject

create: (data) =>
new Promise (resolve, reject) =>
@_sendRequest('POST', @createUrl, @createQueryString, null, data).then (request) =>
@_handleResponse request.response, request.xhr.status, resolve
, =>
@_handleError @request.xhr.response, @request.xhr.status, reject

update: (id, data) =>
new Promise (resolve, reject) =>
@_sendRequest('PUT', @updateUrl, @updateQueryString, id, data).then (request) =>
@_handleResponse request.response, request.xhr.status, resolve
, =>
@_handleError @request.xhr.response, @request.xhr.status, reject

destroy: (id) =>
new Promise (resolve, reject) =>
@_sendRequest('DELETE', @destroyUrl, @destroyQueryString, id).then (request) =>
@_handleResponse request.response, request.xhr.status, resolve
, =>
@_handleError @request.xhr.response, @request.xhr.status, reject

memberAction: (id, action) =>
console.log action
new Promise (resolve, reject) =>
@_sendRequest('PUT', @memberUrl, @memberQueryString, id, null, action).then (request) =>
@_handleResponse request.response, request.xhr.status, resolve
, =>
@_handleError @request.xhr.response, @request.xhr.status, reject

_prepareUrl: (url, queryString, params, id, action) ->
console.log action
url += '?' + queryString if queryString
params = JSON.parse(params) if typeof(params) == 'string'
for name, value of params
url = url.replace ":#{name}", value
Expand All @@ -89,11 +98,11 @@ Polymer
h['Authorization'] = @token if @token
h

_sendRequest: (method, url, id = null, data = null, action = null) ->
_sendRequest: (method, url, queryString, id = null, data = null, action = null) ->
@request = @_createRequestElement()
@request.send
method: method
url: @_prepareUrl url || @url, @params, id, action
url: @_prepareUrl url || @url, queryString || @queryString, @params, id, action
headers: @_prepareHeaders()
body: (if data then JSON.stringify data else undefined)

Expand Down
Loading

0 comments on commit d2f242b

Please sign in to comment.