summaryrefslogtreecommitdiff
path: root/services/Requests.qml
blob: 36e912097b31129770b5b4a04e265f3bf7ed20bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
pragma Singleton

import "root:/config"
import "root:/utils"
import Quickshell

Singleton {
    id: root

    function get(url: string, callback: var): void {
        const xhr = new XMLHttpRequest();

        const cleanup = () => {
            xhr.abort();
            xhr.onreadystatechange = null;
            xhr.onerror = null;
        };

        xhr.open("GET", url, true);
        xhr.onreadystatechange = () => {
            if (xhr.readyState === XMLHttpRequest.DONE) {
                if (xhr.status === 200)
                    callback(xhr.responseText);
                else
                    console.warn(`[REQUESTS] GET request to ${url} failed with status ${xhr.status}`);
                cleanup();
            }
        };
        xhr.onerror = () => {
            console.warn(`[REQUESTS] GET request to ${url} failed`);
            cleanup();
        };

        xhr.send();
    }
}