diff --git a/src/lib/api.ts b/src/lib/api.ts new file mode 100644 index 00000000..5485d9b1 --- /dev/null +++ b/src/lib/api.ts @@ -0,0 +1,61 @@ +import { error, type NumericRange } from '@sveltejs/kit'; + +async function send({ + method, + path, + token, + data +}: { + method: string; + path: string; + token: string; + data?: any; //TODO: Change this +}) { + const opts: { method: string; headers: { [key: string]: string }; body?: string } = { + method, + headers: {} + }; + + if (data) { + opts.headers['Content-Type'] = 'application/json'; + opts.body = JSON.stringify(data); + } + + if (sessionStorage.token) { + token = sessionStorage.token; + } + + if (token) { + opts.headers['Authorization'] = `Token ${token}`; + } + + console.debug('fetching', `${window.location.origin}/${path}`, opts); + const res = await fetch(`${window.location.origin}${path}`, opts); + if (res.ok || res.status === 422) { + const text = await res.text(); + console.log('text', text); + try { + return JSON.parse(text); + } catch (e) { + return text; //TODO: Change this + } + } + + throw error(res.status as NumericRange<400, 599>, await res.text()); +} + +export function get(path: string, token: string) { + return send({ method: 'GET', path, token }); +} + +export function del(path: string, token: string) { + return send({ method: 'DELETE', path, token }); +} + +export function post(path: string, token: string, data: any) { + return send({ method: 'POST', path, token, data }); +} + +export function put(path: string, token: string, data: any) { + return send({ method: 'PUT', path, token, data }); +} diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index 714d87fa..77827b75 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -1,19 +1,57 @@ + + +
-
diff --git a/tests/lib/test.ts b/tests/lib/test.ts index 9c9bba6d..f17b9150 100644 --- a/tests/lib/test.ts +++ b/tests/lib/test.ts @@ -13,12 +13,6 @@ const routes = [ test.describe('Navigation', () => { routes.forEach((route) => { - test(`${route.path} page has expected heading`, async ({ page }) => { - await page.goto('/'); - const navItem = page.locator('#' + route.id); - await navItem.click(); - await expect(page.locator('.main-content>h1')).toHaveText(route.headerText); - }); test(`${route.path} navigation bar has correct active element`, async ({ page }) => { await page.goto(route.path);