Skip to content
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

Add an initial typescript definitions file for re-base version 2.8.0 #207

Open
wants to merge 6 commits into
base: 2.x
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules
**.sw*
**.log
examples/**/bundle.js
./tests/re-base.tests.js
122 changes: 122 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
///<reference types="firebase" />
interface FirebaseConfiguration {
apiKey: string
authDomain: string
databaseURL: string
storageBucket?: string
messagingSenderId?: string
}
interface SyncStateOptions {
context: Object
state: string
asArray?: boolean
isNullable?: boolean
keepKeys?: boolean
queries?: Object
then?: () => void
onFailure?: () => void
}
interface BindToStateOptions {
context: Object
state: string
asArray?: boolean
queries?: Object
then?: () => void
onFailure?: () => void
}

interface ListenToOptions {
context: Object
asArray?: boolean
then: (result : any) => void
onFailure?: (error : any) => void
queries?: Object
}

interface FetchOptions {
context: Object
asArray?: boolean
then?: (result : any) => void
onFailure?: () => void
queries?: Object
}

interface PostOptions {
data: any
then?: (result : any) => void
}
interface PushOptions {
data: any
then?: (result : any) => void
}

interface UpdateOptions {
data: any
then?: (result : any) => void
}

interface RebaseBinding {}

interface Rebase {

delete(callback?: () => void): void

syncState(endpoint: string, options : SyncStateOptions) : RebaseBinding

bindToState(endpoint: string, options : BindToStateOptions) : RebaseBinding

listenTo(endpoint: string, options : ListenToOptions) : RebaseBinding

fetch(endpoint: string, options : FetchOptions) : firebase.Promise<any>

post(endpoint: string, options: PostOptions) : firebase.Promise<any>

push(endpoint: string, options: PushOptions) : firebase.database.ThenableReference

update(endpoint: string, options: UpdateOptions) : firebase.Promise<any>

remove(endpoint: string, callback?: (result) => void) : firebase.Promise<any>

removeBinding(ref : RebaseBinding): void

reset(): void

authWithPassword(auth : { email: string, password: string }, authHandler: (error : Object | null, user: Object) => void): void

authWithOAuthPopup(provider : string, authHandler: (error : Object | null, user: Object) => void, settings?: {scope: Array<any> | string})

authWithOAuthRedirect(provider : string, authHandler: (error : Object | null) => void, settings?: {scope: Array<any> | string})

authGetOAuthRedirectResult(handler: (error : Object | null, user: Object) => void)

authWithCustomToken(token : string, handler: (error : Object | null, user: Object) => void)

unauth()

onAuth(handler: (user: Object) => void) : Function

createUser(auth : { email: string, password: string }, userHandler: (user) => void)

resetPassword({email : string}, errorHandler: (error) => void)

name: string,
storage : firebase.storage.Storage,
database: firebase.database.Database,
auth: firebase.auth.Auth,
messaging: firebase.messaging.Messaging,
app: firebase.app.App,
initializedApp: firebase.app.App
}

declare module 're-base' {

/**
* Accepts a firebase configuration object as the first argument
* and an optional 'name' for the app as the second
* @param {FirebaseConfiguration} configuration: .
* @param {string} name: OPTIONAL, defaults to `[DEFAULT]`.
* @since 2.8
* @return Rebase
*/
export function createClass(configuration: FirebaseConfiguration, name?: string): Rebase
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "2.8.0",
"description": "A Relay inspired library for building React.js + Firebase applications.",
"main": "index.js",
"typings": "index.d.ts",
"repository": {
"type": "git",
"url": "https://github.com/tylermcginnis/re-base"
Expand All @@ -29,11 +30,12 @@
"node-libs-browser": "^1.0.0",
"react": "^15.3.2",
"react-dom": "^15.3.2",
"typescript": "^2.3.2",
"webpack": "^1.13.3"
},
"scripts": {
"build": "webpack",
"test": "karma start tests/karma.conf.js",
"test": "tsc ./tests/re-base.tests.ts && karma start tests/karma.conf.js",
"travis": "./node_modules/karma/bin/karma start tests/karma.conf.js"
},
"license": "MIT"
Expand Down
245 changes: 245 additions & 0 deletions tests/re-base.tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
/// <reference path="../index.d.ts" />

import Rebase = require('re-base');

let base = Rebase.createClass({
apiKey: "apiKey",
authDomain: "projectId.firebaseapp.com",
databaseURL: "https://databaseName.firebaseio.com",
});

base = Rebase.createClass({
apiKey: "apiKey",
authDomain: "projectId.firebaseapp.com",
databaseURL: "https://databaseName.firebaseio.com",
storageBucket: "bucket.appspot.com",
messagingSenderId: "xxxxxxxxxxxxxx"
});

base = Rebase.createClass({
apiKey: "apiKey",
authDomain: "projectId.firebaseapp.com",
databaseURL: "https://databaseName.firebaseio.com",
storageBucket: "bucket.appspot.com",
messagingSenderId: "xxxxxxxxxxxxxx"
}, 'myApp');

() => {
base.delete();
base.delete(() => { /* app has been deleted */ });
}

() => {
let ref = base.syncState(`shoppingList`, {
context: this,
state: 'items',
asArray: true,
then: () => { }
});
base.removeBinding(ref)

ref = base.bindToState('tasks', {
context: this,
state: 'tasks',
asArray: true
});
base.removeBinding(ref)

ref = base.listenTo('votes', {
context: this,
asArray: true,
then: (votesData) => {
var total = 0;
votesData.forEach((vote, index) => {
total += vote
});
},
onFailure: () => { }
})
base.removeBinding(ref);
}

() => {
base.fetch('sales', {
context: this,
asArray: true,
then: (data) => {
console.log(data);
}
})

base.fetch('sales', { context: this, asArray: true })
.then(data => { console.log(data) })
.catch(error => { /** handle error*/ })
}

() => {
base.post(`users/123`, { data: { name: 'Tyler McGinnis', age: 25 } })
.then(() => { })
.catch(err => { });
}


() => {
var immediatelyAvailableReference = base.push('bears', {
data: { name: 'George', type: 'Grizzly' },
then(err) {
console.log(err);
}
});
//available immediately, you don't have to wait for the callback to be called
var generatedKey = immediatelyAvailableReference.key;

immediatelyAvailableReference = base.push('bears', {
data: { name: 'George', type: 'Grizzly' }
}).then(newLocation => {
var generatedKey = newLocation.key;
}).catch(err => {
//handle error
});
//available immediately, you don't have to wait for the Promise to resolve
var generatedKey = immediatelyAvailableReference.key;
}


() => {
base.update('bears', {
data: { name: 'George' },
then(err) { }
});

// bears endpoint currently holds the object { name: 'Bill', type: 'Grizzly' }
base.update('bears', { data: { name: 'George' } })
.then(() => { })
.catch(err => { });
}


() => {
base.remove('bears', function (err) {
if (!err) { }
});

base.remove('bears')
.then(() => { })
.catch(error => { });
}

() => {
base.syncState('users', {
context: this,
state: 'users',
asArray: true,
queries: {
orderByChild: 'iq',
limitToLast: 3,
endAt: ''
}
});
}

() => {
const authHandler = function (error, user) {
if (user) {
console.log(user);
}
}

// Simple email/password authentication
base.authWithPassword({
email: '[email protected]',
password: 'correcthorsebatterystaple'
}, authHandler);
}

() => {
const authHandler = function (error, user) {
if (user) {
console.log(user);
}
}
//basic
base.authWithOAuthPopup('twitter', authHandler);

// with settings
base.authWithOAuthPopup('github', authHandler, { scope: ['repos'] });
base.authWithOAuthPopup('github', authHandler, { scope: 'repos' });
}

() => {
var doSomethingWithAuthenticatedUser = function (user) {
console.log(user);
}

var authHandler = function (error) {
if (error) console.log(error);
// noop if redirect is successful
return;
}

var onRedirectBack = function (error, authData) {
if (error) console.log(error);
if (authData.user) {
doSomethingWithAuthenticatedUser(authData.user);
} else {
//redirect to twitter for auth
base.authWithOAuthRedirect('twitter', authHandler);
}
}

base.authGetOAuthRedirectResult(onRedirectBack);
}

() => {
var doSomethingWithAuthenticatedUser = function (user) {
console.log(user);
}

var doSomethingWithError = function (error) {
console.log(error);
}

var authHandler = function (error, user) {
if (error) doSomethingWithError(error);
doSomethingWithAuthenticatedUser(user);
}

base.authWithCustomToken('token', authHandler);
}

() => {
base.unauth()
}

() => {
function authDataCallback(user) {
if (user) {
console.log("User " + user.uid + " is logged in with " + user.providerId);
} else {
console.log("User is logged out");
}
}

// Listen to authentication
var unsubscribe = base.onAuth(authDataCallback);

//to remove listener
unsubscribe();
}

() => {

const userHandler = (user) => { }
const errorHandler = (error) => { }
// Create
base.createUser({
email: '[email protected]',
password: 'correcthorsebatterystaple'
}, userHandler);


// Reset Password
base.resetPassword({
email: '[email protected]'
}, errorHandler);
}