Skip to content

Commit

Permalink
feat: support empty params on adapter's requests (#1459)
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarodE authored May 3, 2024
1 parent 43af46d commit 11a2efc
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,20 @@ describe('fetch httpClient testing', () => {
expectFetchCallWith(endpoint);
});

it('maps empty values if configured to do so', async () => {
await fetchHttpClient(endpoint, {
sendEmptyParams: true,
parameters: {
q: undefined,
r: '',
s: null,
t: [],
u: {}
}
});
expectFetchCallWith(`${endpoint}?r=&s=null`);
});

it('cancels equal endpoint requests if no requestId parameter is passed', async () => {
await Promise.all([
expect(
Expand Down Expand Up @@ -186,6 +200,43 @@ describe('fetch httpClient testing', () => {
})
});
});

it('sends empty values in the body if configured to do so', async () => {
await fetchHttpClient(endpoint, {
sendParamsInBody: true,
sendEmptyParams: true,
parameters: {
q: 'shirt',
a: undefined,
b: null,
c: '',
d: [],
extraParams: {
lang: 'en',
e: undefined,
f: null,
g: '',
h: [],
i: {}
}
}
});
expectFetchCallWith(endpoint, {
body: JSON.stringify({
q: 'shirt',
b: null,
c: '',
d: [],
extraParams: {
lang: 'en',
f: null,
g: '',
h: [],
i: {}
}
})
});
});
});
});

Expand Down
16 changes: 13 additions & 3 deletions packages/x-adapter/src/http-clients/fetch.http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,22 @@ import { buildUrl, toJson } from './utils';
*/
export const fetchHttpClient: HttpClient = (
endpoint,
{ id = endpoint, cancelable = true, parameters = {}, properties, sendParamsInBody = false } = {}
{
id = endpoint,
cancelable = true,
parameters = {},
properties,
sendParamsInBody = false,
sendEmptyParams = false
} = {}
) => {
const signal = cancelable ? { signal: abortAndGetNewAbortSignal(id) } : {};
if (!sendEmptyParams) {
parameters = cleanEmpty(parameters);
}
const flatParameters = flatObject(parameters);
const url = sendParamsInBody ? endpoint : buildUrl(endpoint, cleanEmpty(flatParameters));
const bodyParameters = sendParamsInBody ? { body: JSON.stringify(cleanEmpty(parameters)) } : {};
const url = sendParamsInBody ? endpoint : buildUrl(endpoint, flatParameters);
const bodyParameters = sendParamsInBody ? { body: JSON.stringify(parameters) } : {};

return fetch(url, {
...properties,
Expand Down
4 changes: 4 additions & 0 deletions packages/x-adapter/src/http-clients/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ export interface RequestOptions {
* A flag to send parameters in the body if true or in the url QueryString if false.
*/
sendParamsInBody?: boolean;
/**
* A flag to always send the parameters even if their values are empty.
*/
sendEmptyParams?: boolean;
/**
* A list of parameters to send to the API.
*/
Expand Down

0 comments on commit 11a2efc

Please sign in to comment.