Skip to content

Commit

Permalink
updated ApplicationAdapter to intercept payload and decompress json…
Browse files Browse the repository at this point in the history
… if compressed json provided
  • Loading branch information
roncodes committed Mar 11, 2024
1 parent a3116fe commit 4e0ffff
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions addon/adapters/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,22 +162,38 @@ export default class ApplicationAdapter extends RESTAdapter {
* It then checks if the response is invalid based on the status code. If invalid, it constructs an `AdapterError` with the normalized errors and detailed message.
* For valid responses, it delegates the handling to the superclass's `handleResponse` method.
*/
handleResponse(status, headers, payload) {
async handleResponse(status, headers, payload, requestData) {
let decompressedPayload = this.decompressPayload(payload, headers);
let errors = this.normalizeErrorResponse(status, headers, payload);
let detailedMessage = this.generatedDetailedMessage(...arguments);

if (this.isInvalid(status, headers, payload)) {
return new AdapterError(errors, detailedMessage);
return new AdapterError(errors);
}

return super.handleResponse(status, headers, decompressedPayload, requestData);
}

/**
* Decompresses the response payload if it's marked as compressed in the response headers.
*
* This method checks the response headers for a specific 'x-compressed-json' flag.
* If this flag is set, indicating that the response payload is compressed, the method
* decompresses the payload. The decompressed payload is then parsed as JSON and returned.
* If the payload is not compressed, it is returned as is.
*
* @param {object} payload - The original payload of the response.
* @param {object} headers - The headers of the response, used to check if the payload is compressed.
* @return {object} The decompressed payload if it was compressed, or the original payload otherwise.
*/
async decompressPayload(payload, headers) {
// Check if the response is compressed
if (headers['x-compressed-json'] === '1') {
if (headers['x-compressed-json'] === '1' || headers['x-compressed-json'] === 1) {
// Decompress the payload
const decompressedPayload = decompressJson(payload);
// Replace payload with decompressed json payload
payload = JSON.parse(decompressedPayload);
console.log('decompressed json payload', payload);
}

return super.handleResponse(...arguments);
return payload;
}
}

0 comments on commit 4e0ffff

Please sign in to comment.