typicode / fetchival Goto Github PK
View Code? Open in Web Editor NEWEasy window.fetch requests
License: MIT License
Easy window.fetch requests
License: MIT License
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?
A 401 unauthorized throws an exception from JS and cannot be handled with a .catch().
Steps to reproduce:
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
Is there away to get the progress for post / upload I know fetch does not support this but was wondering if fetchival does?
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.
fetchival doesn't work?
index.min.js:1 Uncaught (in promise) SyntaxError: Unexpected token A in JSON at position 0
Tested with locale file and https://rawgit.com external
Raises an "Unexpected end of input" error.
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.
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.
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.
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?
Are you planing any ts support on fluent api?
Kind regards
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
Need support in posting data as FormData
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?
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);
});
I think the file object is losing when calling fetchival
so the file will be post as string instead of file
I've not seen recent updates and there are plenty of interesting pull requests to merge.
Tested in Chrome 42, which has native support for fetch and ES6 Promises: http://codepen.io/leonderijke/pen/pJjdxj?editors=001
When calling fetchival('/foo').get()
, fetchival.fetch
throws an Illegal invocation
TypeError.
Running the tests in Chrome 42 gives the same result: all tests fail with TypeError: Illegal invocation
.
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?
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!
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 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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.