diff --git a/.eslintignore b/.eslintignore index 956c7ea05..820e4099e 100644 --- a/.eslintignore +++ b/.eslintignore @@ -721,6 +721,7 @@ packages/app-mobile/utils/polyfills/bufferPolyfill.js packages/app-mobile/utils/polyfills/index.js packages/app-mobile/utils/setupNotifications.js packages/app-mobile/utils/shareHandler.js +packages/app-mobile/utils/shim-init-react.js packages/app-mobile/utils/showMessageBox.js packages/app-mobile/utils/testing/createMockReduxStore.js packages/app-mobile/utils/types.js diff --git a/.gitignore b/.gitignore index 38f234baa..e0f795496 100644 --- a/.gitignore +++ b/.gitignore @@ -700,6 +700,7 @@ packages/app-mobile/utils/polyfills/bufferPolyfill.js packages/app-mobile/utils/polyfills/index.js packages/app-mobile/utils/setupNotifications.js packages/app-mobile/utils/shareHandler.js +packages/app-mobile/utils/shim-init-react.js packages/app-mobile/utils/showMessageBox.js packages/app-mobile/utils/testing/createMockReduxStore.js packages/app-mobile/utils/types.js diff --git a/packages/app-mobile/root.tsx b/packages/app-mobile/root.tsx index 1405d8f8b..7afdce2f9 100644 --- a/packages/app-mobile/root.tsx +++ b/packages/app-mobile/root.tsx @@ -41,7 +41,7 @@ const { BackButtonService } = require('./services/back-button.js'); import NavService from '@joplin/lib/services/NavService'; import { createStore, applyMiddleware, Dispatch } from 'redux'; import reduxSharedMiddleware from '@joplin/lib/components/shared/reduxSharedMiddleware'; -const { shimInit } = require('./utils/shim-init-react.js'); +import shimInit from './utils/shim-init-react'; const { AppNav } = require('./components/app-nav.js'); import Note from '@joplin/lib/models/Note'; import Folder from '@joplin/lib/models/Folder'; diff --git a/packages/app-mobile/utils/shim-init-react.js b/packages/app-mobile/utils/shim-init-react.ts similarity index 87% rename from packages/app-mobile/utils/shim-init-react.js rename to packages/app-mobile/utils/shim-init-react.ts index 16bd2b053..d8529fbe8 100644 --- a/packages/app-mobile/utils/shim-init-react.js +++ b/packages/app-mobile/utils/shim-init-react.ts @@ -1,18 +1,19 @@ -const shim = require('@joplin/lib/shim').default; +import shim from '@joplin/lib/shim'; const { GeolocationReact } = require('./geolocation-react.js'); -const PoorManIntervals = require('@joplin/lib/PoorManIntervals').default; -const RNFetchBlob = require('rn-fetch-blob').default; -const { generateSecureRandom } = require('react-native-securerandom'); -const FsDriverRN = require('./fs-driver/fs-driver-rn').default; -const { Buffer } = require('buffer'); -const { Linking, Platform } = require('react-native'); -const showMessageBox = require('./showMessageBox.js').default; -const mimeUtils = require('@joplin/lib/mime-utils.js'); -const { basename, fileExtension } = require('@joplin/lib/path-utils'); -const uuid = require('@joplin/lib/uuid').default; -const Resource = require('@joplin/lib/models/Resource').default; -const { getLocales } = require('react-native-localize'); -const { setLocale, defaultLocale, closestSupportedLocale } = require('@joplin/lib/locale'); +import PoorManIntervals from '@joplin/lib/PoorManIntervals'; +import RNFetchBlob from 'rn-fetch-blob'; +import { generateSecureRandom } from 'react-native-securerandom'; +import FsDriverRN from './fs-driver/fs-driver-rn'; +import { Buffer } from 'buffer'; +import { Linking, Platform } from 'react-native'; +import showMessageBox from './showMessageBox.js'; +import * as mimeUtils from '@joplin/lib/mime-utils'; +import { basename, fileExtension } from '@joplin/lib/path-utils'; +import uuid from '@joplin/lib/uuid'; +import Resource from '@joplin/lib/models/Resource'; +import { getLocales } from 'react-native-localize'; +import { setLocale, defaultLocale, closestSupportedLocale } from '@joplin/lib/locale'; +import type SettingType from '@joplin/lib/models/Setting'; const injectedJs = { webviewLib: require('@joplin/lib/rnInjectedJs/webviewLib'), @@ -22,7 +23,7 @@ const injectedJs = { noteBodyViewerBundle: require('../lib/rnInjectedJs/noteBodyViewerBundle.bundle'), }; -function shimInit() { +export default function shimInit() { shim.Geolocation = GeolocationReact; shim.sjclModule = require('@joplin/lib/vendor/sjcl-rn.js'); @@ -33,7 +34,7 @@ function shimInit() { return shim.fsDriver_; }; - shim.randomBytes = async count => { + shim.randomBytes = async (count: number) => { const randomBytes = await generateSecureRandom(count); const temp = []; for (const n in randomBytes) { @@ -91,7 +92,7 @@ function shimInit() { /* eslint-enable */ - shim.detectAndSetLocale = (Setting) => { + shim.detectAndSetLocale = (Setting: typeof SettingType) => { // [ // { // "countryCode": "US", @@ -179,7 +180,7 @@ function shimInit() { try { const response = await shim.fetchWithRetry(doFetchBlob, options); - // Returns an object that's roughtly compatible with a standard Response object + // Returns an object that's roughly compatible with a standard Response object const output = { ok: response.respInfo.status < 400, path: response.data, @@ -212,7 +213,7 @@ function shimInit() { trusty: options.ignoreTlsErrors, }).fetch(method, url, headers, RNFetchBlob.wrap(options.path)); - // Returns an object that's roughtly compatible with a standard Response object + // Returns an object that's roughly compatible with a standard Response object return { ok: response.respInfo.status < 400, data: response.data, @@ -239,7 +240,7 @@ function shimInit() { shim.showMessageBox = showMessageBox; shim.openUrl = url => { - Linking.openURL(url); + return Linking.openURL(url); }; shim.httpAgent = () => { @@ -247,7 +248,7 @@ function shimInit() { }; shim.waitForFrame = () => { - return new Promise((resolve) => { + return new Promise((resolve) => { requestAnimationFrame(() => { resolve(); }); @@ -299,7 +300,7 @@ function shimInit() { shim.injectedJs = function(name) { if (!(name in injectedJs)) throw new Error(`Cannot find injectedJs file (add it to "injectedJs" object): ${name}`); - return injectedJs[name]; + return injectedJs[name as keyof typeof injectedJs]; }; shim.setTimeout = (fn, interval) => { @@ -320,4 +321,3 @@ function shimInit() { } -module.exports = { shimInit }; diff --git a/packages/lib/shim.ts b/packages/lib/shim.ts index 18d6313b5..994ea6e45 100644 --- a/packages/lib/shim.ts +++ b/packages/lib/shim.ts @@ -19,6 +19,13 @@ export interface PdfInfo { pageCount: number; } +interface FetchOptions { + method?: string; + headers?: Record; + body?: string; + agent?: unknown; +} + let isTestingEnv_ = false; // We need to ensure that there's only one instance of React being used by all @@ -239,11 +246,14 @@ const shim = { } }, - // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied - fetch: (_url: string, _options: any = null): any => { + fetch: (_url: string, _options: FetchOptions|null = null): Promise => { throw new Error('Not implemented: fetch'); }, + debugFetch: (_url: string, _options: FetchOptions|null): Promise => { + throw new Error('Not implemented: debugFetch'); + }, + // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied fetchText: async (url: string, options: any = null): Promise => { const r = await shim.fetch(url, options || {});