jasonelle-archive / jasonelle-v2

🛸 🏘️ Jasonelle issues, releases, discussions and wiki repository.
https://jasonelle.com
Mozilla Public License 2.0
422 stars 58 forks source link

Allow mixins to optionally make "post" request instead of just "get" #89

Closed vini-brito closed 3 years ago

vini-brito commented 4 years ago

Is your feature request related to a problem? Please describe. I'd like to hit API endpoints to get the mixins, but such endpoints do not allow for GET requests, only POST.

Describe the solution you'd like An option in the mixin node that would make the request a POST.

Describe alternatives you've considered I currently use integromat to make that conversion, for development it works fine, but when I reach production it will not be usable.

Identify which component would benefit from this feature

Additional context None for now.

clsource commented 4 years ago

Seems doable.

Let's first define the current behavior so the enhancement can be similar to the syntax.

http://jasonelle.com/docs/legacy/mixin/

With this new behavior then the mixin should allow both text and object input.

Current Behavior

"data": {
    "simpsons": {
        "@": "https://jsonbase.com/cl.ninjas/simpsons"
    }
}

By default, the current implementation transforms that input to

"data ": {
    "simpsons": {
        "{{#include $root[\"https://jsonbase.com/cl.ninjas/simpsons\"]}}": {}
    }
}

The url is requested and appended to the requires object inside VC.requires

The url is fetched inside the following function


- (void)         include:(id)json
    andCompletionHandler:(void (^)(id obj))callback;

Specifically in this line https://github.com/jasonelle/jasonette-ios/blob/develop/xcode/Jasonette/Core/Jason/Jason.m#L1308

When the object is obtained then is used here

NSMutableDictionary * refs = [VC.requires mutableCopy];
 "https://jsonbase.com/cl.ninjas/simpsons" :     [
        "Homer",
        "Marge",
        "Lisa",
        "Bart",
        "Maggie"
    ]

Which is then resolved by st.js

https://github.com/SelectTransform/st.js/blob/develop/st.js#L242

And appended to the current JSON structure document in Jason.m

refs[@"$document"] = VC.original;
id include_resolved = [JasonParser parse:refs with:tpl];

VC.original = include_resolved;
 return include_resolved;

Then proceeds to resolve the local references that starts with $.

Proposed change

Basically for this to work is needed to pass a special param that indicates the request params.

maybe using the following syntax similar to http://jasonelle.com/docs/legacy/actions/#networkrequest

"data": {
    "simpsons": {
        "@": { 
                     "url": "https://jsonbase.com/cl.ninjas/simpsons",
                     "method": "post|get",
                     "header": {
                      }
                }
    }
}

Naturally the previous syntax would still be valid.

@vini-brito what do you think?

vini-brito commented 4 years ago

I think this seems great!

clsource commented 4 years ago

Upon making some quick tests to implement this syntax. It can be a little cumbersome to implement. Because mixins are resolved before the parsing of the JSON structure.

This means that some regex is done to replace the syntax. Making an object instead of a string requires complex regex rules and probably some sort of JSON parser or curly braces matcher and balancer.

This can be implemented, but it would require some time.

A simpler solution can be made available using another syntax.

something like

{
 "@" : "https://myurl[POST]"
}

following POST specs

https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST

And maybe curl similar syntax

https://gist.github.com/subfuzion/08c5d85437d5d4f00e58

clsource commented 4 years ago

I added simple requests using post in iOS.

https://github.com/jasonelle/jasonette-ios/tree/mixin-post

some little testing should be done before merge :)

clsource commented 4 years ago

Now in Android https://github.com/jasonelle/jasonette-android/tree/mixin-post

clsource commented 3 years ago

Now both android and ios have this :) Thanks for the idea 👍