Skip to content

Commit 51bcf9f

Browse files
committed
refactor: extract loader setup wrapper
1 parent 534f795 commit 51bcf9f

3 files changed

Lines changed: 64 additions & 5 deletions

File tree

index.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ import { applyAccountStorageScopeEntry } from "./lib/runtime/account-storage-sco
211211
import { runBrowserOAuthFlow } from "./lib/runtime/browser-oauth-flow.js";
212212
import { handleRuntimeEvent } from "./lib/runtime/event-handler.js";
213213
import { ensureLiveAccountSyncEntry } from "./lib/runtime/live-sync-entry.js";
214+
import { applyLoaderRuntimeSetup } from "./lib/runtime/loader-setup.js";
214215
import { buildManualOAuthFlow } from "./lib/runtime/manual-oauth-flow.js";
215216
import {
216217
applyPreemptiveQuotaSettingsFromConfig,
@@ -691,11 +692,15 @@ export const OpenAIOAuthPlugin: Plugin = async ({ client }: PluginInput) => {
691692
async loader(getAuth: () => Promise<Auth>, provider: unknown) {
692693
const auth = await getAuth();
693694
const pluginConfig = loadPluginConfig();
694-
applyUiRuntimeFromConfig(pluginConfig, setUiRuntimeOptions);
695-
applyAccountStorageScope(pluginConfig);
696-
ensureSessionAffinity(pluginConfig);
697-
ensureRefreshGuardian(pluginConfig);
698-
applyPreemptiveQuotaSettings(pluginConfig);
695+
applyLoaderRuntimeSetup({
696+
pluginConfig,
697+
applyUiRuntimeFromConfig: (config) =>
698+
applyUiRuntimeFromConfig(config, setUiRuntimeOptions),
699+
applyAccountStorageScope,
700+
ensureSessionAffinity,
701+
ensureRefreshGuardian,
702+
applyPreemptiveQuotaSettings,
703+
});
699704

700705
// Only handle OAuth auth type, skip API key auth
701706
if (auth.type !== "oauth") {

lib/runtime/loader-setup.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export function applyLoaderRuntimeSetup(params: {
2+
pluginConfig: ReturnType<typeof import("../config.js").loadPluginConfig>;
3+
applyUiRuntimeFromConfig: (
4+
pluginConfig: ReturnType<typeof import("../config.js").loadPluginConfig>,
5+
) => void;
6+
applyAccountStorageScope: (
7+
pluginConfig: ReturnType<typeof import("../config.js").loadPluginConfig>,
8+
) => void;
9+
ensureSessionAffinity: (
10+
pluginConfig: ReturnType<typeof import("../config.js").loadPluginConfig>,
11+
) => void;
12+
ensureRefreshGuardian: (
13+
pluginConfig: ReturnType<typeof import("../config.js").loadPluginConfig>,
14+
) => void;
15+
applyPreemptiveQuotaSettings: (
16+
pluginConfig: ReturnType<typeof import("../config.js").loadPluginConfig>,
17+
) => void;
18+
}): void {
19+
params.applyUiRuntimeFromConfig(params.pluginConfig);
20+
params.applyAccountStorageScope(params.pluginConfig);
21+
params.ensureSessionAffinity(params.pluginConfig);
22+
params.ensureRefreshGuardian(params.pluginConfig);
23+
params.applyPreemptiveQuotaSettings(params.pluginConfig);
24+
}

test/loader-setup.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { describe, expect, it, vi } from "vitest";
2+
import { applyLoaderRuntimeSetup } from "../lib/runtime/loader-setup.js";
3+
4+
describe("loader runtime setup", () => {
5+
it("applies runtime setup steps in the existing order", () => {
6+
const calls: string[] = [];
7+
const pluginConfig = { a: 1 } as never;
8+
9+
applyLoaderRuntimeSetup({
10+
pluginConfig,
11+
applyUiRuntimeFromConfig: () => {
12+
calls.push("ui");
13+
},
14+
applyAccountStorageScope: () => {
15+
calls.push("scope");
16+
},
17+
ensureSessionAffinity: () => {
18+
calls.push("session");
19+
},
20+
ensureRefreshGuardian: () => {
21+
calls.push("guardian");
22+
},
23+
applyPreemptiveQuotaSettings: () => {
24+
calls.push("quota");
25+
},
26+
});
27+
28+
expect(calls).toEqual(["ui", "scope", "session", "guardian", "quota"]);
29+
});
30+
});

0 commit comments

Comments
 (0)