import Source from './source.js';

export default class AjaxSource extends Source {
    constructor(endpoint, settings = {}) {
        super(settings);
        this.endpoint = endpoint;
        this.cache = {};
    }

    refresh(query) {
        if (query.length < 2) {
            return this.close();
        }

        if (this.timeout) {
            clearTimeout(this.timeout);
        }

        this.query = query;

        if (this.cache[query]) {
            this.data = this.cache[query];
            delete this.query;
            this.update();
            return;
        }

        this.timeout = setTimeout(() => {
            fetch(this.endpoint + '?q=' + query, {
                headers: {
                    Accept: 'application/json'
                }
            })
                .then(res => {
                    if (res.status < 200 || res.status >= 400) {
                        throw new Error(
                            `The request status code is ${res.status}`
                        );
                    }

                    return res;
                })
                .then(res => res.json())
                .then(data => {
                    this.load(data);
                    this.cache[query] = this.data;
                    this.update();

                    clearTimeout(this.timeout);
                    delete this.timeout;

                    if (this.query && query !== this.query) {
                        query = this.query;
                        delete this.query;
                        return this.update();
                    }

                    delete this.query;
                });
        }, 200);
    }
}

@@ -639,13 +639,15 @@
     
-fetch
+getJson
 (thi
@@ -676,466 +676,57 @@
 uery
-, %7B%0A                headers: %7B%0A                    Accept: 'application/json'%0A                %7D%0A            %7D)%0A                .then(res =%3E %7B%0A                    if (res.status %3C 200 %7C%7C res.status %3E= 400) %7B%0A                        throw new Error(%0A                            %60The request status code is $%7Bres.status%7D%60%0A                        );%0A                    %7D%0A%0A                    return res;%0A                %7D)%0A                .then(res =%3E res.json(
+)%0A                .catch(err =%3E console.error(err
 ))%0A 
@@ -1268,12 +1268,12 @@
 0);%0A
-
     %7D%0A%7D%0A
@@ -1272,8 +1272,541 @@
     %7D%0A%7D%0A
+%0Afunction getJson(url) %7B%0A    return new Promise((resolve, reject) =%3E %7B%0A        const request = new XMLHttpRequest();%0A%0A        request.open('GET', url, true);%0A        request.setRequestHeader('Accept', 'application/json');%0A%0A        request.onload = () =%3E %7B%0A            if (request.status %3E= 200 && request.status %3C 400) %7B%0A                resolve(JSON.parse(request.responseText));%0A            %7D else %7B%0A                reject(%60The request status code is $%7Brequest.status%7D%60);%0A            %7D%0A        %7D;%0A%0A        request.send();%0A    %7D);%0A%7D%0A

use classic xmlHtmlRequest instead fetch due 403 code issues