Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support object and unkown header parameters #186

Merged
merged 2 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading