Skip to content

Commit

Permalink
fix(core): make query value type unknown
Browse files Browse the repository at this point in the history
Closes #188
  • Loading branch information
hamzamahmood committed Sep 10, 2024
1 parent e6eca29 commit d3997b4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 17 deletions.
22 changes: 5 additions & 17 deletions packages/core/src/http/requestBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,6 @@ export type RequestBuilderFactory<BaseUrlParamType, AuthParams> = (

const JSON = JSONBig();

type QueryValue =
| string
| string[]
| number
| number[]
| bigint
| Array<bigint>
| boolean
| boolean[]
| null
| undefined;

export function skipEncode<T extends PathTemplatePrimitiveTypes>(
value: T
): SkipEncode<T> {
Expand Down Expand Up @@ -116,11 +104,11 @@ export interface RequestBuilder<BaseUrlParamType, AuthParams> {
headers(headersToMerge: Record<string, string>): void;
query(
name: string,
value: QueryValue | Record<string, QueryValue>,
value: unknown | Record<string, unknown>,
prefixFormat?: ArrayPrefixFunction
): void;
query(
parameters?: Record<string, QueryValue> | null,
parameters?: Record<string, unknown> | null,
prefixFormat?: ArrayPrefixFunction
): void;
form(
Expand Down Expand Up @@ -287,15 +275,15 @@ export class DefaultRequestBuilder<BaseUrlParamType, AuthParams>
}
public query(
name: string,
value: QueryValue | Record<string, QueryValue>,
value: unknown | Record<string, unknown>,
prefixFormat?: ArrayPrefixFunction
): void;
public query(
parameters?: Record<string, QueryValue> | null,
parameters?: Record<string, unknown> | null,
prefixFormat?: ArrayPrefixFunction
): void;
public query(
nameOrParameters: string | Record<string, QueryValue> | null | undefined,
nameOrParameters: string | Record<string, unknown> | null | undefined,
value?: unknown,
prefixFormat?: ArrayPrefixFunction
): void {
Expand Down
37 changes: 37 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,43 @@ describe('test default request builder behavior with succesful responses', () =>
);
expect(optionalString.result).toEqual(undefined);
});

it('should test request builder query parameters with number, string, bool and BigInt', async () => {
const expectedRequestUrl =
'http://apimatic.hopto.org:3000/test/requestBuilder?number=12345&string=string&bool=true&biginit=12345';

const reqBuilder = defaultRequestBuilder('GET');
reqBuilder.appendPath('/test/requestBuilder');
reqBuilder.baseUrl('default');
reqBuilder.query('number', 12345);
reqBuilder.query('string', 'string');
reqBuilder.query('bool', true);
reqBuilder.query('biginit', BigInt(12345));

const apiResponse = await reqBuilder.callAsText();
expect(apiResponse.request.url).toEqual(expectedRequestUrl);
});

it('should test request builder query parameters with complex object', async () => {
const expectedRequestUrl =
'http://apimatic.hopto.org:3000/test/requestBuilder?object%5Bkey1%5D=value1&object%5Bkey2%5D=value2&object%5Bkey3%5D%5Bsubkey%5D=12345&object%5Bkey4%5D%5B0%5D=item1&object%5Bkey4%5D%5B1%5D=item2';

const reqBuilder = defaultRequestBuilder('GET');
reqBuilder.appendPath('/test/requestBuilder');
reqBuilder.baseUrl('default');
reqBuilder.query('object', {
key1: 'value1',
key2: 'value2',
key3: {
subkey: 12345,
},
key4: ['item1', 'item2'],
});

const apiResponse = await reqBuilder.callAsText();
expect(apiResponse.request.url).toEqual(expectedRequestUrl);
});

it('should test request builder configured with all kind of headers', async () => {
const reqBuilder = defaultRequestBuilder('GET', '/test/requestBuilder');
reqBuilder.baseUrl('default');
Expand Down

0 comments on commit d3997b4

Please sign in to comment.