Add session parametrs to query

This commit is contained in:
Stanislav Romanov 2022-08-20 15:47:02 +03:00
parent 22f442ac70
commit 9298ca4e98
4 changed files with 64 additions and 11 deletions

View file

@ -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 }));

View file

@ -0,0 +1,9 @@
export let instance;
export function initialize(owner) {
instance = owner;
}
export default {
initialize,
};

View file

@ -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());
}

View file

@ -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);
});
});