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

Provide options for unexploded parameters #12

Merged
merged 2 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ export class ActiveNumberApi extends NumbersApi {
const listPromise = buildPageResultPromise<ActiveNumber>(
this.client,
requestOptionsPromise,
operationProperties);
operationProperties,
false,
';');

// 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, false, ';');

return this.client.processCall<AvailableNumbersResponse>({
url,
Expand Down
14 changes: 10 additions & 4 deletions packages/sdk-client/src/api/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class ApiClient {
const prop = data[name];
acc[name] = typeof prop.toJSON === 'function'
? prop.toJSON()
: Array.isArray(prop) ? prop.join(';') : prop.toString();
: Array.isArray(prop) ? prop.join(',') : prop.toString();
JPPortier marked this conversation as resolved.
Show resolved Hide resolved
return acc;
},
{} as { [p in keyof T]: string },
Expand Down Expand Up @@ -128,16 +128,18 @@ export class ApiClient {
* @param {string} url - The base url to be used.
* @param {Object.<string, string|undefined>} queryParameters - Key-value pair with the parameters. If the value is undefined, the key is dropped.
* @param {boolean} repeatParamArray - create as many single parameters with each value of the array
* @param {string} unexplodedSeparator - the separator to use between values when `explode` is false (default is ',')
* @return {string} The prepared URL as a string.
*/
prepareUrl(
url: string,
queryParameters: { [key: string]: string | undefined } = {},
repeatParamArray?: boolean,
unexplodedSeparator?: string,
): string {
const queryPart = Object.keys(queryParameters)
.filter((name) => typeof queryParameters[name] !== 'undefined')
.map((name) => this.formatQueryParameter(name, queryParameters, repeatParamArray))
.map((name) => this.formatQueryParameter(name, queryParameters, repeatParamArray, unexplodedSeparator))
.join('&');

const paramsPrefix = url.indexOf('?') > -1 ? '&' : '?';
Expand All @@ -150,20 +152,24 @@ export class ApiClient {
* @param {string} name - The parameter name
* @param {Object.<string, string|undefined>} queryParameters - Key-value pair with the parameters. If the value is undefined, the key is dropped.
* @param {boolean}repeatParamArray - Create as many single parameters with each value of the array
* @param {string} unexplodedSeparator - the separator to use between values when `explode` is false (default is `,`)
* @return {string} The query parameter formatted as required by the API
*/
private formatQueryParameter = (
name: string,
queryParameters: { [key: string]: string | undefined } = {},
repeatParamArray?: boolean,
unexplodedSeparator?: string,
): string => {
const defaultFormat = `${name}=${queryParameters[name]!}`;
if(repeatParamArray) {
const parameterValue = queryParameters[name];
if (parameterValue && parameterValue.indexOf(';') > 0) {
const parameterValues = parameterValue.split(';');
if (parameterValue && parameterValue.indexOf(',') > 0) {
const parameterValues = parameterValue.split(',');
return parameterValues.map((value) => `${name}=${value}`).join('&');
}
} else if (unexplodedSeparator !== undefined) {
return defaultFormat.replaceAll(',', unexplodedSeparator);
}
return defaultFormat;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,13 @@ export const buildPageResultPromise = async <T>(
client: ApiClient,
requestOptionsPromise: Promise<RequestOptions>,
operationProperties: PaginatedApiProperties,
repeatParamArray?: boolean,
unexplodedSeparator?: string,
): 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, unexplodedSeparator);

return client.processCallWithPagination<T>({
url,
Expand Down
15 changes: 13 additions & 2 deletions packages/sdk-client/tests/api/api-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,29 @@ describe('API client', () => {
const url = 'https://example.com';
const parameters = {
foo: 'fooValue',
bar: '1;2',
bar: '1,2',
baz: undefined,
};
const formattedUrl = apiClient.prepareUrl(url, parameters);
expect(formattedUrl).toBe('https://example.com?foo=fooValue&bar=1,2');
});

it('should format the URL with array parameters and a custom separator', () => {
const url = 'https://example.com';
const parameters = {
foo: 'fooValue',
bar: '1,2',
baz: undefined,
};
const formattedUrl = apiClient.prepareUrl(url, parameters, false, ';');
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 = {
foo: 'fooValue',
bar: '1;2',
bar: '1,2',
baz: undefined,
};
const formattedUrl = apiClient.prepareUrl(url, parameters, true);
Expand Down