_hyperscript icon indicating copy to clipboard operation
_hyperscript copied to clipboard

PROPOSAL: Fetch syntax upgrades

Open benpate opened this issue 4 years ago • 2 comments

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;
    }
}

benpate avatar Apr 10 '21 17:04 benpate

get and put are both already used in the language, and taken from Hypertalk so I don't like changing that. That said, I agree that the current syntax is a little ugly. What about an issue command?

  issue GET from ...
  issue POST to ...

with fetch remaining the current low level wrapper around the fetch API.

Love the config plugin idea.

1cg avatar Apr 15 '21 19:04 1cg

I didn't check to see that get was also used. Yeah, that's too many changes.

I've kicked this around a bit, and while issue is good, it doesn't really scream "remote connection" to me. After typing and rejecting a lot of other alternatives, I'm going to suggest http as in:

http GET [from] <url> [with <options>]
http POST [<body>] [to|into] <url> [with <options>]
http PUT [<body>] [to|into] <url> [with <options>]
http DELETE [<body>] [from] <url> [with <options>]

While it doesn't read exactly like a sentence, it is pretty clear what's about to happen. What do you think?

benpate avatar Apr 16 '21 03:04 benpate