I'm kicking around a number of updates to the fetch
syntax, while still trying to keep things as short and expressive as possible. Can we use this ticket to start a discussion on the best way to make improvements?
HTTP Verbs
One thing that came up is the ability to use HTTP verbs as the specific command: enabling something like this:
get [from] <server-url> [using <headerValue>]
post [<object>] [into] <server-url>
put [<object>] [into] <server-url>
delete [<object>] [from] <server-url>
fetch <server-url> -- could still work as a "GET" request
I think this would bring HTTP verbs to the API surface with little or no cost to the language syntax. It also fits the spirit of hyperscript as "a scripting language designed for the web and inspired by HyperTalk." The one red flag is that put
is already in use as a (virtual) synonym for set
.
If we decide that we like this syntax, it would be a breaking change that we would need to manage carefully. The first step would probably be to confirm that set
is able to accomplish everything that put
does currently, and notify developers of the upcoming change.
Header Values
Though it might be possible to enumerate some of the common header values, I think the cost probably outweighs the benefits.
A statement like get from /my-url using authorization "12345" and cache-control "no-cache"
is really cool, but unwieldy in practice. If I need to send complicated headers to the server, it's more likely that I'll want to manage them like this
set commonHTTPHeaders to {
"Authorization: "12345",
"Cache-Control": "no-cache"
}
get from /my-url using commonHTTPHeaders
Pluggable Fetch Backend
Add a default function into the hyperscript configuration object -- similar to the one in htmx -- that would let developers override the current behavior. For example, I wrote an htmx extension to save authorization headers supplied in any http response, then add them to every subsequent http request. I'd like to make the same feature available in hyperscript as well.
These custom function signatures might look something like this
var config = {
// process request before sending via hyperscript
beforeFetch: function(url, options:Object): Obje t {
return {url:url, options:options};
},
// actually does the fetch command
fetch: function(url:string, options:Object): Promise<Object> {
return fetch(url, options);
},
// process response object before returning to hyperscript
afterFetch: function(response:Object): Object {
return response;
}
}