Skip to content

Commit

Permalink
Merge pull request #555 from seznam/response-type
Browse files Browse the repository at this point in the history
Added new responseType option
  • Loading branch information
jsimck authored Feb 15, 2024
2 parents 28cf635 + 3bb37c8 commit 274a805
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/yellow-jeans-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ima/core": minor
---

Added new `responseType` to `HttpAgentRequestOptions` which enables you to specify proxy response type. This serves as an alternative to existing solution, which parses JSON, null or fallbacks to text. Use this for other response types like globs, arrayBuffers or formData
1 change: 1 addition & 0 deletions packages/core/src/http/HttpAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface HttpAgentRequestOptions {
repeatRequest: number;
fetchOptions: ImaRequestInit;
cache: boolean;
responseType?: 'json' | 'blob' | 'text' | 'arrayBuffer' | 'formData';
postProcessors?: (<B = unknown>(
response: HttpAgentResponse<B>
) => HttpAgentResponse<B>)[];
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/http/HttpProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,14 @@ export class HttpProxy {

const contentType = response.headers.get('content-type');

// Parse content by the new responseType option
if (options?.responseType) {
return response[options.responseType]().then(body => [
response,
body,
]);
}

if (response.status === HttpStatusCode.NO_CONTENT) {
return Promise.resolve([response, null]);
} else if (contentType && contentType.includes('application/json')) {
Expand Down
28 changes: 22 additions & 6 deletions packages/core/src/http/__tests__/HttpProxySpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,16 @@ describe('ima.core.http.HttpProxy', () => {
status: 200,
// @ts-ignore
headers: new Map(), // compatible enough with Headers
json() {
return Promise.resolve(this.body);
},
// @ts-ignore
text() {
return Promise.resolve(this.body);
},
json: () => Promise.resolve({ data: 'json' }),
// @ts-ignore
blob: () => Promise.resolve({ data: 'blob' }),
// @ts-ignore
text: () => Promise.resolve({ data: 'text' }),
// @ts-ignore
arrayBuffer: () => Promise.resolve({ data: 'arrayBuffer' }),
// @ts-ignore
formData: () => Promise.resolve({ data: 'formData' }),
// @ts-ignore
body: { data: 'some data' },
};
Expand Down Expand Up @@ -406,6 +409,19 @@ describe('ima.core.http.HttpProxy', () => {
expect(abortController.signal.reason).toBe('Aborted');
expect(abortController.signal.aborted).toBeTruthy();
});

it.each([['json'], ['blob'], ['text'], ['arrayBuffer'], ['formData']])(
'should parse response with given response type',
async responseType => {
const result = (await proxy.request(method, API_URL, DATA, {
...defaultOptions,
responseType:
responseType as HttpAgentRequestOptions['responseType'],
})) as UnknownParameters;

expect(result.body).toStrictEqual({ data: responseType });
}
);
}
);

Expand Down

0 comments on commit 274a805

Please sign in to comment.