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] Transform function configuration #16

Closed
itsyoboieltr opened this issue Sep 11, 2023 · 6 comments
Closed

[FEATURE REQUEST] Transform function configuration #16

itsyoboieltr opened this issue Sep 11, 2023 · 6 comments

Comments

@itsyoboieltr
Copy link
Contributor

It would be nice if Eden Treaty allowed for the transformation of the response.

Proposed API:

const api = edenTreaty<App>('', {
  transform: (response) => {
    if (response.error) throw response.error;
    return response.data;
  },
});

const data = await api.get();

Current workaround, which needs to be coded manually for each request:

const api = edenTreaty<App>('');

const response = await api.get();
if (response.error) throw response.error;
const data = response.data;

--- Why would this be useful? ---

Lot of current querying libraries like Tanstack query, SWR, depend on errors being thrown, so they can be returned by the hook used for querying. Eden Treaty does not throw, but returns errors and data objects, which is fine, but there should be a way to configure the transformation of the response for these use-cases on the client level. Is this something that would be feasible to implement? @SaltyAom

@SaltyAom
Copy link
Member

Interesting, this should be implemented.

If you have more thoughts to share about DX, and what we are missing in general.
You can share your thought here.

I'll implement this with Elysia 0.7

@itsyoboieltr
Copy link
Contributor Author

@SaltyAom was this implemented as part of the 0.7 update? I cannot find anything about it as of yet. Maybe it is too early to ask :)

@SaltyAom
Copy link
Member

aa8176b, published on 0.7.4

@itsyoboieltr
Copy link
Contributor Author

Do you think there is a way to make it type-safe? @SaltyAom

After doing this:

export const app = edenTreaty<App>('', {
  transform: (response) => {
    if (response.error) throw response.error;
    return response.data;
  },
});

Returning an error should not be possible anymore, because it is always thrown. Calling the treaty still believes like an error can be returned:

const res: ({
    data: string[];
    error: null;
} | {
    data: null;
    error: EdenFetchError<number, string>;
}) & {
    status: number;
    response: Response;
    headers: Record<string, string>;
} = await app.api.hello.get();

Expected output (ideally, if this is even possible):

const res: string[] = await app.api.hello.get();

@itsyoboieltr
Copy link
Contributor Author

Currently the transform function seems to not run? the requests are not transformed at all. It seems to be buggy both on the type and execution level somehow.

@itsyoboieltr
Copy link
Contributor Author

itsyoboieltr commented Dec 23, 2023

Upon inspecting the source code it became apparent: it is not transforming the whole response object, only the data property. I guess this was intended by @SaltyAom , but is probably not very intuitive? I expected that it would transform everything returned by Eden Treaty. The current implementation does seem to work, I just do not know if there is any specific reason why we cannot remove or assign to the other fields?

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

2 participants