aboutsummaryrefslogtreecommitdiff
path: root/resources/assets/javascripts/lib/jsonapi.ts
blob: 80176ccb6ca43091ff0477ed1f445b4d48b600e3 (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: number = 1) {
        super(`jsonapi.php/v${version}`);
    }

    encodeData (data: any, method: string): any {
        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: string, options: any = {}) {
        options.contentType = 'application/vnd.api+json';
        return super.request(url, options);
    }
}

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