From 013ac98b975fb526a4b8b1605da0d0299ddb485e Mon Sep 17 00:00:00 2001 From: "Shaun A. Noordin" Date: Thu, 12 Sep 2024 15:13:38 +0100 Subject: [PATCH 1/2] panoptes.js: add getQueryString function --- packages/lib-panoptes-js/src/panoptes.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/packages/lib-panoptes-js/src/panoptes.js b/packages/lib-panoptes-js/src/panoptes.js index 15c7467e3a..82ed5f652e 100644 --- a/packages/lib-panoptes-js/src/panoptes.js +++ b/packages/lib-panoptes-js/src/panoptes.js @@ -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,6 +53,18 @@ 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) { + const queryParams = getQueryParams(query) + let queryString = Object.entries(queryParams) + .map(([key, val]) => `${encodeURIComponent(key)}=${encodeURIComponent(val)}`) + .join('&') + return queryString.length > 0 ? `?${queryString}` : '' +} + // TODO: Consider how to integrate a GraphQL option function get (endpoint, query = {}, headers = {}, host) { if (!endpoint) return handleMissingParameter('Request needs a defined resource endpoint') From 96096edae89583059e950d7a8a3d97497360fee5 Mon Sep 17 00:00:00 2001 From: "Shaun A. Noordin" Date: Thu, 12 Sep 2024 22:22:22 +0100 Subject: [PATCH 2/2] panoptes.get experimental: replace superagent with fetch --- packages/lib-panoptes-js/src/panoptes.js | 39 ++++++++++++++++-------- 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/packages/lib-panoptes-js/src/panoptes.js b/packages/lib-panoptes-js/src/panoptes.js index 82ed5f652e..1972523e03 100644 --- a/packages/lib-panoptes-js/src/panoptes.js +++ b/packages/lib-panoptes-js/src/panoptes.js @@ -57,12 +57,19 @@ 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) { +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('&') - return queryString.length > 0 ? `?${queryString}` : '' + + 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 @@ -71,16 +78,24 @@ function get (endpoint, query = {}, headers = {}, host) { 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({ + '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) {