SalesforceCommerceCloud / commerce-sdk-isomorphic

Browser & Node.js JavaScript client for B2C Commerce API
https://salesforcecommercecloud.github.io/commerce-sdk-isomorphic/
BSD 3-Clause "New" or "Revised" License
43 stars 21 forks source link

[Feature] Allow providing your own implementation of `fetch` #159

Open johnboxall opened 5 months ago

johnboxall commented 5 months ago

Application Performance Monitoring frameworks often want to spy on HTTP requests being made.

To to this, they typically need to monkey patch / override fetch.

Currently its tricky to do this:

https://sfcc-unofficial.slack.com/archives/C01AWMT7GSZ/p1717714138887519

Ideally, we'd provide some way of providing fetch implementation, potentially as an additional param when creating an API client.

clavery commented 5 months ago

This would also be useful for general request/response interception use cases (headers, logging, etc).

opepin commented 5 months ago

followed @clavery suggestion

var myFetch: typeof globalObject.fetch; return function fetch() { if (!myFetch) { myFetch = globalObject.fetch; } // @ts-ignore return myFetch.apply(globalObject, arguments) }

Fetched can is now instrumented as expected and addressees missing requests https://docs.logrocket.com/docs/troubleshooting-sessions#network-requests-are-missing-or-not-being-recorded