Skip to content
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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 38 additions & 10 deletions packages/lib-panoptes-js/src/panoptes.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function parseHeaders(headers = {}) {
return httpHeaders
}

// Note: if it makes the code more readable, we can merge getQueryParams into getQueryString.
function getQueryParams (query) {
const defaultParams = { admin: checkForAdminFlag(), http_cache: true }

Expand All @@ -52,22 +53,49 @@ function getQueryParams (query) {
}
}

/*
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 ''
}
Comment on lines +56 to +73
Copy link
Contributor

@eatyourgreens eatyourgreens Sep 13, 2024

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.

Copy link
Contributor

@eatyourgreens eatyourgreens Sep 13, 2024

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()


// TODO: Consider how to integrate a GraphQL option
function get (endpoint, query = {}, headers = {}, host) {
if (!endpoint) return handleMissingParameter('Request needs a defined resource endpoint')
if (typeof query !== 'object') return Promise.reject(new TypeError('Query must be an object'))

const apiHost = determineHost(query, host)
const request = superagent.get(`${apiHost}${endpoint}`)
.set('Content-Type', 'application/json')
.set('Accept', 'application/vnd.api+json; version=1')

if (headers){
request.set(parseHeaders(headers))
}
const queryParams = getQueryParams(query)

return request.query(queryParams).then(response => response)
const queryString = getQueryString(query, endpoint)
const fetchHeaders = Object.assign({
Copy link
Contributor

@eatyourgreens eatyourgreens Sep 13, 2024

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.

Copy link
Contributor

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.

'Content-Type': 'application/json',
'Accept': 'application/vnd.api+json; version=1',
}, parseHeaders(headers))
Comment on lines +82 to +85
Copy link
Contributor

@eatyourgreens eatyourgreens Sep 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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)
}

const fetchUrl = `${apiHost}${endpoint}${queryString}`

return fetch(fetchUrl, {
headers: fetchHeaders,
})
// HACK: convert fetch response into superagent response structure
.then(response => response.json())
.then(data => ({
// TODO: we're still missing a lot of the superagent responses!
// e.g. response.status, actual response.ok, response.headers
body: data,
ok: true,
}))
}

function post (endpoint, data, headers = {}, query = {}, host) {
Expand Down
Loading