Code Monkey home page Code Monkey logo

Comments (9)

LeaVerou avatar LeaVerou commented on June 11, 2024 1

Use the Bliss $.type() function. Your check would fail if the object comes from another window/frame.

from bliss.

joyously avatar joyously commented on June 11, 2024 1

Is the fetch function still needed? Is it stated somewhere what range of browser versions this is now intended for?

from bliss.

friday avatar friday commented on June 11, 2024

I'm already wrapping Bliss.fetch, so I'm not in a hurry to get this added. I mainly wanted to share my discoveries (I tried to solve this using query strings at first).

Converting ex: {foo: {bar: 1, x: {y: 'z'}} to foo[bar=1]&foo[x][y]=z requires recursion. In the simplest form, something like this:

// Note: This is simplified/dumb version of https://github.com/friday/query-string-encode

function queryStringEncode(input) {
    // Array of path/value tuples
    var flattened = [];

    (function flatten(input, path) {
        // Add path and value to flat array
        if ([Boolean, Number, String].indexOf(input.constructor) !== -1) {
            var serializedPath = path.map(function (key, index) {
                return index ? "[" + key + "]" : key;
            }).join("");
            flattened.push([serializedPath, input]);
        }

        // Iterate over next level of array/object
        else if ([Array, Object].indexOf(input.constructor) !== -1) {
            for (var key in input) {
                flatten(input[key], path.concat([key]));
            }
        }
    })(input, []);

    // Convert array to query string
    return flattened.map(function (pair) {
        return pair.map(encodeURIComponent).join("=");
    }).join("&");
}

from bliss.

LeaVerou avatar LeaVerou commented on June 11, 2024

The thing is, sometimes we want to send the actual object, e.g. uploading File objects.
So we need to be careful where we apply this stringification.

from bliss.

friday avatar friday commented on June 11, 2024

Yes. Only object literals or objects created with new Object() should be supported imo. This can be checked using the constructor (and probably many other ways)

({}).constructor === Object // true
new Blob().constructor === Object // false
new FormData.constructor === Object // false

from bliss.

friday avatar friday commented on June 11, 2024

Oh yeah, I recall something about this. Seems this applies to my suggestion as well :(

from bliss.

LeaVerou avatar LeaVerou commented on June 11, 2024

Seems this applies to my suggestion as well :(

What do you mean?

from bliss.

friday avatar friday commented on June 11, 2024

Oh, I read that too quickly. Nevermind the "as well" part of the comment.

from bliss.

friday avatar friday commented on June 11, 2024

Perhaps not @joyously. However Bliss's Fetch implementation is not a polyfill. It's different from the Fetch standard in small ways that means people who use it can't just drop-in replace the native browser Fetch or a polyfill.

The browser support for Fetch is very good today. When the Fetch API standard was released and got initial browser support you couldn't get any upload/download progress or abort the request. AbortController and ReadableStream were added later to solve that. They're also well supported today, but this adds to more differences in the Fetch API implementation and Bliss's Fetch.

However you're probably right that this issue may not be important today anymore. I'll leave that decision to Lea.

from bliss.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.