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

Feature request: custom query param serializer #127

Open
mnpenner opened this issue Sep 1, 2024 · 3 comments
Open

Feature request: custom query param serializer #127

mnpenner opened this issue Sep 1, 2024 · 3 comments

Comments

@mnpenner
Copy link

mnpenner commented Sep 1, 2024

undefined is getting serialized to the string "undefined" instead of omitted from the query params, I believe because of this line:

https://github.com/elysiajs/eden/blob/8a0f7b6194ea336b18a1c80d9a7dd6d2b1afbba2/src/treaty2/index.ts#L188C38-L188C56

> encodeURIComponent(undefined)
'undefined'

I would like to omit such params.

I tried using the fetcher option but the URL has already been built by that point. onRequest also doesn't give me access to the query params it seems.

An option like serializeQuery(query: Record<string,any>): string or similar that lets me provide a custom implementation would be great.

@blossomoftheend
Copy link

We just encountered the same issue in our code and were surprised about undefined being serialized into "undefined", would be pretty cool to have a custom param serializer, like in axios for example

@sorousharzani
Copy link

I also encountered this issue. IMO undefined and null params should be sanitized from the request as Axios does it.

@0-don
Copy link

0-don commented Nov 4, 2024

here is what I use

import { Static, t } from "elysia";

const T = t
  .Transform(t.String())
  .Decode((value) => {
    if (value === "null" || value === "undefined" || value === "") return null;
    return value;
  })
  .Encode((value) => {
    if (value === null) return "null";
    if (value === undefined) return "undefined";
    return value;
  });

export const pageable = t.Object({
  cursor: t.Optional(t.Nullable(T)),
});

export type Pageable = Static<typeof pageable>;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants