aspida / aspida

TypeScript friendly HTTP client wrapper for the browser and node.js.

Home Page:https://github.com/aspida/aspida

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Ignore parameter in query if it is undefined

matamatanot opened this issue · comments

Description

I want an option to ignore undefined query values.

Describe the solution you'd like

Add an ignore option.

Describe alternatives you've considered

I'll check object myself.

const removeUndefined(object) {
  return Object.fromEntries(
    Object.entries(object).filter(([k, v]) => v !== undefined)
  )
}

I have not checked recursively.

Finally, I need to check if it is an empty object.
If an empty object is passed, a [?] will be added to the end.
https://localhost/users?

Additional context

There is an API to get a list of users.

  • No query parameter is required the first time.
  • For additional fetch, use the pageToken in the response.

So in the type definition, query->pageToken is optional.

Type definition file

export type Methods = {
  get: {
    query?: {
      pageToken?: string;
      email?: string;
    };
    resBody: {
      user:User[];
      pageToken: string
    };
    status: number;
  };
};

I have created the following function. The argument pageToken is optional.

const getUsers = async (pageToken?: string) => {
  const res = await api.users.$get({ query: { pageToken } });
  return res;
};

However, the expected behavior and the actual behavior were different.

Expected URL
https://localhost/users

Actual URL
https://localhost/users?pageToken=undefined

@matamatanot

Thanks for the suggestion.
I've added support for filtering nullish values, please try it.