From 84bed8dca0de872113cf8fc7896301e2be9bd82c Mon Sep 17 00:00:00 2001 From: Szymon Stasik Date: Tue, 1 Mar 2016 10:40:30 +0100 Subject: [PATCH] handle promises returned by firebase API --- firebase/firebase-tests.ts | 174 +++++++++++++++++++++++++++++++++++++ firebase/firebase.d.ts | 58 ++++++++----- 2 files changed, 210 insertions(+), 22 deletions(-) diff --git a/firebase/firebase-tests.ts b/firebase/firebase-tests.ts index 1b921bcba5..ede8744017 100644 --- a/firebase/firebase-tests.ts +++ b/firebase/firebase-tests.ts @@ -27,9 +27,43 @@ var firebaseRef = new Firebase('https://samplechat.firebaseio-demo.com/'); }); } +() => { + var dataRef = new Firebase('https://samplechat.firebaseio-demo.com'); + + var onComplete = function (authData: FirebaseAuthData) { + console.log('Synchronization succeeded'); + }; + var onError = function (error: any) { + if (error) { + console.log('Synchronization failed'); + } + }; + + // Log me in + dataRef.authWithCustomToken(AUTH_TOKEN).then(onComplete, onError); + // Same as before but use returned Promise to handle the result +} + /* * Firebase.authAnonymously() */ +() => { + var dataRef = new Firebase('https://samplechat.firebaseio-demo.com'); + + var onComplete = function (authData: FirebaseAuthData) { + console.log('Synchronization succeeded'); + }; + var onError = function (error: any) { + if (error) { + console.log('Synchronization failed'); + } + }; + + // Log me in + dataRef.authAnonymously().then(onComplete, onError); + // Same as before but use returned Promise to handle the result +} + () => { var dataRef = new Firebase('https://samplechat.firebaseio-demo.com'); // Log me in @@ -60,6 +94,25 @@ var firebaseRef = new Firebase('https://samplechat.firebaseio-demo.com/'); }); } +() => { + var dataRef = new Firebase('https://samplechat.firebaseio-demo.com'); + var onComplete = function (authData: FirebaseAuthData) { + console.log('Synchronization succeeded'); + }; + var onError = function (error: any) { + if (error) { + console.log('Synchronization failed'); + } + }; + + // Log me in + dataRef.authWithPassword({ + "email": "bobtony@firebase.com", + "password": "correcthorsebatterystaple" + }).then(onComplete, onError); + // Same as before but use returned Promise to handle the result +} + /* * Firebase.authWithOAuthPopup() */ @@ -75,6 +128,23 @@ var firebaseRef = new Firebase('https://samplechat.firebaseio-demo.com/'); }); } +() => { + var dataRef = new Firebase('https://samplechat.firebaseio-demo.com'); + + var onComplete = function (authData: FirebaseAuthData) { + console.log('Synchronization succeeded'); + }; + var onError = function (error: any) { + if (error) { + console.log('Synchronization failed'); + } + }; + + // Log me in + dataRef.authWithOAuthPopup("twitter").then(onComplete, onError); + // Same as before but use returned Promise to handle the result +} + /* * Firebase.authWithOAuthRedirect */ @@ -90,6 +160,23 @@ var firebaseRef = new Firebase('https://samplechat.firebaseio-demo.com/'); }); } +() => { + var dataRef = new Firebase('https://samplechat.firebaseio-demo.com'); + + var onComplete = function () { + // We'll never get here, as the page will redirect on success. + }; + var onError = function (error: any) { + if (error) { + console.log('Synchronization failed'); + } + }; + + // Log me in + dataRef.authWithOAuthRedirect("twitter").then(onComplete, onError); + // Same as before but use returned Promise to handle the result +} + /* * Firebase.authWithOAuthToken() */ @@ -104,6 +191,24 @@ var firebaseRef = new Firebase('https://samplechat.firebaseio-demo.com/'); } }); } + +() => { + var dataRef = new Firebase('https://samplechat.firebaseio-demo.com'); + + var onComplete = function (authData: FirebaseAuthData) { + console.log('Synchronization succeeded'); + }; + var onError = function (error: any) { + if (error) { + console.log('Synchronization failed'); + } + }; + + // Authenticate with Facebook using an existing OAuth 2.0 access token + dataRef.authWithOAuthToken("facebook", "").then(onComplete, onError); + // Same as before but use returned Promise to handle the result +} + () => { var dataRef = new Firebase('https://samplechat.firebaseio-demo.com'); // Authenticate with Twitter using an existing OAuth 1.0a credential set @@ -226,6 +331,20 @@ var x4:string = fredRef3.name(); // when the data has finished synchronizing } +() => { + var fredNameRef = new Firebase('https://samplechat.firebaseio-demo.com/users/fred/name'); + var onComplete = function () { + console.log('Synchronization succeeded'); + }; + var onError = function (error: any) { + if (error) { + console.log('Synchronization failed'); + } + }; + fredNameRef.set({ first: 'Fred', last: 'Flintstone' }).then(onComplete, onError); + // Same as the previous example but use returned Promise to handle the result +} + /* * Firebase.update() */ @@ -247,6 +366,21 @@ var x4:string = fredRef3.name(); }; fredNameRef.update({ first: 'Wilma', last: 'Flintstone' }, onComplete); } + +() => { + var fredNameRef = new Firebase('https://samplechat.firebaseio-demo.com/users/fred/name'); + var onComplete = function () { + console.log('Synchronization succeeded'); + }; + var onError = function (error: any) { + if (error) { + console.log('Synchronization failed'); + } + }; + fredNameRef.update({ first: 'Fred', last: 'Flintstone' }).then(onComplete, onError); + // Same as the previous example but use returned Promise to handle the result +} + () => { var fredRef = new Firebase('https://samplechat.firebaseio-demo.com/users/fred'); //The following 2 function calls are equivalent @@ -276,6 +410,19 @@ var x4:string = fredRef3.name(); // a message when the delete has finished synchronizing } +() => { + var onComplete = function () { + console.log('Synchronization succeeded'); + }; + var onError = function (error: any) { + if (error) { + console.log('Synchronization failed'); + } + }; + fredRef.remove().then(onComplete, onError); + // Same as the previous example but use returned Promise to handle the result +} + /* * Firebase.push() */ @@ -313,6 +460,33 @@ var x4:string = fredRef3.name(); // priority of the data so he'll be ordered relative to other users by his rank } + +() => { + var fredRef = new Firebase('https://samplechat.firebaseio-demo.com/users/fred'); + + var user = { + name: { + first: 'Fred', + last: 'Flintstone' + }, + rank: 1000 + }; + + fredRef.setWithPriority(user, 1000); + + var onComplete = function () { + console.log('Synchronization succeeded'); + }; + var onError = function (error: any) { + if (error) { + console.log('Synchronization failed'); + } + }; + + fredRef.setWithPriority(user, 1000).then(onComplete, onError); + // Same as the previous example but use returned Promise to handle the result +} + /* * Firebase.setPriority() */ diff --git a/firebase/firebase.d.ts b/firebase/firebase.d.ts index 58d7cd8742..c4cda65bfb 100644 --- a/firebase/firebase.d.ts +++ b/firebase/firebase.d.ts @@ -1,6 +1,6 @@ -// Type definitions for Firebase API 2.0.2 +// Type definitions for Firebase API 2.4.1 // Project: https://www.firebase.com/docs/javascript/firebase -// Definitions by: Vincent Botone , Shin1 Kashimura , Sebastien Dubois +// Definitions by: Vincent Botone , Shin1 Kashimura , Sebastien Dubois , Szymon Stasik // Definitions: https://github.com/borisyankov/DefinitelyTyped interface FirebaseAuthResult { @@ -68,27 +68,31 @@ interface FirebaseOnDisconnect { * Ensures the data at this location is set to the specified value when the client is disconnected * (due to closing the browser, navigating to a new page, or network issues). */ - set(value: any, onComplete?: (error: any) => void): void; + set(value: any, onComplete: (error: any) => void): void; + set(value: any): Promise; /** * Ensures the data at this location is set to the specified value and priority when the client is disconnected * (due to closing the browser, navigating to a new page, or network issues). */ - setWithPriority(value: any, priority: string, onComplete?: (error: any) => void): void; - setWithPriority(value: any, priority: number, onComplete?: (error: any) => void): void; + setWithPriority(value: any, priority: string|number, onComplete: (error: any) => void): void; + setWithPriority(value: any, priority: string|number): Promise; /** * Writes the enumerated children at this Firebase location when the client is disconnected * (due to closing the browser, navigating to a new page, or network issues). */ - update(value: Object, onComplete?: (error: any) => void): void; + update(value: Object, onComplete: (error: any) => void): void; + update(value: Object): Promise; /** * Ensures the data at this location is deleted when the client is disconnected * (due to closing the browser, navigating to a new page, or network issues). */ - remove(onComplete?: (error: any) => void): void; + remove(onComplete: (error: any) => void): void; + remove(): Promise; /** * Cancels all previously queued onDisconnect() set or update events for this location and all children. */ - cancel(onComplete?: (error: any) => void): void; + cancel(onComplete: (error: any) => void): void; + cancel(): Promise; } interface FirebaseQuery { @@ -141,9 +145,7 @@ interface FirebaseQuery { /** * Creates a Query which includes children which match the specified value. */ - equalTo(value: string, key?: string): FirebaseQuery; - equalTo(value: number, key?: string): FirebaseQuery; - equalTo(value: boolean, key?: string): FirebaseQuery; + equalTo(value: string|number|boolean, key?: string): FirebaseQuery; /** * Generates a new Query object limited to the first certain number of children. */ @@ -163,32 +165,38 @@ interface Firebase extends FirebaseQuery { * @deprecated Use authWithCustomToken() instead. * Authenticates a Firebase client using the provided authentication token or Firebase Secret. */ - auth(authToken: string, onComplete?: (error: any, result: FirebaseAuthResult) => void, onCancel?:(error: any) => void): void; + auth(authToken: string, onComplete: (error: any, result: FirebaseAuthResult) => void, onCancel?:(error: any) => void): void; + auth(authToken: string): Promise; /** * Authenticates a Firebase client using an authentication token or Firebase Secret. */ authWithCustomToken(autoToken: string, onComplete: (error: any, authData: FirebaseAuthData) => void, options?:Object): void; + authWithCustomToken(autoToken: string, options?:Object): Promise; /** * Authenticates a Firebase client using a new, temporary guest account. */ authAnonymously(onComplete: (error: any, authData: FirebaseAuthData) => void, options?: Object): void; + authAnonymously(options?: Object): Promise; /** * Authenticates a Firebase client using an email / password combination. */ authWithPassword(credentials: FirebaseCredentials, onComplete: (error: any, authData: FirebaseAuthData) => void, options?: Object): void; + authWithPassword(credentials: FirebaseCredentials, options?: Object): Promise; /** * Authenticates a Firebase client using a popup-based OAuth flow. */ authWithOAuthPopup(provider: string, onComplete:(error: any, authData: FirebaseAuthData) => void, options?: Object): void; + authWithOAuthPopup(provider: string, options?: Object): Promise; /** * Authenticates a Firebase client using a redirect-based OAuth flow. */ authWithOAuthRedirect(provider: string, onComplete: (error: any) => void, options?: Object): void; + authWithOAuthRedirect(provider: string, options?: Object): Promise; /** * Authenticates a Firebase client using OAuth access tokens or credentials. */ - authWithOAuthToken(provider: string, credentials: string, onComplete: (error: any, authData: FirebaseAuthData) => void, options?: Object): void; - authWithOAuthToken(provider: string, credentials: Object, onComplete: (error: any, authData: FirebaseAuthData) => void, options?: Object): void; + authWithOAuthToken(provider: string, credentials: string|Object, onComplete: (error: any, authData: FirebaseAuthData) => void, options?: Object): void; + authWithOAuthToken(provider: string, credentials: string|Object, options?: Object): Promise; /** * Synchronously access the current authentication state of the client. */ @@ -233,30 +241,33 @@ interface Firebase extends FirebaseQuery { /** * Writes data to this Firebase location. */ - set(value: any, onComplete?: (error: any) => void): void; + set(value: any, onComplete: (error: any) => void): void; + set(value: any): Promise; /** * Writes the enumerated children to this Firebase location. */ - update(value: Object, onComplete?: (error: any) => void): void; + update(value: Object, onComplete: (error: any) => void): void; + update(value: Object): Promise; /** * Removes the data at this Firebase location. */ - remove(onComplete?: (error: any) => void): void; + remove(onComplete: (error: any) => void): void; + remove(): Promise; /** * Generates a new child location using a unique name and returns a Firebase reference to it. * @returns {Firebase} A Firebase reference for the generated location. */ - push(value?: any, onComplete?: (error: any) => void): Firebase; + push(value?: any, onComplete?: (error: any) => void): FirebaseWithPromise; /** * Writes data to this Firebase location. Like set() but also specifies the priority for that data. */ - setWithPriority(value: any, priority: string, onComplete?: (error: any) => void): void; - setWithPriority(value: any, priority: number, onComplete?: (error: any) => void): void; + setWithPriority(value: any, priority: string|number, onComplete: (error: any) => void): void; + setWithPriority(value: any, priority: string|number): Promise; /** * Sets a priority for the data at this Firebase location. */ - setPriority(priority: string, onComplete?: (error: any) => void): void; - setPriority(priority: number, onComplete?: (error: any) => void): void; + setPriority(priority: string|number, onComplete: (error: any) => void): void; + setPriority(priority: string|number): Promise; /** * Atomically modifies the data at this location. */ @@ -283,6 +294,9 @@ interface Firebase extends FirebaseQuery { resetPassword(credentials: FirebaseResetPasswordCredentials, onComplete: (error: any) => void): void; onDisconnect(): FirebaseOnDisconnect; } + +interface FirebaseWithPromise extends Firebase, Promise {} + interface FirebaseStatic { /** * Constructs a new Firebase reference from a full Firebase URL.