Skip to content

Commit f717a2b

Browse files
authored
refactor: Replace require with import statement (#2143)
1 parent 09a2d0b commit f717a2b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+654
-209
lines changed

Diff for: .eslintrc.json

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"require-atomic-updates": "off",
4141
"prefer-spread": "off",
4242
"prefer-rest-params": "off",
43+
"@typescript-eslint/ban-ts-comment": "off",
4344
"@typescript-eslint/triple-slash-reference": "off",
4445
"@typescript-eslint/no-empty-function": "off",
4546
"@typescript-eslint/no-explicit-any": "off",

Diff for: src/Analytics.js renamed to src/Analytics.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
/**
2-
* @flow
3-
*/
4-
51
import CoreManager from './CoreManager';
62

73
/**
@@ -44,7 +40,7 @@ import CoreManager from './CoreManager';
4440
* @returns {Promise} A promise that is resolved when the round-trip
4541
* to the server completes.
4642
*/
47-
export function track(name: string, dimensions: { [key: string]: string }): Promise {
43+
export function track(name: string, dimensions: { [key: string]: string }): Promise<void> {
4844
name = name || '';
4945
name = name.replace(/^\s*/, '');
5046
name = name.replace(/\s*$/, '');
@@ -62,10 +58,10 @@ export function track(name: string, dimensions: { [key: string]: string }): Prom
6258
}
6359

6460
const DefaultController = {
65-
track(name, dimensions) {
61+
track(name: string, dimensions: { [key: string]: string }) {
6662
const path = 'events/' + name;
6763
const RESTController = CoreManager.getRESTController();
68-
return RESTController.request('POST', path, { dimensions: dimensions });
64+
return RESTController.request('POST', path, { dimensions });
6965
},
7066
};
7167

Diff for: src/AnonymousUtils.js renamed to src/AnonymousUtils.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
/**
2-
* @flow-weak
3-
*/
4-
51
import ParseUser from './ParseUser';
62
import type { RequestOptions } from './RESTController';
7-
const uuidv4 = require('./uuid');
3+
import uuidv4 from './uuid';
84

95
let registered = false;
106

Diff for: src/CloudCode.js renamed to src/CloudCode.ts

File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
let AES;
2-
let ENC;
1+
let AES: any;
2+
let ENC: any;
33

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

1313
const CryptoController = {
14-
encrypt(obj: any, secretKey: string): ?string {
14+
encrypt(obj: any, secretKey: string): string {
1515
const encrypted = AES.encrypt(JSON.stringify(obj), secretKey);
1616
return encrypted.toString();
1717
},
1818

19-
decrypt(encryptedText: string, secretKey: string): ?string {
19+
decrypt(encryptedText: string, secretKey: string): string {
2020
const decryptedStr = AES.decrypt(encryptedText, secretKey).toString(ENC);
2121
return decryptedStr;
2222
},
2323
};
2424

2525
module.exports = CryptoController;
26+
export default CryptoController;

Diff for: src/EventEmitter.js renamed to src/EventEmitter.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* This is a simple wrapper to unify EventEmitter implementations across platforms.
33
*/
44

5-
let EventEmitter;
5+
let EventEmitter: any;
66

77
try {
88
if (process.env.PARSE_BUILD === 'react-native') {
@@ -18,3 +18,4 @@ try {
1818
// EventEmitter unavailable
1919
}
2020
module.exports = EventEmitter;
21+
export default EventEmitter;

Diff for: src/LiveQueryClient.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,8 @@ const generateInterval = k => {
7272
* We expose three events to help you monitor the status of the LiveQueryClient.
7373
*
7474
* <pre>
75-
* let Parse = require('parse/node');
76-
* let LiveQueryClient = Parse.LiveQueryClient;
77-
* let client = new LiveQueryClient({
75+
* const LiveQueryClient = Parse.LiveQueryClient;
76+
* const client = new LiveQueryClient({
7877
* applicationId: '',
7978
* serverURL: '',
8079
* javascriptKey: '',

Diff for: src/LocalDatastore.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import CoreManager from './CoreManager';
2-
2+
import LocalDatastoreController from './LocalDatastoreController';
33
import type ParseObject from './ParseObject';
44
import ParseQuery from './ParseQuery';
55
import { DEFAULT_PIN, PIN_PREFIX, OBJECT_PREFIX } from './LocalDatastoreUtils';
@@ -390,9 +390,5 @@ const LocalDatastore = {
390390
module.exports = LocalDatastore;
391391
export default LocalDatastore;
392392

393-
if (process.env.PARSE_BUILD === 'react-native') {
394-
CoreManager.setLocalDatastoreController(require('./LocalDatastoreController.react-native'));
395-
} else {
396-
CoreManager.setLocalDatastoreController(require('./LocalDatastoreController'));
397-
}
393+
CoreManager.setLocalDatastoreController(LocalDatastoreController);
398394
CoreManager.setLocalDatastore(LocalDatastore);

Diff for: src/LocalDatastoreController.js

-5
This file was deleted.

Diff for: src/LocalDatastoreController.react-native.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { isLocalDatastoreKey } from './LocalDatastoreUtils';
2-
const RNStorage = require('./StorageController.react-native');
2+
import RNStorage from './StorageController.react-native';
33

44
const LocalDatastoreController = {
55
async fromPinWithName(name: string): Promise<Array<any>> {
@@ -35,7 +35,7 @@ const LocalDatastoreController = {
3535
}
3636
}
3737
const LDS = {};
38-
let results: any[] = [];
38+
let results: any = [];
3939
try {
4040
results = await RNStorage.multiGet(batch);
4141
} catch (error) {
@@ -57,8 +57,8 @@ const LocalDatastoreController = {
5757
async getRawStorage(): Promise<any> {
5858
const keys = await RNStorage.getAllKeysAsync();
5959
const storage = {};
60-
const results = await RNStorage.multiGet(keys);
61-
results.map(pair => {
60+
const results = await RNStorage.multiGet(keys as string[]);
61+
results!.map(pair => {
6262
const [key, value] = pair;
6363
storage[key] = value;
6464
});
@@ -74,7 +74,7 @@ const LocalDatastoreController = {
7474
batch.push(key);
7575
}
7676
}
77-
return RNStorage.multiRemove(batch).catch(error =>
77+
await RNStorage.multiRemove(batch).catch(error =>
7878
console.error('Error clearing local datastore: ', error)
7979
);
8080
},

Diff for: src/LocalDatastoreController.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import RNLocalDatastoreController from './LocalDatastoreController.react-native';
2+
import DefaultLocalDatastoreController from './LocalDatastoreController.default';
3+
4+
let LocalDatastoreController: any = DefaultLocalDatastoreController;
5+
6+
if (process.env.PARSE_BUILD === 'react-native') {
7+
LocalDatastoreController = RNLocalDatastoreController;
8+
}
9+
module.exports = LocalDatastoreController;
10+
export default LocalDatastoreController;

Diff for: src/OfflineQuery.js renamed to src/OfflineQuery.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const equalObjects = require('./equals').default;
2-
const decode = require('./decode').default;
3-
const ParseError = require('./ParseError').default;
4-
const ParsePolygon = require('./ParsePolygon').default;
5-
const ParseGeoPoint = require('./ParseGeoPoint').default;
1+
import equalObjects from './equals';
2+
import decode from './decode';
3+
import ParseError from './ParseError';
4+
import ParsePolygon from './ParsePolygon';
5+
import ParseGeoPoint from './ParseGeoPoint';
66
/**
77
* contains -- Determines if an object is contained in a list with special handling for Parse pointers.
88
*
@@ -333,7 +333,9 @@ function matchesKeyConstraints(className, object, objects, key, constraints) {
333333
if (
334334
toString.call(compareTo) === '[object Date]' ||
335335
(typeof compareTo === 'string' &&
336+
// @ts-ignore
336337
new Date(compareTo) !== 'Invalid Date' &&
338+
// @ts-ignore
337339
!isNaN(new Date(compareTo)))
338340
) {
339341
object[key] = new Date(object[key].iso ? object[key].iso : object[key]);
@@ -594,3 +596,4 @@ const OfflineQuery = {
594596
};
595597

596598
module.exports = OfflineQuery;
599+
export default OfflineQuery;

Diff for: src/Parse.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import Config from './ParseConfig';
1717
import ParseError from './ParseError';
1818
import FacebookUtils from './FacebookUtils';
1919
import File from './ParseFile';
20+
import * as Hooks from './ParseHooks';
2021
import GeoPoint from './ParseGeoPoint';
2122
import Polygon from './ParsePolygon';
2223
import Installation from './ParseInstallation';
@@ -474,7 +475,7 @@ if (process.env.PARSE_BUILD === 'node') {
474475
Parse.Cloud.useMasterKey = function () {
475476
CoreManager.set('USE_MASTER_KEY', true);
476477
};
477-
Parse.Hooks = require('./ParseHooks');
478+
Parse.Hooks = Hooks;
478479
}
479480

480481
// For legacy requires, of the form `var Parse = require('parse').Parse`

Diff for: src/ParseFile.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
import CoreManager from './CoreManager';
33
import type { FullOptions } from './RESTController';
44
import ParseError from './ParseError';
5+
import XhrWeapp from './Xhr.weapp';
56

6-
let XHR = null;
7+
let XHR: any = null;
78
if (typeof XMLHttpRequest !== 'undefined') {
89
XHR = XMLHttpRequest;
910
}
1011
if (process.env.PARSE_BUILD === 'weapp') {
11-
XHR = require('./Xhr.weapp');
12+
XHR = XhrWeapp;
1213
}
1314

1415
type Base64 = { base64: string };
@@ -240,7 +241,7 @@ class ParseFile {
240241
* </ul>
241242
* @returns {Promise | undefined} Promise that is resolved when the save finishes.
242243
*/
243-
save(options?: FileSaveOptions): Promise<ParseFile> | undefined {
244+
save(options?: FileSaveOptions & { requestTask?: any }): Promise<ParseFile> | undefined {
244245
options = options || {};
245246
options.requestTask = task => (this._requestTask = task);
246247
options.metadata = this._metadata;

Diff for: src/ParseUser.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1059,7 +1059,7 @@ const DefaultController = {
10591059
);
10601060
}
10611061
const path = Storage.generatePath(CURRENT_USER_KEY);
1062-
let userData = Storage.getItem(path);
1062+
let userData: any = Storage.getItem(path);
10631063
currentUserCacheMatchesDisk = true;
10641064
if (!userData) {
10651065
currentUserCache = null;
@@ -1097,7 +1097,7 @@ const DefaultController = {
10971097
return Promise.resolve(null);
10981098
}
10991099
const path = Storage.generatePath(CURRENT_USER_KEY);
1100-
return Storage.getItemAsync(path).then(userData => {
1100+
return Storage.getItemAsync(path).then((userData: any) => {
11011101
currentUserCacheMatchesDisk = true;
11021102
if (!userData) {
11031103
currentUserCache = null;

Diff for: src/Push.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export function send(data: PushData, options: FullOptions = {}): Promise<string>
6565
throw new Error('expiration_time and expiration_interval cannot both be set.');
6666
}
6767

68-
const pushOptions = { useMasterKey: true };
68+
const pushOptions: FullOptions = { useMasterKey: true };
6969
if (options.hasOwnProperty('useMasterKey')) {
7070
pushOptions.useMasterKey = options.useMasterKey;
7171
}
@@ -90,7 +90,7 @@ export function getPushStatus(
9090
pushStatusId: string,
9191
options: FullOptions = {}
9292
): Promise<ParseObject> {
93-
const pushOptions = { useMasterKey: true };
93+
const pushOptions: FullOptions = { useMasterKey: true };
9494
if (options.hasOwnProperty('useMasterKey')) {
9595
pushOptions.useMasterKey = options.useMasterKey;
9696
}
@@ -99,8 +99,8 @@ export function getPushStatus(
9999
}
100100

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

0 commit comments

Comments
 (0)