import axios from 'axios';
export var STORAGE_KEY_TOKEN = 'token';
export function getLocalToken() {
    return localStorage.getItem(STORAGE_KEY_TOKEN);
}
export function removeLocalToken() {
    localStorage.removeItem(STORAGE_KEY_TOKEN);
}
var Request = /** @class */ (function () {
    function Request(config) {
        this.baseConfig = { timeout: 30000 };
        this.instance = axios.create(Object.assign(this.baseConfig, config));
        this.instance.interceptors.request.use(function (config) {
            var token = localStorage.getItem('token');
            var organizationId = localStorage.getItem('organization:id');
            if (token) {
                config.headers.Authorization = "Bearer ".concat(token);
            }
            if (organizationId) {
                config.headers['OPEN-PLATFORM-ORGANIZATION'] = organizationId;
            }
            return config;
        }, function (err) {
            // error alert
            return Promise.reject(err);
        });
        this.instance.interceptors.response.use(function (res) {
            return res;
        }, function (err) {
            var message = '';
            switch (err.response.status) {
                case 401:
                    message = 'request auth error (401)';
                    removeLocalToken();
                    break;
                default:
                    message = "request error (".concat(err.response.status, ")!");
            }
            console.log('axios error:', message);
            /// global error alert
            return Promise.reject(err.response);
        });
    }
    Request.prototype.request = function (config) {
        return this.instance.request(config);
    };
    Request.prototype.get = function (url, config) {
        return this.instance.get(url, config);
    };
    Request.prototype.post = function (url, data, config) {
        return this.instance.post(url, data, config);
    };
    Request.prototype.put = function (url, data, config) {
        return this.instance.put(url, data, config);
    };
    Request.prototype.delete = function (url, config) {
        return this.instance.delete(url, config);
    };
    return Request;
}());
export { Request };
export default new Request({});
