Skip to content

Commit

Permalink
fix(transformers): xhr requests incorrectly parsing JSON response types
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael committed Nov 25, 2022
1 parent 6eea194 commit c75376b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
5 changes: 4 additions & 1 deletion lib/core/api/xhr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ export class BrowserRequest implements UrbexRequestApi {

request.open(uppercase(config.method), config.url.href, true);

if (BROWSER_RESPONSE_TYPES.includes(config.responseType)) {
if (
BROWSER_RESPONSE_TYPES.includes(config.responseType) &&
config.responseType !== "json"
) {
request.responseType = config.responseType as BrowserResponseTypes;
}

Expand Down
5 changes: 3 additions & 2 deletions lib/core/request-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ export class RequestConfig {
this.set(this.createConfigurationObject(config, true));
}

this.$config.pipelines.request.unshift(transformRequestData);
this.$config.pipelines.request.push(transformRequestData);
this.$config.pipelines.response.push(transformResponseData);

if (environment.isNode) {
this.$config.pipelines.response.unshift(decodeResponseData, transformResponseData);
this.$config.pipelines.response.unshift(decodeResponseData);
}
}

Expand Down
17 changes: 9 additions & 8 deletions lib/core/transformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const transformRequestData = new PipelineExecutor<RequestExecutor>((confi
});

export const decodeResponseData = new PipelineExecutor<ResponseExecutor>(async (response) => {
const responseType = response.config.responseType;
const { responseType, maxContentLength } = response.config;

if (responseType === "raw" || responseType === "stream") {
return Promise.resolve(response);
Expand All @@ -24,7 +24,6 @@ export const decodeResponseData = new PipelineExecutor<ResponseExecutor>(async (

if (decoder) {
const decompressed = await decoder(response.data);
const maxContentLength = response.config.maxContentLength;

if (maxContentLength > -1 || maxContentLength !== Infinity) {
if (decompressed.length > maxContentLength) {
Expand All @@ -45,23 +44,25 @@ export const decodeResponseData = new PipelineExecutor<ResponseExecutor>(async (
});

export const transformResponseData = new PipelineExecutor<ResponseExecutor>((response) => {
const responseType = response.config.responseType;
const { responseType, responseEncoding } = response.config;

if (responseType === "raw" || responseType === "arraybuffer" || responseType === "stream") {
return Promise.resolve(response);
}

if (response.data) {
// https://stackoverflow.com/questions/24356713/node-js-readfile-error-with-utf8-encoded-file-on-windows
let data = response.data;

const { responseEncoding, responseType } = response.config;
if (environment.isNode) {
// https://stackoverflow.com/questions/24356713/node-js-readfile-error-with-utf8-encoded-file-on-windows

const bufferString = response.data.toString(responseEncoding);
data = response.data.toString(responseEncoding);
}

if (responseType === "json") {
response.data = safeJSONParse(bufferString, true);
response.data = safeJSONParse(data, true);
} else {
response.data = bufferString;
response.data = data;
}
}

Expand Down

0 comments on commit c75376b

Please sign in to comment.