View | Details | Raw Unified | Return to bug 36084
Collapse All | Expand All

(-)a/koha-tmpl/intranet-tmpl/prog/js/fetch/api-client.js (+5 lines)
Line 0 Link Here
1
import ArticleRequestAPIClient from "./article-request-api-client.js";
2
3
export const APIClient = {
4
    article_request: new ArticleRequestAPIClient(),
5
};
(-)a/koha-tmpl/intranet-tmpl/prog/js/fetch/article-request-api-client.js (+62 lines)
Line 0 Link Here
1
import HttpClient from "./http-client.js";
2
3
export class ArticleRequestAPIClient extends HttpClient {
4
    constructor() {
5
        super({
6
            baseURL: "/cgi-bin/koha/svc/article_request",
7
            headers: {
8
                "Content-Type":
9
                    "application/x-www-form-urlencoded;charset=utf-8",
10
            },
11
        });
12
    }
13
14
    get articleRequests() {
15
        return {
16
            process: id =>
17
                this.post({
18
                    endpoint: "",
19
                    body: "id=%s&op=%s".format(
20
                        id,
21
                        'cud-process',
22
                    ),
23
                }),
24
            complete: id =>
25
                this.post({
26
                    endpoint: "",
27
                    body: "id=%s&op=%s".format(
28
                        id,
29
                        'cud-complete',
30
                    ),
31
                }),
32
            pending: id =>
33
                this.post({
34
                    endpoint: "",
35
                    body: "id=%s&op=%s".format(
36
                        id,
37
                        'cud-pending',
38
                    ),
39
                }),
40
            update_urls: (id, urls) =>
41
                this.post({
42
                    endpoint: "",
43
                    body: "id=%s&urls=%s&op=%s".format(
44
                        id,
45
                        urls,
46
                        'cud-update_urls'
47
                    ),
48
                }),
49
            update_library_id: (id, library_id) =>
50
                this.post({
51
                    endpoint: "",
52
                    body: "id=%s&library_id=%s&op=%s".format(
53
                        id,
54
                        library_id,
55
                        'cud-update_library_id'
56
                    ),
57
                }),
58
        };
59
    }
60
}
61
62
export default ArticleRequestAPIClient;
(-)a/koha-tmpl/intranet-tmpl/prog/js/fetch/http-client.js (-1 / +83 lines)
Line 0 Link Here
0
- 
1
//import { setError, submitting, submitted } from "../messages";
2
3
class HttpClient {
4
    constructor(options = {}) {
5
        this._baseURL = options.baseURL || "";
6
        this._headers = options.headers || {
7
            "Content-Type": "application/json;charset=utf-8",
8
        };
9
        this.csrf_token = $('meta[name="csrf-token"]').attr('content');
10
    }
11
12
    async _fetchJSON(
13
        endpoint,
14
        headers = {},
15
        options = {},
16
        return_response = false,
17
        mark_submitting = false
18
    ) {
19
        let res, error;
20
        //if (mark_submitting) submitting();
21
        await fetch(this._baseURL + endpoint, {
22
            ...options,
23
            headers: { ...this._headers, ...headers },
24
        })
25
            .then(response => {
26
                if (!response.ok) {
27
                    return response.text().then(text => {
28
                        let message;
29
                        if (text) {
30
                            let json = JSON.parse(text);
31
                            message =
32
                                json.error ||
33
                                json.errors.map(e => e.message).join("\n") ||
34
                                json;
35
                        } else {
36
                            message = response.statusText;
37
                        }
38
                        throw new Error(message);
39
                    });
40
                }
41
                return return_response ? response : response.json();
42
            })
43
            .then(result => {
44
                res = result;
45
            })
46
            .catch(err => {
47
                error = err;
48
                //setError(err);
49
                console.error(err);
50
            })
51
            .then(() => {
52
                //if (mark_submitting) submitted();
53
            });
54
55
        if (error) throw Error(error);
56
57
        return res;
58
    }
59
60
    post(params = {}) {
61
        const body = params.body
62
            ? typeof params.body === "string"
63
                ? params.body
64
                : JSON.stringify(params.body)
65
            : params.data || undefined;
66
        let csrf_token = { csrf_token: this.csrf_token };
67
        let headers = { ...csrf_token, ...params.headers };
68
        return this._fetchJSON(
69
            params.endpoint,
70
            headers,
71
            {
72
                ...params.options,
73
                body,
74
                method: "POST",
75
            },
76
            false,
77
            true
78
        );
79
    }
80
81
}
82
83
export default HttpClient;

Return to bug 36084