Skip to content

Instantly share code, notes, and snippets.

@christopherbauer
Created May 20, 2024 01:01
Show Gist options
  • Save christopherbauer/f6ad8ce7985b424ad8964448c4f128ed to your computer and use it in GitHub Desktop.
Save christopherbauer/f6ad8ce7985b424ad8964448c4f128ed to your computer and use it in GitHub Desktop.
import { Conversation, Message } from "./conversation/types";
import { Persona } from "./persona/types";
export const getPersonas: () => Promise<Persona[]> = () => {
return fetch("http://localhost:3010/persona", {
method: "GET",
headers: {
"Content-Type": "application/json",
},
}).then((response) => {
if (!response.ok) {
throw new Error("Failed to fetch personas");
}
return response.json();
});
};
export const getConversationList: () => Promise<Conversation[]> = () => {
return fetch(`http://localhost:3010/conversation`, {
method: "GET",
headers: {
"Content-Type": "application/json",
},
}).then((response) => {
if (!response.ok) {
throw new Error("Failed to fetch messages");
}
return response.json();
});
};
export const getMessages: (id: string) => Promise<Message[]> = (id) => {
return fetch(`http://localhost:3010/conversation/${id}`, {
method: "GET",
headers: {
"Content-Type": "application/json",
},
}).then((response) => {
if (!response.ok) {
throw new Error("Failed to fetch messages");
}
return response.json();
});
};
export const startConversation: (
id: string,
persona: string,
message: string
) => Promise<Message[]> = (id, persona, message) => {
return fetch(`http://localhost:3010/conversation/${id}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ persona, message }),
}).then((response) => {
if (!response.ok) {
throw new Error("Failed to fetch messages");
}
return response.json();
});
};
export const sendMessage: (
id: string,
message: string
) => Promise<Message[]> = (id, message) => {
return fetch(`http://localhost:3010/conversation/${id}`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ message }),
}).then((response) => {
if (!response.ok) {
throw new Error("Failed to fetch messages");
}
return response.json();
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment