aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/utils/requests.ts
blob: acbde374978dcabe9a3ff8cc32c54bcf7655feae (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
37
38
39
40
41
42
43
44
45
46
47
48
49
import Config from "../config";
import * as CompileConfig from "../../config.json";
import { FetchResponse, sendRequestToCustomServer } from "../../maze-utils/src/background-request-proxy";

/**
 * Sends a request to a custom server
 * 
 * @param type The request type. "GET", "POST", etc.
 * @param address The address to add to the SponsorBlock server address
 * @param callback 
 */    
export function asyncRequestToCustomServer(type: string, url: string, data = {}, headers = {}): Promise<FetchResponse> {
    return sendRequestToCustomServer(type, url, data, headers);
}

/**
 * Sends a request to the SponsorBlock server with address added as a query
 * 
 * @param type The request type. "GET", "POST", etc.
 * @param address The address to add to the SponsorBlock server address
 * @param callback 
 */    
export async function asyncRequestToServer(type: string, address: string, data = {}, headers = {}): Promise<FetchResponse> {
    const serverAddress = Config.config.testingServer ? CompileConfig.testingServerAddress : Config.config.serverAddress;

    console.log(address, headers)

    return await (asyncRequestToCustomServer(type, serverAddress + address, data, headers));
}

/**
 * Sends a request to the SponsorBlock server with address added as a query
 * 
 * @param type The request type. "GET", "POST", etc.
 * @param address The address to add to the SponsorBlock server address
 * @param callback 
 */
export function sendRequestToServer(type: string, address: string, callback?: (response: FetchResponse) => void): void {
    const serverAddress = Config.config.testingServer ? CompileConfig.testingServerAddress : Config.config.serverAddress;

    // Ask the background script to do the work
    chrome.runtime.sendMessage({
        message: "sendRequest",
        type,
        url: serverAddress + address
    }, (response) => {
        callback(response);
    });
}