dijs / wiki

Wikipedia Interface for Node.js
MIT License
315 stars 61 forks source link

Wikipedia now enforces HTTPS #31

Closed petersandor closed 7 years ago

petersandor commented 7 years ago

Hi, this module is still using HTTP as the base API url, so the default configuration doesn't work on Chrome since it blocks the request. I solved it by providing the apiUrl option with https.

Another issue I have, I'm getting CORS error when I try to use the module:

Fetch API cannot load https://en.wikipedia.org/w/api.php?prop=info%7Cpageprops&inprop=url&ppprop=disambiguation&titles=Batman&format=json&action=query&redirects=.
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:3000' is therefore not allowed access.
If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

I'm using this in the browser (not in service worker), and I haven't found any possibility to pass an option for the fetch to circumvent the above issue.

I'm new to Wiki APIs, but I think that the API was recently changed, because even when I manage to get the response it can't be parsed.

Any help and/or clarification greatly appreciated, thanks!

dijs commented 7 years ago

Hmm... I do not think there was a change in the API. I just tested this code:

import wiki from 'wikijs';

wiki({
  apiUrl: 'https://en.wikipedia.org/w/api.php'
}).page('Batman')
    .then(page => page.info('alter_ego'))
    .then(console.log);

And it printed out Bruce Wayne as expected.

Let me try to look into the CORS issues though.

Do you have any example code I can test?

And do you have the un-parsable response you spoke about?

dijs commented 7 years ago

After giving it a try, I go the same result as you.

I think we are going to have to use the JSONP method to fix this issue.

https://www.mediawiki.org/wiki/API:Cross-site_requests

This change may not be straightforward...

I would look into trying to use service workers to circumvent the CORS issue for now.

asherism commented 7 years ago

any fix for this?

dijs commented 7 years ago

I commented above how to get around the issue for now. If you must us this module browser based, try using it inside of a service worker. If you are using it within node, there should not be an issue.

I have yet to implement the JSONP feature.

dijs commented 7 years ago

Fixed with #40