Test Setup Failed
Push — master ( 624702...8ee19e )
by recca
02:47 queued 19s
created

resources/assets/js/commands/command.js   A

Size

Lines of Code 65

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
nc 1
dl 0
loc 65
rs 10
c 1
b 0
f 0
noi 2
1
import '../jquery';
2
import '../polyfill';
3
4
export default class Command {
5
    constructor(api, options) {
6
        Object.assign(this, {
7
            api: api,
8
            requestId: 0
9
        });
10
    }
11
12
    match(name) {
13
        return false;
14
    }
15
16
    call(cmd) {
17
        this.api.loading.show();
18
        this.makeRequest(cmd.command).then((response) => {
19
            let responseResult = response.result ? response.result : response;
20
            this.api.loading.hide();
21
            this.api.echo(responseResult);
22
            this.api.serverInfo();
23
        }, (response) => {
24
            let responseResult = response.result ? response.result : response;
25
            this.api.loading.hide();
26
            this.api.echo(responseResult);
27
            this.api.serverInfo();
28
        });
29
    }
30
31
    addslashes(str) {
32
        return (str + '').replace(/[\\"']/g, '\\$&').replace(/\u0000/g, '\\0');
33
    }
34
35
    makeRequest(command) {
36
        return new Promise((resolve, reject) => {
37
            $.ajax({
38
                url: this.api.options.endpoint,
39
                dataType: 'json',
40
                type: 'post',
41
                headers: {
42
                    'X-CSRF-TOKEN': this.api.options.csrfToken
43
                },
44
                data: {
45
                    jsonrpc: '2.0',
46
                    id: ++this.requestId,
47
                    command: command
48
                },
49
                success: (response) => {
50
                    if (response.error !== 0) {
51
                        reject(response);
52
                        return;
53
                    }
54
55
                    resolve(response);
56
                },
57
                error: (jqXhr, json, errorThrown) => {
58
                    reject({
59
                        result: `${jqXhr.status}: ${errorThrown}`
60
                    });
61
                }
62
            });
63
        });
64
    }
65
}
66