import axios from "axios"; import { fetchEventSource } from "@microsoft/fetch-event-source"; import { Message } from "@/store/zustand/message"; export function getSystemStatus() { return axios.get("/api/v1/status"); } export function getSystemSetting() { return axios.get("/api/v1/system/setting"); } export function upsertSystemSetting(systemSetting: SystemSetting) { return axios.post("/api/v1/system/setting", systemSetting); } export function vacuumDatabase() { return axios.post("/api/v1/system/vacuum"); } export function signin(username: string, password: string) { return axios.post("/api/v1/auth/signin", { username, password, }); } export function signinWithSSO(identityProviderId: IdentityProviderId, code: string, redirectUri: string) { return axios.post("/api/v1/auth/signin/sso", { identityProviderId, code, redirectUri, }); } export function signup(username: string, password: string) { return axios.post("/api/v1/auth/signup", { username, password, }); } export function signout() { return axios.post("/api/v1/auth/signout"); } export function createUser(userCreate: UserCreate) { return axios.post("/api/v1/user", userCreate); } export function getMyselfUser() { return axios.get("/api/v1/user/me"); } export function getUserList() { return axios.get("/api/v1/user"); } export function getUserByUsername(username: string) { return axios.get(`/api/v1/user/${username}`); } export function upsertUserSetting(upsert: UserSettingUpsert) { return axios.post(`/api/v1/user/setting`, upsert); } export function patchUser(userPatch: UserPatch) { return axios.patch(`/api/v1/user/${userPatch.id}`, userPatch); } export function deleteUser(userDelete: UserDelete) { return axios.delete(`/api/v1/user/${userDelete.id}`); } export function getAllMemos(memoFind?: MemoFind) { const queryList = []; if (memoFind?.offset) { queryList.push(`offset=${memoFind.offset}`); } if (memoFind?.limit) { queryList.push(`limit=${memoFind.limit}`); } if (memoFind?.creatorUsername) { queryList.push(`creatorUsername=${memoFind.creatorUsername}`); } return axios.get(`/api/v1/memo/all?${queryList.join("&")}`); } export function getMemoList(memoFind?: MemoFind) { const queryList = []; if (memoFind?.creatorUsername) { queryList.push(`creatorUsername=${memoFind.creatorUsername}`); } if (memoFind?.rowStatus) { queryList.push(`rowStatus=${memoFind.rowStatus}`); } if (memoFind?.pinned) { queryList.push(`pinned=${memoFind.pinned}`); } if (memoFind?.offset) { queryList.push(`offset=${memoFind.offset}`); } if (memoFind?.limit) { queryList.push(`limit=${memoFind.limit}`); } return axios.get(`/api/v1/memo?${queryList.join("&")}`); } export function getMemoStats(username: string) { return axios.get(`/api/v1/memo/stats?creatorUsername=${username}`); } export function getMemoById(id: MemoId) { return axios.get(`/api/v1/memo/${id}`); } export function createMemo(memoCreate: MemoCreate) { return axios.post("/api/v1/memo", memoCreate); } export function patchMemo(memoPatch: MemoPatch) { return axios.patch(`/api/v1/memo/${memoPatch.id}`, memoPatch); } export function pinMemo(memoId: MemoId) { return axios.post(`/api/v1/memo/${memoId}/organizer`, { pinned: true, }); } export function unpinMemo(memoId: MemoId) { return axios.post(`/api/v1/memo/${memoId}/organizer`, { pinned: false, }); } export function deleteMemo(memoId: MemoId) { return axios.delete(`/api/v1/memo/${memoId}`); } export function checkOpenAIEnabled() { return axios.get(`/api/openai/enabled`); } export async function chatStreaming(messageList: Array, onmessage: any, onclose: any) { await fetchEventSource("/api/v1/openai/chat-streaming", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify(messageList), async onopen() { // to do nth }, onmessage(event: any) { onmessage(event); }, onclose() { onclose(); }, onerror(error: any) { console.log("error", error); }, }); } export function getShortcutList(shortcutFind?: ShortcutFind) { const queryList = []; if (shortcutFind?.creatorUsername) { queryList.push(`creatorUsername=${shortcutFind.creatorUsername}`); } return axios.get(`/api/v1/shortcut?${queryList.join("&")}`); } export function createShortcut(shortcutCreate: ShortcutCreate) { return axios.post("/api/v1/shortcut", shortcutCreate); } export function patchShortcut(shortcutPatch: ShortcutPatch) { return axios.patch(`/api/v1/shortcut/${shortcutPatch.id}`, shortcutPatch); } export function deleteShortcutById(shortcutId: ShortcutId) { return axios.delete(`/api/v1/shortcut/${shortcutId}`); } export function getResourceList() { return axios.get("/api/v1/resource"); } export function getResourceListWithLimit(resourceFind?: ResourceFind) { const queryList = []; if (resourceFind?.offset) { queryList.push(`offset=${resourceFind.offset}`); } if (resourceFind?.limit) { queryList.push(`limit=${resourceFind.limit}`); } return axios.get(`/api/v1/resource?${queryList.join("&")}`); } export function createResource(resourceCreate: ResourceCreate) { return axios.post("/api/v1/resource", resourceCreate); } export function createResourceWithBlob(formData: FormData) { return axios.post("/api/v1/resource/blob", formData); } export function patchResource(resourcePatch: ResourcePatch) { return axios.patch(`/api/v1/resource/${resourcePatch.id}`, resourcePatch); } export function deleteResourceById(id: ResourceId) { return axios.delete(`/api/v1/resource/${id}`); } export function getMemoResourceList(memoId: MemoId) { return axios.get(`/api/v1/memo/${memoId}/resource`); } export function upsertMemoResource(memoId: MemoId, resourceId: ResourceId) { return axios.post(`/api/v1/memo/${memoId}/resource`, { resourceId, }); } export function deleteMemoResource(memoId: MemoId, resourceId: ResourceId) { return axios.delete(`/api/v1/memo/${memoId}/resource/${resourceId}`); } export function getTagList(tagFind?: TagFind) { const queryList = []; if (tagFind?.creatorUsername) { queryList.push(`creatorUsername=${tagFind.creatorUsername}`); } return axios.get(`/api/v1/tag?${queryList.join("&")}`); } export function getTagSuggestionList() { return axios.get(`/api/v1/tag/suggestion`); } export function upsertTag(tagName: string) { return axios.post(`/api/v1/tag`, { name: tagName, }); } export function deleteTag(tagName: string) { return axios.post(`/api/v1/tag/delete`, { name: tagName, }); } export function getStorageList() { return axios.get(`/api/v1/storage`); } export function createStorage(storageCreate: StorageCreate) { return axios.post(`/api/v1/storage`, storageCreate); } export function patchStorage(storagePatch: StoragePatch) { return axios.patch(`/api/v1/storage/${storagePatch.id}`, storagePatch); } export function deleteStorage(storageId: StorageId) { return axios.delete(`/api/v1/storage/${storageId}`); } export function getIdentityProviderList() { return axios.get(`/api/v1/idp`); } export function createIdentityProvider(identityProviderCreate: IdentityProviderCreate) { return axios.post(`/api/v1/idp`, identityProviderCreate); } export function patchIdentityProvider(identityProviderPatch: IdentityProviderPatch) { return axios.patch(`/api/v1/idp/${identityProviderPatch.id}`, identityProviderPatch); } export function deleteIdentityProvider(id: IdentityProviderId) { return axios.delete(`/api/v1/idp/${id}`); } export async function getRepoStarCount() { const { data } = await axios.get(`https://api.github.com/repos/usememos/memos`, { headers: { Accept: "application/vnd.github.v3.star+json", Authorization: "", }, }); return data.stargazers_count as number; } export async function getRepoLatestTag() { const { data } = await axios.get(`https://api.github.com/repos/usememos/memos/tags`, { headers: { Accept: "application/vnd.github.v3.star+json", Authorization: "", }, }); return data[0].name as string; }