Skip to content

Commit

Permalink
improve fetch service and fix handling of download method
Browse files Browse the repository at this point in the history
  • Loading branch information
roncodes committed May 16, 2024
1 parent 0aab986 commit 9e9cb90
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
27 changes: 17 additions & 10 deletions addon/services/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import corslite from '../utils/corslite';
import getMimeType from '../utils/get-mime-type';
import download from '../utils/download';
import getUserOptions from '../utils/get-user-options';
import isEmptyObject from '../utils/is-empty-object';
import fetch from 'fetch';

if (isBlank(config.API.host)) {
Expand Down Expand Up @@ -339,7 +340,7 @@ export default class FetchService extends Service {
return this.cachedGet(...arguments);
}

const urlParams = !isBlank(query) ? new URLSearchParams(query).toString() : '';
const urlParams = !isEmptyObject(query) ? new URLSearchParams(query).toString() : '';

return this.request(`${path}${urlParams ? '?' + urlParams : ''}`, 'GET', {}, options);
}
Expand Down Expand Up @@ -506,7 +507,7 @@ export default class FetchService extends Service {
let version = options?.version ?? 'v1';
let host = options?.host ?? `https://${options?.subdomain ?? 'routing'}.fleetbase.io`;
let route = coordinates.map((coords) => coords.join(',')).join(';');
let params = !isBlank(query) ? new URLSearchParams(query).toString() : '';
let params = !isEmptyObject(query) ? new URLSearchParams(query).toString() : '';
let path = `${host}/${service}/${version}/${profile}/${route}`;
let url = `${path}${params ? '?' + params : ''}`;

Expand Down Expand Up @@ -601,16 +602,22 @@ export default class FetchService extends Service {
const method = options.method ?? 'GET';
const credentials = options.credentials ?? this.credentials;
const baseUrl = `${options.host || this.host}/${options.namespace || this.namespace}`;
const params = method.toUpperCase() === 'GET' ? `?${!isBlank(query) ? new URLSearchParams(query).toString() : ''}` : '';
const body = method.toUpperCase() !== 'GET' ? JSON.stringify(query) : {};
const isReadOnlyRequest = ['GET', 'HEAD'].includes(method.toUpperCase());
const params = isReadOnlyRequest && !isEmptyObject(query) ? `?${new URLSearchParams(query).toString()}` : '';
const body = !isReadOnlyRequest ? JSON.stringify(query) : {};
const fetchOptions = {
method,
credentials,
headers,
};

// Only supply body to fetch if not GET or HEAD request
if (!isReadOnlyRequest) {
fetchOptions.body = body;
}

return new Promise((resolve, reject) => {
return fetch(`${baseUrl}/${path}${params}`, {
method,
credentials,
headers,
body,
})
return fetch(`${baseUrl}/${path}${params}`, fetchOptions)
.then((response) => {
options.fileName = this.getFilenameFromResponse(response, options.fileName);
options.mimeType = this.getMimeTypeFromResponse(response, options.mimeType);
Expand Down
3 changes: 3 additions & 0 deletions addon/utils/is-empty-object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function isEmptyObject(obj) {
return Object.keys(obj).length === 0 && obj.constructor === Object;
}
1 change: 1 addition & 0 deletions app/utils/is-empty-object.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from '@fleetbase/ember-core/utils/is-empty-object';
10 changes: 10 additions & 0 deletions tests/unit/utils/is-empty-object-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import isEmptyObject from 'dummy/utils/is-empty-object';
import { module, test } from 'qunit';

module('Unit | Utility | is-empty-object', function () {
// TODO: Replace this with your real tests.
test('it works', function (assert) {
let result = isEmptyObject();
assert.ok(result);
});
});

0 comments on commit 9e9cb90

Please sign in to comment.