Skip to content

Commit

Permalink
fix: rename ...url properties to ...path (browsers seem to like to ad…
Browse files Browse the repository at this point in the history
…d the document base url to an url attribute if it doesn't start with http://)
  • Loading branch information
grappendorf committed Sep 23, 2015
1 parent ccc062b commit 2b30c3a
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 101 deletions.
36 changes: 18 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ Properties

- *type:* string

If a base url is specified, it is prepended to all request urls.
The base url of all requests (e.g. https://api.exmaple.com:8000).

* **url**
* **path**

- *type:* string

A pattern for the REST URL. Can contain colon-parameters and data bindings, for example:
A pattern for the url path. Can contain colon-parameters and data bindings, for example:

`url="/users/:userId?q={{searchText}}"`
`path="/users/:userId"`

Default URLS:
Default paths:

Method | HTTP method | URL
------------- | ----------- | ---
Expand Down Expand Up @@ -56,37 +56,37 @@ Properties

Use this URL instead of the default one for "GET /resources" requests.

* **showUrl**
* **showPath**

- *type:* string

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

* **newUrl**
* **newPath**

- *type:* string

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

* **createUrl**
* **createPath**

- *type:* string

Use this URL instead of the default one for "POST /resources" requests.

* **updateUrl**
* **updatePath**

- *type:* string

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

* **destroyUrl**
* **destroyPath**

- *type:* string

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

* **memberUrl**
* **memberPath**

- *type:* string

Expand All @@ -102,43 +102,43 @@ Properties

- *type:* string

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

* **showQuery**

- *type:* string

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

* **newQuery**

- *type:* string

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

* **createQuery**

- *type:* string

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

* **updateQuery**

- *type:* string

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

* **destroyQuery**

- *type:* string

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

* **memberQuery**

- *type:* string

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

* **headers**

Expand Down
44 changes: 22 additions & 22 deletions grapp-rest-resource.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
Polymer({
is: 'grapp-rest-resource',
properties: {
url: {
path: {
type: String
},
baseUrl: {
Expand All @@ -33,25 +33,25 @@
type: Object,
notify: true
},
indexUrl: {
indexPath: {
type: String
},
showUrl: {
showPath: {
type: String
},
newUrl: {
newPath: {
type: String
},
createUrl: {
createPath: {
type: String
},
updateUrl: {
updatePath: {
type: String
},
destroyUrl: {
destroyPath: {
type: String
},
memberUrl: {
memberPath: {
type: String
},
query: {
Expand Down Expand Up @@ -96,7 +96,7 @@
index: (function(_this) {
return function() {
return new Promise(function(resolve, reject) {
return _this._sendRequest('GET', _this.indexUrl, _this.indexQuery).then(function(request) {
return _this._sendRequest('GET', _this.indexPath, _this.indexQuery).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);
Expand All @@ -107,7 +107,7 @@
show: (function(_this) {
return function(id) {
return new Promise(function(resolve, reject) {
return _this._sendRequest('GET', _this.showUrl, _this.showQuery, id).then(function(request) {
return _this._sendRequest('GET', _this.showPath, _this.showQuery, 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);
Expand All @@ -118,7 +118,7 @@
"new": (function(_this) {
return function() {
return new Promise(function(resolve, reject) {
return _this._sendRequest('GET', _this.newUrl, _this.newQuery, null, 'new').then(function(request) {
return _this._sendRequest('GET', _this.newPath, _this.newQuery, 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);
Expand All @@ -129,7 +129,7 @@
create: (function(_this) {
return function(data) {
return new Promise(function(resolve, reject) {
return _this._sendRequest('POST', _this.createUrl, _this.createQuery, null, data).then(function(request) {
return _this._sendRequest('POST', _this.createPath, _this.createQuery, 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);
Expand All @@ -140,7 +140,7 @@
update: (function(_this) {
return function(id, data) {
return new Promise(function(resolve, reject) {
return _this._sendRequest('PUT', _this.updateUrl, _this.updateQuery, id, data).then(function(request) {
return _this._sendRequest('PUT', _this.updatePath, _this.updateQuery, 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);
Expand All @@ -151,7 +151,7 @@
destroy: (function(_this) {
return function(id) {
return new Promise(function(resolve, reject) {
return _this._sendRequest('DELETE', _this.destroyUrl, _this.destroyQuery, id).then(function(request) {
return _this._sendRequest('DELETE', _this.destroyPath, _this.destroyQuery, 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);
Expand All @@ -162,7 +162,7 @@
memberAction: (function(_this) {
return function(id, action) {
return new Promise(function(resolve, reject) {
return _this._sendRequest('PUT', _this.memberUrl, _this.memberQuery, id, null, action).then(function(request) {
return _this._sendRequest('PUT', _this.memberPath, _this.memberQuery, 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);
Expand All @@ -172,11 +172,11 @@
})(this)
};
},
_prepareUrl: function(url, query, params, id, action) {
var name, value;
if (this.baseUrl) {
url = this.baseUrl + url;
}
_prepareUrl: function(path, query, params, id, action) {
var name, url, value;
console.log(path);
url = this.baseUrl ? this.baseUrl + path : path;
console.log(url);
if (query) {
url += '?' + query;
}
Expand Down Expand Up @@ -212,7 +212,7 @@
}
return h;
},
_sendRequest: function(method, url, query, id, data, action) {
_sendRequest: function(method, path, query, id, data, action) {
if (id == null) {
id = null;
}
Expand All @@ -225,7 +225,7 @@
this.request = this._createRequestElement();
return this.request.send({
method: method,
url: this._prepareUrl(url || this.url, query || this.query, this.params, id, action),
url: this._prepareUrl(path || this.path, query || this.query, this.params, id, action),
headers: this._prepareHeaders(),
body: (data ? JSON.stringify(data) : void 0)
});
Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ <h2>Demo</h2>
<dom-module id="demo-element">
<template>

<grapp-rest-resource id="data" url="/example_data"></grapp-rest-resource>
<grapp-rest-resource id="data" path="/example_data"></grapp-rest-resource>

<div>Response code: <span>[[status]]</span></div>
<template is="dom-repeat" items="[[items]]">
Expand Down
40 changes: 21 additions & 19 deletions src/grapp-rest-resource.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ Polymer
is: 'grapp-rest-resource',

properties:
url: {type: String}
path: {type: String}
baseUrl: {type: String}
params: {type: String, value: -> {}}
resource: {type: Object, notify: true}
indexUrl: {type: String}
showUrl: {type: String}
newUrl: {type: String}
createUrl: {type: String}
updateUrl: {type: String}
destroyUrl: {type: String}
memberUrl: {type: String}
indexPath: {type: String}
showPath: {type: String}
newPath: {type: String}
createPath: {type: String}
updatePath: {type: String}
destroyPath: {type: String}
memberPath: {type: String}
query: {type: String}
indexQuery: {type: String}
showQuery: {type: String}
Expand All @@ -30,55 +30,57 @@ Polymer
@resource =
index: =>
new Promise (resolve, reject) =>
@_sendRequest('GET', @indexUrl, @indexQuery).then (request) =>
@_sendRequest('GET', @indexPath, @indexQuery).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, @showQuery, id).then (request) =>
@_sendRequest('GET', @showPath, @showQuery, 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, @newQuery, null, 'new').then (request) =>
@_sendRequest('GET', @newPath, @newQuery, 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, @createQuery, null, data).then (request) =>
@_sendRequest('POST', @createPath, @createQuery, 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, @updateQuery, id, data).then (request) =>
@_sendRequest('PUT', @updatePath, @updateQuery, 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, @destroyQuery, id).then (request) =>
@_sendRequest('DELETE', @destroyPath, @destroyQuery, id).then (request) =>
@_handleResponse request.response, request.xhr.status, resolve
, =>
@_handleError @request.xhr.response, @request.xhr.status, reject

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

_prepareUrl: (url, query, params, id, action) ->
url = @baseUrl + url if @baseUrl
_prepareUrl: (path, query, params, id, action) ->
console.log path
url = if @baseUrl then @baseUrl + path else path
console.log url
url += '?' + query if query
params = JSON.parse(params) if typeof(params) == 'string'
for name, value of params
Expand All @@ -98,11 +100,11 @@ Polymer
h['Authorization'] = @token if @token
h

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

Expand Down
Loading

0 comments on commit 2b30c3a

Please sign in to comment.