Skip to content
Snippets Groups Projects
Commit ebd56c2a authored by Esko Ikkala's avatar Esko Ikkala
Browse files

Use node-fetch instead of superagent

parent 3eedd2e4
No related branches found
No related tags found
No related merge requests found
import request from 'superagent'; //import request from 'superagent';
import fetch from 'node-fetch'; import fetch from 'node-fetch';
const { URLSearchParams } = require('url');
const defaultSelectHeaders = { const defaultSelectHeaders = {
'Content-Type': 'application/x-www-form-urlencoded', 'Content-Type': 'application/x-www-form-urlencoded',
...@@ -18,26 +19,38 @@ class SparqlApi { ...@@ -18,26 +19,38 @@ class SparqlApi {
query(query, { headers }) { query(query, { headers }) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const params = new URLSearchParams();
if (this.endpoint === 'http://vocab.getty.edu/sparql.json') { params.append('query', query);
const url = this.endpoint + '?query=' + query; fetch(this.endpoint, {
fetch(url) method: 'post',
.then(response => { body: params,
return response.json(); headers: headers,
}) })
.then(responseData => { .then(res => res.text())
return resolve(responseData); .then(data => {
}) return resolve(data);
.catch(error => console.log('error is', error)); })
} else { .catch(error => console.log('error is', error));
request.post(this.endpoint) // if (this.endpoint === 'http://vocab.getty.edu/sparql.json') {
.send({ query }) // const url = this.endpoint + '?query=' + query;
.set(headers) // fetch(url)
.end((err, res) => { // .then(response => {
if (err || !res.ok) return reject(err); // return response.json();
return resolve(res.text); // })
}); // .then(responseData => {
} // return resolve(responseData);
// })
// .catch(error => console.log('error is', error));
// } else {
// const body = { query: query};
// request.post(this.endpoint)
// .send(body)
// .set(headers)
// .end((err, res) => {
// if (err || !res.ok) return reject(err);
// return resolve(res.text);
// });
// }
}); });
} }
...@@ -54,7 +67,6 @@ class SparqlApi { ...@@ -54,7 +67,6 @@ class SparqlApi {
constructQuery(query, params = { headers: defaultConstructHeaders }) { constructQuery(query, params = { headers: defaultConstructHeaders }) {
return this.query(query, params); return this.query(query, params);
} }
} }
export default SparqlApi; export default SparqlApi;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment