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

fix: only use credentials include for chainlit requestsC #1672

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 26 additions & 17 deletions frontend/src/hooks/useFetch.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
import { useContext } from 'react';
import useSWR, { SWRResponse } from 'swr';

const fetcher = async (url: string): Promise<any> => {
const response = await fetch(url, { credentials: 'include' });

if (!response.ok) {
throw new Error('Network response was not ok');
}

// Check if the response is JSON
const contentType = response.headers.get('content-type');
if (contentType && contentType.includes('application/json')) {
return response.json();
} else {
// If it's not JSON, return the raw response body
return response.text();
}
};
import { ChainlitContext } from '@chainlit/react-client';

const fetcher =
(isChainlitRequest: boolean) =>
async (url: string): Promise<any> => {
const fetchOptions: RequestInit = {
...(isChainlitRequest && { credentials: 'include' })
};

const response = await fetch(url, fetchOptions);

if (!response.ok) {
throw new Error('Network response was not ok');
}

const contentType = response.headers.get('content-type');
return contentType?.includes('application/json')
? response.json()
: response.text();
};

const useFetch = (endpoint: string | null): SWRResponse<any, Error> => {
return useSWR<any, Error>(endpoint, fetcher);
const apiClient = useContext(ChainlitContext);
const isChainlitRequest = endpoint?.startsWith(apiClient.httpEndpoint);

return useSWR<any, Error>(endpoint, fetcher(!!isChainlitRequest));
};

export { useFetch };
Loading