feat(network-activity-plugin): Override and mock network responses#116
feat(network-activity-plugin): Override and mock network responses#116V3RON merged 10 commits intocallstackincubator:mainfrom
Conversation
|
@IanLuan is attempting to deploy a commit to the Callstack Team on Vercel. A member of the Team first needs to authorize it. |
There was a problem hiding this comment.
Let's split the implementation into two components:
a) one for showing up real responses
b) one for showing up overrides
When user clicks 'override', the second component will be shown.
There was a problem hiding this comment.
Moved override logic into a new component – 79695a6 . Let me know if you think I should change anything.
| }), | ||
| { | ||
| name: 'rozenite-network-activity-storage', | ||
| storage: createJSONStorage(() => localStorage, { |
There was a problem hiding this comment.
Let's add some sort of version control system, so if we change the state in the future, we will have the option to purge the local state and replace it with the new one. A simple STORE_VERSION constant and a __version property should be sufficient. If there is a mismatch, we should ignore the persisted state.
| return; | ||
| } | ||
|
|
||
| request.addEventListener('readystatechange', () => { |
There was a problem hiding this comment.
Isn't handleRequestOverride already called in readystatechange? Should we verify that readyState has a specific value?
There was a problem hiding this comment.
You're right, I moved the overrideCallback invocation outside of event listener. It's on the same level as sendCallback (da64858)
There was a problem hiding this comment.
This PR is being reviewed by Cursor Bugbot
Details
Your team is on the Bugbot Free tier. On this plan, Bugbot will review limited PRs each billing cycle for each member of your team.
To receive Bugbot reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.
| request.status = override.status; | ||
| } | ||
| }); | ||
| }; |
There was a problem hiding this comment.
Bug: Inefficient Handling and Incorrect JSON Parsing
The handleRequestOverride function's readystatechange listener applies overrides on every state change, causing inefficient, repeated property assignments and potential timing issues. Additionally, when responseType is 'json', request.response is incorrectly assigned a string instead of a parsed JSON object, which violates the XMLHttpRequest specification.
| value={editedStatus} | ||
| onChange={(e) => { | ||
| setEditedStatus(parseInt(e.target.value)); | ||
| }} |
There was a problem hiding this comment.
Bug: Override Component Fails with Invalid Status Codes
The OverrideResponse component saves NaN as the override status if the status code input is empty or contains non-numeric characters. This occurs because parseInt returns NaN in these cases, which can lead to unexpected behavior when applying the network override.
Override and mock network responses
This feature is inspired by Chrome DevTools overrides. It allows developers to edit the response and see how their app behaves, without changing the backend. Currently, it’s possible to override the response body and status.
Demo:
Override.network.compressed.mp4
How it works
The DevTools and the app both store the overrides. Every time a response is overridden, the DevTools send it to the app. The overrides list is persisted in the DevTools’ local storage, so it survives app reloads. On the DevTools side, it’s stored in Zustand store; in the app, it’s stored in a registry. If a request has an override, the response body and status are monkey-patched during XMLHttpRequest interception. On load, the DevTools send all overrides stored in local storage to the app, keeping the data in sync.
Note
Adds editable, persisted HTTP response overrides (status/body) in DevTools that sync to the app and are applied via XHR interception at runtime.
RequestOverridetype andset-overridesevent inshared/client.overrides-registryto store URL→override mappings.XHRInterceptorwithsetOverrideCallbackand invoke before listeners.network-inspectoruses overrides to monkey‑patchXMLHttpRequestresponse/status andresponseTypebased onContent-Type.useNetworkActivityDevToolslistens forset-overridesto update registry.overrides(Map) tolocalStorageand exposesaddOverride/clearOverrideactions; sendsset-overridesto app.InspectorViewsends current overrides on setup and when overrides change.CodeEditorandOverrideResponsecomponents for editing overrides.ResponseTabintegrates override workflow (edit/save/clear) and action slot via updatedSection.RequestListandSidePanelindicate when a request/response has an override.Written by Cursor Bugbot for commit da64858. This will update automatically on new commits. Configure here.