diff --git a/src/libs/Log.js b/src/libs/Log.ts similarity index 70% rename from src/libs/Log.js rename to src/libs/Log.ts index e51fb74aedd5..cf139eec2682 100644 --- a/src/libs/Log.js +++ b/src/libs/Log.ts @@ -1,45 +1,43 @@ // Making an exception to this rule here since we don't need an "action" for Log and Log should just be used directly. Creating a Log // action would likely cause confusion about which one to use. But most other API methods should happen inside an action file. /* eslint-disable rulesdir/no-api-in-views */ +import {Merge} from 'type-fest'; import Logger from 'expensify-common/lib/Logger'; import getPlatform from './getPlatform'; import pkg from '../../package.json'; import requireParameters from './requireParameters'; import * as Network from './Network'; -let timeout = null; +let timeout: NodeJS.Timeout; -/** - * @param {Object} parameters - * @param {String} parameters.expensifyCashAppVersion - * @param {Object[]} parameters.logPacket - * @returns {Promise} - */ -function LogCommand(parameters) { +type LogCommandParameters = { + expensifyCashAppVersion: string; + logPacket: string; +}; + +function LogCommand(parameters: LogCommandParameters): Promise<{requestID: string}> { const commandName = 'Log'; requireParameters(['logPacket', 'expensifyCashAppVersion'], parameters, commandName); // Note: We are forcing Log to run since it requires no authToken and should only be queued when we are offline. // Non-cancellable request: during logout, when requests are cancelled, we don't want to cancel any remaining logs - return Network.post(commandName, {...parameters, forceNetworkRequest: true, canCancel: false}); + return Network.post(commandName, {...parameters, forceNetworkRequest: true, canCancel: false}) as Promise<{requestID: string}>; } +// eslint-disable-next-line +type ServerLoggingCallbackOptions = {api_setCookie: boolean; logPacket: string}; +type RequestParams = Merge; + /** * Network interface for logger. - * - * @param {Logger} logger - * @param {Object} params - * @param {Object} params.parameters - * @param {String} params.message - * @return {Promise} */ -function serverLoggingCallback(logger, params) { - const requestParams = params; +function serverLoggingCallback(logger: Logger, params: ServerLoggingCallbackOptions): Promise<{requestID: string}> { + const requestParams = params as RequestParams; requestParams.shouldProcessImmediately = false; requestParams.shouldRetry = false; requestParams.expensifyCashAppVersion = `expensifyCash[${getPlatform()}]${pkg.version}`; if (requestParams.parameters) { - requestParams.parameters = JSON.stringify(params.parameters); + requestParams.parameters = JSON.stringify(requestParams.parameters); } clearTimeout(timeout); timeout = setTimeout(() => logger.info('Flushing logs older than 10 minutes', true, {}, true), 10 * 60 * 1000); diff --git a/src/libs/Permissions.js b/src/libs/Permissions.js deleted file mode 100644 index 0294236b1cd7..000000000000 --- a/src/libs/Permissions.js +++ /dev/null @@ -1,126 +0,0 @@ -import _ from 'underscore'; -import CONST from '../CONST'; - -/** - * @private - * @param {Array} betas - * @returns {Boolean} - */ -function canUseAllBetas(betas) { - return _.contains(betas, CONST.BETAS.ALL); -} - -/** - * @param {Array} betas - * @returns {Boolean} - */ -function canUseChronos(betas) { - return _.contains(betas, CONST.BETAS.CHRONOS_IN_CASH) || canUseAllBetas(betas); -} - -/** - * @param {Array} betas - * @returns {Boolean} - */ -function canUsePayWithExpensify(betas) { - return _.contains(betas, CONST.BETAS.PAY_WITH_EXPENSIFY) || canUseAllBetas(betas); -} - -/** - * @param {Array} betas - * @returns {Boolean} - */ -function canUseDefaultRooms(betas) { - return _.contains(betas, CONST.BETAS.DEFAULT_ROOMS) || canUseAllBetas(betas); -} - -/** - * IOU Send feature is temporarily disabled. - * - * @returns {Boolean} - */ -function canUseIOUSend() { - return false; -} - -/** - * @param {Array} betas - * @returns {Boolean} - */ -function canUseWallet(betas) { - return _.contains(betas, CONST.BETAS.BETA_EXPENSIFY_WALLET) || canUseAllBetas(betas); -} - -/** - * @param {Array} betas - * @returns {Boolean} - */ -function canUseCommentLinking(betas) { - return _.contains(betas, CONST.BETAS.BETA_COMMENT_LINKING) || canUseAllBetas(betas); -} - -/** - * We're requiring you to be added to the policy rooms beta on dev, - * since contributors have been reporting a number of false issues related to the feature being under development. - * See https://expensify.slack.com/archives/C01GTK53T8Q/p1641921996319400?thread_ts=1641598356.166900&cid=C01GTK53T8Q - * @param {Array} betas - * @returns {Boolean} - */ -function canUsePolicyRooms(betas) { - return _.contains(betas, CONST.BETAS.POLICY_ROOMS) || canUseAllBetas(betas); -} - -/** - * @param {Array} betas - * @returns {Boolean} - */ -function canUseTasks(betas) { - return _.contains(betas, CONST.BETAS.TASKS) || canUseAllBetas(betas); -} - -/** - * @param {Array} betas - * @returns {Boolean} - */ -function canUseCustomStatus(betas) { - return _.contains(betas, CONST.BETAS.CUSTOM_STATUS) || canUseAllBetas(betas); -} - -/** - * @param {Array} betas - * @returns {Boolean} - */ -function canUseCategories(betas) { - return _.contains(betas, CONST.BETAS.NEW_DOT_CATEGORIES) || canUseAllBetas(betas); -} - -/** - * @param {Array} betas - * @returns {Boolean} - */ -function canUseTags(betas) { - return _.contains(betas, CONST.BETAS.NEW_DOT_TAGS) || canUseAllBetas(betas); -} - -/** - * Link previews are temporarily disabled. - * @returns {Boolean} - */ -function canUseLinkPreviews() { - return false; -} - -export default { - canUseChronos, - canUsePayWithExpensify, - canUseDefaultRooms, - canUseIOUSend, - canUseWallet, - canUseCommentLinking, - canUsePolicyRooms, - canUseTasks, - canUseCustomStatus, - canUseCategories, - canUseTags, - canUseLinkPreviews, -}; diff --git a/src/libs/Permissions.ts b/src/libs/Permissions.ts new file mode 100644 index 000000000000..05322472a407 --- /dev/null +++ b/src/libs/Permissions.ts @@ -0,0 +1,80 @@ +import CONST from '../CONST'; +import Beta from '../types/onyx/Beta'; + +function canUseAllBetas(betas: Beta[]): boolean { + return betas?.includes(CONST.BETAS.ALL); +} + +function canUseChronos(betas: Beta[]): boolean { + return betas?.includes(CONST.BETAS.CHRONOS_IN_CASH) || canUseAllBetas(betas); +} + +function canUsePayWithExpensify(betas: Beta[]): boolean { + return betas?.includes(CONST.BETAS.PAY_WITH_EXPENSIFY) || canUseAllBetas(betas); +} + +function canUseDefaultRooms(betas: Beta[]): boolean { + return betas?.includes(CONST.BETAS.DEFAULT_ROOMS) || canUseAllBetas(betas); +} + +/** + * IOU Send feature is temporarily disabled. + */ +function canUseIOUSend(): boolean { + return false; +} + +function canUseWallet(betas: Beta[]): boolean { + return betas?.includes(CONST.BETAS.BETA_EXPENSIFY_WALLET) || canUseAllBetas(betas); +} + +function canUseCommentLinking(betas: Beta[]): boolean { + return betas?.includes(CONST.BETAS.BETA_COMMENT_LINKING) || canUseAllBetas(betas); +} + +/** + * We're requiring you to be added to the policy rooms beta on dev, + * since contributors have been reporting a number of false issues related to the feature being under development. + * See https://expensify.slack.com/archives/C01GTK53T8Q/p1641921996319400?thread_ts=1641598356.166900&cid=C01GTK53T8Q + */ +function canUsePolicyRooms(betas: Beta[]): boolean { + return betas?.includes(CONST.BETAS.POLICY_ROOMS) || canUseAllBetas(betas); +} + +function canUseTasks(betas: Beta[]): boolean { + return betas?.includes(CONST.BETAS.TASKS) || canUseAllBetas(betas); +} + +function canUseCustomStatus(betas: Beta[]): boolean { + return betas?.includes(CONST.BETAS.CUSTOM_STATUS) || canUseAllBetas(betas); +} + +function canUseCategories(betas: Beta[]): boolean { + return betas?.includes(CONST.BETAS.NEW_DOT_CATEGORIES) || canUseAllBetas(betas); +} + +function canUseTags(betas: Beta[]): boolean { + return betas?.includes(CONST.BETAS.NEW_DOT_TAGS) || canUseAllBetas(betas); +} + +/** + * Link previews are temporarily disabled. + */ +function canUseLinkPreviews(): boolean { + return false; +} + +export default { + canUseChronos, + canUsePayWithExpensify, + canUseDefaultRooms, + canUseIOUSend, + canUseWallet, + canUseCommentLinking, + canUsePolicyRooms, + canUseTasks, + canUseCustomStatus, + canUseCategories, + canUseTags, + canUseLinkPreviews, +}; diff --git a/src/libs/__mocks__/Log.js b/src/libs/__mocks__/Log.js deleted file mode 100644 index 179a665d2bd9..000000000000 --- a/src/libs/__mocks__/Log.js +++ /dev/null @@ -1,9 +0,0 @@ -// Set up manual mocks for any Logging methods that are supposed hit the 'server', -// this is needed because before, the Logging queue would get flushed while tests were running, -// causing unexpected calls to HttpUtils.xhr() which would cause mock mismatches and flaky tests. -export default { - info: (message) => console.debug(`[info] ${message} (mocked)`), - alert: (message) => console.debug(`[alert] ${message} (mocked)`), - warn: (message) => console.debug(`[warn] ${message} (mocked)`), - hmmm: (message) => console.debug(`[hmmm] ${message} (mocked)`), -}; diff --git a/src/libs/__mocks__/Log.ts b/src/libs/__mocks__/Log.ts new file mode 100644 index 000000000000..39336db1fa51 --- /dev/null +++ b/src/libs/__mocks__/Log.ts @@ -0,0 +1,9 @@ +// Set up manual mocks for any Logging methods that are supposed hit the 'server', +// this is needed because before, the Logging queue would get flushed while tests were running, +// causing unexpected calls to HttpUtils.xhr() which would cause mock mismatches and flaky tests. +export default { + info: (message: string) => console.debug(`[info] ${message} (mocked)`), + alert: (message: string) => console.debug(`[alert] ${message} (mocked)`), + warn: (message: string) => console.debug(`[warn] ${message} (mocked)`), + hmmm: (message: string) => console.debug(`[hmmm] ${message} (mocked)`), +}; diff --git a/src/libs/__mocks__/Permissions.js b/src/libs/__mocks__/Permissions.ts similarity index 54% rename from src/libs/__mocks__/Permissions.js rename to src/libs/__mocks__/Permissions.ts index fffaea5793d4..2c062590573e 100644 --- a/src/libs/__mocks__/Permissions.js +++ b/src/libs/__mocks__/Permissions.ts @@ -1,5 +1,5 @@ -import _ from 'underscore'; import CONST from '../../CONST'; +import Beta from '../../types/onyx/Beta'; /** * This module is mocked in tests because all the permission methods call canUseAllBetas() and that will @@ -10,8 +10,8 @@ import CONST from '../../CONST'; export default { ...jest.requireActual('../Permissions'), - canUseDefaultRooms: (betas) => _.contains(betas, CONST.BETAS.DEFAULT_ROOMS), - canUsePolicyRooms: (betas) => _.contains(betas, CONST.BETAS.POLICY_ROOMS), - canUseIOUSend: (betas) => _.contains(betas, CONST.BETAS.IOU_SEND), - canUseCustomStatus: (betas) => _.contains(betas, CONST.BETAS.CUSTOM_STATUS), + canUseDefaultRooms: (betas: Beta[]) => betas.includes(CONST.BETAS.DEFAULT_ROOMS), + canUsePolicyRooms: (betas: Beta[]) => betas.includes(CONST.BETAS.POLICY_ROOMS), + canUseIOUSend: (betas: Beta[]) => betas.includes(CONST.BETAS.IOU_SEND), + canUseCustomStatus: (betas: Beta[]) => betas.includes(CONST.BETAS.CUSTOM_STATUS), };