-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PanoptesJS: remove superagent #6299
base: master
Are you sure you want to change the base?
Conversation
/* | ||
Converts a query object (e.g. { foo: 'bar', img: 'http://foo.bar/baz.jpg' }) | ||
into a query string (e.g. "foo=bar&img=http%3A%2F%2Ffoo.bar%") | ||
*/ | ||
function getQueryString (query, endpoint = '') { | ||
const queryParams = getQueryParams(query) | ||
let queryString = Object.entries(queryParams) | ||
.filter(([key, val]) => val !== undefined) | ||
.map(([key, val]) => `${encodeURIComponent(key)}=${encodeURIComponent(val)}`) | ||
.join('&') | ||
|
||
if (queryString.length > 0) { | ||
return endpoint.includes('?') | ||
? `&${queryString}` // If endpoint already has a query '?', prepend the query string with a '&'. | ||
: `?${queryString}` // Otherwise, prepend with a '?'. | ||
} | ||
return '' | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use URLSearchParams to convert an object to a query string in all browsers and in Node too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const searchParams = new URLSearchParams(queryParams)
return searchParams.toString()
Might be a good idea to upgrade the code to ES6 too. At the moment, it’s written for old versions of Node that are no longer supported. |
|
||
return request.query(queryParams).then(response => response) | ||
const queryString = getQueryString(query, endpoint) | ||
const fetchHeaders = Object.assign({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Object.assign
has been replaced by the object spread operator, which is much faster, in modern JavaScript.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Object.assign
should only appear in code if you need to support IE.
const fetchHeaders = Object.assign({ | ||
'Content-Type': 'application/json', | ||
'Accept': 'application/vnd.api+json; version=1', | ||
}, parseHeaders(headers)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const fetchHeaders = Object.assign({ | |
'Content-Type': 'application/json', | |
'Accept': 'application/vnd.api+json; version=1', | |
}, parseHeaders(headers)) | |
const fetchHeaders = { | |
'Content-Type': 'application/json', | |
'Accept': 'application/vnd.api+json; version=1', | |
...parseHeaders(headers) | |
} |
PR Overview
Package: panoptes-js
This PR attempts to remove
superagent
from the PanoptesJS clientsuperagent
is a dependency that we don't technically need anymore, since the standardfetch
API works fine, while maintaining superagent can be a bit of a faff.panoptes.js
, so replacing the dependency should be very easy.Dev Notes
As of 12 Sep 2024, this PR is in an experimental stage. The biggest lessons we've learnt are:
superagent()
calls inpanoptes.js
, as initially observed.panoptes.get()/panoptes.put()/panoptes.post()/panoptes.del()
,Comparison of the default response data we get from superagent vs the default response data we get from fetch().
Superagent response:
Fetch API response:
So in practice, we either need to...
Status
Experimental. Put on pause as of 12 Sep 2024 while higher priority items in PanoptesJS is addressed.
As of commit 96096e: panoptes.get experimental: replace superagent with fetch, the get() function has superagent() successfully replaced with fetch(), with a very rudimentary fetch-response-to-superagent-response transformer in place. You can run
lib-classifier > yarn dev
and load a local test project, and the data will be fetched fine for this instance.