Giter Site home page Giter Site logo

fetchival's People

Contributors

gfelizola avatar gustavobigardi avatar klardotsh avatar pedropinheiro avatar posva avatar termina1 avatar typicode avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fetchival's Issues

Update documentation

I noticed that that README says you can do soemthing like this:

//posts/1
posts(1).get()

This leads me to believe that the method will issue a request to /posts/1, but this is not actually the case. The method actually builds a querystring /user?. If I make the number a string, I get this : /user?0=1.

What is the intended behavior?
Attach?

401 Unauthorized does not get handled by .catch

A 401 unauthorized throws an exception from JS and cannot be handled with a .catch().

Steps to reproduce:

  1. fetchival('api.php').then(...).catch(function (error) { console.log(error); });
  2. Have server return a 401
  3. Exception: index.min.js:1 Uncaught (in promise) Error: Unauthorized
    at index.min.js:1
    at
  4. catch block is never called

Browser:
Chrome 63.0.3239.132 (Official Build) (64-bit)
Fetchival latest pulled from https://github.com/typicode/fetchival/blob/master/index.min.js yesterday

Shouldn't assume all requests will return JSON. Alternatively, document that this is the expectation.

fetchival's API is nice (because with ES7, fetching an HTML snippet looks like the following with plain ol' fetch - it's pretty awkward)

(async function main() {
    let content = fetch('/index.html');
    console.log(await (await content).text());
})()

Trying to pull this file through fetchival ends up bugging out as JSON.parse() obviously has no clue how to handle XML/HTML, and a quick glance through the source made it clear that this was what was being done, but throwing this in the README might be useful, or adding an option to parse the return as plain text.

I might tackle this myself and PR, but the issue is good to have documented either way.

There is currently no ability to remove a Content-Type header

Defaults currently uses the following logic to extend the options object:

function defaults (target, obj) {
    for (var prop in obj) target[prop] = target[prop] || obj[prop]
}

If you would prefer to not send a Content-Type at all, you'd not want it in the headers array (I don't believe fetch sets a Content-Type by default, if it does then my logic is wrong). By passing { 'Content-Type': undefined } into the fetchival options, it will hit the || logic of the extender, keeping the default header rather than removing the header as is the expected behavior.

The fix will be to forcibly set any "own properties" of the incoming object, even if they are not truthy.

Timeout setup and handling?

Is there any support in fetchival for timeout handling?
I'm using it on an esp8266 and as a small device that is battery operated it may die at any point or lose wifi connectivity and I'd like to know that nothing is updating. A timeout on the request will be useful also for the recovery from such a condition or I will have many requests up in the air.

response status and headers

Is there a way how to get every time information on response status and headers?
Not just when status is different from 200. Would be cool feature.

get body on 400 error

How to get json body from a code 400 server response?
I can see only the body: PassThrough { ... } stream. Aren't chunks collected and json parsed?

GET body not empty

Seems that if I do a GET after POST, opts.body of GET request will send body that sent by POST
I had to modify fetchival to fix it.

    if (data) {
      opts.body = JSON.stringify(data)
    } else {
      delete opts.body
    }

another possible fix is let function fetchival return a new object

Promise Rejection

This is what I'm getting

Possible Unhandled Promise Rejection ... JSON Parse error: Unexpected EOF

And this is my request

export const request = (endPoint, payload = {}, method = 'get', headers = {}) => {
    return new Promise((resolve, reject) => {
        fetchival(`http://localhost...`, options)[method.toLowerCase()](payload)
        .then((json) => {
            return resolve(json)
        })
        .catch((e) => {
            if (e.response && e.response.status === 500) {
                console.warn('URL:', tmp, 'OPTIONS:', options, 'PAYLOAD:', payload, 'EXCEPTION:', e)
                return reject(new Error('Server error.'))
            }
            if (e.response && e.response.json) {
                e.response.json().then((json) => {
                    return reject(json.error)
                })
            } else {
                console.warn('URL:', tmp, 'OPTIONS:', options, 'PAYLOAD:', payload, 'EXCEPTION:', e)
                return reject(new Error('Server error.'))
            }
        })
    })
})
request().then(() => {

}).catch(() => {
    // still can't catch the propper error
})

But none of these catch it's working

What could I be doing wrong?

Fetchival not working with Safari

I suspect something inside a Promise is not working inside fetchival. Its is not working with Version 9.1.3, and no error messages are shown. The promise never gest fulfilled.

Code example bellow (work on Chrome and Firefox).

console.log('Console log works here');

fetchival(module.settings.endpoint(['user', apiKey])).get().then(function(response) {
    console.log("Iam here, but never works on safari");
    console.log(response);

});

Unable to upload file

I think the file object is losing when calling fetchival
so the file will be post as string instead of file

Works fetchival with php $_POST?

I tested fetch with a php application and it doesn't work with $_POST and I need to get the data with php://input and loose CMF features.

Is it possible to send a post request with fetchival and $_POST will be populated by php?

Set {credentials: 'include'} by default

Fetchival is already handling almost everything I need by default. Handling the headers, status codes, JSONing, etc is great!
Would you consider setting {credentials: include'} by default?
I understand that I can now use the wrapper as follows const users = fetchival('/users', {credentials: 'include'});, but it would be very convenient if I didn't have to set this with each instantiation.

Great library BTW! I look forward to using it more!

Support custom headers

Currently, the request headers are fixed by:

opts.headers = {
    'Accept': 'application/json',
    'Content-Type': 'application/json'
}

It would be nice to support extra/custom headers. For example:

opts.headers = Object.assign({}, {
    'Accept': 'application/json',
    'Content-Type': 'application/json'
}, opts.headers);

This would support overridding the default headers, but Object.assign would need to be polyfilled in a lot of browsers.

What do you think?

POST with empty body

POST with empty body is not forbidden by the HTTP spec.
However I cannot do it using fetchival.
Probably the problem is caused by the Content-Type header (set to "application/json") which is not removed when the body is deleted.
It is also expected that when the body is empty the Content-Lenght header is set to 0.

If you agree, I can make a PR fixing it.

Thanks,
Damian.

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.