1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-19 16:24:51 +00:00

Add query string support for tm-http-request

This commit is contained in:
jeremy@jermolene.com 2023-01-13 11:34:25 +00:00
parent 570bc95181
commit 261f0f5261
2 changed files with 28 additions and 1 deletions

View File

@ -32,7 +32,9 @@ HttpClient.prototype.handleHttpRequest = function(event) {
bindProgress = paramObject["bind-progress"],
method = paramObject.method || "GET",
HEADER_PARAMETER_PREFIX = "header-",
QUERY_PARAMETER_PREFIX = "query-",
PASSWORD_HEADER_PARAMETER_PREFIX = "password-header-",
PASSWORD_QUERY_PARAMETER_PREFIX = "password-query-",
CONTEXT_VARIABLE_PARAMETER_PREFIX = "var-",
requestHeaders = {},
contextVariables = {},
@ -45,11 +47,19 @@ HttpClient.prototype.handleHttpRequest = function(event) {
setBinding(bindStatus,"pending");
setBinding(bindProgress,"0");
$tw.utils.each(paramObject,function(value,name) {
// Look for query- parameters
if(name.substr(0,QUERY_PARAMETER_PREFIX.length) === QUERY_PARAMETER_PREFIX) {
url = $tw.utils.setQueryStringParameter(url,name.substr(QUERY_PARAMETER_PREFIX.length),value);
}
// Look for header- parameters
if(name.substr(0,HEADER_PARAMETER_PREFIX.length) === HEADER_PARAMETER_PREFIX) {
requestHeaders[name.substr(HEADER_PARAMETER_PREFIX.length)] = value;
}
// Look for password-header- parameters
if(name.substr(0,PASSWORD_QUERY_PARAMETER_PREFIX.length) === PASSWORD_QUERY_PARAMETER_PREFIX) {
url = $tw.utils.setQueryStringParameter(url,name.substr(PASSWORD_QUERY_PARAMETER_PREFIX.length),$tw.utils.getPassword(value) || "");
}
// Look for password-query- parameters
if(name.substr(0,PASSWORD_HEADER_PARAMETER_PREFIX.length) === PASSWORD_HEADER_PARAMETER_PREFIX) {
requestHeaders[name.substr(PASSWORD_HEADER_PARAMETER_PREFIX.length)] = $tw.utils.getPassword(value) || "";
}
@ -201,4 +211,19 @@ exports.httpRequest = function(options) {
return request;
};
exports.setQueryStringParameter = function(url,paramName,paramValue) {
var URL = $tw.browser ? window.URL : require("url").URL,
newUrl;
try {
newUrl = new URL(url);
} catch(e) {
}
if(newUrl && paramName) {
newUrl.searchParams.set(paramName,paramValue || "");
return newUrl.toString();
} else {
return url;
}
};
})();

View File

@ -18,8 +18,10 @@ The following parameters are used:
|!Name |!Description |
|method |HTTP method (eg "GET", "POST") |
|body |String data to be sent with the request |
|header-* |Headers with string values|
|query-* |Query string parameters with string values |
|header-* |Headers with string values |
|password-header-* |Headers with values taken from the password store |
|password-query-* |Query string parameters with values taken from the password store |
|var-* |Variables to be passed to the completion and progress handlers (without the "var-" prefix) |
|bind-status |Title of tiddler to which the status of the request ("pending", "complete", "error") should be bound |
|bind-progress |Title of tiddler to which the progress of the request (0 to 100) should be bound |