Skip to content

Commit

Permalink
Manage different configuration of query arrays parameters (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
asein-sinch authored Feb 7, 2024
1 parent 0473728 commit b81e3eb
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 24 deletions.
1 change: 1 addition & 0 deletions examples/simple-examples/src/numbers/active/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const populateActiveNumbersList = (
const requestData: ListActiveNumbersRequestData = {
regionCode: 'US',
type: 'LOCAL',
capability: 'SMS',
pageSize: 2,
};

Expand Down
1 change: 1 addition & 0 deletions examples/simple-examples/src/numbers/available/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ListAvailableNumbersRequestData } from '@sinch/sdk-core';
const requestData: ListAvailableNumbersRequestData= {
regionCode: 'US',
type: 'LOCAL',
capabilities: ['SMS', 'VOICE'],
};

const sinchClient = initClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export interface ListActiveNumbersRequestData {
'numberPattern.pattern'?: string;
/** Search pattern to apply. The options are, `START`, `CONTAIN`, and `END`. */
'numberPattern.searchPattern'?: SearchPatternEnum;
/** Number capabilities to filter by, `SMS` and/or `VOICE`. */
capability?: Array<CapabilitiesEnum>;
/** Number capabilities to filter by, `SMS` or `VOICE`. */
capability?: CapabilitiesEnum;
/** The maximum number of items to return. */
pageSize?: number;
/** The next page token value returned from a previous List request, if any. */
Expand Down Expand Up @@ -139,7 +139,8 @@ export class ActiveNumberApi extends NumbersApi {
const listPromise = buildPageResultPromise<ActiveNumber>(
this.client,
requestOptionsPromise,
operationProperties);
operationProperties,
);

// Add properties to the Promise to offer the possibility to use it as an iterator
Object.assign(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export class AvailableNumberApi extends NumbersApi {
headers,
body || undefined,
);
const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams);
const url = this.client.prepareUrl(requestOptions.basePath, requestOptions.queryParams, true);

return this.client.processCall<AvailableNumbersResponse>({
url,
Expand Down
21 changes: 11 additions & 10 deletions packages/sdk-client/src/api/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ export class ApiClient {
.reduce(
(acc, name) => {
const prop = data[name];
acc[name] = typeof prop.toJSON === 'function'
? prop.toJSON()
: Array.isArray(prop) ? prop.join(';') : prop.toString();
acc[name] = typeof prop.toJSON === 'object'
? JSON.stringify(prop.toJSON())
: JSON.stringify(prop);
return acc;
},
{} as { [p in keyof T]: string },
Expand Down Expand Up @@ -157,15 +157,16 @@ export class ApiClient {
queryParameters: { [key: string]: string | undefined } = {},
repeatParamArray?: boolean,
): string => {
const defaultFormat = `${name}=${queryParameters[name]!}`;
if(repeatParamArray) {
const parameterValue = queryParameters[name];
if (parameterValue && parameterValue.indexOf(';') > 0) {
const parameterValues = parameterValue.split(';');
return parameterValues.map((value) => `${name}=${value}`).join('&');
const parameterValue = JSON.parse(queryParameters[name] || '');
if (Array.isArray(parameterValue)) {
if (repeatParamArray) {
return parameterValue.map((value: string) => `${name}=${value}`).join('&');
} else {
return `${name}=${parameterValue.join(',')}`;
}
} else {
return `${name}=${parameterValue}`;
}
return defaultFormat;
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,12 @@ export const buildPageResultPromise = async <T>(
client: ApiClient,
requestOptionsPromise: Promise<RequestOptions>,
operationProperties: PaginatedApiProperties,
repeatParamArray?: boolean,
): Promise<PageResult<T>> => {
// Await the promise in this async method and store the result in client so that they can be reused
const requestOptions = await requestOptionsPromise;
const url = client.prepareUrl(requestOptions.basePath, requestOptions.queryParams);
const url = client.prepareUrl(
requestOptions.basePath, requestOptions.queryParams, repeatParamArray);

return client.processCallWithPagination<T>({
url,
Expand Down
18 changes: 9 additions & 9 deletions packages/sdk-client/tests/api/api-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,33 @@ describe('API client', () => {

it('should format the URL with simple parameters', () => {
const url = 'https://example.com';
const parameters = {
const parameters = apiClient.extractQueryParams({
foo: 'fooValue',
bar: '1',
baz: undefined,
};
}, ['foo', 'bar', 'baz'] );
const formattedUrl = apiClient.prepareUrl(url, parameters);
expect(formattedUrl).toBe('https://example.com?foo=fooValue&bar=1');
});

it('should format the URL with array parameters', () => {
const url = 'https://example.com';
const parameters = {
const parameters = apiClient.extractQueryParams({
foo: 'fooValue',
bar: '1;2',
bar: ['1' ,'2'],
baz: undefined,
};
}, ['foo', 'bar', 'baz'] );
const formattedUrl = apiClient.prepareUrl(url, parameters);
expect(formattedUrl).toBe('https://example.com?foo=fooValue&bar=1;2');
expect(formattedUrl).toBe('https://example.com?foo=fooValue&bar=1,2');
});

it('should format the URL with array parameters with repeat key', () => {
const url = 'https://example.com';
const parameters = {
const parameters = apiClient.extractQueryParams({
foo: 'fooValue',
bar: '1;2',
bar: ['1' ,'2'],
baz: undefined,
};
}, ['foo', 'bar', 'baz'] );
const formattedUrl = apiClient.prepareUrl(url, parameters, true);
expect(formattedUrl).toBe('https://example.com?foo=fooValue&bar=1&bar=2');
});
Expand Down

0 comments on commit b81e3eb

Please sign in to comment.