diff --git a/packages/fetch/src/fetch.ts b/packages/fetch/src/fetch.ts index ea97ee485..a5d5644aa 100644 --- a/packages/fetch/src/fetch.ts +++ b/packages/fetch/src/fetch.ts @@ -1,4 +1,3 @@ -import { SapiomClient } from "@sapiom/core"; import { BaseSapiomIntegrationConfig, initializeSapiomClient, @@ -120,8 +119,14 @@ export function createFetch(config?: SapiomFetchConfig): typeof fetch { input: string | URL | Request, init?: RequestInit, ): Promise => { + const inputMetadata = + input instanceof Request ? (input as any).__sapiom : undefined; let request = new Request(input, init); + if (inputMetadata !== undefined) { + (request as any).__sapiom = inputMetadata; + } + const requestMetadata = (request as any).__sapiom || {}; const userMetadata = { ...defaultMetadata, ...requestMetadata }; @@ -138,10 +143,12 @@ export function createFetch(config?: SapiomFetchConfig): typeof fetch { const headers = new Headers(request.headers); headers.set("Sapiom-Identity", identityHeaders["Sapiom-Identity"]); request = new Request(request, { headers }); + (request as any).__sapiom = requestMetadata; } } request = await handleAuthorization(request, authConfig, defaultMetadata); + (request as any).__sapiom = requestMetadata; const startTime = Date.now(); let response: Response | null = null; diff --git a/packages/fetch/src/http-client.integration.test.ts b/packages/fetch/src/http-client.integration.test.ts index c91abb52c..75050cdce 100644 --- a/packages/fetch/src/http-client.integration.test.ts +++ b/packages/fetch/src/http-client.integration.test.ts @@ -549,6 +549,24 @@ describe("HTTP Client Integration Tests", () => { expect(mocks.create).not.toHaveBeenCalled(); expect(mocks.complete).not.toHaveBeenCalled(); }); + + it("should skip Sapiom when Request metadata sets enabled false", async () => { + fetchMock.get("https://api.example.com/public", { + status: 200, + body: { public: true }, + }); + + const fetch = createFetch({ sapiomClient: mockSapiomClient }); + const request = new Request("https://api.example.com/public"); + (request as any).__sapiom = { enabled: false }; + + const response = await fetch(request); + await flushPromises(); + + expect(response.status).toBe(200); + expect(mocks.create).not.toHaveBeenCalled(); + expect(mocks.complete).not.toHaveBeenCalled(); + }); }); // ============================================================================