Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Replace require with import statement #2143

Merged
merged 3 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"require-atomic-updates": "off",
"prefer-spread": "off",
"prefer-rest-params": "off",
"@typescript-eslint/ban-ts-comment": "off",
"@typescript-eslint/triple-slash-reference": "off",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/no-explicit-any": "off",
Expand Down
10 changes: 3 additions & 7 deletions src/Analytics.js → src/Analytics.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* @flow
*/

import CoreManager from './CoreManager';

/**
Expand Down Expand Up @@ -44,7 +40,7 @@ import CoreManager from './CoreManager';
* @returns {Promise} A promise that is resolved when the round-trip
* to the server completes.
*/
export function track(name: string, dimensions: { [key: string]: string }): Promise {
export function track(name: string, dimensions: { [key: string]: string }): Promise<void> {
name = name || '';
name = name.replace(/^\s*/, '');
name = name.replace(/\s*$/, '');
Expand All @@ -62,10 +58,10 @@ export function track(name: string, dimensions: { [key: string]: string }): Prom
}

const DefaultController = {
track(name, dimensions) {
track(name: string, dimensions: { [key: string]: string }) {
const path = 'events/' + name;
const RESTController = CoreManager.getRESTController();
return RESTController.request('POST', path, { dimensions: dimensions });
return RESTController.request('POST', path, { dimensions });
},
};

Expand Down
6 changes: 1 addition & 5 deletions src/AnonymousUtils.js → src/AnonymousUtils.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
/**
* @flow-weak
*/

import ParseUser from './ParseUser';
import type { RequestOptions } from './RESTController';
const uuidv4 = require('./uuid');
import uuidv4 from './uuid';

let registered = false;

Expand Down
File renamed without changes.
9 changes: 5 additions & 4 deletions src/CryptoController.js → src/CryptoController.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
let AES;
let ENC;
let AES: any;
let ENC: any;

if (process.env.PARSE_BUILD === 'react-native') {
const CryptoJS = require('react-native-crypto-js');
Expand All @@ -11,15 +11,16 @@ if (process.env.PARSE_BUILD === 'react-native') {
}

const CryptoController = {
encrypt(obj: any, secretKey: string): ?string {
encrypt(obj: any, secretKey: string): string {
const encrypted = AES.encrypt(JSON.stringify(obj), secretKey);
return encrypted.toString();
},

decrypt(encryptedText: string, secretKey: string): ?string {
decrypt(encryptedText: string, secretKey: string): string {
const decryptedStr = AES.decrypt(encryptedText, secretKey).toString(ENC);
return decryptedStr;
},
};

module.exports = CryptoController;
export default CryptoController;
3 changes: 2 additions & 1 deletion src/EventEmitter.js → src/EventEmitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* This is a simple wrapper to unify EventEmitter implementations across platforms.
*/

let EventEmitter;
let EventEmitter: any;

try {
if (process.env.PARSE_BUILD === 'react-native') {
Expand All @@ -18,3 +18,4 @@ try {
// EventEmitter unavailable
}
module.exports = EventEmitter;
export default EventEmitter;
5 changes: 2 additions & 3 deletions src/LiveQueryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ const generateInterval = k => {
* We expose three events to help you monitor the status of the LiveQueryClient.
*
* <pre>
* let Parse = require('parse/node');
* let LiveQueryClient = Parse.LiveQueryClient;
* let client = new LiveQueryClient({
* const LiveQueryClient = Parse.LiveQueryClient;
* const client = new LiveQueryClient({
* applicationId: '',
* serverURL: '',
* javascriptKey: '',
Expand Down
8 changes: 2 additions & 6 deletions src/LocalDatastore.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import CoreManager from './CoreManager';

import LocalDatastoreController from './LocalDatastoreController';
import type ParseObject from './ParseObject';
import ParseQuery from './ParseQuery';
import { DEFAULT_PIN, PIN_PREFIX, OBJECT_PREFIX } from './LocalDatastoreUtils';
Expand Down Expand Up @@ -390,9 +390,5 @@ const LocalDatastore = {
module.exports = LocalDatastore;
export default LocalDatastore;

if (process.env.PARSE_BUILD === 'react-native') {
CoreManager.setLocalDatastoreController(require('./LocalDatastoreController.react-native'));
} else {
CoreManager.setLocalDatastoreController(require('./LocalDatastoreController'));
}
CoreManager.setLocalDatastoreController(LocalDatastoreController);
CoreManager.setLocalDatastore(LocalDatastore);
5 changes: 0 additions & 5 deletions src/LocalDatastoreController.js

This file was deleted.

10 changes: 5 additions & 5 deletions src/LocalDatastoreController.react-native.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isLocalDatastoreKey } from './LocalDatastoreUtils';
const RNStorage = require('./StorageController.react-native');
import RNStorage from './StorageController.react-native';

const LocalDatastoreController = {
async fromPinWithName(name: string): Promise<Array<any>> {
Expand Down Expand Up @@ -35,7 +35,7 @@ const LocalDatastoreController = {
}
}
const LDS = {};
let results: any[] = [];
let results: any = [];
try {
results = await RNStorage.multiGet(batch);
} catch (error) {
Expand All @@ -57,8 +57,8 @@ const LocalDatastoreController = {
async getRawStorage(): Promise<any> {
const keys = await RNStorage.getAllKeysAsync();
const storage = {};
const results = await RNStorage.multiGet(keys);
results.map(pair => {
const results = await RNStorage.multiGet(keys as string[]);
results!.map(pair => {
const [key, value] = pair;
storage[key] = value;
});
Expand All @@ -74,7 +74,7 @@ const LocalDatastoreController = {
batch.push(key);
}
}
return RNStorage.multiRemove(batch).catch(error =>
await RNStorage.multiRemove(batch).catch(error =>
console.error('Error clearing local datastore: ', error)
);
},
Expand Down
10 changes: 10 additions & 0 deletions src/LocalDatastoreController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import RNLocalDatastoreController from './LocalDatastoreController.react-native';
import DefaultLocalDatastoreController from './LocalDatastoreController.default';

let LocalDatastoreController: any = DefaultLocalDatastoreController;

if (process.env.PARSE_BUILD === 'react-native') {
LocalDatastoreController = RNLocalDatastoreController;
}
module.exports = LocalDatastoreController;
export default LocalDatastoreController;
13 changes: 8 additions & 5 deletions src/OfflineQuery.js → src/OfflineQuery.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const equalObjects = require('./equals').default;
const decode = require('./decode').default;
const ParseError = require('./ParseError').default;
const ParsePolygon = require('./ParsePolygon').default;
const ParseGeoPoint = require('./ParseGeoPoint').default;
import equalObjects from './equals';
import decode from './decode';
import ParseError from './ParseError';
import ParsePolygon from './ParsePolygon';
import ParseGeoPoint from './ParseGeoPoint';
/**
* contains -- Determines if an object is contained in a list with special handling for Parse pointers.
*
Expand Down Expand Up @@ -333,7 +333,9 @@ function matchesKeyConstraints(className, object, objects, key, constraints) {
if (
toString.call(compareTo) === '[object Date]' ||
(typeof compareTo === 'string' &&
// @ts-ignore
new Date(compareTo) !== 'Invalid Date' &&
// @ts-ignore
!isNaN(new Date(compareTo)))
) {
object[key] = new Date(object[key].iso ? object[key].iso : object[key]);
Expand Down Expand Up @@ -594,3 +596,4 @@ const OfflineQuery = {
};

module.exports = OfflineQuery;
export default OfflineQuery;
3 changes: 2 additions & 1 deletion src/Parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Config from './ParseConfig';
import ParseError from './ParseError';
import FacebookUtils from './FacebookUtils';
import File from './ParseFile';
import * as Hooks from './ParseHooks';
import GeoPoint from './ParseGeoPoint';
import Polygon from './ParsePolygon';
import Installation from './ParseInstallation';
Expand Down Expand Up @@ -474,7 +475,7 @@ if (process.env.PARSE_BUILD === 'node') {
Parse.Cloud.useMasterKey = function () {
CoreManager.set('USE_MASTER_KEY', true);
};
Parse.Hooks = require('./ParseHooks');
Parse.Hooks = Hooks;
}

// For legacy requires, of the form `var Parse = require('parse').Parse`
Expand Down
7 changes: 4 additions & 3 deletions src/ParseFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
import CoreManager from './CoreManager';
import type { FullOptions } from './RESTController';
import ParseError from './ParseError';
import XhrWeapp from './Xhr.weapp';

let XHR = null;
let XHR: any = null;
if (typeof XMLHttpRequest !== 'undefined') {
XHR = XMLHttpRequest;
}
if (process.env.PARSE_BUILD === 'weapp') {
XHR = require('./Xhr.weapp');
XHR = XhrWeapp;
}

type Base64 = { base64: string };
Expand Down Expand Up @@ -240,7 +241,7 @@ class ParseFile {
* </ul>
* @returns {Promise | undefined} Promise that is resolved when the save finishes.
*/
save(options?: FileSaveOptions): Promise<ParseFile> | undefined {
save(options?: FileSaveOptions & { requestTask?: any }): Promise<ParseFile> | undefined {
options = options || {};
options.requestTask = task => (this._requestTask = task);
options.metadata = this._metadata;
Expand Down
4 changes: 2 additions & 2 deletions src/ParseUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,7 @@ const DefaultController = {
);
}
const path = Storage.generatePath(CURRENT_USER_KEY);
let userData = Storage.getItem(path);
let userData: any = Storage.getItem(path);
currentUserCacheMatchesDisk = true;
if (!userData) {
currentUserCache = null;
Expand Down Expand Up @@ -1097,7 +1097,7 @@ const DefaultController = {
return Promise.resolve(null);
}
const path = Storage.generatePath(CURRENT_USER_KEY);
return Storage.getItemAsync(path).then(userData => {
return Storage.getItemAsync(path).then((userData: any) => {
currentUserCacheMatchesDisk = true;
if (!userData) {
currentUserCache = null;
Expand Down
8 changes: 4 additions & 4 deletions src/Push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export function send(data: PushData, options: FullOptions = {}): Promise<string>
throw new Error('expiration_time and expiration_interval cannot both be set.');
}

const pushOptions = { useMasterKey: true };
const pushOptions: FullOptions = { useMasterKey: true };
if (options.hasOwnProperty('useMasterKey')) {
pushOptions.useMasterKey = options.useMasterKey;
}
Expand All @@ -90,7 +90,7 @@ export function getPushStatus(
pushStatusId: string,
options: FullOptions = {}
): Promise<ParseObject> {
const pushOptions = { useMasterKey: true };
const pushOptions: FullOptions = { useMasterKey: true };
if (options.hasOwnProperty('useMasterKey')) {
pushOptions.useMasterKey = options.useMasterKey;
}
Expand All @@ -99,8 +99,8 @@ export function getPushStatus(
}

const DefaultController = {
async send(data: PushData, options?: FullOptions) {
options.returnStatus = true;
async send(data: PushData, options?: FullOptions & { returnStatus?: boolean }) {
options!.returnStatus = true;
const response = await CoreManager.getRESTController().request('POST', 'push', data, options);
return response._headers?.['X-Parse-Push-Status-Id'];
},
Expand Down
Loading
Loading