UNPKG

mattermost-redux

Version:

Common code (API client, Redux stores, logic, utility functions) for building a Mattermost client

133 lines (132 loc) 4.22 kB
"use strict"; // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. Object.defineProperty(exports, "__esModule", { value: true }); exports.FormattedError = void 0; exports.forceLogoutIfNecessary = forceLogoutIfNecessary; exports.requestData = requestData; exports.requestSuccess = requestSuccess; exports.requestFailure = requestFailure; exports.bindClientFunc = bindClientFunc; exports.debounce = debounce; const action_types_1 = require("mattermost-redux/action_types"); const client_1 = require("mattermost-redux/client"); const errors_1 = require("./errors"); const HTTP_UNAUTHORIZED = 401; function forceLogoutIfNecessary(err, dispatch, getState) { const { currentUserId } = getState().entities.users; if ('status_code' in err && err.status_code === HTTP_UNAUTHORIZED && err.url && err.url.indexOf('/login') === -1 && currentUserId) { client_1.Client4.setToken(''); dispatch({ type: action_types_1.UserTypes.LOGOUT_SUCCESS, data: {} }); } } function dispatcher(type, data, dispatch) { if (type.indexOf('SUCCESS') === -1) { // we don't want to pass the data for the request types dispatch(requestSuccess(type, data)); } else { dispatch(requestData(type)); } } function requestData(type) { return { type, data: null, }; } function requestSuccess(type, data) { return { type, data, }; } function requestFailure(type, error) { return { type, error, }; } /** * Returns an ActionFunc which calls a specfied (client) function and * dispatches the specifed actions on request, success or failure. * * @export * @param {Object} obj an object for destructirung required properties * @param {() => Promise<mixed>} obj.clientFunc clientFunc to execute * @param {ActionType} obj.onRequest ActionType to dispatch on request * @param {(ActionType | Array<ActionType>)} obj.onSuccess ActionType to dispatch on success * @param {ActionType} obj.onFailure ActionType to dispatch on failure * @param {...Array<any>} obj.params * @returns {ActionFunc} ActionFunc */ function bindClientFunc({ clientFunc, onRequest, onSuccess, onFailure, params, }) { return async (dispatch, getState) => { if (onRequest) { dispatch(requestData(onRequest)); } let data; try { if (params) { data = await clientFunc(...params); } else { data = await clientFunc(); } } catch (error) { forceLogoutIfNecessary(error, dispatch, getState); if (onFailure) { dispatch(requestFailure(onFailure, error)); } dispatch((0, errors_1.logError)(error)); return { error }; } if (Array.isArray(onSuccess)) { onSuccess.forEach((s) => { dispatcher(s, data, dispatch); }); } else if (onSuccess) { dispatcher(onSuccess, data, dispatch); } return { data }; }; } // Debounce function based on underscores modified to use es6 and a cb function debounce(func, wait, immediate, cb) { let timeout; return function fx(...args) { const runLater = () => { timeout = null; if (!immediate) { Reflect.apply(func, null, args); if (cb) { cb(); } } }; const callNow = immediate && !timeout; if (timeout) { clearTimeout(timeout); } timeout = setTimeout(runLater, wait); if (callNow) { Reflect.apply(func, null, args); if (cb) { cb(); } } }; } class FormattedError extends Error { intl; constructor(id, defaultMessage, values = {}) { super(defaultMessage); this.intl = { id, defaultMessage, values, }; } } exports.FormattedError = FormattedError;