Skip to content

Commit

Permalink
feat: support object and unkown header parameters (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
asadali214 authored Aug 29, 2024
1 parent 96d47bc commit 190d463
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
14 changes: 8 additions & 6 deletions packages/core/src/http/requestBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export interface RequestBuilder<BaseUrlParamType, AuthParams> {
acceptJson(): void;
accept(acceptHeaderValue: string): void;
contentType(contentTypeHeaderValue: string): void;
header(name: string, value?: string | boolean | number | bigint | null): void;
header(name: string, value?: unknown): void;
headers(headersToMerge: Record<string, string>): void;
query(
name: string,
Expand Down Expand Up @@ -271,14 +271,16 @@ export class DefaultRequestBuilder<BaseUrlParamType, AuthParams>
public contentType(contentTypeHeaderValue: string): void {
this._contentType = contentTypeHeaderValue;
}
public header(
name: string,
value?: string | boolean | number | bigint | null
): void {
public header(name: string, value?: unknown): void {
if (value === null || typeof value === 'undefined') {
return;
}
setHeader(this._headers, name, value.toString());
if (typeof value === 'object') {
setHeader(this._headers, name, JSON.stringify(value));
return;
}
// String() is used to convert boolean, number, bigint, or unknown types
setHeader(this._headers, name, String(value));
}
public headers(headersToMerge: Record<string, string>): void {
mergeHeaders(this._headers, headersToMerge);
Expand Down
24 changes: 24 additions & 0 deletions packages/core/test/http/requestBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,30 @@ describe('test default request builder behavior with succesful responses', () =>
);
expect(optionalString.result).toEqual(undefined);
});
it('should test request builder configured with all kind of headers', async () => {
const reqBuilder = defaultRequestBuilder('GET', '/test/requestBuilder');
reqBuilder.baseUrl('default');
reqBuilder.header('test-header-missing1');
reqBuilder.header('test-header-missing2', null);
reqBuilder.header('test-header1', 'test-value\'"\n1');
reqBuilder.header('test-header2', true);
reqBuilder.header('test-header3', false);
reqBuilder.header('test-header4', 12345);
reqBuilder.header('test-header5', BigInt(12345));
reqBuilder.header('test-header6', { key: 'v a l u e' });
reqBuilder.header('test-header7', Symbol());

const apiResponse = await reqBuilder.callAsText();
expect(apiResponse.request.headers).toEqual({
'test-header1': 'test-value\'"\n1',
'test-header2': 'true',
'test-header3': 'false',
'test-header4': '12345',
'test-header5': '12345',
'test-header6': '{"key":"v a l u e"}',
'test-header7': 'Symbol()',
});
});

function customRequestBuilder(
response: HttpResponse
Expand Down

0 comments on commit 190d463

Please sign in to comment.