diff --git a/app/authenticators/meilisearch.js b/app/authenticators/meilisearch.js index 05d574a..71ef8ad 100644 --- a/app/authenticators/meilisearch.js +++ b/app/authenticators/meilisearch.js @@ -1,5 +1,4 @@ import Base from 'ember-simple-auth/authenticators/base'; -// import query from '../utils/query'; function makeRequest({ url, key }) { const headers = new Headers({ @@ -10,6 +9,7 @@ function makeRequest({ url, key }) { headers.append('Authorization', `Bearer ${key}`); } + // TODO: how to check session? const req = new Request(new URL('/version', url), { headers }); return fetch(req).then(() => ({ url, key })); diff --git a/app/instance-initializers/application.js b/app/instance-initializers/application.js new file mode 100644 index 0000000..49e21c3 --- /dev/null +++ b/app/instance-initializers/application.js @@ -0,0 +1,9 @@ +export let instance; + +export function initialize(owner) { + instance = owner; +} + +export default { + initialize, +}; diff --git a/app/utils/query.js b/app/utils/query.js index 5cc8e29..af752b6 100644 --- a/app/utils/query.js +++ b/app/utils/query.js @@ -1,14 +1,19 @@ -const host = 'http://127.0.0.1:7700'; +import { instance } from '../instance-initializers/application'; -export default function query(path, options) { - const url = `${host}/${path}`; - const defaultOptions = { - headers: { - 'Content-Type': 'application/json', - }, - }; +export default function query(path, options = {}) { + const session = instance.lookup('service:session'); + const { url, key } = session.data.authenticated; + const defaultHeaders = { 'Content-Type': 'application/json' }; + const headers = new Headers(Object.assign(defaultHeaders, options.headers)); - return fetch(url, Object.assign(defaultOptions, options)).then((response) => - response.json() + if (key) { + headers.append('Authorization', `Bearer ${key}`); + } + + const req = new Request( + new URL(path, url), + Object.assign(options, { headers }) ); + + return fetch(req).then((res) => res.json()); } diff --git a/tests/unit/instance-initializers/application-test.js b/tests/unit/instance-initializers/application-test.js new file mode 100644 index 0000000..4493ff4 --- /dev/null +++ b/tests/unit/instance-initializers/application-test.js @@ -0,0 +1,39 @@ +import Application from '@ember/application'; + +import config from 'meilisearch-admin/config/environment'; +import { initialize } from 'meilisearch-admin/instance-initializers/application'; +import { module, test } from 'qunit'; +import Resolver from 'ember-resolver'; +import { run } from '@ember/runloop'; + +module('Unit | Instance Initializer | application', function (hooks) { + hooks.beforeEach(function () { + this.TestApplication = class TestApplication extends Application { + modulePrefix = config.modulePrefix; + podModulePrefix = config.podModulePrefix; + Resolver = Resolver; + }; + + this.TestApplication.instanceInitializer({ + name: 'initializer under test', + initialize, + }); + + this.application = this.TestApplication.create({ + autoboot: false, + }); + + this.instance = this.application.buildInstance(); + }); + hooks.afterEach(function () { + run(this.instance, 'destroy'); + run(this.application, 'destroy'); + }); + + // TODO: Replace this with your real tests. + test('it works', async function (assert) { + await this.instance.boot(); + + assert.ok(true); + }); +});