Skip to content

refactor: Fix TypeScript type correctness test #2134

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

Merged
merged 6 commits into from
May 16, 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
2 changes: 1 addition & 1 deletion src/CoreManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type ObjectController = {
forceFetch: boolean,
options: RequestOptions
) => Promise<Array<ParseObject | undefined> | ParseObject | undefined>,
save: (object: ParseObject | Array<ParseObject | ParseFile> | null, options: RequestOptions) => Promise<ParseObject | Array<ParseObject> | ParseFile>,
save: (object: ParseObject | Array<ParseObject | ParseFile> | null, options: RequestOptions) => Promise<ParseObject | Array<ParseObject> | ParseFile | undefined>,
destroy: (object: ParseObject | Array<ParseObject>, options: RequestOptions) => Promise<ParseObject | Array<ParseObject>>,
};
type ObjectStateController = {
Expand Down
10 changes: 3 additions & 7 deletions src/ObjectStateMutations.js → src/ObjectStateMutations.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
/**
* @flow
*/

import encode from './encode';
import CoreManager from './CoreManager';
import ParseFile from './ParseFile';
import ParseRelation from './ParseRelation';
import TaskQueue from './TaskQueue';
import { RelationOp } from './ParseOp';

import type { Op } from './ParseOp';
import type ParseObject from './ParseObject';

export type AttributeMap = { [attr: string]: any };
export type OpsMap = { [attr: string]: Op };
Expand Down Expand Up @@ -43,7 +39,7 @@ export function setServerData(serverData: AttributeMap, attributes: AttributeMap
}
}

export function setPendingOp(pendingOps: Array<OpsMap>, attr: string, op: ?Op) {
export function setPendingOp(pendingOps: Array<OpsMap>, attr: string, op?: Op) {
const last = pendingOps.length - 1;
if (op) {
pendingOps[last][attr] = op;
Expand Down Expand Up @@ -84,7 +80,7 @@ export function estimateAttribute(
pendingOps: Array<OpsMap>,
object: ParseObject,
attr: string
): mixed {
): any {
let value = serverData[attr];
for (let i = 0; i < pendingOps.length; i++) {
if (pendingOps[i][attr]) {
Expand Down
3 changes: 1 addition & 2 deletions src/Parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ import WebSocketController from './WebSocketController';
* @global
* @class
* @hideconstructor
*/

*/
interface ParseType {
ACL: typeof ACL,
Parse?: ParseType,
Expand Down
60 changes: 28 additions & 32 deletions src/ParseFile.js → src/ParseFile.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
/**
* @flow
*/
/* global XMLHttpRequest, Blob */
import CoreManager from './CoreManager';
import type { FullOptions } from './RESTController';

const ParseError = require('./ParseError').default;
import ParseError from './ParseError';

let XHR = null;
if (typeof XMLHttpRequest !== 'undefined') {
Expand Down Expand Up @@ -66,13 +62,13 @@ function b64Digit(number: number): string {
*/
class ParseFile {
_name: string;
_url: ?string;
_url?: string;
_source: FileSource;
_previousSave: ?Promise<ParseFile>;
_data: ?string;
_requestTask: ?any;
_metadata: ?Object;
_tags: ?Object;
_previousSave?: Promise<ParseFile>;
_data?: string;
_requestTask?: any;
_metadata?: Object;
_tags?: Object;

/**
* @param name {String} The file's name. This will be prefixed by a unique
Expand Down Expand Up @@ -125,17 +121,17 @@ class ParseFile {
file: data,
type: specifiedType,
};
} else if (data && typeof data.uri === 'string' && data.uri !== undefined) {
} else if (data && typeof (data as Uri).uri === 'string' && (data as Uri).uri !== undefined) {
this._source = {
format: 'uri',
uri: data.uri,
uri: (data as Uri).uri,
type: specifiedType,
};
} else if (data && typeof data.base64 === 'string') {
const base64 = data.base64.split(',').slice(-1)[0];
} else if (data && typeof (data as Base64).base64 === 'string') {
const base64 = (data as Base64).base64.split(',').slice(-1)[0];
const dataType =
specifiedType ||
data.base64.split(';').slice(0, 1)[0].split(':').slice(1, 2)[0] ||
(data as Base64).base64.split(';').slice(0, 1)[0].split(':').slice(1, 2)[0] ||
'text/plain';
this._data = base64;
this._source = {
Expand All @@ -156,7 +152,7 @@ class ParseFile {
*
* @returns {Promise} Promise that is resolve with base64 data
*/
async getData(): Promise<String> {
async getData(): Promise<string> {
if (this._data) {
return this._data;
}
Expand Down Expand Up @@ -190,7 +186,7 @@ class ParseFile {
* @param {object} options An object to specify url options
* @returns {string | undefined}
*/
url(options?: { forceSecure?: boolean }): ?string {
url(options?: { forceSecure?: boolean }): string | undefined {
options = options || {};
if (!this._url) {
return;
Expand All @@ -207,7 +203,7 @@ class ParseFile {
*
* @returns {object}
*/
metadata(): Object {
metadata() {
return this._metadata;
}

Expand All @@ -216,7 +212,7 @@ class ParseFile {
*
* @returns {object}
*/
tags(): Object {
tags() {
return this._tags;
}

Expand All @@ -243,7 +239,7 @@ class ParseFile {
* </ul>
* @returns {Promise | undefined} Promise that is resolved when the save finishes.
*/
save(options?: FullOptions): ?Promise {
save(options?: FileSaveOptions): Promise<ParseFile> | undefined {
options = options || {};
options.requestTask = task => (this._requestTask = task);
options.metadata = this._metadata;
Expand All @@ -267,15 +263,15 @@ class ParseFile {
return {};
}
const newSource = {
format: 'base64',
format: 'base64' as const,
base64: result.base64,
type: result.contentType,
};
this._data = result.base64;
this._requestTask = null;
return controller.saveBase64(this._name, newSource, options);
})
.then(res => {
.then((res: { name?: string, url?: string }) => {
this._name = res.name;
this._url = res.url;
this._requestTask = null;
Expand Down Expand Up @@ -317,7 +313,7 @@ class ParseFile {
* <pre>
* @returns {Promise} Promise that is resolved when the delete finishes.
*/
destroy(options?: FullOptions = {}) {
destroy(options: FullOptions = {}) {
if (!this._name) {
throw new ParseError(ParseError.FILE_DELETE_UNNAMED_ERROR, 'Cannot delete an unnamed file.');
}
Expand All @@ -333,15 +329,15 @@ class ParseFile {
});
}

toJSON(): { name: ?string, url: ?string } {
toJSON(): { __type: 'File', name?: string, url?: string } {
return {
__type: 'File',
name: this._name,
url: this._url,
};
}

equals(other: mixed): boolean {
equals(other: any): boolean {
if (this === other) {
return true;
}
Expand Down Expand Up @@ -413,7 +409,7 @@ class ParseFile {
return file;
}

static encodeBase64(bytes: Array<number>): string {
static encodeBase64(bytes: Array<number> | Uint8Array): string {
const chunks = [];
chunks.length = Math.ceil(bytes.length / 3);
for (let i = 0; i < chunks.length; i++) {
Expand Down Expand Up @@ -441,10 +437,10 @@ const DefaultController = {
if (source.format !== 'file') {
throw new Error('saveFile can only be used with File-type sources.');
}
const base64Data = await new Promise((res, rej) => {
const base64Data = await new Promise<string>((res, rej) => {
// eslint-disable-next-line no-undef
const reader = new FileReader();
reader.onload = () => res(reader.result);
reader.onload = () => res(reader.result as string);
reader.onerror = error => rej(error);
reader.readAsDataURL(source.file);
});
Expand All @@ -455,9 +451,9 @@ const DefaultController = {
// use the entire string instead
const data = second ? second : first;
const newSource = {
format: 'base64',
format: 'base64' as const,
base64: data,
type: source.type || (source.file ? source.file.type : null),
type: source.type || (source.file ? source.file.type : undefined),
};
return await DefaultController.saveBase64(name, newSource, options);
},
Expand Down Expand Up @@ -510,7 +506,7 @@ const DefaultController = {
}
},

downloadAjax: function (uri, options) {
downloadAjax: function (uri: string, options: any) {
return new Promise((resolve, reject) => {
const xhr = new XHR();
xhr.open('GET', uri, true);
Expand Down
12 changes: 9 additions & 3 deletions src/ParseGeoPoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,15 @@ class ParseGeoPoint {
* Creates a GeoPoint with the user's current location, if available.
*
* @param {object} options The options.
* @param {boolean} [options.enableHighAccuracy=false] A boolean value that indicates the application would like to receive the best possible results. If true and if the device is able to provide a more accurate position, it will do so. Note that this can result in slower response times or increased power consumption (with a GPS chip on a mobile device for example). On the other hand, if false, the device can take the liberty to save resources by responding more quickly and/or using less power. Default: false.
* @param {number} [options.timeout=Infinity] A positive long value representing the maximum length of time (in milliseconds) the device is allowed to take in order to return a position. The default value is Infinity, meaning that getCurrentPosition() won't return until the position is available.
* @param {number} [options.maximumAge=0] A positive long value indicating the maximum age in milliseconds of a possible cached position that is acceptable to return. If set to 0, it means that the device cannot use a cached position and must attempt to retrieve the real current position. If set to Infinity the device must return a cached position regardless of its age. Default: 0.
* @param {boolean} [options.enableHighAccuracy=false] A boolean value that indicates the application would like to receive the best possible results.
* If true and if the device is able to provide a more accurate position, it will do so.
* Note that this can result in slower response times or increased power consumption (with a GPS chip on a mobile device for example).
* On the other hand, if false, the device can take the liberty to save resources by responding more quickly and/or using less power. Default: false.
* @param {number} [options.timeout=Infinity] A positive long value representing the maximum length of time (in milliseconds) the device is allowed to take in order to return a position.
* The default value is Infinity, meaning that getCurrentPosition() won't return until the position is available.
* @param {number} [options.maximumAge=0] A positive long value indicating the maximum age in milliseconds of a possible cached position that is acceptable to return.
* If set to 0, it means that the device cannot use a cached position and must attempt to retrieve the real current position.
* If set to Infinity the device must return a cached position regardless of its age. Default: 0.
* @static
* @returns {Promise<Parse.GeoPoint>} User's current location
*/
Expand Down
26 changes: 13 additions & 13 deletions src/ParseHooks.js → src/ParseHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,52 +13,52 @@ export function getTriggers() {
return CoreManager.getHooksController().get('triggers');
}

export function getFunction(name) {
export function getFunction(name: string) {
return CoreManager.getHooksController().get('functions', name);
}

export function getTrigger(className, triggerName) {
export function getTrigger(className: string, triggerName: string) {
return CoreManager.getHooksController().get('triggers', className, triggerName);
}

export function createFunction(functionName, url) {
export function createFunction(functionName: string, url: string) {
return create({ functionName: functionName, url: url });
}

export function createTrigger(className, triggerName, url) {
export function createTrigger(className: string, triggerName: string, url: string) {
return create({ className: className, triggerName: triggerName, url: url });
}

export function create(hook) {
export function create(hook: HookDeclaration) {
return CoreManager.getHooksController().create(hook);
}

export function updateFunction(functionName, url) {
export function updateFunction(functionName: string, url: string) {
return update({ functionName: functionName, url: url });
}

export function updateTrigger(className, triggerName, url) {
export function updateTrigger(className: string, triggerName: string, url: string) {
return update({ className: className, triggerName: triggerName, url: url });
}

export function update(hook) {
export function update(hook: HookDeclaration) {
return CoreManager.getHooksController().update(hook);
}

export function removeFunction(functionName) {
export function removeFunction(functionName: string) {
return remove({ functionName: functionName });
}

export function removeTrigger(className, triggerName) {
export function removeTrigger(className: string, triggerName: string) {
return remove({ className: className, triggerName: triggerName });
}

export function remove(hook) {
export function remove(hook: HookDeleteArg) {
return CoreManager.getHooksController().remove(hook);
}

const DefaultController = {
get(type, functionName, triggerName) {
get(type: string, functionName?: string, triggerName?: string) {
let url = '/hooks/' + type;
if (functionName) {
url += '/' + functionName;
Expand Down Expand Up @@ -111,7 +111,7 @@ const DefaultController = {
return this.sendRequest('PUT', url, hook);
},

sendRequest(method, url, body) {
sendRequest(method: string, url: string, body?: any) {
return CoreManager.getRESTController()
.request(method, url, body, { useMasterKey: true })
.then(res => {
Expand Down
Loading
Loading