My function is set up to make API calls:
interface getEventsPropsShape {
eventId?: string;
accountId?: string;
eventsStartAfter?: string;
}
const getEvents = async ({
eventId,
accountId,
eventsStartAfter,
}: getEventsPropsShape): Promise<void> => {
let apiUrl: string = '/v1/developer/events?limit=25&ascending=false';
eventId !== undefined ?? (apiUrl += `&eventId=${eventId}`);
accountId !== undefined ?? (apiUrl += `&accountId=${accountId}`);
eventsStartAfter !== undefined ??
(apiUrl += `&eventsStartAfter=${eventsStartAfter}`);
const response = await get(apiUrl);
The approach works successfully as it doesn't include eventId
in the apiUrl
let apiUrl: string = '/v1/developer/events?limit=25&ascending=false';
eventId !== undefined ?? (apiUrl += `&eventId=${eventId}`);
However, the following method does not produce the desired result because it adds eventId = undefined
to the apiUrl
let apiUrl: string = '/v1/developer/events?limit=25&ascending=false';
eventId ?? (apiUrl += `&eventId=${eventId}`);
In essence, I am aiming to eliminate the need for conditional if statements like those seen here:
if (eventId) apiUrl += `&eventId=${eventId}`;
if (accountId) apiUrl += `&accountId=${accountId}`;
if (eventsStartAfter) apiUrl += `&eventsStartAfter=${eventsStartAfter}`;