Skip to content

fix(fetch): preserve __sapiom metadata across internal Request cloning - #747

Open
ygd58 wants to merge 1 commit into
sapiom:mainfrom
ygd58:fix/sapiom-metadata-clone-loss
Open

fix(fetch): preserve __sapiom metadata across internal Request cloning#747
ygd58 wants to merge 1 commit into
sapiom:mainfrom
ygd58:fix/sapiom-metadata-clone-loss

Conversation

@ygd58

@ygd58 ygd58 commented Aug 30, 2026

Copy link
Copy Markdown

Fixes #690

sapiomFetch reconstructs the request via new Request(input, init) before reading __sapiom. Native Request cloning does not preserve custom properties, so a __sapiom override set on the original Request was dropped before it could be read - the documented per-request bypass (e.g. request.__sapiom = { enabled: false }) silently had no effect.

Fix: __sapiom is now captured from the original input before cloning and re-attached to the clone, including after the identity-header re-wrap later in the same function (the same class of loss, one function down).

Scope: deliberately limited to the reported reproduction and this identical pattern within sapiomFetch itself. handleAuthorization (interceptors.ts) does its own internal Request re-wrap before returning, which could still drop __sapiom for a caller relying on it past the authorization step (e.g. at payment-retry time) - left as a separate, deeper propagation concern rather than folded into this fix, since it wasn't part of the reported repro.

Tests: added a regression test verifying the enabled:false bypass calls globalThis.fetch directly (skipping authorization) when __sapiom is set on a Request input. Verified it fails against the pre-fix code (hits the unmocked authorization path and throws) and passes against the fix.

Changeset: added (@sapiom/fetch, patch).

Verified locally: pnpm --filter @sapiom/fetch test - 44/44 passing (full package), eslint clean (the one pre-existing warning in fetch.ts is unrelated to this diff).

Fixes sapiom#690

sapiomFetch reconstructs the request via new Request(input, init)
before reading __sapiom. Native Request cloning does not preserve
custom properties, so a __sapiom override set on the original Request
was dropped before it could be read - the documented per-request
bypass (e.g. request.__sapiom = { enabled: false }) silently had no
effect.

__sapiom is now captured from the original input before cloning and
re-attached to the clone, including after the identity-header re-wrap
later in the same function (the same class of loss, one function
down).

Deliberately scoped to the reported reproduction and this identical
pattern within sapiomFetch itself. handleAuthorization
(interceptors.ts) does its own internal Request re-wrap before
returning, which could still drop __sapiom for a caller relying on it
past the authorization step (e.g. at payment-retry time) - left as a
separate, deeper propagation concern rather than folded into this fix.

Added a regression test verifying the enabled:false bypass calls
globalThis.fetch directly (skipping authorization) when __sapiom is
set on a Request input. Verified it fails against the pre-fix code
(hits the unmocked authorization path and throws) and passes against
the fix.

pnpm --filter @sapiom/fetch test - 44/44 passing (full package).
Changeset added (patch, @sapiom/fetch).
@github-actions github-actions Bot added contribution: incomplete Required pull request information is incomplete or ambiguous contributor: external Pull request author does not have write, maintain, or admin access to sapiom-js needs-triage Awaiting maintainer review and classification review: manual External pull request requires maintainer review before automation size: small Review size is at most 100 changed lines area: integrations Changes to HTTP or framework integrations labels Aug 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: integrations Changes to HTTP or framework integrations contribution: incomplete Required pull request information is incomplete or ambiguous contributor: external Pull request author does not have write, maintain, or admin access to sapiom-js needs-triage Awaiting maintainer review and classification review: manual External pull request requires maintainer review before automation size: small Review size is at most 100 changed lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: @sapiom/fetch loses Request.__sapiom metadata when cloning input

1 participant