Skip to content

Commit 74c4ba1

Browse files
committed
Move getRoutePrefix to coreProvider
1 parent f587f74 commit 74c4ba1

6 files changed

Lines changed: 39 additions & 26 deletions

File tree

src/hooks.client.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ consumeAuthCookies();
2424
initCoreProvider({
2525
getAccessToken: async () => getAuthUser().accessToken ?? '',
2626
getIdToken: async () => getAuthUser().idToken,
27+
getRoutePrefix: () => '',
2728
api: {
2829
preRequest: ossPreRequest,
2930
postResponse: ossPostResponse,

src/lib/stores/route-prefix.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/lib/utilities/core-provider.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export type PostResponseHook = (
1717
export type CoreProvider = {
1818
getAccessToken: () => Promise<string>;
1919
getIdToken: () => Promise<string | undefined>;
20+
getRoutePrefix: () => string;
2021
api: {
2122
preRequest: PreRequestHook;
2223
postResponse: PostResponseHook;
@@ -28,6 +29,7 @@ export type CoreProvider = {
2829
export type InitOptions = {
2930
getAccessToken: () => Promise<string>;
3031
getIdToken?: () => Promise<string | undefined>;
32+
getRoutePrefix?: () => string;
3133
api?: {
3234
preRequest?: PreRequestHook;
3335
postResponse?: PostResponseHook;
@@ -45,6 +47,7 @@ export function initCoreProvider(options: InitOptions): void {
4547
provider = {
4648
getAccessToken: options.getAccessToken,
4749
getIdToken: options.getIdToken ?? (async () => undefined),
50+
getRoutePrefix: options.getRoutePrefix ?? (() => ''),
4851
api: {
4952
preRequest: options.api?.preRequest ?? passthrough,
5053
postResponse: options.api?.postResponse ?? passthroughResponse,
@@ -64,6 +67,11 @@ export async function getIdToken(): Promise<string | undefined> {
6467
return provider.getIdToken();
6568
}
6669

70+
export function getRoutePrefix(): string {
71+
if (!BROWSER || !provider) return '';
72+
return provider.getRoutePrefix();
73+
}
74+
6775
export async function getDataEncoderEndpoint(
6876
namespace: string,
6977
): Promise<string> {

src/lib/utilities/route-for-base-path.test.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import { afterEach, describe, expect, it } from 'vitest';
22

33
import { base } from '$app/paths';
44

5-
import { routePrefix } from '$lib/stores/route-prefix';
6-
5+
import { initCoreProvider } from './core-provider';
76
import * as routeForModule from './route-for';
87
import {
98
routeForArchivalEventHistory,
@@ -237,7 +236,10 @@ describe('routeFor functions with prefix should resolve base + prefix correctly'
237236
};
238237

239238
afterEach(() => {
240-
routePrefix.set('');
239+
initCoreProvider({
240+
getAccessToken: async () => '',
241+
getRoutePrefix: () => '',
242+
});
241243
});
242244

243245
const prefixedCases: [string, () => string | undefined][] = [
@@ -369,7 +371,10 @@ describe('routeFor functions with prefix should resolve base + prefix correctly'
369371
it.each(prefixedCases)(
370372
'%s should include base + prefix when prefix is set',
371373
(_name, fn) => {
372-
routePrefix.set(prefix);
374+
initCoreProvider({
375+
getAccessToken: async () => '',
376+
getRoutePrefix: () => prefix,
377+
});
373378
const result = fn();
374379
expect(typeof result).toBe('string');
375380
expect(result).toMatch(new RegExp(`^${base}${prefix}`));
@@ -382,7 +387,10 @@ describe('routeFor functions with prefix should resolve base + prefix correctly'
382387
it.each(authCases)(
383388
'%s should NOT include prefix (auth routes excluded)',
384389
(_name, fn) => {
385-
routePrefix.set(prefix);
390+
initCoreProvider({
391+
getAccessToken: async () => '',
392+
getRoutePrefix: () => prefix,
393+
});
386394
const result = fn();
387395
expect(typeof result).toBe('string');
388396
expect(result).not.toContain(prefix);

src/lib/utilities/route-for.test.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { writable } from 'svelte/store';
22

3-
import { afterEach, describe, expect, it, vi } from 'vitest';
3+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
44

55
import { base } from '$app/paths';
66

77
import type {
88
EventSortOrder,
99
WorkflowViewPreference,
1010
} from '$lib/stores/event-view';
11-
import { routePrefix } from '$lib/stores/route-prefix';
1211

12+
import { initCoreProvider } from './core-provider';
1313
import {
1414
baseRouteForWorkflow,
1515
hasParameters,
@@ -536,31 +536,30 @@ describe('routeForWorkflow', () => {
536536
describe('routeFor with prefix', () => {
537537
const prefix = '/projects/my-project';
538538

539-
afterEach(() => {
540-
routePrefix.set('');
539+
beforeEach(() => {
540+
initCoreProvider({
541+
getAccessToken: async () => '',
542+
getRoutePrefix: () => prefix,
543+
});
541544
});
542545

543546
it('should prepend prefix to root route', () => {
544-
routePrefix.set(prefix);
545547
expect(routeForNamespaces()).toBe(`${base}${prefix}/namespaces`);
546548
});
547549

548550
it('should prepend prefix to namespace route', () => {
549-
routePrefix.set(prefix);
550551
expect(routeForNamespace({ namespace: 'default' })).toBe(
551552
`${base}${prefix}/namespaces/default`,
552553
);
553554
});
554555

555556
it('should propagate prefix through leaf functions', () => {
556-
routePrefix.set(prefix);
557557
expect(routeForWorkflows({ namespace: 'default' })).toBe(
558558
`${base}${prefix}/namespaces/default/workflows`,
559559
);
560560
});
561561

562562
it('should propagate prefix through deep leaf functions', () => {
563-
routePrefix.set(prefix);
564563
expect(
565564
routeForCallStack({
566565
namespace: 'default',
@@ -571,40 +570,41 @@ describe('routeFor with prefix', () => {
571570
});
572571

573572
it('should propagate prefix to nexus routes', () => {
574-
routePrefix.set(prefix);
575573
expect(routeForNexus()).toBe(`${base}${prefix}/nexus`);
576574
});
577575

578576
it('should propagate prefix to schedule routes', () => {
579-
routePrefix.set(prefix);
580577
expect(routeForSchedules({ namespace: 'default' })).toBe(
581578
`${base}${prefix}/namespaces/default/schedules`,
582579
);
583580
});
584581

585582
it('should not apply prefix when store is empty', () => {
586-
routePrefix.set('');
583+
initCoreProvider({
584+
getAccessToken: async () => '',
585+
getRoutePrefix: () => '',
586+
});
587587
expect(routeForNamespaces()).toBe(`${base}/namespaces`);
588588
});
589589

590590
it('should not apply prefix to auth routes', () => {
591-
routePrefix.set(prefix);
592591
const settings = { auth: {}, baseUrl: 'https://localhost' };
593592
const searchParams = new URLSearchParams();
594593
const sso = routeForAuthentication({ settings, searchParams });
595594
expect(sso).not.toContain(prefix);
596595
});
597596

598597
it('should not apply prefix to login page', () => {
599-
routePrefix.set(prefix);
600598
const login = routeForLoginPage('', false);
601599
expect(login).not.toContain(prefix);
602600
});
603601

604602
it('should revert to default behavior when prefix is cleared', () => {
605-
routePrefix.set(prefix);
606603
expect(routeForNamespaces()).toBe(`${base}${prefix}/namespaces`);
607-
routePrefix.set('');
604+
initCoreProvider({
605+
getAccessToken: async () => '',
606+
getRoutePrefix: () => '',
607+
});
608608
expect(routeForNamespaces()).toBe(`${base}/namespaces`);
609609
});
610610
});

src/lib/utilities/route-for.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ import {
99
eventFilterSort,
1010
workflowViewPreference,
1111
} from '$lib/stores/event-view';
12-
import { getRoutePrefix } from '$lib/stores/route-prefix';
1312
import type { EventView } from '$lib/types/events';
1413
import type { Settings } from '$lib/types/global';
1514
import { encodeURIForSvelte } from '$lib/utilities/encode-uri';
1615
import { toURL } from '$lib/utilities/to-url';
1716

17+
import { getRoutePrefix } from './core-provider';
18+
1819
const withPrefix = (path: ResolvedPathname): ResolvedPathname => {
1920
const prefix = getRoutePrefix();
2021
if (!prefix) return path;

0 commit comments

Comments
 (0)