Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

Yeah I know, I know.
but serously!

Let me explain.

I am using nestjs and as expected I am sending HTML across the wire almost all of the time.
However there is one scenario where I run into a hard block with nestjs, unpoly and the attempt to send html.
that is when the server receives unfixable formdata from the client that the Validation Pipes within nestjs reject the incoming data altogether. In such cases nestjs throws an exception returns always a JSON structure describing the error.
There are ways to circumvent that on the server side but that is really nasty and creates massive overhead.

So i opted to handle such case by creating the proper HTML on the server then throwing an exception using the HTML which then will be packaged as a JSON and on the client side I intercept the onLoaded callback, extract the response.text on status === 400 then I try to parse the text as JSON. if that works I extract the HTML from the JSON an put this back into the response.text

altogether this looks like this

    let result = await up.render({target, url, params, failTarget: "#errors_dialog", method: "POST", onLoaded: (event) => {
        if (event.response.status === 400) {
            try {
                const error = JSON.parse(event.response.text)
                if (error.message) {
                    event.response.text = error.message
                }
            }
            catch (e) {
                console.log("onLoaded ERROR", event.response.text)
            }
        }
        else {
            console.log("onLoaded NOT ERROR?", event)
        }
    }});

I think it would be cool if instead of that callback one could do something like this

up.render({target, url, params, failTarget: "#errors_dialog", method: "POST", returnType: "JSON", JSONPath: "error.message"})

where upon unpoly parses the JSON and has a path to the property where the HTML is stored.

Now if you say all this isnt a good fit for unpoly - maybe you could add my example (maybe improved) to the docs.
It took me roughly a day to figure this out and others can benefit from my experiences

Best regards
Robert

You must be logged in to vote

Replies: 1 comment

Comment options

Thanks @robkuz, I'm going to add something to the docs.

htmx has an extension to convert JSON via templates, but I'd prefer not to go down that rabbit hole.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
💡
Ideas
Labels
None yet
2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.