Skip to content

fix(sdk): preserve injected custom HttpClient across LilySdk.withConfig (Closes #442) - #563

Merged
David-patrick-chuks-02 merged 1 commit into
Lilly-Protocol:mainfrom
foxxx009:fix/withconfig-injected-httpclient
Sep 6, 2026
Merged

fix(sdk): preserve injected custom HttpClient across LilySdk.withConfig (Closes #442)#563
David-patrick-chuks-02 merged 1 commit into
Lilly-Protocol:mainfrom
foxxx009:fix/withconfig-injected-httpclient

Conversation

@foxxx009

@foxxx009 foxxx009 commented Sep 6, 2026

Copy link
Copy Markdown
Contributor

Closes #442

Problem

LilySdk.withConfig() in src/sdk.ts:103-130 ends with return new LilySdk(merged) without passing this.httpClient. When the source SDK was constructed with a custom HttpClient, the derived instance silently drops it and rebuilds a default fetch client via createFetchHttpClient(this.config).

Note: naively passing this.httpClient unconditionally would trade this bug for the opposite one (issue #405): when no client was injected, the default fetch client captures the ORIGINAL baseUrl/credentials in its closure, so a derived instance's baseUrl/apiKey overrides would never reach the wire. This PR fixes #442 while preserving #405's routing semantics.

Changes

src/sdk.ts

  • New private field injectedHttpClient, set only when a client was explicitly passed to the constructor
  • withConfig() now returns new LilySdk(merged, this.injectedHttpClient) when a client was injected, and new LilySdk(merged) otherwise — the default fetch client is rebuilt from the merged config

tests/sdk-withConfig.test.ts

  • preserves an injected custom HttpClient across withConfig (issue #442): child created via sdk.withConfig({ apiKey: 'tenant2' }) exposes httpClient === mockHttpClient and routes an actual request through it (request recorded, response returned)
  • rebuilds the default fetch client when none was injected (issue #405 semantics): a derived instance sends to the overridden baseUrl (tracked via injected fetch), not the original host

Verification

  • All 3 withConfig test files: 13/13 pass (2 new regression tests)
  • npm run typecheck — clean
  • npm run test:unit — 875 passed, only the 4 pre-existing Windows path-resolution failures remain (zero new failures vs. baseline)
  • prettier --check and eslint clean on both touched files

withConfig() dropped the caller-injected HttpClient: it always returned
new LilySdk(merged), so children of an SDK built with a custom transport
silently rebuilt a default fetch client from createFetchHttpClient.

The fix distinguishes the two transports:
- an explicitly injected HttpClient is shared with the derived instance,
  so custom transport behavior survives withConfig (issue Lilly-Protocol#442)
- the default fetch client is NOT shared: it is rebuilt from the merged
  config so baseUrl/credential overrides are captured in the transport
  closure and actually reach the wire (issue Lilly-Protocol#405 semantics)

Adds two regression tests: a child routes requests through the parent's
injected client (request invoked, response returned), and a derived
instance built without an injected client sends to the overridden
baseUrl instead of the original host.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bounty: $85] Preserve an injected custom HttpClient across LilySdk.withConfig

2 participants