ap0nia / eden-query

🙏 type-safe Elysia.js client with powerful asynchronous state management.
https://ap0nia.github.io/eden-query/
MIT License
34 stars 3 forks source link

Svelte Doc: Add CORS examples #55

Open Extarys opened 1 month ago

Extarys commented 1 month ago

Link : https://ap0nia.github.io/eden-query/eden-query/cors.html

The current example illustration how CORS works with react but not with Svelte.

Using typescript, I found I can put credentials: 'includes' in the following places:

export const eden = createEdenTreatySvelteQuery<Routes>({
    abortOnUnmount: true,
    domain: API_ENDPOINT,
    fetch: {
        credentials: "include", // This works for GET requests but not mutations
    },
});
const client = eden.createClient({
        links: [
            httpLink({ // RangeError: Maximum call stack size exceeded
                domain: API_ENDPOINT,
                fetcher(url, options) {
                    return fetch(url, {
                        ...options,
                        credentials: "include",
                    });
                },
            }),
            httpBatchLink({
                fetcher: event.fetch,
                fetch: {
                    credentials: "include", // Doesn't seem to work with mutations
                },
            }),
        ],
    });

I was finally able to send cookies when using mutations with:

    const client = eden.createClient({
        links: [
            httpBatchLink({
                // fetcher: event.fetch,
                fetcher(url, options) {
                    return fetch(url, {
                        ...options,
                        credentials: "include",
                    });
                },
            }),
        ],
    });

Note that this involves not using event.fetch, not sure what are the implications when not doing SPA though.

ap0nia commented 1 month ago

I cannot seem to replicate the issue with credentials: 'include' not being forwarded to the fetch request. Could you share a reproduction repository demonstrating when this occurs?

I made a mistake with the httpLink export, I will need to fix that.

Extarys commented 1 month ago

I don't have much time right now, but I might later this week. At least it's working with what I come up with and if someone else randomly has this issue, this ticket might enlighten them.