type ResponseType < T = unknown > = { succeed : boolean ; status : number ; message : string ; data : T ; } ; async function get < T > ( url ) { const response = await fetch ( url , { method : "GET" , } ) ; const resData = ( await response . json ( ) ) as ResponseType < T > ; if ( ! resData . succeed ) { throw resData ; } return resData ; } async function post < T > ( url , data ? ) { const response = await fetch ( url , { method : "POST" , headers : { "Content-Type" : "application/json" , } , body : JSON . stringify ( data ) , } ) ; const resData = ( await response . json ( ) ) as ResponseType < T > ; if ( ! resData . succeed ) { throw resData ; } return resData ; } namespace api { export function getUserInfo ( ) { return get < Model . User > ( "/api/user/me" ) ; } export function signin ( username , password ) { return post ( "/api/user/signin" , { username , password } ) ; } export function signup ( username , password ) { return post ( "/api/user/signup" , { username , password } ) ; } export function signout ( ) { return post ( "/api/user/signout" ) ; } export function checkUsernameUsable ( username ) { return get < boolean > ( "/api/user/checkusername?username=" + username ) ; } export function checkPasswordValid ( password ) { return post < boolean > ( "/api/user/checkpassword" , { password } ) ; } export function updateUserinfo ( username ? , password ? , githubName ? , wxUserId ? ) { return post ( "/api/user/update" , { username , password , githubName , wxUserId , } ) ; } export function getMyMemos ( ) { return get < Model . Memo [ ] > ( "/api/memo/all" ) ; } export function getMyDeletedMemos ( ) { return get < Model . Memo [ ] > ( "/api/memo/deleted" ) ; } export function createMemo ( content ) { return post < Model . Memo > ( "/api/memo/new" , { content } ) ; } export function getMemoById ( id ) { return get < Model . Memo > ( "/api/memo/?id=" + id ) ; } export function hideMemo ( memoId ) { return post ( "/api/memo/hide" , { memoId , } ) ; } export function restoreMemo ( memoId ) { return post ( "/api/memo/restore" , { memoId , } ) ; } export function deleteMemo ( memoId ) { return post ( "/api/memo/delete" , { memoId , } ) ; } export function updateMemo ( memoId , content ) { return post < Model . Memo > ( "/api/memo/update" , { memoId , content } ) ; } export function getLinkedMemos ( memoId ) { return get < Model . Memo [ ] > ( "/api/memo/linked?memoId=" + memoId ) ; } export function removeGithubName ( ) { return post ( "/api/user/updategh" , { githubName : "" } ) ; } export function getMyQueries ( ) { return get < Model . Query [ ] > ( "/api/query/all" ) ; } export function createQuery ( title , querystring ) { return post < Model . Query > ( "/api/query/new" , { title , querystring } ) ; } export function updateQuery ( queryId , title , querystring ) { return post < Model . Query > ( "/api/query/update" , { queryId , title , querystring } ) ; } export function deleteQueryById ( queryId ) { return post ( "/api/query/delete" , { queryId } ) ; } export function pinQuery ( queryId ) { return post ( "/api/query/pin" , { queryId } ) ; } export function unpinQuery ( queryId ) { return post ( "/api/query/unpin" , { queryId } ) ; } } export default api ;