-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from fleetbase/dev-v0.2.13
`UniverseService` can now handle all engine booting from either regis…
- Loading branch information
Showing
8 changed files
with
255 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import config from 'ember-get-config'; | ||
|
||
export default async function fleetbaseApiFetch(method, uri, params = {}, fetchOptions = {}) { | ||
// Prepare base URL | ||
const baseUrl = `${config.API.host}/${fetchOptions.namespace ?? config.API.namespace}`; | ||
|
||
// Initialize headers | ||
const headers = { | ||
'Content-Type': 'application/json', | ||
}; | ||
|
||
// Check localStorage for the session data | ||
const localStorageSession = JSON.parse(window.localStorage.getItem('ember_simple_auth-session')); | ||
let token; | ||
if (localStorageSession) { | ||
const { authenticated } = localStorageSession; | ||
if (authenticated) { | ||
token = authenticated.token; | ||
} | ||
} | ||
|
||
// Set Authorization header if token is available | ||
if (token) { | ||
headers['Authorization'] = `Bearer ${token}`; | ||
} | ||
|
||
// Configure request options | ||
const options = { | ||
method, | ||
headers, | ||
}; | ||
|
||
// Handle params based on method | ||
if (['POST', 'PUT', 'PATCH', 'DELETE'].includes(method) && params) { | ||
options.body = JSON.stringify(params); | ||
} else if (method === 'GET' && params) { | ||
// Add params to URL for GET requests | ||
const urlParams = new URLSearchParams(params).toString(); | ||
uri += `?${urlParams}`; | ||
} | ||
|
||
try { | ||
// Make the fetch request | ||
const response = await fetch(`${baseUrl}/${uri}`, options); | ||
|
||
// Check if the response is OK (status in the range 200-299) | ||
if (!response.ok) { | ||
throw new Error(`HTTP error! status: ${response.status}`); | ||
} | ||
|
||
// Parse and return the JSON response | ||
return await response.json(); | ||
} catch (error) { | ||
// If a fallback response is provided use it instead | ||
if (fetchOptions && fetchOptions.fallbackResponse !== undefined) { | ||
return fetchOptions.fallbackResponse; | ||
} | ||
|
||
// Handle errors (network errors, JSON parsing errors, etc.) | ||
console.error('Error making request:', error); | ||
throw error; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import loadExtensions from '../utils/load-extensions'; | ||
import fleetbaseApiFetch from '../utils/fleetbase-api-fetch'; | ||
|
||
export default async function loadInstalledExtensions() { | ||
const CORE_ENGINES = ['@fleetbase/fleetops-engine', '@fleetbase/storefront-engine', '@fleetbase/registry-bridge-engine', '@fleetbase/dev-engine', '@fleetbase/iam-engine']; | ||
const INDEXED_ENGINES = await loadExtensions(); | ||
const INSTALLED_ENGINES = await fleetbaseApiFetch('get', 'engines', {}, { namespace: '~registry/v1', fallbackResponse: [] }); | ||
|
||
const isInstalledEngine = (engineName) => { | ||
return CORE_ENGINES.includes(engineName) || INSTALLED_ENGINES.find((pkg) => pkg.name === engineName); | ||
}; | ||
|
||
return INDEXED_ENGINES.filter((pkg) => { | ||
return isInstalledEngine(pkg.name); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from '@fleetbase/ember-core/utils/fleetbase-api-fetch'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from '@fleetbase/ember-core/utils/load-installed-extensions'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import fleetbaseApiFetch from 'dummy/utils/fleetbase-api-fetch'; | ||
import { module, test } from 'qunit'; | ||
|
||
module('Unit | Utility | fleetbase-api-fetch', function () { | ||
// TODO: Replace this with your real tests. | ||
test('it works', function (assert) { | ||
let result = fleetbaseApiFetch(); | ||
assert.ok(result); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import loadInstalledExtensions from 'dummy/utils/load-installed-extensions'; | ||
import { module, test } from 'qunit'; | ||
|
||
module('Unit | Utility | load-installed-extensions', function () { | ||
// TODO: Replace this with your real tests. | ||
test('it works', function (assert) { | ||
let result = loadInstalledExtensions(); | ||
assert.ok(result); | ||
}); | ||
}); |