loading...

More secure `fetch`, but not overdone like Axios?

patarapolw profile image Pacharapol Withayasakpunt ・1 min read

I love fetch in that I can easily control whether to output JSON or plainText via chaining; but obviously, it is lacking a URL builder.

I do love axios in that I can create an Instance, but sometimes I don't get whether it will return JSON or plainText (by default settings, i.e. no transformers)?

Also, WHATWG URL object might be doing it wrong, keeping .searchParams and .href apart, making it impossible for chaining?

Discussion

markdown guide
 

URL building? Here's a helper I wrote, are you looking for this?

function fmtQueryParam(entry) {                                                                                          
        const k = entry[0];                                                                                              
        const v = entry[1];                                                                                              
        if (v instanceof Array)
                return v.map(i => encodeURIComponent(k) + '=' + encodeURIComponent(i)).join('&');                        
        return encodeURIComponent(k) + '=' + encodeURIComponent(v);                                                      
}

I use it with Object.entries(query).map(fmtQueryParam).join('&').

Edit: ok the whitespace got messed up in pasting cause I copied it from Nano. w/e