Skip to content
Snippets Groups Projects
ServerHttpService.ts 2.67 KiB
Newer Older
  • Learn to ignore specific revisions
  • fz2907's avatar
    fz2907 committed
    /**
    
    fz2907's avatar
    fz2907 committed
     * This class will handle the http request that send to server.
    
    fz2907's avatar
    fz2907 committed
     * It will help debuging and handle cookie if needed.
     */
    
    import * as Constants from "./Constans";
    
    
    fz2907's avatar
    fz2907 committed
    function Get(path: string) {
        const url = Constants.Server_URL + path;
        console.log("GET from: " + url);
    
    fz2907's avatar
    fz2907 committed
    
    
    fz2907's avatar
    fz2907 committed
        return fetch(url, {
            method: "GET",
        })
            .then((response) => {
                if (response.ok) {
                    return response.json();
                }
                throw new Error(
                    "Unable to receive GET request from server with url:" + url
                );
    
    fz2907's avatar
    fz2907 committed
            })
    
    fz2907's avatar
    fz2907 committed
            .catch((reason) => {
                console.log("Error on GET request", reason);
            });
    }
    
    fz2907's avatar
    fz2907 committed
    
    
    fz2907's avatar
    fz2907 committed
    function Post(path: string, bodyData: any) {
        const url = Constants.Server_URL + path;
        console.log("POST from: " + url);
        return fetch(url, {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
            },
            body: JSON.stringify(bodyData),
        })
            .then((response) => {
                if (response.ok) {
    
    fz2907's avatar
    fz2907 committed
                    return response.json();
                }
    
    fz2907's avatar
    fz2907 committed
                throw new Error(
                    "Unable to receive POST request from server with url:" + url
                );
    
    fz2907's avatar
    fz2907 committed
            })
    
    fz2907's avatar
    fz2907 committed
            .catch((reason) => {
    
    fz2907's avatar
    fz2907 committed
                console.log("Error on POST request", reason);
    
    fz2907's avatar
    fz2907 committed
            });
    }
    
    fz2907's avatar
    fz2907 committed
    
    
    fz2907's avatar
    fz2907 committed
    function Put(path: string, bodyData: any) {
        const url = Constants.Server_URL + path;
        console.log("PUT from: " + url);
        return fetch(url, {
            method: "PUT",
            headers: {
                "Content-Type": "application/json",
            },
            body: JSON.stringify(bodyData),
        })
            .then((response) => {
                if (response.ok) {
    
    fz2907's avatar
    fz2907 committed
                    return response.json();
                }
    
    fz2907's avatar
    fz2907 committed
                throw new Error(
                    "Unable to receive PUT request from server with url:" + url
                );
    
    fz2907's avatar
    fz2907 committed
            })
    
    fz2907's avatar
    fz2907 committed
            .catch((reason) => {
    
    fz2907's avatar
    fz2907 committed
                console.log("Error on PUT request", reason);
    
    fz2907's avatar
    fz2907 committed
            });
    }
    
    fz2907's avatar
    fz2907 committed
    
    
    fz2907's avatar
    fz2907 committed
    function Delete(path: string) {
        const url = Constants.Server_URL + path;
        console.log("Delete from: " + url);
        return fetch(url, {
            method: "Delete",
        })
            .then((response) => {
                if (response.ok) {
    
    fz2907's avatar
    fz2907 committed
                    return response.json();
                }
    
    fz2907's avatar
    fz2907 committed
                throw new Error(
                    "Unable to receive DELETE request from server with url:" + url
                );
    
    fz2907's avatar
    fz2907 committed
            })
    
    fz2907's avatar
    fz2907 committed
            .catch((reason) => {
    
    fz2907's avatar
    fz2907 committed
                console.log("Error on DELETE request", reason);
    
    fz2907's avatar
    fz2907 committed
            });
    
    fz2907's avatar
    fz2907 committed
    }
    
    
    fz2907's avatar
    fz2907 committed
    
    export { Get, Post, Put, Delete };
    
    
    fz2907's avatar
    fz2907 committed
    /*
    Change logs:
    Date        |       Author          |   Description
    2022-10-12  |    Fangzheng Zhang    |    create class and init
    2022-10-17  |    Fangzheng Zhang    |    change to TS
    
    
    fz2907's avatar
    fz2907 committed
     */