Skip to content

Commit 3046552

Browse files
Alex-Tidemanrossnelson
authored andcommitted
Move getRoutePrefix to coreProvider
1 parent e336604 commit 3046552

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,
@@ -277,7 +276,10 @@ describe('routeFor functions with prefix should resolve base + prefix correctly'
277276
};
278277

279278
afterEach(() => {
280-
routePrefix.set('');
279+
initCoreProvider({
280+
getAccessToken: async () => '',
281+
getRoutePrefix: () => '',
282+
});
281283
});
282284

283285
const prefixedCases: [string, () => string | undefined][] = [
@@ -409,7 +411,10 @@ describe('routeFor functions with prefix should resolve base + prefix correctly'
409411
it.each(prefixedCases)(
410412
'%s should include base + prefix when prefix is set',
411413
(_name, fn) => {
412-
routePrefix.set(prefix);
414+
initCoreProvider({
415+
getAccessToken: async () => '',
416+
getRoutePrefix: () => prefix,
417+
});
413418
const result = fn();
414419
expect(typeof result).toBe('string');
415420
expect(result).toMatch(new RegExp(`^${base}${prefix}`));
@@ -422,7 +427,10 @@ describe('routeFor functions with prefix should resolve base + prefix correctly'
422427
it.each(authCases)(
423428
'%s should NOT include prefix (auth routes excluded)',
424429
(_name, fn) => {
425-
routePrefix.set(prefix);
430+
initCoreProvider({
431+
getAccessToken: async () => '',
432+
getRoutePrefix: () => prefix,
433+
});
426434
const result = fn();
427435
expect(typeof result).toBe('string');
428436
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,
@@ -567,31 +567,30 @@ describe('routeFor worker deployment version and serverless routes', () => {
567567
describe('routeFor with prefix', () => {
568568
const prefix = '/projects/my-project';
569569

570-
afterEach(() => {
571-
routePrefix.set('');
570+
beforeEach(() => {
571+
initCoreProvider({
572+
getAccessToken: async () => '',
573+
getRoutePrefix: () => prefix,
574+
});
572575
});
573576

574577
it('should prepend prefix to root route', () => {
575-
routePrefix.set(prefix);
576578
expect(routeForNamespaces()).toBe(`${base}${prefix}/namespaces`);
577579
});
578580

579581
it('should prepend prefix to namespace route', () => {
580-
routePrefix.set(prefix);
581582
expect(routeForNamespace({ namespace: 'default' })).toBe(
582583
`${base}${prefix}/namespaces/default`,
583584
);
584585
});
585586

586587
it('should propagate prefix through leaf functions', () => {
587-
routePrefix.set(prefix);
588588
expect(routeForWorkflows({ namespace: 'default' })).toBe(
589589
`${base}${prefix}/namespaces/default/workflows`,
590590
);
591591
});
592592

593593
it('should propagate prefix through deep leaf functions', () => {
594-
routePrefix.set(prefix);
595594
expect(
596595
routeForCallStack({
597596
namespace: 'default',
@@ -602,40 +601,41 @@ describe('routeFor with prefix', () => {
602601
});
603602

604603
it('should propagate prefix to nexus routes', () => {
605-
routePrefix.set(prefix);
606604
expect(routeForNexus()).toBe(`${base}${prefix}/nexus`);
607605
});
608606

609607
it('should propagate prefix to schedule routes', () => {
610-
routePrefix.set(prefix);
611608
expect(routeForSchedules({ namespace: 'default' })).toBe(
612609
`${base}${prefix}/namespaces/default/schedules`,
613610
);
614611
});
615612

616613
it('should not apply prefix when store is empty', () => {
617-
routePrefix.set('');
614+
initCoreProvider({
615+
getAccessToken: async () => '',
616+
getRoutePrefix: () => '',
617+
});
618618
expect(routeForNamespaces()).toBe(`${base}/namespaces`);
619619
});
620620

621621
it('should not apply prefix to auth routes', () => {
622-
routePrefix.set(prefix);
623622
const settings = { auth: {}, baseUrl: 'https://localhost' };
624623
const searchParams = new URLSearchParams();
625624
const sso = routeForAuthentication({ settings, searchParams });
626625
expect(sso).not.toContain(prefix);
627626
});
628627

629628
it('should not apply prefix to login page', () => {
630-
routePrefix.set(prefix);
631629
const login = routeForLoginPage('', false);
632630
expect(login).not.toContain(prefix);
633631
});
634632

635633
it('should revert to default behavior when prefix is cleared', () => {
636-
routePrefix.set(prefix);
637634
expect(routeForNamespaces()).toBe(`${base}${prefix}/namespaces`);
638-
routePrefix.set('');
635+
initCoreProvider({
636+
getAccessToken: async () => '',
637+
getRoutePrefix: () => '',
638+
});
639639
expect(routeForNamespaces()).toBe(`${base}/namespaces`);
640640
});
641641
});

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)