[@types/parse] Update definitions up to version 2.12 of Parse (#43854)

* Updated trigger definitions

* Added afterLogin and afterLogout trigger definitions
* Added FileTriggerRequest definition
* Added all files triggers definitions

* Update definitions for last 2 releases

ParseFile:
* Add metadata and tags definitions
* Add cancel, destroy definitions

ParseQuery:
* Update each definition
* Add map reduce and filter definitions
* Add explain and hint definitions
* Update from source definition to return ParseQuery instead of void
* Add cancel definition
* Add fromNetwork definition

* Add tests for ParseFile and ParseQuery

* Add tests for cloud code triggers

* Pleases npmlint

* Update types/parse/index.d.ts

Co-Authored-By: Linus Unnebäck <linus@folkdatorn.se>

* Apply suggestions from code review

Co-Authored-By: Linus Unnebäck <linus@folkdatorn.se>

* Update tests to match method signatures

* Ping travis because of pending state

Co-authored-by: Linus Unnebäck <linus@folkdatorn.se>
This commit is contained in:
Asen Lekov 2020-04-18 00:17:35 +03:00 committed by GitHub
parent 9016a34ac9
commit a954d782fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 266 additions and 16 deletions

View File

@ -1,4 +1,4 @@
// Type definitions for parse 2.10
// Type definitions for parse 2.12
// Project: https://parseplatform.org/
// Definitions by: Ullisen Media Group <https://github.com/ullisenmedia>
// David Poetzsch-Heffter <https://github.com/dpoetzsch>
@ -22,6 +22,7 @@
// Patrick O'Sullivan <https://github.com/REPTILEHAUS>
// Jerome De Leon <https://github.com/JeromeDeLeon>
// Kent Robin Haugen <https://github.com/kentrh>
// Asen Lekov <https://github.com/L3K0V>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 3.5
@ -293,10 +294,19 @@ namespace Parse {
* @returns Promise that is resolved with base64 data
*/
getData(): Promise<string>;
url(options?: { forceSecure?: boolean }): string;
metadata(): Record<string, any>;
tags(): Record<string, any>;
name(): string;
save(options?: SuccessFailureOptions): Promise<File>;
cancel(): void;
destroy(): Promise<File>;
toJSON(): { __type: string, name: string, url: string };
url(options?: { forceSecure: boolean }): string;
equals(other: File): boolean;
setMetadata(metadata: Record<string, any>): void;
addMetadata(key: string, value: any): void;
setTags(tags: Record<string, any>): void;
addTag(key: string, value: any): void;
}
/**
@ -422,6 +432,7 @@ namespace Parse {
getACL(): ACL | undefined;
has(attr: Extract<keyof T, string>): boolean;
increment(attr: Extract<keyof T, string>, amount?: number): this | false;
decrement(attr: Extract<keyof T, string>, amount?: number): this | false;
initialize(): void;
isDataAvailable(): boolean;
isNew(): boolean;
@ -657,15 +668,24 @@ namespace Parse {
X extends Extract<keyof U['attributes'], string>>(key: K, queryKey: X, query: Query<U>): this;
doesNotMatchQuery<U extends Object, K extends keyof T['attributes']>(key: K, query: Query<U>): this;
distinct<K extends keyof T['attributes'], V = T['attributes'][K]>(key: K): Promise<V>;
each(callback: (obj: T) => PromiseLike<void> | void, options?: Query.EachOptions): Promise<void>;
eachBatch(callback: (objs: T[]) => PromiseLike<void> | void, options?: Query.BatchOptions): Promise<void>;
each(callback: (obj: T) => PromiseLike<void> | void, options?: Query.BatchOptions): Promise<void>;
hint(value: string | object): this;
explain(explain: boolean): this;
map<U>(callback: (currentObject: T, index: number, query: Query) => PromiseLike<U> | U, options?: Query.BatchOptions): Promise<U[]>;
reduce(callback: (accumulator: T, currentObject: T, index: number) => PromiseLike<T> | T, initialValue?: undefined, options?: Query.BatchOptions): Promise<T>;
reduce<U>(callback: (accumulator: U, currentObject: T, index: number) => PromiseLike<U> | U, initialValue: U, options?: Query.BatchOptions): Promise<U>;
filter(callback: (currentObject: T, index: number, query: Query) => PromiseLike<boolean> | boolean, options?: Query.BatchOptions): Promise<T[]>;
endsWith<K extends (keyof T['attributes'] | keyof BaseAttributes)>(key: K, suffix: string): this;
equalTo<K extends (keyof T['attributes'] | keyof BaseAttributes)>(key: K, value: T['attributes'][K] | (T['attributes'][K] extends Object ? Pointer : never)): this;
exists<K extends (keyof T['attributes'] | keyof BaseAttributes)>(key: K): this;
find(options?: Query.FindOptions): Promise<T[]>;
first(options?: Query.FirstOptions): Promise<T | undefined>;
fromLocalDatastore(): void;
fromPin(): void;
fromPinWithName(name: string): void;
fromNetwork(): this;
fromLocalDatastore(): this;
fromPin(): this;
fromPinWithName(name: string): this;
cancel(): this;
fullText<K extends (keyof T['attributes'] | keyof BaseAttributes)>(key: K, value: string, options?: Query.FullTextOptions): this;
get(objectId: string, options?: Query.GetOptions): Promise<T>;
greaterThan<K extends (keyof T['attributes'] | keyof BaseAttributes)>(key: K, value: T['attributes'][K]): this;
@ -720,6 +740,10 @@ namespace Parse {
caseSensitive?: boolean;
diacriticSensitive?: boolean;
}
interface BatchOptions extends FullOptions {
batchSize?: number;
}
}
/**
@ -1109,6 +1133,12 @@ namespace Parse {
context: object;
}
interface FileTriggerRequest extends TriggerRequest {
file: File;
fileSize: number;
contentLength: number;
}
// Read preference describes how MongoDB driver route read operations to the members of a replica set.
enum ReadPreferenceOption {
Primary = 'PRIMARY',
@ -1138,7 +1168,16 @@ namespace Parse {
func?: (request: BeforeFindRequest) => Promise<Query> | Promise<void> | Query | void
): void;
function afterFind(arg1: any, func?: (request: AfterFindRequest) => any): void;
function beforeLogin(func?: (request: TriggerRequest) => any): void;
function beforeLogin(func?: (request: TriggerRequest) => PromiseLike<void> | void): void;
function afterLogin(func?: (request: TriggerRequest) => PromiseLike<void> | void): void;
function afterLogout(func?: (request: TriggerRequest) => PromiseLike<void> | void): void;
function beforeSaveFile(func?: (request: FileTriggerRequest) => PromiseLike<File> | void): void;
function afterSaveFile(func?: (request: FileTriggerRequest) => PromiseLike<void> | void): void;
function beforeDeleteFile(func?: (request: FileTriggerRequest) => PromiseLike<void> | void): void;
function afterDeleteFile(func?: (request: FileTriggerRequest) => PromiseLike<void> | void): void;
function define(name: string, func: (request: FunctionRequest) => any): void;
function define<T extends () => any>(
name: string,

View File

@ -99,6 +99,10 @@ function test_query() {
query.notEqualTo('playerName', 'Michael Yabuti');
query.fullText('playerName', 'dan', { language: 'en', caseSensitive: false, diacriticSensitive: true });
query.greaterThan('playerAge', 18);
query.eachBatch((objs) => Promise.resolve(), {batchSize: 10});
query.each((score) => Promise.resolve());
query.hint('_id_');
query.explain(true);
query.limit(10);
query.skip(10);
@ -181,6 +185,12 @@ async function test_query_promise() {
} catch (error) {
// noop
}
await getQuery.map((score, index) => score.increment("score", index));
await getQuery.reduce((accum, score, index) => accum + score.get("score"), 0);
await getQuery.reduce((accum, score, index) => accum + score.get("score"), 0, { batchSize: 200 });
await getQuery.filter((scores) => scores.get('score') > 0);
await getQuery.filter((scores) => scores.get('score') > 0, { batchSize: 10 });
}
async function test_live_query() {
@ -242,6 +252,7 @@ function test_file() {
file = new Parse.File('myfile.zzz', new Blob(), 'image/png');
const src = file.url();
const secure = file.url({forceSecure: true});
file.save().then(
() => {
@ -255,8 +266,25 @@ function test_file() {
Parse.Cloud.httpRequest({ url: file.url() }).then((response: Parse.Cloud.HttpResponse) => {
// result
});
// TODO: Check
file.cancel();
file.destroy();
}
function test_file_tags_and_metadata() {
const base64 = 'V29ya2luZyBhdCBQYXJzZSBpcyBncmVhdCE=';
const file = new Parse.File('myfile.txt', { base64 });
file.setTags({ownerId: 42, status: "okay"});
file.addTag('labes', ['one', 'two', 'three']);
file.setMetadata({contentType: 'plain/text', contentLength: 579});
file.addMetadata('author', 'John Doe');
const tags = file.tags();
const ownerId = tags['ownerId'];
const metadata = file.metadata();
const contentType = metadata['contentType'];
}
function test_analytics() {
@ -562,7 +590,28 @@ async function test_cloud_functions() {
});
Parse.Cloud.beforeLogin((request: Parse.Cloud.TriggerRequest) => {
return 'Some result';
return Promise.resolve();
});
Parse.Cloud.afterLogin((request: Parse.Cloud.TriggerRequest) => {
return Promise.resolve();
});
Parse.Cloud.afterLogout((request: Parse.Cloud.TriggerRequest) => {
return Promise.resolve();
});
Parse.Cloud.beforeSaveFile((request: Parse.Cloud.FileTriggerRequest) => {
return Promise.resolve(new Parse.File("myFile.txt", {base64: ''}));
});
Parse.Cloud.beforeSaveFile((request: Parse.Cloud.FileTriggerRequest) => {
});
Parse.Cloud.beforeDeleteFile((request: Parse.Cloud.FileTriggerRequest) => {
});
Parse.Cloud.afterDeleteFile((request: Parse.Cloud.FileTriggerRequest) => {
});
Parse.Cloud.define('AFunc', (request: Parse.Cloud.FunctionRequest) => {
@ -764,6 +813,23 @@ async function test_local_datastore() {
Parse.setLocalDatastoreController({});
}
async function test_from_network() {
const obj = new Parse.Object('TestObject');
await obj.save();
const query = new Parse.Query('TestObject');
query.fromNetwork();
}
async function test_cancel_query() {
const obj = new Parse.Object('TestObject');
await obj.save();
const query = new Parse.Query('TestObject');
query.fromNetwork().find();
query.cancel();
}
type FieldType = string | number | boolean | Date | Parse.File | Parse.GeoPoint | any[] | object | Parse.Pointer | Parse.Polygon | Parse.Relation;
async function test_schema(
anyField: FieldType,
@ -1093,6 +1159,26 @@ function testObject() {
objTyped.increment('other');
}
function testDecrement(objUntyped: Parse.Object, objTyped: Parse.Object<{ example: number }>) {
// $ExpectType false | Object<Attributes>
objUntyped.decrement('whatever');
// $ExpectType false | Object<Attributes>
objUntyped.decrement('whatever', 10);
// $ExpectType false | Object<{ example: number; }>
objTyped.decrement('example');
// $ExpectType false | Object<{ example: number; }>
objTyped.decrement('example', 20);
// $ExpectError
objTyped.decrement('example', true);
// $ExpectError
objTyped.decrement('other');
}
function testNewInstance(objUntyped: Parse.Object, objTyped: Parse.Object<{ example: number }>) {
// $ExpectType Object<Attributes>
objUntyped.newInstance();

View File

@ -266,10 +266,19 @@ namespace Parse {
* @returns Promise that is resolved with base64 data
*/
getData(): Promise<string>;
url(options?: { forceSecure?: boolean }): string;
metadata(): Record<string, any>;
tags(): Record<string, any>;
name(): string;
save(options?: SuccessFailureOptions): Promise<File>;
cancel(): void;
destroy(): Promise<File>;
toJSON(): { __type: string, name: string, url: string };
url(options?: { forceSecure: boolean }): string;
equals(other: File): boolean;
setMetadata(metadata: Record<string, any>): void;
addMetadata(key: string, value: any): void;
setTags(tags: Record<string, any>): void;
addTag(key: string, value: any): void;
}
/**
@ -395,6 +404,7 @@ namespace Parse {
getACL(): ACL | undefined;
has(attr: Extract<keyof T, string>): boolean;
increment(attr: Extract<keyof T, string>, amount?: number): this | false;
decrement(attr: Extract<keyof T, string>, amount?: number): this | false;
initialize(): void;
isDataAvailable(): boolean;
isNew(): boolean;
@ -630,15 +640,24 @@ namespace Parse {
X extends Extract<keyof U['attributes'], string>>(key: K, queryKey: X, query: Query<U>): this;
doesNotMatchQuery<U extends Object, K extends keyof T['attributes']>(key: K, query: Query<U>): this;
distinct<K extends keyof T['attributes'], V = T['attributes'][K]>(key: K): Promise<V>;
each(callback: (obj: T) => PromiseLike<void> | void, options?: Query.EachOptions): Promise<void>;
eachBatch(callback: (objs: T[]) => PromiseLike<void> | void, options?: Query.BatchOptions): Promise<void>;
each(callback: (obj: T) => PromiseLike<void> | void, options?: Query.BatchOptions): Promise<void>;
hint(value: string | object): this;
explain(explain: boolean): this;
map<U>(callback: (currentObject: T, index: number, query: Query) => PromiseLike<U> | U, options?: Query.BatchOptions): Promise<U[]>;
reduce(callback: (accumulator: T, currentObject: T, index: number) => PromiseLike<T> | T, initialValue?: undefined, options?: Query.BatchOptions): Promise<T>;
reduce<U>(callback: (accumulator: U, currentObject: T, index: number) => PromiseLike<U> | U, initialValue: U, options?: Query.BatchOptions): Promise<U>;
filter(callback: (currentObject: T, index: number, query: Query) => PromiseLike<boolean> | boolean, options?: Query.BatchOptions): Promise<T[]>;
endsWith<K extends (keyof T['attributes'] | keyof BaseAttributes)>(key: K, suffix: string): this;
equalTo<K extends (keyof T['attributes'] | keyof BaseAttributes)>(key: K, value: T['attributes'][K] | (T['attributes'][K] extends Object ? Pointer : never)): this;
exists<K extends (keyof T['attributes'] | keyof BaseAttributes)>(key: K): this;
find(options?: Query.FindOptions): Promise<T[]>;
first(options?: Query.FirstOptions): Promise<T | undefined>;
fromLocalDatastore(): void;
fromPin(): void;
fromPinWithName(name: string): void;
fromNetwork(): this;
fromLocalDatastore(): this;
fromPin(): this;
fromPinWithName(name: string): this;
cancel(): this;
fullText<K extends (keyof T['attributes'] | keyof BaseAttributes)>(key: K, value: string, options?: Query.FullTextOptions): this;
get(objectId: string, options?: Query.GetOptions): Promise<T>;
greaterThan<K extends (keyof T['attributes'] | keyof BaseAttributes)>(key: K, value: T['attributes'][K]): this;
@ -693,6 +712,10 @@ namespace Parse {
caseSensitive?: boolean;
diacriticSensitive?: boolean;
}
interface BatchOptions extends FullOptions {
batchSize?: number;
}
}
/**
@ -1081,6 +1104,12 @@ namespace Parse {
context: object;
}
interface FileTriggerRequest extends TriggerRequest {
file: File;
fileSize: number;
contentLength: number;
}
// Read preference describes how MongoDB driver route read operations to the members of a replica set.
enum ReadPreferenceOption {
Primary = 'PRIMARY',
@ -1110,7 +1139,16 @@ namespace Parse {
func?: (request: BeforeFindRequest) => Promise<Query> | Promise<void> | Query | void
): void;
function afterFind(arg1: any, func?: (request: AfterFindRequest) => any): void;
function beforeLogin(func?: (request: TriggerRequest) => any): void;
function beforeLogin(func?: (request: TriggerRequest) => PromiseLike<void> | void): void;
function afterLogin(func?: (request: TriggerRequest) => PromiseLike<void> | void): void;
function afterLogout(func?: (request: TriggerRequest) => PromiseLike<void> | void): void;
function beforeSaveFile(func?: (request: FileTriggerRequest) => PromiseLike<File> | void): void;
function afterSaveFile(func?: (request: FileTriggerRequest) => PromiseLike<void> | void): void;
function beforeDeleteFile(func?: (request: FileTriggerRequest) => PromiseLike<void> | void): void;
function afterDeleteFile(func?: (request: FileTriggerRequest) => PromiseLike<void> | void): void;
function define(name: string, func: (request: FunctionRequest) => any): void;
function define<T extends () => any>(
name: string,

View File

@ -99,6 +99,10 @@ function test_query() {
query.notEqualTo('playerName', 'Michael Yabuti');
query.fullText('playerName', 'dan', { language: 'en', caseSensitive: false, diacriticSensitive: true });
query.greaterThan('playerAge', 18);
query.eachBatch((objs) => Promise.resolve(), {batchSize: 10});
query.each((score) => Promise.resolve());
query.hint('_id_');
query.explain(true);
query.limit(10);
query.skip(10);
@ -181,6 +185,12 @@ async function test_query_promise() {
} catch (error) {
// noop
}
await getQuery.map((score, index) => score.increment("score", index));
await getQuery.reduce((accum, score, index) => accum += score.get("score"), 0);
await getQuery.reduce((accum, score, index) => accum += score.get("score"), 0, { batchSize: 200 });
await getQuery.filter((scores) => scores.get('score') > 0);
await getQuery.filter((scores) => scores.get('score') > 0, { batchSize: 10 });
}
async function test_live_query() {
@ -242,6 +252,7 @@ function test_file() {
file = new Parse.File('myfile.zzz', new Blob(), 'image/png');
const src = file.url();
const secure = file.url({forceSecure: true});
file.save().then(
() => {
@ -257,6 +268,24 @@ function test_file() {
});
// TODO: Check
file.cancel();
file.destroy();
}
function test_file_tags_and_metadata() {
const base64 = 'V29ya2luZyBhdCBQYXJzZSBpcyBncmVhdCE=';
const file = new Parse.File('myfile.txt', { base64 });
file.setTags({ownerId: 42, status: "okay"});
file.addTag('labes', ['one', 'two', 'three']);
file.setMetadata({contentType: 'plain/text', contentLength: 579});
file.addMetadata('author', 'John Doe');
const tags = file.tags();
const ownerId = tags['ownerId'];
const metadata = file.metadata();
const contentType = metadata['contentType'];
}
function test_analytics() {
@ -562,7 +591,28 @@ async function test_cloud_functions() {
});
Parse.Cloud.beforeLogin((request: Parse.Cloud.TriggerRequest) => {
return 'Some result';
return Promise.resolve();
});
Parse.Cloud.afterLogin((request: Parse.Cloud.TriggerRequest) => {
return Promise.resolve();
});
Parse.Cloud.afterLogout((request: Parse.Cloud.TriggerRequest) => {
return Promise.resolve();
});
Parse.Cloud.beforeSaveFile((request: Parse.Cloud.FileTriggerRequest) => {
return Promise.resolve(new Parse.File("myFile.txt", {base64: ''}));
});
Parse.Cloud.beforeSaveFile((request: Parse.Cloud.FileTriggerRequest) => {
});
Parse.Cloud.beforeDeleteFile((request: Parse.Cloud.FileTriggerRequest) => {
});
Parse.Cloud.afterDeleteFile((request: Parse.Cloud.FileTriggerRequest) => {
});
Parse.Cloud.define('AFunc', (request: Parse.Cloud.FunctionRequest) => {
@ -764,6 +814,23 @@ async function test_local_datastore() {
Parse.setLocalDatastoreController({});
}
async function test_from_network() {
const obj = new Parse.Object('TestObject');
await obj.save();
const query = new Parse.Query('TestObject');
query.fromNetwork();
}
async function test_cancel_query() {
const obj = new Parse.Object('TestObject');
await obj.save();
const query = new Parse.Query('TestObject');
query.fromNetwork().find();
query.cancel();
}
type FieldType = string | number | boolean | Date | Parse.File | Parse.GeoPoint | any[] | object | Parse.Pointer | Parse.Polygon | Parse.Relation;
async function test_schema(
anyField: FieldType,
@ -1093,6 +1160,26 @@ function testObject() {
objTyped.increment('other');
}
function testDecrement(objUntyped: Parse.Object, objTyped: Parse.Object<{ example: number }>) {
// $ExpectType false | Object<Attributes>
objUntyped.decrement('whatever');
// $ExpectType false | Object<Attributes>
objUntyped.decrement('whatever', 10);
// $ExpectType false | Object<{ example: number; }>
objTyped.decrement('example');
// $ExpectType false | Object<{ example: number; }>
objTyped.decrement('example', 20);
// $ExpectError
objTyped.decrement('example', true);
// $ExpectError
objTyped.decrement('other');
}
function testNewInstance(objUntyped: Parse.Object, objTyped: Parse.Object<{ example: number }>) {
// $ExpectType Object<Attributes>
objUntyped.newInstance();