elysiajs / elysia

Ergonomic Framework for Humans
https://elysiajs.com
MIT License
9.23k stars 198 forks source link

treaty does not pass fetch props to fetcher when using URL Endpoint #590

Open hranum opened 3 months ago

hranum commented 3 months ago

What version of Elysia.JS is running?

1.0.11

What platform is your computer?

Linux 6.8.2-arch2-1 x86_64 unknown

What steps can reproduce the bug?

Server:


import { Server } from "bun";
import Elysia from "elysia";
const app = new Elysia().get("/", ({ set }) => (set.redirect = "https://google.com"));
app.listen(3000);

Some other place:


export const treatyApp = treaty<App>("http://localhost:3000", {
    fetcher: (url, options) => {
        console.log(options); // Should output the options we pass
        return fetch(url, options);
    },
});

const res = await treatyApp.index.get({ fetch: { redirect: "manual" } });

console.log(res.data);
console.log(res.headers);

What is the expected behavior?

Given the signature of the treaty function, I'm expecting to be able to pass options to fetch, or a custom fetch function.

(property) get: (options?: {
    headers?: Record<string, unknown> | undefined;
    query?: Record<string, unknown> | undefined;
    fetch?: RequestInit | undefined;
} | undefined) => Promise<TreatyResponse<...>>

The expected output should be:

{ method: 'GET', headers: {}, redirect: 'manual' }

In the example above, with legacy treaty, I could stop a redirect, or pass other options to the fetch funciton.

What do you see instead?

The log output is for options is: { method: 'GET', headers: {} } and the data is the html document of the redirect location. I.e the redirect happens.

I'm using the "fetcher" option to treaty to demonstrate, it is the same error when using the env default.

Additional information

Does not seem to be an issue if passing the Elysia instance instead of the Endpoint URL.

Globally changing redirect or other options, like this does work, but I have to be able to control it per request as with legacy:


const treatyApp = treaty<App>("http://localhost:3000", {
    fetch: {
        redirect: "manual",
    },
});