Skip to content

Commit

Permalink
chore: fix host url display
Browse files Browse the repository at this point in the history
  • Loading branch information
jedwards1230 committed Apr 20, 2024
1 parent 08500bd commit bf48683
Show file tree
Hide file tree
Showing 10 changed files with 355 additions and 370 deletions.
5 changes: 2 additions & 3 deletions client/src/hooks/fetchers/useModelsQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import { fetcher } from "@/lib/fetcher";
import { ModelInformation } from "@/types";

export const modelQueryOptions = queryOptions({
queryKey: ["models"],
queryFn: () => fetcher<ModelInformation[]>("/models"),
retryOnMount: true,
queryKey: ["models"],
queryFn: () => fetcher<ModelInformation[]>("/models"),
});

export const useModelsQuery = () => useQuery(modelQueryOptions);
68 changes: 32 additions & 36 deletions client/src/hooks/stores/configStore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,48 @@ import type { Agent, User } from "@/types";
import { createSelectors } from "@/lib/zustand";

type State = {
threadId: string | null;
host: string;
stream: boolean;
user: User;
theme: string;
defaultAgent: Agent;
threadId: string | null;
stream: boolean;
user: User;
theme: string;
defaultAgent: Agent;
};

interface Actions {
setThreadId: (threadId: string | null) => void;
setHost: (host: string) => void;
setStream: (stream: boolean) => void;
setUser: (user: User) => void;
setTheme: (theme: string) => void;
setThreadId: (threadId: string | null) => void;
setStream: (stream: boolean) => void;
setUser: (user: User) => void;
setTheme: (theme: string) => void;

reset: () => void;
reset: () => void;
}

const initial: State = {
threadId: null,
host: process.env.EXPO_PUBLIC_API_URL || "http://localhost:3000",
stream: true,
user: { id: "user-1" } as User,
theme: "system",
defaultAgent: {} as Agent,
threadId: null,
stream: true,
user: { id: "user-1" } as User,
theme: "system",
defaultAgent: {} as Agent,
};

const name = "config";

export const useConfigStore = createSelectors(
create<State & Actions>()(
persist(
(set, get) => ({
...initial,
setThreadId: (threadId) => set({ threadId }),
setHost: (host) => set({ host }),
setStream: (stream) => set({ stream }),
setUser: (user) => set({ user }),
setTheme: (theme) => set({ theme }),

reset: () => set(initial),
}),
{
name,
storage: createJSONStorage(() => AsyncStorage),
}
)
)
create<State & Actions>()(
persist(
(set, get) => ({
...initial,
setThreadId: (threadId) => set({ threadId }),
setStream: (stream) => set({ stream }),
setUser: (user) => set({ user }),
setTheme: (theme) => set({ theme }),

reset: () => set(initial),
}),
{
name,
storage: createJSONStorage(() => AsyncStorage),
}
)
)
);
118 changes: 59 additions & 59 deletions client/src/lib/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,81 +3,81 @@ import { Platform } from "react-native";
export type AuthParams = [url: string, userId: string];

class FetchError extends Error {
constructor(public res: Response | XMLHttpRequest | Error, message?: string) {
super(message);
this.name = "FetchError";
}
constructor(public res: Response | XMLHttpRequest | Error, message?: string) {
super(message);
this.name = "FetchError";
}

get status() {
if (this.res instanceof Response) {
return this.res.status;
} else if (this.res instanceof XMLHttpRequest) {
return this.res.status;
} else if (this.res instanceof Error) {
return 500;
}
return undefined;
}
get status() {
if (this.res instanceof Response) {
return this.res.status;
} else if (this.res instanceof XMLHttpRequest) {
return this.res.status;
} else if (this.res instanceof Error) {
return 500;
}
return undefined;
}

get response() {
if (this.res instanceof Response) {
return this.res;
} else if (this.res instanceof XMLHttpRequest) {
return this.res.response;
}
return undefined;
}
get response() {
if (this.res instanceof Response) {
return this.res;
} else if (this.res instanceof XMLHttpRequest) {
return this.res.response;
}
return undefined;
}
}

export function isFetchError(error: any): error is FetchError {
return error instanceof FetchError;
return error instanceof FetchError;
}

export type FetcherRequestInit = FetchRequestInit & {
/** If true, returns raw Response. If false, returns JSON Response. */
stream?: boolean;
/** If true, removes default "Content-Type" header (Default: false) */
file?: boolean;
/** apiKey for Authorization header */
apiKey?: string;
/** If true, returns raw Response. If false, returns JSON Response. */
stream?: boolean;
/** If true, removes default "Content-Type" header (Default: false) */
file?: boolean;
/** apiKey for Authorization header */
apiKey?: string;
};

export const BASE_HOST =
Platform.select({
web: process.env.NODE_ENV === "development" ? "http://localhost:3000" : "",
default: process.env.EXPO_PUBLIC_API_URL,
}) + "/api";
Platform.select({
web: process.env.NODE_ENV === "development" ? "http://localhost:3000" : "",
default: process.env.EXPO_PUBLIC_API_URL,
}) + "/api";

/**
* Fetcher for react-query.
* Sets the Authorization header with the userId
* */
export async function fetcher<T = any>(
url: string | URL | Request,
{ stream, file, apiKey, ...init }: FetcherRequestInit = {
stream: false,
file: false,
}
url: string | URL | Request,
{ stream, file, apiKey, ...init }: FetcherRequestInit = {
stream: false,
file: false,
}
): Promise<T> {
try {
const res = await fetch(BASE_HOST + url, {
...init,
headers: {
...(!file && { "Content-Type": "application/json" }),
...(apiKey && { Authorization: apiKey }),
...(init?.headers || {}),
},
});
if (res.ok) {
return stream ? res : res.json();
}
throw new FetchError(res, `HTTP error! status: ${res.status}`);
} catch (error: any) {
// ensure it is fetch error
if (error instanceof FetchError) {
throw error;
}
console.warn(error);
throw new FetchError(error, "Failed to connect to server!");
}
try {
const res = await fetch(BASE_HOST + url, {
...init,
headers: {
...(!file && { "Content-Type": "application/json" }),
...(apiKey && { Authorization: apiKey }),
...(init?.headers || {}),
},
});
if (res.ok) {
return stream ? res : res.json();
}
throw new FetchError(res, `HTTP error! status: ${res.status}`);
} catch (error: any) {
// ensure it is fetch error
if (error instanceof FetchError) {
throw error;
}
console.warn(error);
throw new FetchError(error, "Failed to connect to server!");
}
}
6 changes: 3 additions & 3 deletions client/src/providers/AppStateProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useUser } from "@/hooks/useUser";

export function AppStateProvider({ children }: { children: React.ReactNode }) {
const { loading } = useUser();
if (loading) return null;
return children;
const { loading } = useUser();
if (loading) return null;
return children;
}
Loading

0 comments on commit bf48683

Please sign in to comment.