Sixtens hemsida Uppgifter Blogg Om

VED House bokningssystem

Gå till sida

Källkod

api/

database.php

api.php

ved/

monthBookingData.php

openingHoursException.php

bookingData.php

dayTimeframes.php

bookings.php

admin/

revokeToken.php

authorization.php

login.php

modules/

vedDatabase.php

utility.php

gyar/

log.php

auth/

revokeToken.php

authorization.php

login.php

station/

settings.php

log.php

modules/

gyarDatabase.php

databaseConnection.php

database.php

utility.php

credentials.json

weather/

latest.php

old.php

admin/

index.html

changeTimetable.html

login.html

js/

login.js

modules.js

admin.js

header.js

changeTimetable.js

cron/

vedClearExpiredTokens.php

weuweb01/ved/admin/js/modules.js

1 lines
export class API {
    baseApiEndpoint;
    constructor(apiEndpoint) {
        this.baseApiEndpoint = apiEndpoint;
    }

    async fetch(endpoint, method = "GET", requestInit = null, relocateIfUnauthorized = true) {
        if (!requestInit)
            requestInit = {};
        requestInit["method"] = method;
        const response = await fetch(this.baseApiEndpoint + endpoint, requestInit);
        if (response.status == 401 && relocateIfUnauthorized) {
            Cookies.deleteEntry("token");
            window.location = "login.html";
            return;
        }
        return response;
    }
}

export const vedAPI = new API("https://als070804sn.hemsida.eu/api/ved/");
//export const vedAPI = new API("http://localhost/api/ved/");

export class Cookies {
    static setValue(key, value, expiresInSec = 86400) {
        let expirationDate = new Date()
        expirationDate.setTime(expirationDate.getTime() + expiresInSec * 1000);
        document.cookie = `${key}=${value}; expires=${expirationDate}; path:/`;
    }

    static getValue(key) {
        let name = key + "=";
        let decodedCookie = decodeURIComponent(document.cookie);
        let segments = decodedCookie.split(';');
        for (let i = 0; i < segments.length; i++) {
            let segment = segments[i];
            while (segment.charAt(0) == ' ') {
                segment = segment.substring(1);
            }
            if (segment.indexOf(name) == 0) {
                return segment.substring(name.length, segment.length);
            }
        }
        return "";
    }

    static deleteEntry(key) {
        document.cookie = `${key}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path:/`;
    }
}

export function getAuthHeader() {
    let token = Cookies.getValue("token");
    if (!token)
        window.location = "login.html";
    return `Bearer ${token}`;
}

export function httpErrorMessage(code) {
    switch (code) {
        case 400:
            return "Felaktigt värde.";
        case 401:
        case 403:
            return "Saknar åtkomst.";
        case 404:
        case 503:
            return "Kunde inte ansluta till server. Försök igen senare.";
        case 405:
            return "Metod ej accepterad.";
        case 500:
            return "Internt serverfel.";
        default:
            return "Fel.";
    }
}