From bdaccfdf4b56bac97b57de885455c2c07af0fd2f Mon Sep 17 00:00:00 2001 From: "Raschid J.F. Rafeally" Date: Fri, 6 Dec 2019 18:37:22 -0600 Subject: [PATCH] Feature/parse/various (#38878) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(parse): add Cloud job functions; SaveOptions.cascadSave; Object.revert(keys) * Apply suggestions from code review Co-Authored-By: Yago Tomé * refactor(SaveOptions): move cascadeSave opt to its own interface ...in order to keep consistency with the rest of the options. * refactor: upgrade version to 2.2.9 * fix(Parse.Config): update params for functions `get` and `save` * feat(LiveQuerySubscription): add default events * fix(Query): function subscribe() should return a Promise * improve(File): add missing constructor variations * bump version to 2.3.0 * feat(Object): add function * fix(ParseObject): remove as return type of fetchFromLocalDatastore() * refactor(parse/File): use union type instead of several constructors * feat(parse/File): add function getData() * chore(parse): bump version --- types/parse/index.d.ts | 34 +++++++++++++++++++++------------- types/parse/parse-tests.ts | 36 ++++++++++++++++++++++++++++++++---- 2 files changed, 53 insertions(+), 17 deletions(-) diff --git a/types/parse/index.d.ts b/types/parse/index.d.ts index 94c142984b..94a3e41bd8 100644 --- a/types/parse/index.d.ts +++ b/types/parse/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for parse 2.9 +// Type definitions for parse 2.10 // Project: https://parseplatform.org/ // Definitions by: Ullisen Media Group // David Poetzsch-Heffter @@ -254,10 +254,19 @@ declare namespace Parse { * extension. */ class File { - constructor(name: string, data: any, type?: string); + constructor(name: string, data: number[] | { base64: string } | Blob | { uri: string }, type?: string); + /** + * Return the data for the file, downloading it if not already present. + * Data is present if initialized with Byte Array, Base64 or Saved with Uri. + * Data is cleared if saved with File object selected with a file upload control + * + * @returns Promise that is resolved with base64 data + */ + getData(): Promise; name(): string; - url(): string; - save(options?: FullOptions): Promise; + save(options?: SuccessFailureOptions): Promise; + toJSON(): { __type: string, name: string, url: string }; + url(options?: { forceSecure: boolean }): string; } /** @@ -357,12 +366,9 @@ declare namespace Parse { static destroyAll(list: T[], options?: Object.DestroyAllOptions): Promise; static extend(className: string | { className: string }, protoProps?: any, classProps?: any): any; static fetchAll(list: T[], options: Object.FetchAllOptions): Promise; - static fetchAllIfNeeded(list: T[], options: Object.FetchAllOptions): Promise; - static fetchAllWithInclude( - list: T[], - keys: string | Array>, - options: RequestOptions, - ): Promise; + static fetchAllIfNeeded(list: T[], options?: Object.FetchAllOptions): Promise; + static fetchAllIfNeededWithInclude(list: T[], keys: string | Array>, options?: RequestOptions): Promise; + static fetchAllWithInclude(list: T[], keys: string | Array>, options?: RequestOptions): Promise; static fromJSON(json: any, override?: boolean): T; static pinAll(objects: Object[]): Promise; static pinAllWithName(name: string, objects: Object[]): Promise; @@ -388,7 +394,7 @@ declare namespace Parse { escape(attr: string): string; existed(): boolean; fetch(options?: Object.FetchOptions): Promise; - fetchFromLocalDatastore(): Promise | void; + fetchFromLocalDatastore(): Promise; fetchWithInclude(keys: string | Array>, options?: RequestOptions): Promise; get>(attr: K): T[K]; getACL(): ACL | undefined; @@ -709,6 +715,8 @@ subscription.on('close', () => {}); */ constructor(id: string, query: string, sessionToken?: string); + on(event: 'open' | 'create' | 'update' | 'enter' | 'leave' | 'delete' | 'close', listener: (object: Object) => void): this; + /** * Closes the subscription. * @@ -740,9 +748,9 @@ subscription.on('close', () => {}); } class Config extends Object { - static get(options?: SuccessFailureOptions): Promise; + static get(options?: UseMasterKeyOption): Promise; static current(): Config; - static save(attr: any): Promise; + static save(attr: any, options?: { [attr: string]: boolean }): Promise; get(attr: string): any; escape(attr: string): any; diff --git a/types/parse/parse-tests.ts b/types/parse/parse-tests.ts index ee38af7e98..2dd4fade70 100644 --- a/types/parse/parse-tests.ts +++ b/types/parse/parse-tests.ts @@ -11,7 +11,8 @@ class Game extends Parse.Object { } function test_config() { - Parse.Config.save({ foo: 'bar' }); + Parse.Config.save({ foo: 'bar' }, { foo: true }); + Parse.Config.get({ useMasterKey: true }); } function test_object() { @@ -170,6 +171,31 @@ async function test_query_promise() { } } +async function test_live_query() { + const subscription = await new Parse.Query('Test').subscribe(); + subscription.on('close', (object) => { + (object instanceof Parse.Object) == true; + }); + subscription.on('create', (object) => { + (object instanceof Parse.Object) == true; + }); + subscription.on('delete', (object) => { + (object instanceof Parse.Object) == true; + }); + subscription.on('enter', (object) => { + (object instanceof Parse.Object) == true; + }); + subscription.on('leave', (object) => { + (object instanceof Parse.Object) == true; + }); + subscription.on('open', (object) => { + (object instanceof Parse.Object) == true; + }); + subscription.on('update', (object) => { + (object instanceof Parse.Object) == true; + }); +} + function return_a_generic_query(): Parse.Query { return new Parse.Query(Game); } @@ -182,10 +208,12 @@ function test_file() { const base64 = 'V29ya2luZyBhdCBQYXJzZSBpcyBncmVhdCE='; let file = new Parse.File('myfile.txt', { base64: base64 }); + file = new Parse.File('nana', { uri: 'http://example.com/image.jps' }); + const bytes = [0xbe, 0xef, 0xca, 0xfe]; file = new Parse.File('myfile.txt', bytes); - file = new Parse.File('myfile.zzz', {}, 'image/png'); + file = new Parse.File('myfile.zzz', new Blob(), 'image/png'); const src = file.url(); @@ -699,7 +727,7 @@ function test_generic_object() { const exampleAttrFromOptions = objWithAttrsAndOptions.attributes.example // number const exampleGetFromOptions = objWithAttrsAndOptions.get('example') // number // const badAttr2 = objWithAttrsAndOptions.attributes.other // error - + } function test_to_json_generic() { @@ -771,7 +799,7 @@ function test_set_generic() { exampleObjAny.set('propA', 'some value') const exampleObjTyped = new Parse.Object<{ example: boolean }>('TestObject') - + // Parse.Object.set({}) type checks existing keys, does not allow new keys const setThingObj = exampleObjTyped.set({ example: false }) setThingObj && setThingObj.attributes.example // boolean