Skip to content

Commit

Permalink
refactor: Convert helper functions to TypeScript (#2132)
Browse files Browse the repository at this point in the history
  • Loading branch information
dplewis committed May 16, 2024
1 parent 1ac1890 commit f673df6
Show file tree
Hide file tree
Showing 25 changed files with 60 additions and 98 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ jobs:
- run: npm ci
- name: Build Types
run: npm run build:types
- name: Lint Types
run: npm run lint:types
- name: Test Types
run: npm run test:types
- name: Lint Types
run: npm run lint:types
check-docs:
name: Check Docs
timeout-minutes: 5
Expand Down
21 changes: 9 additions & 12 deletions src/CoreManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ import type ParseConfig from './ParseConfig';
import type LiveQueryClient from './LiveQueryClient';
import type ParseSchema from './ParseSchema';
import type ParseInstallation from './ParseInstallation';
import type ParseQuery from './ParseQuery';
import type * as ParseOp from './ParseOp';
import type ParseRole from './ParseRole';

type AnalyticsController = {
track: (name: string, dimensions: { [key: string]: string }) => Promise<any>,
Expand Down Expand Up @@ -598,43 +595,43 @@ const CoreManager = {
return config['HooksController']!;
},

setParseOp(op: typeof ParseOp) {
setParseOp(op: any) {
config['ParseOp'] = op;
},

getParseOp() {
return config['ParseOp']!;
},

setParseObject(object: typeof ParseObject) {
setParseObject(object: any) {
config['ParseObject'] = object;
},

getParseObject(): ParseObject {
getParseObject() {
return config['ParseObject']!;
},

setParseQuery(query: typeof ParseQuery) {
setParseQuery(query: any) {
config['ParseQuery'] = query;
},

getParseQuery(): ParseQuery {
getParseQuery() {
return config['ParseQuery']!;
},

setParseRole(role: typeof ParseRole) {
setParseRole(role: any) {
config['ParseRole'] = role;
},

getParseRole(): ParseRole {
getParseRole() {
return config['ParseRole']!;
},

setParseUser(user: typeof ParseUser) {
setParseUser(user: any) {
config['ParseUser'] = user;
},

getParseUser(): ParseUser {
getParseUser() {
return config['ParseUser']!;
},
};
Expand Down
10 changes: 5 additions & 5 deletions src/ParseObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2385,7 +2385,7 @@ const DefaultController = {
return Promise.resolve(target);
},

save(target: ParseObject | Array<ParseObject | ParseFile>, options: RequestOptions): Promise<ParseObject | Array<ParseObject> | ParseFile> {
save(target: ParseObject | null | Array<ParseObject | ParseFile>, options: RequestOptions): Promise<ParseObject | Array<ParseObject> | ParseFile> {
const batchSize =
options && options.batchSize ? options.batchSize : CoreManager.get('REQUEST_BATCH_SIZE');
const localDatastore = CoreManager.getLocalDatastore();
Expand Down Expand Up @@ -2452,11 +2452,11 @@ const DefaultController = {

// Queue up tasks for each object in the batch.
// When every task is ready, the API request will execute
const batchReturned = new resolvingPromise();
const batchReady = [];
const batchTasks = [];
const batchReturned = resolvingPromise();
const batchReady: ReturnType<typeof resolvingPromise<void>>[] = [];
const batchTasks: Promise<void>[] = [];
batch.forEach((obj, index) => {
const ready = new resolvingPromise();
const ready = resolvingPromise<void>();
batchReady.push(ready);
const task = function () {
ready.resolve();
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/escape-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
jest.autoMockOff();

const escape = require('../escape.js').default;
const escape = require('../escape').default;

describe('escape', () => {
it('escapes special HTML characters', () => {
Expand Down
4 changes: 0 additions & 4 deletions src/arrayContainsObject.js → src/arrayContainsObject.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* @flow
*/

import CoreManager from './CoreManager';
import type ParseObject from './ParseObject';

Expand Down
4 changes: 0 additions & 4 deletions src/canBeSerialized.js → src/canBeSerialized.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* @flow
*/

import CoreManager from './CoreManager';
import ParseFile from './ParseFile';
import type ParseObject from './ParseObject';
Expand Down
4 changes: 0 additions & 4 deletions src/decode.js → src/decode.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
/**
* @flow
*/
import CoreManager from './CoreManager';
import ParseACL from './ParseACL'; // eslint-disable-line no-unused-vars
import ParseFile from './ParseFile';
import ParseGeoPoint from './ParseGeoPoint';
import ParsePolygon from './ParsePolygon';
Expand Down
14 changes: 5 additions & 9 deletions src/encode.js → src/encode.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* @flow
*/

import CoreManager from './CoreManager';
import ParseACL from './ParseACL';
import ParseFile from './ParseFile';
Expand All @@ -10,10 +6,10 @@ import ParsePolygon from './ParsePolygon';
import ParseRelation from './ParseRelation';

function encode(
value: mixed,
value: any,
disallowObjects: boolean,
forcePointers: boolean,
seen: Array<mixed>,
seen: Array<any>,
offline: boolean
): any {
const ParseObject = CoreManager.getParseObject();
Expand Down Expand Up @@ -57,7 +53,7 @@ function encode(
if (isNaN(value)) {
throw new Error('Tried to encode an invalid date.');
}
return { __type: 'Date', iso: (value: any).toJSON() };
return { __type: 'Date', iso: (value as Date).toJSON() };
}
if (
Object.prototype.toString.call(value) === '[object RegExp]' &&
Expand All @@ -84,10 +80,10 @@ function encode(
}

export default function (
value: mixed,
value: any,
disallowObjects?: boolean,
forcePointers?: boolean,
seen?: Array<mixed>,
seen?: Array<any>,
offline?: boolean
): any {
return encode(value, !!disallowObjects, !!forcePointers, seen || [], offline);
Expand Down
2 changes: 1 addition & 1 deletion src/equals.js → src/equals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import ParseACL from './ParseACL';
import ParseFile from './ParseFile';
import ParseGeoPoint from './ParseGeoPoint';

export default function equals(a, b) {
export default function equals(a: any, b: any): boolean {
const toString = Object.prototype.toString;
if (toString.call(a) === '[object Date]' || toString.call(b) === '[object Date]') {
const dateA = new Date(a);
Expand Down
4 changes: 0 additions & 4 deletions src/escape.js → src/escape.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/*
* @flow
*/

const encoded = {
'&': '&amp;',
'<': '&lt;',
Expand Down
4 changes: 0 additions & 4 deletions src/isRevocableSession.js → src/isRevocableSession.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* @flow
*/

export default function isRevocableSession(token: string): boolean {
return token.indexOf('r:') > -1;
}
6 changes: 1 addition & 5 deletions src/parseDate.js → src/parseDate.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
/**
* @flow
*/

export default function parseDate(iso8601: string): ?Date {
export default function parseDate(iso8601: string): Date | null {
const regexp = new RegExp(
'^([0-9]{1,4})-([0-9]{1,2})-([0-9]{1,2})' +
'T' +
Expand Down
23 changes: 12 additions & 11 deletions src/promiseUtils.js → src/promiseUtils.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
// Create Deferred Promise
export function resolvingPromise() {
let res;
let rej;
const promise = new Promise((resolve, reject) => {
export function resolvingPromise<T = any>() {
let res: (value: T) => void;
let rej: (error: T) => void;
const promise = new Promise<T>((resolve, reject) => {
res = resolve;
rej = reject;
});
promise.resolve = res;
promise.reject = rej;
return promise;
const defer: typeof promise & { resolve: (res: T) => void, reject: (err: any) => void } = promise as any;
defer.resolve = res!;
defer.reject = rej!;
return defer;
}

export function when(promises) {
export function when(promises: any) {
let objects;
const arrayArgument = Array.isArray(promises);
if (arrayArgument) {
Expand All @@ -32,7 +33,7 @@ export function when(promises) {
return Promise.resolve(returnValue);
}

const promise = new resolvingPromise();
const promise = resolvingPromise();

const resolveOne = function () {
total--;
Expand All @@ -45,7 +46,7 @@ export function when(promises) {
}
};

const chain = function (object, index) {
const chain = function (object: Promise<any>, index: number) {
if (object && typeof object.then === 'function') {
object.then(
function (result) {
Expand All @@ -70,7 +71,7 @@ export function when(promises) {
return promise;
}

export function continueWhile(test, emitter) {
export function continueWhile(test: () => any, emitter: () => Promise<any>) {
if (test()) {
return emitter().then(() => {
return continueWhile(test, emitter);
Expand Down
8 changes: 2 additions & 6 deletions src/unique.js → src/unique.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
/**
* @flow
*/

import arrayContainsObject from './arrayContainsObject';
import CoreManager from './CoreManager';

export default function unique<T>(arr: Array<T>): Array<T> {
const uniques = [];
const uniques: T[] = [];
arr.forEach(value => {
const ParseObject = CoreManager.getParseObject();
if (value instanceof ParseObject) {
if (!arrayContainsObject(uniques, value)) {
if (!arrayContainsObject(uniques, value as typeof ParseObject)) {
uniques.push(value);
}
} else {
Expand Down
6 changes: 1 addition & 5 deletions src/unsavedChildren.js → src/unsavedChildren.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* @flow
*/

import CoreManager from './CoreManager';
import ParseFile from './ParseFile';
import type ParseObject from './ParseObject';
Expand Down Expand Up @@ -69,7 +65,7 @@ function traverse(
return;
}
if (obj instanceof ParseFile) {
if (!obj.url() && encountered.files.indexOf(obj) < 0) {
if (!(obj as ParseFile).url() && encountered.files.indexOf(obj) < 0) {
encountered.files.push(obj);
}
return;
Expand Down
2 changes: 1 addition & 1 deletion src/uuid.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { v4 } from 'uuid';

let uuid: () => string = null as any;
let uuid: () => string;

if (process.env.PARSE_BUILD === 'weapp') {
uuid = function () {
Expand Down
10 changes: 5 additions & 5 deletions types/CoreManager.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,13 @@ declare const CoreManager: {
getHooksController(): HooksController;
setParseOp(op: any): void;
getParseOp(): any;
setParseObject(object: typeof ParseObject): void;
getParseObject(): ParseObject;
setParseObject(object: any): void;
getParseObject(): any;
setParseQuery(query: any): void;
getParseQuery(): ParseQuery;
getParseQuery(): any;
setParseRole(role: any): void;
getParseRole(): ParseRole;
getParseRole(): any;
setParseUser(user: any): void;
getParseUser(): ParseUser;
getParseUser(): any;
};
export default CoreManager;
2 changes: 1 addition & 1 deletion types/arrayContainsObject.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import type ParseObject from './ParseObject';
export default function arrayContainsObject(array: Array<any>, object: ParseObject): boolean;
import ParseObject from './ParseObject';
2 changes: 1 addition & 1 deletion types/canBeSerialized.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import type ParseObject from './ParseObject';
export default function canBeSerialized(obj: ParseObject): boolean;
import ParseObject from './ParseObject';
2 changes: 1 addition & 1 deletion types/encode.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default function _default(value: mixed, disallowObjects?: boolean, forcePointers?: boolean, seen?: Array<mixed>, offline?: boolean): any;
export default function (value: any, disallowObjects?: boolean, forcePointers?: boolean, seen?: Array<any>, offline?: boolean): any;
3 changes: 0 additions & 3 deletions types/isRevocableSession.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
/**
* @flow
*/
export default function isRevocableSession(token: string): boolean;
3 changes: 0 additions & 3 deletions types/parseDate.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
/**
* @flow
*/
export default function parseDate(iso8601: string): Date | null;
12 changes: 9 additions & 3 deletions types/promiseUtils.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
export function resolvingPromise(): Promise<any>;
export function when(promises: any, ...args: any[]): any;
export function continueWhile(test: any, emitter: any): any;
export declare function resolvingPromise<T = any>(): Promise<T> & {
resolve: (res: T) => void;
reject: (err: any) => void;
};
export declare function when(promises: any): Promise<any[]> | (Promise<any> & {
resolve: (res: any) => void;
reject: (err: any) => void;
});
export declare function continueWhile(test: () => any, emitter: () => Promise<any>): any;
2 changes: 1 addition & 1 deletion types/unique.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default function unique<T>(arr: T[]): T[];
export default function unique<T>(arr: Array<T>): Array<T>;
4 changes: 2 additions & 2 deletions types/unsavedChildren.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import ParseFile from './ParseFile';
import type ParseObject from './ParseObject';
/**
* Return an array of unsaved children, which are either Parse Objects or Files.
* If it encounters any dirty Objects without Ids, it will throw an exception.
Expand All @@ -7,5 +9,3 @@
* @returns {Array}
*/
export default function unsavedChildren(obj: ParseObject, allowDeepUnsaved?: boolean): Array<ParseFile | ParseObject>;
import ParseObject from './ParseObject';
import ParseFile from './ParseFile';

0 comments on commit f673df6

Please sign in to comment.