forked from aurelia/http-client
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(request-builder): allow message parameters to be fully specified…
… without sending the message This change reworks the `RequestBuilder` API so that URI, HTTP method, and content can be specified without forcing the request to be sent. This is a breaking API change to `RequestBuilder`. To update code, replaces uses of `.get(uri)`, `.post(uri, content)`, etc. with `.asGet().withUri(uri).send()`, `.asPost().withUri(uri).withContent(content).send()`. Fixes aurelia#29
- Loading branch information
1 parent
93a6e38
commit 5a2284f
Showing
3 changed files
with
92 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ export class RequestBuilder { | |
constructor (client) { | ||
this.client = client; | ||
this.transformers = client.requestTransformers.slice(0); | ||
this.useJsonp = false; | ||
} | ||
|
||
/** | ||
|
@@ -30,105 +31,77 @@ export class RequestBuilder { | |
} | ||
|
||
/** | ||
* Sends an HTTP DELETE request. | ||
* Sends the request. | ||
* | ||
* @method delete | ||
* @param {String} uri The target URI. | ||
* @method send | ||
* @return {Promise} A cancellable promise object. | ||
*/ | ||
delete(uri){ | ||
var message = new HttpRequestMessage('DELETE', uri); | ||
send(){ | ||
let message = this.useJsonp ? new JSONPRequestMessage() : new HttpRequestMessage(); | ||
return this.client.send(message, this.transformers); | ||
} | ||
} | ||
|
||
/** | ||
* Sends an HTTP GET request. | ||
* | ||
* @method get | ||
* @param {String} uri The target URI. | ||
* @return {Promise} A cancellable promise object. | ||
*/ | ||
get(uri){ | ||
var message = new HttpRequestMessage('GET', uri); | ||
return this.client.send(message, this.transformers); | ||
} | ||
RequestBuilder.addHelper('asDelete', function(){ | ||
return function(client, processor, message){ | ||
message.method = 'DELETE'; | ||
}; | ||
}); | ||
|
||
/** | ||
* Sends an HTTP HEAD request. | ||
* | ||
* @method head | ||
* @param {String} uri The target URI. | ||
* @return {Promise} A cancellable promise object. | ||
*/ | ||
head(uri){ | ||
var message = new HttpRequestMessage('HEAD', uri); | ||
return this.client.send(message, this.transformers); | ||
} | ||
RequestBuilder.addHelper('asGet', function(){ | ||
return function(client, processor, message){ | ||
message.method = 'GET'; | ||
}; | ||
}); | ||
|
||
/** | ||
* Sends a JSONP request. | ||
* | ||
* @method jsonp | ||
* @param {String} uri The target URI. | ||
* @param {String} [callbackParameterName=jsoncallback] The target Javascript expression to invoke. | ||
* @return {Promise} A cancellable promise object. | ||
*/ | ||
jsonp(uri, callbackParameterName='jsoncallback'){ | ||
var message = new JSONPRequestMessage(uri, callbackParameterName); | ||
return this.client.send(message, this.transformers); | ||
} | ||
RequestBuilder.addHelper('asHead', function(){ | ||
return function(client, processor, message){ | ||
message.method = 'HEAD'; | ||
}; | ||
}); | ||
|
||
/** | ||
* Sends an HTTP OPTIONS request. | ||
* | ||
* @method options | ||
* @param {String} uri The target URI. | ||
* @return {Promise} A cancellable promise object. | ||
*/ | ||
options(uri){ | ||
var message = new HttpRequestMessage('OPTIONS', uri); | ||
return this.client.send(message, this.transformers); | ||
} | ||
RequestBuilder.addHelper('asOptions', function(){ | ||
return function(client, processor, message){ | ||
message.method = 'OPTIONS'; | ||
}; | ||
}); | ||
|
||
/** | ||
* Sends an HTTP PUT request. | ||
* | ||
* @method put | ||
* @param {String} uri The target URI. | ||
* @param {Object} uri The request payload. | ||
* @return {Promise} A cancellable promise object. | ||
*/ | ||
put(uri, content){ | ||
var message = new HttpRequestMessage('PUT', uri, content); | ||
return this.client.send(message, this.transformers); | ||
} | ||
RequestBuilder.addHelper('asPatch', function(){ | ||
return function(client, processor, message){ | ||
message.method = 'PATCH'; | ||
}; | ||
}); | ||
|
||
/** | ||
* Sends an HTTP PATCH request. | ||
* | ||
* @method patch | ||
* @param {String} uri The target URI. | ||
* @param {Object} uri The request payload. | ||
* @return {Promise} A cancellable promise object. | ||
*/ | ||
patch(uri, content){ | ||
var message = new HttpRequestMessage('PATCH', uri, content); | ||
return this.client.send(message, this.transformers); | ||
} | ||
RequestBuilder.addHelper('asPost', function(){ | ||
return function(client, processor, message){ | ||
message.method = 'POST'; | ||
}; | ||
}); | ||
|
||
/** | ||
* Sends an HTTP POST request. | ||
* | ||
* @method post | ||
* @param {String} uri The target URI. | ||
* @param {Object} uri The request payload. | ||
* @return {Promise} A cancellable promise object. | ||
*/ | ||
post(uri, content){ | ||
var message = new HttpRequestMessage('POST', uri, content); | ||
return this.client.send(message, this.transformers); | ||
} | ||
} | ||
RequestBuilder.addHelper('asPut', function(){ | ||
return function(client, processor, message){ | ||
message.method = 'PUT'; | ||
}; | ||
}); | ||
|
||
RequestBuilder.addHelper('withJsonpParameter', function(jsonpParameterName){ | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
bryanrsmith
Author
Owner
|
||
this.useJsonp = true; | ||
return function(client, processor, message){ | ||
message.jsonpParameterName = jsonpParameterName; | ||
}; | ||
}); | ||
|
||
RequestBuilder.addHelper('withUri', function(uri){ | ||
return function(client, processor, message){ | ||
message.uri = uri; | ||
}; | ||
}); | ||
|
||
RequestBuilder.addHelper('withContent', function(content){ | ||
return function(client, processor, message){ | ||
message.content = content; | ||
}; | ||
}); | ||
|
||
RequestBuilder.addHelper('withBaseUrl', function(baseUrl){ | ||
return function(client, processor, message){ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
asJsonp
? It felt a little odd with thejsonpParameterName
, but it would be more consistent with the other methods.