-
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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 } | ||||||||||||||||||||
|
||||||||||||||||||||
|
@@ -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 '' | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
// 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({ | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||||||||||||
'Content-Type': 'application/json', | ||||||||||||||||||||
'Accept': 'application/vnd.api+json; version=1', | ||||||||||||||||||||
}, parseHeaders(headers)) | ||||||||||||||||||||
Comment on lines
+82
to
+85
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||
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) { | ||||||||||||||||||||
|
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.