Better Access to Response Headers
Problem
On a successful API request the Stripe Node SDK doesn't provide a natural way of accessing response headers which can be useful in the case you're interested in response metadata (such as `Idempotency-Key` and `Request-ID`. For example today it is possible to do this but it is a little bit awkward since response metadata is exposed through an event emitter interface, while the response body is exposed through a callback interface: [code block] It would be a much better experience if this data was passed in the callback like so: [code block] My idea is that we add headers, statusCode and known Stripe headers such as `idempotencyKey` to go along with `requestId` to StripeResource here Thoughts?
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Enhance Stripe Node SDK to Include Response Headers in Callbacks
The current implementation of the Stripe Node SDK separates response body handling via callbacks and response metadata handling via event emitters. This design leads to an awkward experience for developers who need to access both response data and headers simultaneously, particularly for important metadata like 'Idempotency-Key' and 'Request-ID'.
Awaiting Verification
Be the first to verify this fix
- 1
Modify StripeResource to Include Headers
Update the StripeResource class to include response headers in the callback function. This will involve modifying the existing callback signature to accept an additional parameter for headers.
typescriptcallback(null, responseBody, { headers, statusCode, idempotencyKey, requestId }); - 2
Update API Call Methods
Adjust all API call methods within the SDK that utilize the callback pattern to ensure they pass the new headers and status code to the callback. This will ensure consistency across the SDK.
typescriptthis.request('POST', '/endpoint', data, (err, responseBody) => { callback(err, responseBody, { headers: response.headers, statusCode: response.statusCode, idempotencyKey: response.headers['Idempotency-Key'], requestId: response.headers['Request-ID'] }); }); - 3
Update Documentation
Revise the SDK documentation to reflect the changes made to the callback interface. Include examples demonstrating how to access the new response headers and status code.
typescript// Example usage: apiCall((err, responseBody, { headers, statusCode, idempotencyKey, requestId }) => { console.log(headers); }); - 4
Run Unit Tests
Create or update unit tests to verify that the new headers and status code are correctly passed to the callback. Ensure that existing tests are not broken by the changes.
typescriptexpect(callback).toHaveBeenCalledWith(null, responseBody, { headers, statusCode, idempotencyKey, requestId });
Validation
Confirm the fix by running the updated SDK and making a sample API call. Check that the callback receives the response body along with headers and status code as expected. Additionally, run all unit tests to ensure they pass without errors.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep