aboutsummaryrefslogtreecommitdiff
path: root/resources/assets/javascripts/lib/jsonapi.js
blob: f3217bc6e6e50cd3ed17763600e364dc4333ad63 (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
import AbstractAPI from './abstract-api.js';

// Actual JSONAPI object
class JSONAPI extends AbstractAPI
{
    constructor(version = 1) {
        super(`jsonapi.php/v${version}`);
    }

    encodeData (data, method) {
        data = super.encodeData(data);

        if (['DELETE', 'GET', 'HEAD'].includes(method)) {
            return data;
        }

        if (Object.keys(data).length === 0) {
            return null;
        }

        return JSON.stringify(data);
    }

    request (url, options = {}) {
        options.contentType = 'application/vnd.api+json';
        return super.request(url, options);
    }
}

export default JSONAPI;
export const jsonapi = new JSONAPI();