Skip to content

Commit 3229447

Browse files
committed
fix(core): compose nested withAuth by merging from enclosing scope
`runWithConfig` was building its merged config from the process-wide global, not from the enclosing ALS scope. That broke the documented `auth.withAuth(...)` + `auth.withPublicToken(...)` composition: the inner `withAuth` (called by withPublicToken internally) silently dropped the outer scope's baseURL/branch overrides. Read from the active scope first, fall back to the global, then merge in the new config. Pre-existing concurrency-safety (parallel scopes) holds. New test covers the nested-composition case.
1 parent 5968d6a commit 3229447

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

packages/core/src/v3/apiClientManager/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,8 @@ export class APIClientManagerAPI {
120120
config: ApiClientConfiguration,
121121
fn: R
122122
): Promise<ReturnType<R>> {
123-
const merged = this.resolveApiClientConfig({ ...this.#getConfig(), ...config });
123+
const current = sdkScope.getStore()?.apiClientConfig ?? this.#getConfig();
124+
const merged = this.resolveApiClientConfig({ ...current, ...config });
124125

125126
if (sdkScope.hasStorage()) {
126127
return sdkScope.withScope({ apiClientConfig: merged, inheritContext: true }, fn);

packages/trigger-sdk/src/v3/triggerClient.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,22 @@ describe("auth.withAuth", () => {
241241
).toBe("https://override.example.com");
242242
});
243243

244+
it("composes nested withAuth: outer-scope fields flow into the inner scope", async () => {
245+
vi.stubEnv("TRIGGER_SECRET_KEY", "tr_env_token");
246+
247+
let observedBaseURL: string | undefined;
248+
let observedAuth: string | undefined;
249+
await auth.withAuth({ baseURL: "https://outer.example.com" }, async () => {
250+
await auth.withAuth({ accessToken: "tr_inner_token" }, async () => {
251+
observedBaseURL = apiClientManager.baseURL;
252+
observedAuth = apiClientManager.accessToken;
253+
});
254+
});
255+
256+
expect(observedBaseURL).toBe("https://outer.example.com");
257+
expect(observedAuth).toBe("tr_inner_token");
258+
});
259+
244260
it("does not stomp on a parallel withAuth call with a different config", async () => {
245261
configure({ accessToken: "tr_global" });
246262

0 commit comments

Comments
 (0)