From 9c401045a8fbf603c6b4f4035009eb40378ad892 Mon Sep 17 00:00:00 2001 From: Jordan Janzen Date: Fri, 18 May 2018 01:15:32 +0100 Subject: [PATCH 01/11] Initial re-base@3.x type definitions --- types/re-base/index.d.ts | 567 +++++++++++++++++++++++++++++++++ types/re-base/re-base-tests.ts | 9 + types/re-base/tsconfig.json | 22 ++ types/re-base/tslint.json | 1 + 4 files changed, 599 insertions(+) create mode 100644 types/re-base/index.d.ts create mode 100644 types/re-base/re-base-tests.ts create mode 100644 types/re-base/tsconfig.json create mode 100644 types/re-base/tslint.json diff --git a/types/re-base/index.d.ts b/types/re-base/index.d.ts new file mode 100644 index 0000000000..928bca7c47 --- /dev/null +++ b/types/re-base/index.d.ts @@ -0,0 +1,567 @@ +// Type definitions for re-base 3.2 +// Project: https://github.com/tylermcginnis/re-base#readme +// Definitions by: jordandrako +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +interface QueryOptions {} + +interface RebaseBinding {} + +declare namespace firebase { + +} + +declare namespace firebase.app { + interface App {} +} + +declare namespace firebase.database { + interface Database {} +} + +declare namespace firebase.firestore { + class Firestore {} +} + +declare namespace rebase { + interface SyncStateOptions { + /** + * The context of your component. + */ + context: {}; + + /** + * The state property you want to sync with Firebase; can be an arbitrarily nested property a là `foo.bar`. + */ + state: string; + + /** + * A default value to set when the Firebase endpoint has no value (i.e., on init) (use this if you want a value other than an empty object or empty array) + */ + defaultValue?: string | boolean | number | {}; + + /** + * Returns the Firebase data at the specified endpoint as an Array instead of an Object. + */ + asArray?: boolean; + + /** + * Will keep any firebase generated keys intact when manipulating data using the asArray option. + */ + keepKeys?: boolean; + + /** + * Queries to be used with your read operations. See [Query Options](https://github.com/tylermcginnis/re-base#queries) for more details. + */ + queries?: QueryOptions; + + /** + * The callback function that will be invoked when the initial listener is established with Firebase. Typically used (with syncState) to change this.state.loading to false. + */ + then?: () => void; + + /** + * A callback function that will be invoked if the current user does not have read or write permissions at the location. + */ + onFailure?: () => void; + } + + interface BindToStateOptions { + /** + * The context of your component. + */ + context: {}; + + /** + * The state property you want to sync with Firebase; can be an arbitrarily nested property a là `foo.bar` (no arrays). + */ + state: string; + + /** + * Returns the Firebase data at the specified endpoint as an Array instead of an Object. + */ + asArray?: boolean; + + /** + * Queries to be used with your read operations. See [Query Options](https://github.com/tylermcginnis/re-base#queries) for more details. + */ + queries?: QueryOptions; + + /** + * The callback function that will be invoked when the initial listener is established with Firebase. Typically used (with bindToState) to change this.state.loading to false. + */ + then?: () => void; + + /** + * A callback function that will be invoked if the current user does not have read permissions at the location. + */ + onFailure?: () => void; + } + + interface ListenToOptions { + /** + * The context of your component. + */ + context: {}; + + /** + * Returns the Firebase data at the specified endpoint as an Array instead of an Object. + */ + asArray?: boolean; + + /** + * The callback function that will be invoked with the data from the specified endpoint when the endpoint changes. + */ + then: (result: any) => void; + + /** + * The callback function that will be invoked if the current user does not have read permissions at the location. + */ + onFailure?: (error: any) => void; + + /** + * Queries to be used with your read operations. See [Query Options](https://github.com/tylermcginnis/re-base#queries) for more details. + */ + queries?: QueryOptions; + } + + interface FetchOptions { + /** + * The context of your component. + */ + context: {}; + + /** + * Returns the Firebase data at the specified endpoint as an Array instead of an Object. + */ + asArray?: boolean; + + /** + * The callback function that will be invoked with the data from the specified endpoint when the endpoint changes. + */ + then?: (result: any) => void; + + /** + * The callback function that will be invoked with an error that occurs reading data from the specified endpoint. + */ + onFailure?: () => void; + + /** + * Queries to be used with your read operations. See [Query Options](https://github.com/tylermcginnis/re-base#queries) for more details. + */ + queries?: QueryOptions; + } + + interface PostOptions { + /** + * The data you're wanting to persist to Firebase. + */ + data: any; + + /** + * A callback that will get invoked once the new data has been saved to Firebase. If there is an error, it will be the only argument to this function. + */ + then?: (result: any) => void; + } + + interface PushOptions { + /** + * The data you're wanting to persist to Firebase. + */ + data: any; + + /** + * A callback that will get invoked once the new data has been saved to Firebase. If there is an error, it will be the only argument to this function. + */ + then?: (result: any) => void; + } + + interface UpdateOptions { + /** + * The data you're wanting to persist to Firebase. + */ + data: any; + + /** + * A callback that will get invoked once the new data has been saved to Firebase. If there is an error, it will be the only argument to this function. + */ + then?: (result: any) => void; + } + + interface DocumentReference {} + + interface CollectionReference {} + + interface bindDocOptions { + /** + * Your react component. + */ + context: {}; + + /** + * A property name on your state to bind your document to, if omitted the document will be merged into your existing state. + */ + state?: string; + + /** + * A callback that will be called when the listener is set, use for loading indicators. + */ + then?: () => void; + + /** + * A callback that will be called with any errors such as permissions errors. + */ + onFailure?: () => void; + } + + interface listenToDocOptions { + /** + * Your react component. + */ + context: {}; + + /** + * A callback that will be called when the listener is set, use for loading indicators. + */ + then?: () => void; + + /** + * A callback that will be called with any errors such as permissions errors. + */ + onFailure?: () => void; + } + + interface bindCollectionOptions { + /** + * Your react component. + */ + context: {}; + + /** + * The state property to bind the collection to. + */ + state?: string; + + /** + * A function that receives the created ref as its only argument. You can chain any Firestore queries you want to perform. See [Using Firestore Queries](https://github.com/tylermcginnis/re-base#firestorequeries). + */ + query?: () => void; + + /** + * Will embed firestore generated document ids inside each document in your collections on the `id` property. + */ + withIds?: boolean; + + /** + * will embed the DocumentReference inside each document in your collection on the `ref` property. + */ + withRefs?: boolean; + + /** + * A callback that will be called when the listener is set, use for loading indicators. + */ + then?: () => void; + + /** + * A callback that will be called with any errors such as permissions errors. + */ + onFailure?: () => void; + } + + interface listenToCollectionOptions { + /** + * Your react component. + */ + context: {}; + + /** + * A callback that will be called with the data. + */ + then: () => void; + + /** + * A function that receives the created ref as its only argument. You can chain any Firestore queries you want to perform. See [Using Firestore Queries](https://github.com/tylermcginnis/re-base#firestorequeries). + */ + query?: () => void; + + /** + * Will embed firestore generated document ids inside each document in your collections on the `id` property. + */ + withIds?: boolean; + + /** + * will embed the DocumentReference inside each document in your collection on the `ref` property. + */ + withRefs?: boolean; + + /** + * A callback that will be called with any errors such as permissions errors. + */ + onFailure?: () => void; + } + + interface getOptions { + /** + * A function that receives the created ref as its only argument. You can chain any Firestore queries you want to perform. See [Using Firestore Queries](https://github.com/tylermcginnis/re-base#firestorequeries). + */ + query?: () => void; + + /** + * Will embed firestore generated document ids inside each document in your collections on the `id` property. + */ + withIds?: boolean; + + /** + * will embed the DocumentReference inside each document in your collection on the `ref` property. + */ + withRefs?: boolean; + } + + interface removeFromCollectionOptions { + /** + * A function that receives the created ref as its only argument. You can chain any Firestore queries you want to perform. See [Using Firestore Queries](https://github.com/tylermcginnis/re-base#firestorequeries). + */ + query?: () => void; + } + + interface syncDocOptions { + /** + * Your react component. + */ + context: {}; + + /** + * The state property to sync. + */ + state: string; + + /** + * A callback that will be called when the listener is set, use for loading indicators. + */ + then?: () => void; + + /** + * A callback that will be called with any errors such as permissions errors. + */ + onFailure?: () => void; + } + + interface Rebase { + /** + * This property contains the initialized firebase app that was passed into re-base. You can access any of the firebase services that you are using off this object. For instance, if you want to use some firebase database methods that re-base doesn't have helpers for or if you are using the auth service and want to quickly access it off of re-base. + */ + initializedApp: firebase.app.App; + + /** + * This property contains an object that you can use when adding data that will be converted to a timestamp by Firebase. See [the docs](https://firebase.google.com/docs/reference/js/firebase.database.ServerValue) for more info. + */ + timestamp: {}; + + /** + * Allows you to set up two way data binding between your component's state and your Firebase. Whenever your Firebase changes, your component's state will change. Whenever your component's state changes, Firebase will change. + * @param endpoint The relative Firebase endpoint to which you'd like to bind your component's state. + * @param options syncState Options. + * @returns An object which you can pass to `removeBinding` if you want to remove the listener while the component is still mounted. + */ + syncState(endpoint: string, options: SyncStateOptions): RebaseBinding; + + /** + * One way data binding from Firebase to your component's state. Allows you to bind a component's state property to a Firebase endpoint so whenever that Firebase endpoint changes, your component's state will be updated with that change. + * @param endpoint The relative Firebase endpoint that you'd like to bind to your component's state. + * @param options bindToState Options. + * @returns An object which you can pass to removeBinding if you want to remove the listener while the component is still mounted. + */ + bindToState( + endpoint: string, + options: BindToStateOptions + ): RebaseBinding; + + /** + * Allows you to listen to Firebase endpoints without binding those changes to a state property. Instead, a callback will be invoked with the newly updated data. + * @param endpoint The relative Firebase endpoint which contains the data with which you'd like to invoke your callback function. + * @param options listenTo Options. + * @returns An object which you can pass to removeBinding when your component unmounts to remove the Firebase listeners. + */ + listenTo(endpoint: string, options: ListenToOptions): RebaseBinding; + + /** + * Allows you to retrieve the data from a Firebase endpoint just once without subscribing or listening for data changes. + * @param endpoint The relative Firebase endpoint which contains the data you're wanting to fetch. + * @param options fetch Options. + * @returns A Firebase [Promise](https://firebase.google.com/docs/reference/js/firebase.Promise) which resolves when the write is complete and rejects if there is an error. + */ + fetch(endpoint: string, options: FetchOptions): Promise; + + /** + * Allows you to update a Firebase endpoint with new data. *Replace all the data at this endpoint with the new data*. + * @param endpoint The relative Firebase endpoint that you'd like to update with the new data. + * @param options post Options. + * @returns A Firebase [Promise](https://firebase.google.com/docs/reference/js/firebase.Promise) which resolves when the write is complete and rejects if there is an error. + */ + post(endpoint: string, options: PostOptions): Promise; + + /** + * Allows you to add data to a Firebase endpoint. *Adds data to a child of the endpoint with a new Firebase push key*. + * @param endpoint The relative Firebase endpoint that you'd like to push the new data to. + * @param options push Options. + * @returns A Firebase [ThenableReference](https://firebase.google.com/docs/reference/js/firebase.database.ThenableReference) which is defined by Firebase as a "Combined Promise and reference; resolves when write is complete, but can be used immediately as the reference to the child location." + */ + push(endpoint: string, options: PushOptions): Promise; + + /** + * Allows you to update data at a Firebase endpoint changing only the properties you pass to it. **Warning: calling update with `options.data` being null will remove all the data at that endpoint**. + * @param endpoint The relative Firebase endpoint that you'd like to update. + * @param options update Options. + * @returns A Firebase [Promise](https://firebase.google.com/docs/reference/js/firebase.Promise) which resolves when the write is complete and rejects if there is an error. + */ + update(endpoint: string, options: UpdateOptions): Promise; + + /** + * Allows you to delete all data at the endpoint location. + * @param endpoint The relative Firebase endpoint that you'd like to delete data from. + * @param callback A callback that will get invoked once the data is successfully removed Firebase. If there is an error, it will be the only argument to this function. + * @returns A Firebase [Promise](https://firebase.google.com/docs/reference/js/firebase.Promise) which resolves when the write is complete and rejects if there is an error. + */ + remove( + endpoint: string, + callback?: (result: Promise) => void + ): Promise; + + /** + * Bind a document to your component. When then document changes in firestore, your component will re-render with the latest data. + * @param refOrPath DocumentReference or path. + * @param options bindDoc Options. + * @returns An object which you can pass to `removeBinding` if you want to remove the listener while the component is still mounted. + */ + bindDoc( + refOrPath: DocumentReference | string, + options: bindDocOptions + ): RebaseBinding; + + /** + * Listen to a document, when the data changes it will invoke a callback passing it the new data from Firestore. + * @param refOrPath DocumentReference or path. + * @param options listenToDoc Options. + * @returns An object which you can pass to `removeBinding` if you want to remove the listener while the component is still mounted. + */ + listenToDoc( + refOrPath: DocumentReference | string, + options: listenToDocOptions + ): RebaseBinding; + + /** + * Bind a collection to a state property in your component. When then collection changes in firestore, your component will re-render with the latest data. + * @param refOrPath CollectionReference or path. + * @param options bindCollection Options. + * @returns An object which you can pass to `removeBinding` if you want to remove the listener while the component is still mounted. + */ + bindCollection( + refOrPath: CollectionReference | string, + options: bindCollectionOptions + ): RebaseBinding; + + /** + * Listen to a collection, when the data changes it will invoke a callback passing it the new data from Firestore. + * @param refOrPath CollectionReference or path. + * @param options listenToCollection Options. + * @returns An object which you can pass to `removeBinding` if you want to remove the listener while the component is still mounted. + */ + listenToCollection( + refOrPath: CollectionReference | string, + options: listenToCollectionOptions + ): RebaseBinding; + + /** + * Fetch either a Collection or Document. + * @param refOrPath CollectionReference, DocumentReference or path. + * @param options get Options. + * @returns A Promise thats resolve with the resulting data or rejects if the document/collection does not exist or there are any read errors. + */ + get( + refOrPath: CollectionReference | DocumentReference | string, + options: listenToCollectionOptions + ): Promise; + + /** + * Add a new Document to a Collection. + * @param refOrPath CollectionReference or path. + * @param data The document data. + * @param id The id for the document. If omitted, the Firestore will generate an id for you. + * @returns A Promise thats resolve with the resulting data or rejects if the document/collection does not exist or there are any read errors. + */ + addToCollection( + refOrPath: CollectionReference | string, + data: {}, + id?: string + ): Promise; + + /** + * Update an existing document. + * @param refOrPath DocumentReference or path. + * @param data The document data. + * @returns A Promise thats resolve with the resulting data or rejects if the document/collection does not exist or there are any read errors. + */ + updateDoc( + refOrPath: DocumentReference | string, + data: {} + ): Promise; + + /** + * Deletes a document. + * @param refOrPath DocumentReference or path. + * @param data The document data. + * @returns A Promise thats resolve with the resulting data or rejects if the document/collection does not exist or there are any read errors. + */ + removeDoc( + refOrPath: DocumentReference | string, + data: {} + ): Promise; + + /** + * Removes documents from a collection. If no query is supplied, it will remove all the documents. If a query is supplied, it will only remove documents that match the query. + * @param refOrPath CollectionReference or path. + * @param options removeFromCollection Options. + * @returns A Promise thats resolve with the resulting data or rejects if the document/collection does not exist or there are any read errors. + */ + removeFromCollection( + refOrPath: CollectionReference | string, + options: removeFromCollectionOptions + ): Promise; + + /** + * Syncs a component's local state with a document in Firestore. + * @param refOrPath DocumentReference or path. + * @param options removeFromCollection Options. + * @returns A Promise thats resolve with the resulting data or rejects if the document/collection does not exist or there are any read errors. + */ + syncDoc( + refOrPath: DocumentReference | string, + options: syncDocOptions + ): RebaseBinding; + + /** + * Clean up a listener. Listeners are automatically cleaned up when components unmount, however if you wish to remove a listener while the component is still mounted this will allow you to do that. An example would be if you want to start listening to a new document or change a query on all collection in response to a prop change. + * @param ref The return value of syncState`, `bindToState`, `listenTo`, `listenToCollection`, `bindCollection`, `listenToDoc`, `bindDoc` or `syncDoc`. + */ + removeBinding(ref: RebaseBinding): void; + + /** + * Removes every Firebase/Firestore listener. + */ + reset(): void; + } +} + +declare module "re-base" { + /** + * Accepts an initialized firebase database or firestore database object. + * @param {object} firebaseDatabase Initialized firebase or firestore + * database. + * @return {Rebase} An instance of re-base. + */ + export function createClass( + firebaseDatabase: + | firebase.database.Database + | firebase.firestore.Firestore + ): rebase.Rebase; +} diff --git a/types/re-base/re-base-tests.ts b/types/re-base/re-base-tests.ts new file mode 100644 index 0000000000..702a6ab2dd --- /dev/null +++ b/types/re-base/re-base-tests.ts @@ -0,0 +1,9 @@ +import * as rebase from "re-base"; + +const database = {}; +const base = rebase.createClass(database); + +const _ref = base.syncState("foo", { + context: "bar", + state: "baz" +}); diff --git a/types/re-base/tsconfig.json b/types/re-base/tsconfig.json new file mode 100644 index 0000000000..eb916d27f6 --- /dev/null +++ b/types/re-base/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "re-base-tests.ts" + ] +} diff --git a/types/re-base/tslint.json b/types/re-base/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/re-base/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } From c97480f5771b860388cfa599aca2c1ed434e2c83 Mon Sep 17 00:00:00 2001 From: Jordan Janzen Date: Thu, 17 May 2018 17:51:13 -0700 Subject: [PATCH 02/11] Fix linting errors. --- types/re-base/index.d.ts | 96 ++++++++++--------------------------- types/re-base/tsconfig.json | 14 ++---- types/re-base/tslint.json | 8 +++- 3 files changed, 36 insertions(+), 82 deletions(-) diff --git a/types/re-base/index.d.ts b/types/re-base/index.d.ts index 928bca7c47..5934b67295 100644 --- a/types/re-base/index.d.ts +++ b/types/re-base/index.d.ts @@ -3,27 +3,7 @@ // Definitions by: jordandrako // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -interface QueryOptions {} - -interface RebaseBinding {} - -declare namespace firebase { - -} - -declare namespace firebase.app { - interface App {} -} - -declare namespace firebase.database { - interface Database {} -} - -declare namespace firebase.firestore { - class Firestore {} -} - -declare namespace rebase { +export namespace rebase { interface SyncStateOptions { /** * The context of your component. @@ -53,7 +33,7 @@ declare namespace rebase { /** * Queries to be used with your read operations. See [Query Options](https://github.com/tylermcginnis/re-base#queries) for more details. */ - queries?: QueryOptions; + queries?: {}; /** * The callback function that will be invoked when the initial listener is established with Firebase. Typically used (with syncState) to change this.state.loading to false. @@ -85,7 +65,7 @@ declare namespace rebase { /** * Queries to be used with your read operations. See [Query Options](https://github.com/tylermcginnis/re-base#queries) for more details. */ - queries?: QueryOptions; + queries?: {}; /** * The callback function that will be invoked when the initial listener is established with Firebase. Typically used (with bindToState) to change this.state.loading to false. @@ -122,7 +102,7 @@ declare namespace rebase { /** * Queries to be used with your read operations. See [Query Options](https://github.com/tylermcginnis/re-base#queries) for more details. */ - queries?: QueryOptions; + queries?: {}; } interface FetchOptions { @@ -149,7 +129,7 @@ declare namespace rebase { /** * Queries to be used with your read operations. See [Query Options](https://github.com/tylermcginnis/re-base#queries) for more details. */ - queries?: QueryOptions; + queries?: {}; } interface PostOptions { @@ -188,10 +168,6 @@ declare namespace rebase { then?: (result: any) => void; } - interface DocumentReference {} - - interface CollectionReference {} - interface bindDocOptions { /** * Your react component. @@ -350,7 +326,7 @@ declare namespace rebase { /** * This property contains the initialized firebase app that was passed into re-base. You can access any of the firebase services that you are using off this object. For instance, if you want to use some firebase database methods that re-base doesn't have helpers for or if you are using the auth service and want to quickly access it off of re-base. */ - initializedApp: firebase.app.App; + initializedApp: {}; /** * This property contains an object that you can use when adding data that will be converted to a timestamp by Firebase. See [the docs](https://firebase.google.com/docs/reference/js/firebase.database.ServerValue) for more info. @@ -363,7 +339,7 @@ declare namespace rebase { * @param options syncState Options. * @returns An object which you can pass to `removeBinding` if you want to remove the listener while the component is still mounted. */ - syncState(endpoint: string, options: SyncStateOptions): RebaseBinding; + syncState(endpoint: string, options: SyncStateOptions): {}; /** * One way data binding from Firebase to your component's state. Allows you to bind a component's state property to a Firebase endpoint so whenever that Firebase endpoint changes, your component's state will be updated with that change. @@ -371,10 +347,7 @@ declare namespace rebase { * @param options bindToState Options. * @returns An object which you can pass to removeBinding if you want to remove the listener while the component is still mounted. */ - bindToState( - endpoint: string, - options: BindToStateOptions - ): RebaseBinding; + bindToState(endpoint: string, options: BindToStateOptions): {}; /** * Allows you to listen to Firebase endpoints without binding those changes to a state property. Instead, a callback will be invoked with the newly updated data. @@ -382,7 +355,7 @@ declare namespace rebase { * @param options listenTo Options. * @returns An object which you can pass to removeBinding when your component unmounts to remove the Firebase listeners. */ - listenTo(endpoint: string, options: ListenToOptions): RebaseBinding; + listenTo(endpoint: string, options: ListenToOptions): {}; /** * Allows you to retrieve the data from a Firebase endpoint just once without subscribing or listening for data changes. @@ -433,10 +406,7 @@ declare namespace rebase { * @param options bindDoc Options. * @returns An object which you can pass to `removeBinding` if you want to remove the listener while the component is still mounted. */ - bindDoc( - refOrPath: DocumentReference | string, - options: bindDocOptions - ): RebaseBinding; + bindDoc(refOrPath: {} | string, options: bindDocOptions): {}; /** * Listen to a document, when the data changes it will invoke a callback passing it the new data from Firestore. @@ -444,10 +414,7 @@ declare namespace rebase { * @param options listenToDoc Options. * @returns An object which you can pass to `removeBinding` if you want to remove the listener while the component is still mounted. */ - listenToDoc( - refOrPath: DocumentReference | string, - options: listenToDocOptions - ): RebaseBinding; + listenToDoc(refOrPath: {} | string, options: listenToDocOptions): {}; /** * Bind a collection to a state property in your component. When then collection changes in firestore, your component will re-render with the latest data. @@ -456,9 +423,9 @@ declare namespace rebase { * @returns An object which you can pass to `removeBinding` if you want to remove the listener while the component is still mounted. */ bindCollection( - refOrPath: CollectionReference | string, + refOrPath: {} | string, options: bindCollectionOptions - ): RebaseBinding; + ): {}; /** * Listen to a collection, when the data changes it will invoke a callback passing it the new data from Firestore. @@ -467,9 +434,9 @@ declare namespace rebase { * @returns An object which you can pass to `removeBinding` if you want to remove the listener while the component is still mounted. */ listenToCollection( - refOrPath: CollectionReference | string, + refOrPath: {} | string, options: listenToCollectionOptions - ): RebaseBinding; + ): {}; /** * Fetch either a Collection or Document. @@ -478,7 +445,7 @@ declare namespace rebase { * @returns A Promise thats resolve with the resulting data or rejects if the document/collection does not exist or there are any read errors. */ get( - refOrPath: CollectionReference | DocumentReference | string, + refOrPath: {} | {} | string, options: listenToCollectionOptions ): Promise; @@ -490,7 +457,7 @@ declare namespace rebase { * @returns A Promise thats resolve with the resulting data or rejects if the document/collection does not exist or there are any read errors. */ addToCollection( - refOrPath: CollectionReference | string, + refOrPath: {} | string, data: {}, id?: string ): Promise; @@ -501,10 +468,7 @@ declare namespace rebase { * @param data The document data. * @returns A Promise thats resolve with the resulting data or rejects if the document/collection does not exist or there are any read errors. */ - updateDoc( - refOrPath: DocumentReference | string, - data: {} - ): Promise; + updateDoc(refOrPath: {} | string, data: {}): Promise; /** * Deletes a document. @@ -512,10 +476,7 @@ declare namespace rebase { * @param data The document data. * @returns A Promise thats resolve with the resulting data or rejects if the document/collection does not exist or there are any read errors. */ - removeDoc( - refOrPath: DocumentReference | string, - data: {} - ): Promise; + removeDoc(refOrPath: {} | string, data: {}): Promise; /** * Removes documents from a collection. If no query is supplied, it will remove all the documents. If a query is supplied, it will only remove documents that match the query. @@ -524,7 +485,7 @@ declare namespace rebase { * @returns A Promise thats resolve with the resulting data or rejects if the document/collection does not exist or there are any read errors. */ removeFromCollection( - refOrPath: CollectionReference | string, + refOrPath: {} | string, options: removeFromCollectionOptions ): Promise; @@ -534,16 +495,13 @@ declare namespace rebase { * @param options removeFromCollection Options. * @returns A Promise thats resolve with the resulting data or rejects if the document/collection does not exist or there are any read errors. */ - syncDoc( - refOrPath: DocumentReference | string, - options: syncDocOptions - ): RebaseBinding; + syncDoc(refOrPath: {} | string, options: syncDocOptions): {}; /** * Clean up a listener. Listeners are automatically cleaned up when components unmount, however if you wish to remove a listener while the component is still mounted this will allow you to do that. An example would be if you want to start listening to a new document or change a query on all collection in response to a prop change. * @param ref The return value of syncState`, `bindToState`, `listenTo`, `listenToCollection`, `bindCollection`, `listenToDoc`, `bindDoc` or `syncDoc`. */ - removeBinding(ref: RebaseBinding): void; + removeBinding(ref: {}): void; /** * Removes every Firebase/Firestore listener. @@ -555,13 +513,9 @@ declare namespace rebase { declare module "re-base" { /** * Accepts an initialized firebase database or firestore database object. - * @param {object} firebaseDatabase Initialized firebase or firestore + * @param firebaseDatabase Initialized firebase or firestore * database. - * @return {Rebase} An instance of re-base. + * @return An instance of re-base. */ - export function createClass( - firebaseDatabase: - | firebase.database.Database - | firebase.firestore.Firestore - ): rebase.Rebase; + function createClass(firebaseDatabase: {}): rebase.Rebase; } diff --git a/types/re-base/tsconfig.json b/types/re-base/tsconfig.json index eb916d27f6..ff88a407d4 100644 --- a/types/re-base/tsconfig.json +++ b/types/re-base/tsconfig.json @@ -1,22 +1,16 @@ { "compilerOptions": { "module": "commonjs", - "lib": [ - "es6" - ], + "lib": ["es6"], "noImplicitAny": true, "noImplicitThis": true, + "strictFunctionTypes": true, "strictNullChecks": true, "baseUrl": "../", - "typeRoots": [ - "../" - ], + "typeRoots": ["../"], "types": [], "noEmit": true, "forceConsistentCasingInFileNames": true }, - "files": [ - "index.d.ts", - "re-base-tests.ts" - ] + "files": ["index.d.ts", "re-base-tests.ts"] } diff --git a/types/re-base/tslint.json b/types/re-base/tslint.json index 3db14f85ea..c077883976 100644 --- a/types/re-base/tslint.json +++ b/types/re-base/tslint.json @@ -1 +1,7 @@ -{ "extends": "dtslint/dt.json" } +{ + "extends": "dtslint/dt.json", + "rules": { + "max-line-length": false, + "no-declare-current-package": false + } +} From 3388373368ef946a9864a4bb125a857b81e0cea0 Mon Sep 17 00:00:00 2001 From: Jordan Janzen Date: Thu, 17 May 2018 19:38:18 -0700 Subject: [PATCH 03/11] Stricter linting rules --- types/re-base/tslint.json | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/types/re-base/tslint.json b/types/re-base/tslint.json index c077883976..3db14f85ea 100644 --- a/types/re-base/tslint.json +++ b/types/re-base/tslint.json @@ -1,7 +1 @@ -{ - "extends": "dtslint/dt.json", - "rules": { - "max-line-length": false, - "no-declare-current-package": false - } -} +{ "extends": "dtslint/dt.json" } From cc4a1d7951a0edeeea649f03f613e0ad69f27626 Mon Sep 17 00:00:00 2001 From: Jordan Janzen Date: Thu, 17 May 2018 19:40:03 -0700 Subject: [PATCH 04/11] - JSdoc line lengths - remove needless namespace - remove declare namesake module --- types/re-base/index.d.ts | 1155 +++++++++++++++++++++----------------- 1 file changed, 642 insertions(+), 513 deletions(-) diff --git a/types/re-base/index.d.ts b/types/re-base/index.d.ts index 5934b67295..ba838df0aa 100644 --- a/types/re-base/index.d.ts +++ b/types/re-base/index.d.ts @@ -3,519 +3,648 @@ // Definitions by: jordandrako // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -export namespace rebase { - interface SyncStateOptions { - /** - * The context of your component. - */ - context: {}; - - /** - * The state property you want to sync with Firebase; can be an arbitrarily nested property a là `foo.bar`. - */ - state: string; - - /** - * A default value to set when the Firebase endpoint has no value (i.e., on init) (use this if you want a value other than an empty object or empty array) - */ - defaultValue?: string | boolean | number | {}; - - /** - * Returns the Firebase data at the specified endpoint as an Array instead of an Object. - */ - asArray?: boolean; - - /** - * Will keep any firebase generated keys intact when manipulating data using the asArray option. - */ - keepKeys?: boolean; - - /** - * Queries to be used with your read operations. See [Query Options](https://github.com/tylermcginnis/re-base#queries) for more details. - */ - queries?: {}; - - /** - * The callback function that will be invoked when the initial listener is established with Firebase. Typically used (with syncState) to change this.state.loading to false. - */ - then?: () => void; - - /** - * A callback function that will be invoked if the current user does not have read or write permissions at the location. - */ - onFailure?: () => void; - } - - interface BindToStateOptions { - /** - * The context of your component. - */ - context: {}; - - /** - * The state property you want to sync with Firebase; can be an arbitrarily nested property a là `foo.bar` (no arrays). - */ - state: string; - - /** - * Returns the Firebase data at the specified endpoint as an Array instead of an Object. - */ - asArray?: boolean; - - /** - * Queries to be used with your read operations. See [Query Options](https://github.com/tylermcginnis/re-base#queries) for more details. - */ - queries?: {}; - - /** - * The callback function that will be invoked when the initial listener is established with Firebase. Typically used (with bindToState) to change this.state.loading to false. - */ - then?: () => void; - - /** - * A callback function that will be invoked if the current user does not have read permissions at the location. - */ - onFailure?: () => void; - } - - interface ListenToOptions { - /** - * The context of your component. - */ - context: {}; - - /** - * Returns the Firebase data at the specified endpoint as an Array instead of an Object. - */ - asArray?: boolean; - - /** - * The callback function that will be invoked with the data from the specified endpoint when the endpoint changes. - */ - then: (result: any) => void; - - /** - * The callback function that will be invoked if the current user does not have read permissions at the location. - */ - onFailure?: (error: any) => void; - - /** - * Queries to be used with your read operations. See [Query Options](https://github.com/tylermcginnis/re-base#queries) for more details. - */ - queries?: {}; - } - - interface FetchOptions { - /** - * The context of your component. - */ - context: {}; - - /** - * Returns the Firebase data at the specified endpoint as an Array instead of an Object. - */ - asArray?: boolean; - - /** - * The callback function that will be invoked with the data from the specified endpoint when the endpoint changes. - */ - then?: (result: any) => void; - - /** - * The callback function that will be invoked with an error that occurs reading data from the specified endpoint. - */ - onFailure?: () => void; - - /** - * Queries to be used with your read operations. See [Query Options](https://github.com/tylermcginnis/re-base#queries) for more details. - */ - queries?: {}; - } - - interface PostOptions { - /** - * The data you're wanting to persist to Firebase. - */ - data: any; - - /** - * A callback that will get invoked once the new data has been saved to Firebase. If there is an error, it will be the only argument to this function. - */ - then?: (result: any) => void; - } - - interface PushOptions { - /** - * The data you're wanting to persist to Firebase. - */ - data: any; - - /** - * A callback that will get invoked once the new data has been saved to Firebase. If there is an error, it will be the only argument to this function. - */ - then?: (result: any) => void; - } - - interface UpdateOptions { - /** - * The data you're wanting to persist to Firebase. - */ - data: any; - - /** - * A callback that will get invoked once the new data has been saved to Firebase. If there is an error, it will be the only argument to this function. - */ - then?: (result: any) => void; - } - - interface bindDocOptions { - /** - * Your react component. - */ - context: {}; - - /** - * A property name on your state to bind your document to, if omitted the document will be merged into your existing state. - */ - state?: string; - - /** - * A callback that will be called when the listener is set, use for loading indicators. - */ - then?: () => void; - - /** - * A callback that will be called with any errors such as permissions errors. - */ - onFailure?: () => void; - } - - interface listenToDocOptions { - /** - * Your react component. - */ - context: {}; - - /** - * A callback that will be called when the listener is set, use for loading indicators. - */ - then?: () => void; - - /** - * A callback that will be called with any errors such as permissions errors. - */ - onFailure?: () => void; - } - - interface bindCollectionOptions { - /** - * Your react component. - */ - context: {}; - - /** - * The state property to bind the collection to. - */ - state?: string; - - /** - * A function that receives the created ref as its only argument. You can chain any Firestore queries you want to perform. See [Using Firestore Queries](https://github.com/tylermcginnis/re-base#firestorequeries). - */ - query?: () => void; - - /** - * Will embed firestore generated document ids inside each document in your collections on the `id` property. - */ - withIds?: boolean; - - /** - * will embed the DocumentReference inside each document in your collection on the `ref` property. - */ - withRefs?: boolean; - - /** - * A callback that will be called when the listener is set, use for loading indicators. - */ - then?: () => void; - - /** - * A callback that will be called with any errors such as permissions errors. - */ - onFailure?: () => void; - } - - interface listenToCollectionOptions { - /** - * Your react component. - */ - context: {}; - - /** - * A callback that will be called with the data. - */ - then: () => void; - - /** - * A function that receives the created ref as its only argument. You can chain any Firestore queries you want to perform. See [Using Firestore Queries](https://github.com/tylermcginnis/re-base#firestorequeries). - */ - query?: () => void; - - /** - * Will embed firestore generated document ids inside each document in your collections on the `id` property. - */ - withIds?: boolean; - - /** - * will embed the DocumentReference inside each document in your collection on the `ref` property. - */ - withRefs?: boolean; - - /** - * A callback that will be called with any errors such as permissions errors. - */ - onFailure?: () => void; - } - - interface getOptions { - /** - * A function that receives the created ref as its only argument. You can chain any Firestore queries you want to perform. See [Using Firestore Queries](https://github.com/tylermcginnis/re-base#firestorequeries). - */ - query?: () => void; - - /** - * Will embed firestore generated document ids inside each document in your collections on the `id` property. - */ - withIds?: boolean; - - /** - * will embed the DocumentReference inside each document in your collection on the `ref` property. - */ - withRefs?: boolean; - } - - interface removeFromCollectionOptions { - /** - * A function that receives the created ref as its only argument. You can chain any Firestore queries you want to perform. See [Using Firestore Queries](https://github.com/tylermcginnis/re-base#firestorequeries). - */ - query?: () => void; - } - - interface syncDocOptions { - /** - * Your react component. - */ - context: {}; - - /** - * The state property to sync. - */ - state: string; - - /** - * A callback that will be called when the listener is set, use for loading indicators. - */ - then?: () => void; - - /** - * A callback that will be called with any errors such as permissions errors. - */ - onFailure?: () => void; - } - - interface Rebase { - /** - * This property contains the initialized firebase app that was passed into re-base. You can access any of the firebase services that you are using off this object. For instance, if you want to use some firebase database methods that re-base doesn't have helpers for or if you are using the auth service and want to quickly access it off of re-base. - */ - initializedApp: {}; - - /** - * This property contains an object that you can use when adding data that will be converted to a timestamp by Firebase. See [the docs](https://firebase.google.com/docs/reference/js/firebase.database.ServerValue) for more info. - */ - timestamp: {}; - - /** - * Allows you to set up two way data binding between your component's state and your Firebase. Whenever your Firebase changes, your component's state will change. Whenever your component's state changes, Firebase will change. - * @param endpoint The relative Firebase endpoint to which you'd like to bind your component's state. - * @param options syncState Options. - * @returns An object which you can pass to `removeBinding` if you want to remove the listener while the component is still mounted. - */ - syncState(endpoint: string, options: SyncStateOptions): {}; - - /** - * One way data binding from Firebase to your component's state. Allows you to bind a component's state property to a Firebase endpoint so whenever that Firebase endpoint changes, your component's state will be updated with that change. - * @param endpoint The relative Firebase endpoint that you'd like to bind to your component's state. - * @param options bindToState Options. - * @returns An object which you can pass to removeBinding if you want to remove the listener while the component is still mounted. - */ - bindToState(endpoint: string, options: BindToStateOptions): {}; - - /** - * Allows you to listen to Firebase endpoints without binding those changes to a state property. Instead, a callback will be invoked with the newly updated data. - * @param endpoint The relative Firebase endpoint which contains the data with which you'd like to invoke your callback function. - * @param options listenTo Options. - * @returns An object which you can pass to removeBinding when your component unmounts to remove the Firebase listeners. - */ - listenTo(endpoint: string, options: ListenToOptions): {}; - - /** - * Allows you to retrieve the data from a Firebase endpoint just once without subscribing or listening for data changes. - * @param endpoint The relative Firebase endpoint which contains the data you're wanting to fetch. - * @param options fetch Options. - * @returns A Firebase [Promise](https://firebase.google.com/docs/reference/js/firebase.Promise) which resolves when the write is complete and rejects if there is an error. - */ - fetch(endpoint: string, options: FetchOptions): Promise; - - /** - * Allows you to update a Firebase endpoint with new data. *Replace all the data at this endpoint with the new data*. - * @param endpoint The relative Firebase endpoint that you'd like to update with the new data. - * @param options post Options. - * @returns A Firebase [Promise](https://firebase.google.com/docs/reference/js/firebase.Promise) which resolves when the write is complete and rejects if there is an error. - */ - post(endpoint: string, options: PostOptions): Promise; - - /** - * Allows you to add data to a Firebase endpoint. *Adds data to a child of the endpoint with a new Firebase push key*. - * @param endpoint The relative Firebase endpoint that you'd like to push the new data to. - * @param options push Options. - * @returns A Firebase [ThenableReference](https://firebase.google.com/docs/reference/js/firebase.database.ThenableReference) which is defined by Firebase as a "Combined Promise and reference; resolves when write is complete, but can be used immediately as the reference to the child location." - */ - push(endpoint: string, options: PushOptions): Promise; - - /** - * Allows you to update data at a Firebase endpoint changing only the properties you pass to it. **Warning: calling update with `options.data` being null will remove all the data at that endpoint**. - * @param endpoint The relative Firebase endpoint that you'd like to update. - * @param options update Options. - * @returns A Firebase [Promise](https://firebase.google.com/docs/reference/js/firebase.Promise) which resolves when the write is complete and rejects if there is an error. - */ - update(endpoint: string, options: UpdateOptions): Promise; - - /** - * Allows you to delete all data at the endpoint location. - * @param endpoint The relative Firebase endpoint that you'd like to delete data from. - * @param callback A callback that will get invoked once the data is successfully removed Firebase. If there is an error, it will be the only argument to this function. - * @returns A Firebase [Promise](https://firebase.google.com/docs/reference/js/firebase.Promise) which resolves when the write is complete and rejects if there is an error. - */ - remove( - endpoint: string, - callback?: (result: Promise) => void - ): Promise; - - /** - * Bind a document to your component. When then document changes in firestore, your component will re-render with the latest data. - * @param refOrPath DocumentReference or path. - * @param options bindDoc Options. - * @returns An object which you can pass to `removeBinding` if you want to remove the listener while the component is still mounted. - */ - bindDoc(refOrPath: {} | string, options: bindDocOptions): {}; - - /** - * Listen to a document, when the data changes it will invoke a callback passing it the new data from Firestore. - * @param refOrPath DocumentReference or path. - * @param options listenToDoc Options. - * @returns An object which you can pass to `removeBinding` if you want to remove the listener while the component is still mounted. - */ - listenToDoc(refOrPath: {} | string, options: listenToDocOptions): {}; - - /** - * Bind a collection to a state property in your component. When then collection changes in firestore, your component will re-render with the latest data. - * @param refOrPath CollectionReference or path. - * @param options bindCollection Options. - * @returns An object which you can pass to `removeBinding` if you want to remove the listener while the component is still mounted. - */ - bindCollection( - refOrPath: {} | string, - options: bindCollectionOptions - ): {}; - - /** - * Listen to a collection, when the data changes it will invoke a callback passing it the new data from Firestore. - * @param refOrPath CollectionReference or path. - * @param options listenToCollection Options. - * @returns An object which you can pass to `removeBinding` if you want to remove the listener while the component is still mounted. - */ - listenToCollection( - refOrPath: {} | string, - options: listenToCollectionOptions - ): {}; - - /** - * Fetch either a Collection or Document. - * @param refOrPath CollectionReference, DocumentReference or path. - * @param options get Options. - * @returns A Promise thats resolve with the resulting data or rejects if the document/collection does not exist or there are any read errors. - */ - get( - refOrPath: {} | {} | string, - options: listenToCollectionOptions - ): Promise; - - /** - * Add a new Document to a Collection. - * @param refOrPath CollectionReference or path. - * @param data The document data. - * @param id The id for the document. If omitted, the Firestore will generate an id for you. - * @returns A Promise thats resolve with the resulting data or rejects if the document/collection does not exist or there are any read errors. - */ - addToCollection( - refOrPath: {} | string, - data: {}, - id?: string - ): Promise; - - /** - * Update an existing document. - * @param refOrPath DocumentReference or path. - * @param data The document data. - * @returns A Promise thats resolve with the resulting data or rejects if the document/collection does not exist or there are any read errors. - */ - updateDoc(refOrPath: {} | string, data: {}): Promise; - - /** - * Deletes a document. - * @param refOrPath DocumentReference or path. - * @param data The document data. - * @returns A Promise thats resolve with the resulting data or rejects if the document/collection does not exist or there are any read errors. - */ - removeDoc(refOrPath: {} | string, data: {}): Promise; - - /** - * Removes documents from a collection. If no query is supplied, it will remove all the documents. If a query is supplied, it will only remove documents that match the query. - * @param refOrPath CollectionReference or path. - * @param options removeFromCollection Options. - * @returns A Promise thats resolve with the resulting data or rejects if the document/collection does not exist or there are any read errors. - */ - removeFromCollection( - refOrPath: {} | string, - options: removeFromCollectionOptions - ): Promise; - - /** - * Syncs a component's local state with a document in Firestore. - * @param refOrPath DocumentReference or path. - * @param options removeFromCollection Options. - * @returns A Promise thats resolve with the resulting data or rejects if the document/collection does not exist or there are any read errors. - */ - syncDoc(refOrPath: {} | string, options: syncDocOptions): {}; - - /** - * Clean up a listener. Listeners are automatically cleaned up when components unmount, however if you wish to remove a listener while the component is still mounted this will allow you to do that. An example would be if you want to start listening to a new document or change a query on all collection in response to a prop change. - * @param ref The return value of syncState`, `bindToState`, `listenTo`, `listenToCollection`, `bindCollection`, `listenToDoc`, `bindDoc` or `syncDoc`. - */ - removeBinding(ref: {}): void; - - /** - * Removes every Firebase/Firestore listener. - */ - reset(): void; - } -} - -declare module "re-base" { +export interface SyncStateOptions { /** - * Accepts an initialized firebase database or firestore database object. - * @param firebaseDatabase Initialized firebase or firestore - * database. - * @return An instance of re-base. + * The context of your component. */ - function createClass(firebaseDatabase: {}): rebase.Rebase; + context: {}; + + /** + * The state property you want to sync with Firebase; can be an + * arbitrarily nested property a là `foo.bar`. + */ + state: string; + + /** + * A default value to set when the Firebase endpoint has no value (i.e., + * on init) (use this if you want a value other than an empty object or + * empty array) + */ + defaultValue?: string | boolean | number | {}; + + /** + * Returns the Firebase data at the specified endpoint as an Array + * instead of an Object. + */ + asArray?: boolean; + + /** + * Will keep any firebase generated keys intact when manipulating data + * using the asArray option. + */ + keepKeys?: boolean; + + /** + * Queries to be used with your read operations. See + * [Query Options](https://github.com/tylermcginnis/re-base#queries) + * for more details. + */ + queries?: {}; + + /** + * The callback function that will be invoked when the initial listener + * is established with Firebase. Typically used (with syncState) to + * change this.state.loading to false. + */ + then?: () => void; + + /** + * A callback function that will be invoked if the current user does + * not have read or write permissions at the location. + */ + onFailure?: () => void; } + +export interface BindToStateOptions { + /** + * The context of your component. + */ + context: {}; + + /** + * The state property you want to sync with Firebase; can be an + * arbitrarily nested property a là `foo.bar` (no arrays). + */ + state: string; + + /** + * Returns the Firebase data at the specified endpoint as an Array + * instead of an Object. + */ + asArray?: boolean; + + /** + * Queries to be used with your read operations. See + * [Query Options](https://github.com/tylermcginnis/re-base#queries) + * for more details. + */ + queries?: {}; + + /** + * The callback function that will be invoked when the initial listener + * is established with Firebase. Typically used (with bindToState) to + * change this.state.loading to false. + */ + then?: () => void; + + /** + * A callback function that will be invoked if the current user does + * not have read permissions at the location. + */ + onFailure?: () => void; +} + +export interface ListenToOptions { + /** + * The context of your component. + */ + context: {}; + + /** + * Returns the Firebase data at the specified endpoint as an Array + * instead of an Object. + */ + asArray?: boolean; + + /** + * The callback function that will be invoked with the data from the + * specified endpoint when the endpoint changes. + */ + then: (result: any) => void; + + /** + * The callback function that will be invoked if the current user does + * not have read permissions at the location. + */ + onFailure?: (error: any) => void; + + /** + * Queries to be used with your read operations. See + * [Query Options](https://github.com/tylermcginnis/re-base#queries) + * for more details. + */ + queries?: {}; +} + +export interface FetchOptions { + /** + * The context of your component. + */ + context: {}; + + /** + * Returns the Firebase data at the specified endpoint as an Array + * instead of an Object. + */ + asArray?: boolean; + + /** + * The callback function that will be invoked with the data from the + * specified endpoint when the endpoint changes. + */ + then?: (result: any) => void; + + /** + * The callback function that will be invoked with an error that occurs + * reading data from the specified endpoint. + */ + onFailure?: () => void; + + /** + * Queries to be used with your read operations. See + * [Query Options](https://github.com/tylermcginnis/re-base#queries) + * for more details. + */ + queries?: {}; +} + +export interface PostOptions { + /** + * The data you're wanting to persist to Firebase. + */ + data: any; + + /** + * A callback that will get invoked once the new data has been saved to + * Firebase. If there is an error, it will be the only argument to this + * function. + */ + then?: (result: any) => void; +} + +export interface PushOptions { + /** + * The data you're wanting to persist to Firebase. + */ + data: any; + + /** + * A callback that will get invoked once the new data has been saved to + * Firebase. If there is an error, it will be the only argument to this + * function. + */ + then?: (result: any) => void; +} + +export interface UpdateOptions { + /** + * The data you're wanting to persist to Firebase. + */ + data: any; + + /** + * A callback that will get invoked once the new data has been saved to + * Firebase. If there is an error, it will be the only argument to this + * function. + */ + then?: (result: any) => void; +} + +export interface bindDocOptions { + /** + * The context of your component. + */ + context: {}; + + /** + * A property name on your state to bind your document to, if omitted + * the document will be merged into your existing state. + */ + state?: string; + + /** + * A callback that will be called when the listener is set, use for + * loading indicators. + */ + then?: () => void; + + /** + * A callback that will be called with any errors such as permissions + * errors. + */ + onFailure?: () => void; +} + +export interface listenToDocOptions { + /** + * The context of your component. + */ + context: {}; + + /** + * A callback that will be called when the listener is set, use for + * loading indicators. + */ + then?: () => void; + + /** + * A callback that will be called with any errors such as permissions + * errors. + */ + onFailure?: () => void; +} + +export interface bindCollectionOptions { + /** + * The context of your component. + */ + context: {}; + + /** + * The state property to bind the collection to. + */ + state?: string; + + /** + * A function that receives the created ref as its only argument. You + * can chain any Firestore queries you want to perform. See + * [Using Firestore Queries](https://github.com/tylermcginnis/re-base#firestorequeries). + */ + query?: () => void; + + /** + * Will embed firestore generated document ids inside each document in + * your collections on the `id` property. + */ + withIds?: boolean; + + /** + * will embed the DocumentReference inside each document in your + * collection on the `ref` property. + */ + withRefs?: boolean; + + /** + * A callback that will be called when the listener is set, use for + * loading indicators. + */ + then?: () => void; + + /** + * A callback that will be called with any errors such as permissions + * errors. + */ + onFailure?: () => void; +} + +export interface listenToCollectionOptions { + /** + * The context of your component. + */ + context: {}; + + /** + * A callback that will be called with the data. + */ + then: () => void; + + /** + * A function that receives the created ref as its only argument. You + * can chain any Firestore queries you want to perform. See + * [Using Firestore Queries](https://github.com/tylermcginnis/re-base#firestorequeries). + */ + query?: () => void; + + /** + * Will embed firestore generated document ids inside each document in + * your collections on the `id` property. + */ + withIds?: boolean; + + /** + * will embed the DocumentReference inside each document in your + * collection on the `ref` property. + */ + withRefs?: boolean; + + /** + * A callback that will be called with any errors such as permissions + * errors. + */ + onFailure?: () => void; +} + +export interface getOptions { + /** + * A function that receives the created ref as its only argument. You + * can chain any Firestore queries you want to perform. See + * [Using Firestore Queries](https://github.com/tylermcginnis/re-base#firestorequeries). + */ + query?: () => void; + + /** + * Will embed firestore generated document ids inside each document in + * your collections on the `id` property. + */ + withIds?: boolean; + + /** + * will embed the DocumentReference inside each document in your + * collection on the `ref` property. + */ + withRefs?: boolean; +} + +export interface removeFromCollectionOptions { + /** + * A function that receives the created ref as its only argument. You + * can chain any Firestore queries you want to perform. See + * [Using Firestore Queries](https://github.com/tylermcginnis/re-base#firestorequeries). + */ + query?: () => void; +} + +export interface syncDocOptions { + /** + * The context of your component. + */ + context: {}; + + /** + * The state property to sync. + */ + state: string; + + /** + * A callback that will be called when the listener is set, use for + * loading indicators. + */ + then?: () => void; + + /** + * A callback that will be called with any errors such as permissions + * errors. + */ + onFailure?: () => void; +} + +export interface Rebase { + /** + * This property contains the initialized firebase app that was passed + * into re-base. You can access any of the firebase services that you + * are using off this object. For instance, if you want to use some + * firebase database methods that re-base doesn't have helpers for or + * if you are using the auth service and want to quickly access it off + * of re-base. + */ + initializedApp: {}; + + /** + * This property contains an object that you can use when adding data + * that will be converted to a timestamp by Firebase. See + * [the docs](https://firebase.google.com/docs/reference/js/firebase.database.ServerValue) + * for more info. + */ + timestamp: {}; + + /** + * Allows you to set up two way data binding between your component's + * state and your Firebase. Whenever your Firebase changes, your + * component's state will change. Whenever your component's state + * changes, Firebase will change. + * @param endpoint The relative Firebase endpoint to which you'd like + * to bind your component's state. + * @param options syncState Options. + * @returns An object which you can pass to `removeBinding` if you want + * to remove the listener while the component is still mounted. + */ + syncState(endpoint: string, options: SyncStateOptions): {}; + + /** + * One way data binding from Firebase to your component's state. Allows + * you to bind a component's state property to a Firebase endpoint so + * whenever that Firebase endpoint changes, your component's state will + * be updated with that change. + * @param endpoint The relative Firebase endpoint that you'd like to + * bind to your component's state. + * @param options bindToState Options. + * @returns An object which you can pass to `removeBinding` if you want + * to remove the listener while the component is still mounted. + */ + bindToState(endpoint: string, options: BindToStateOptions): {}; + + /** + * Allows you to listen to Firebase endpoints without binding those + * changes to a state property. Instead, a callback will be invoked + * with the newly updated data. + * @param endpoint The relative Firebase endpoint which contains the + * data with which you'd like to invoke your callback function. + * @param options listenTo Options. + * @returns An object which you can pass to `removeBinding` when your + * component unmounts to remove the Firebase listeners. + */ + listenTo(endpoint: string, options: ListenToOptions): {}; + + /** + * Allows you to retrieve the data from a Firebase endpoint just once + * without subscribing or listening for data changes. + * @param endpoint The relative Firebase endpoint which contains the + * data you're wanting to fetch. + * @param options fetch Options. + * @returns A Firebase [Promise](https://firebase.google.com/docs/reference/js/firebase.Promise) + * which resolves when the write is complete and rejects if there is an + * error. + */ + fetch(endpoint: string, options: FetchOptions): Promise; + + /** + * Allows you to update a Firebase endpoint with new data. *Replace all + * the data at this endpoint with the new data*. + * @param endpoint The relative Firebase endpoint that you'd like to + * update with the new data. + * @param options post Options. + * @returns A Firebase [Promise](https://firebase.google.com/docs/reference/js/firebase.Promise) + * which resolves when the write is complete and rejects if there is an + * error. + */ + post(endpoint: string, options: PostOptions): Promise; + + /** + * Allows you to add data to a Firebase endpoint. *Adds data to a child + * of the endpoint with a new Firebase push key*. + * @param endpoint The relative Firebase endpoint that you'd like to + * push the new data to. + * @param options push Options. + * @returns A Firebase [ThenableReference](https://firebase.google.com/docs/reference/js/firebase.database.ThenableReference) + * which is defined by Firebase as a "Combined Promise and reference; + * resolves when write is complete, but can be used immediately as the + * reference to the child location." + */ + push(endpoint: string, options: PushOptions): Promise; + + /** + * Allows you to update data at a Firebase endpoint changing only the + * properties you pass to it. **Warning: calling update with + * `options.data` being null will remove all the data at that + * endpoint**. + * @param endpoint The relative Firebase endpoint that you'd like to + * update. + * @param options update Options. + * @returns A Firebase [Promise](https://firebase.google.com/docs/reference/js/firebase.Promise) + * which resolves when the write is complete and rejects if there is an + * error. + */ + update(endpoint: string, options: UpdateOptions): Promise; + + /** + * Allows you to delete all data at the endpoint location. + * @param endpoint The relative Firebase endpoint that you'd like to + * delete data from. + * @param callback A callback that will get invoked once the data is + * successfully removed Firebase. If there is an error, it will be the + * only argument to this function. + * @returns A Firebase [Promise](https://firebase.google.com/docs/reference/js/firebase.Promise) + * which resolves when the write is complete and rejects if there is an + * error. + */ + remove( + endpoint: string, + callback?: (result: Promise) => void + ): Promise; + + /** + * Bind a document to your component. When then document changes in + * firestore, your component will re-render with the latest data. + * @param refOrPath DocumentReference or path. + * @param options bindDoc Options. + * @returns An object which you can pass to `removeBinding` if you want + * to remove the listener while the component is still mounted. + */ + bindDoc(refOrPath: {} | string, options: bindDocOptions): {}; + + /** + * Listen to a document, when the data changes it will invoke a + * callback passing it the new data from Firestore. + * @param refOrPath DocumentReference or path. + * @param options listenToDoc Options. + * @returns An object which you can pass to `removeBinding` if you want + * to remove the listener while the component is still mounted. + */ + listenToDoc(refOrPath: {} | string, options: listenToDocOptions): {}; + + /** + * Bind a collection to a state property in your component. When then + * collection changes in firestore, your component will re-render with + * the latest data. + * @param refOrPath CollectionReference or path. + * @param options bindCollection Options. + * @returns An object which you can pass to `removeBinding` if you want + * to remove the listener while the component is still mounted. + */ + bindCollection( + refOrPath: {} | string, + options: bindCollectionOptions + ): {}; + + /** + * Listen to a collection, when the data changes it will invoke a + * callback passing it the new data from Firestore. + * @param refOrPath CollectionReference or path. + * @param options listenToCollection Options. + * @returns An object which you can pass to `removeBinding` if you want + * to remove the listener while the component is still mounted. + */ + listenToCollection( + refOrPath: {} | string, + options: listenToCollectionOptions + ): {}; + + /** + * Fetch either a Collection or Document. + * @param refOrPath CollectionReference, DocumentReference or path. + * @param options get Options. + * @returns A Promise thats resolve with the resulting data or rejects + * if the document/collection does not exist or there are any read + * errors. + */ + get( + refOrPath: {} | {} | string, + options: listenToCollectionOptions + ): Promise; + + /** + * Add a new Document to a Collection. + * @param refOrPath CollectionReference or path. + * @param data The document data. + * @param id The id for the document. If omitted, the Firestore will + * generate an id for you. + * @returns A Promise thats resolve with the resulting data or rejects + * if the document/collection does not exist or there are any read + * errors. + */ + addToCollection( + refOrPath: {} | string, + data: {}, + id?: string + ): Promise; + + /** + * Update an existing document. + * @param refOrPath DocumentReference or path. + * @param data The document data. + * @returns A Promise thats resolve with the resulting data or rejects + * if the document/collection does not exist or there are any read + * errors. + */ + updateDoc(refOrPath: {} | string, data: {}): Promise; + + /** + * Deletes a document. + * @param refOrPath DocumentReference or path. + * @param data The document data. + * @returns A Promise thats resolve with the resulting data or rejects + * if the document/collection does not exist or there are any read + * errors. + */ + removeDoc(refOrPath: {} | string, data: {}): Promise; + + /** + * Removes documents from a collection. If no query is supplied, it + * will remove all the documents. If a query is supplied, it will only + * remove documents that match the query. + * @param refOrPath CollectionReference or path. + * @param options removeFromCollection Options. + * @returns A Promise thats resolve with the resulting data or rejects + * if the document/collection does not exist or there are any read + * errors. + */ + removeFromCollection( + refOrPath: {} | string, + options: removeFromCollectionOptions + ): Promise; + + /** + * Syncs a component's local state with a document in Firestore. + * @param refOrPath DocumentReference or path. + * @param options removeFromCollection Options. + * @returns A Promise thats resolve with the resulting data or rejects + * if the document/collection does not exist or there are any read + * errors. + */ + syncDoc(refOrPath: {} | string, options: syncDocOptions): {}; + + /** + * Clean up a listener. Listeners are automatically cleaned up when + * components unmount, however if you wish to remove a listener while + * the component is still mounted this will allow you to do that. An + * example would be if you want to start listening to a new document or + * change a query on all collection in response to a prop change. + * @param ref The return value of syncState`, `bindToState`, `listenTo`, + * `listenToCollection`, `bindCollection`, `listenToDoc`, `bindDoc` or + * `syncDoc`. + */ + removeBinding(ref: {}): void; + + /** + * Removes every Firebase/Firestore listener. + */ + reset(): void; +} + +/** + * Accepts an initialized firebase/firestore database object. + * @param database Initialized firebase/firestore + * database. + * @return An instance of re-base. + */ +export function createClass(database: {}): Rebase; From 6ef11f7677759c1034acdee6d5e38ec8b1f35ea7 Mon Sep 17 00:00:00 2001 From: Jordan Janzen Date: Fri, 18 May 2018 01:15:32 +0100 Subject: [PATCH 05/11] Initial re-base@3.x type definitions --- types/re-base/index.d.ts | 567 +++++++++++++++++++++++++++++++++ types/re-base/re-base-tests.ts | 9 + types/re-base/tsconfig.json | 22 ++ types/re-base/tslint.json | 1 + 4 files changed, 599 insertions(+) create mode 100644 types/re-base/index.d.ts create mode 100644 types/re-base/re-base-tests.ts create mode 100644 types/re-base/tsconfig.json create mode 100644 types/re-base/tslint.json diff --git a/types/re-base/index.d.ts b/types/re-base/index.d.ts new file mode 100644 index 0000000000..928bca7c47 --- /dev/null +++ b/types/re-base/index.d.ts @@ -0,0 +1,567 @@ +// Type definitions for re-base 3.2 +// Project: https://github.com/tylermcginnis/re-base#readme +// Definitions by: jordandrako +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +interface QueryOptions {} + +interface RebaseBinding {} + +declare namespace firebase { + +} + +declare namespace firebase.app { + interface App {} +} + +declare namespace firebase.database { + interface Database {} +} + +declare namespace firebase.firestore { + class Firestore {} +} + +declare namespace rebase { + interface SyncStateOptions { + /** + * The context of your component. + */ + context: {}; + + /** + * The state property you want to sync with Firebase; can be an arbitrarily nested property a là `foo.bar`. + */ + state: string; + + /** + * A default value to set when the Firebase endpoint has no value (i.e., on init) (use this if you want a value other than an empty object or empty array) + */ + defaultValue?: string | boolean | number | {}; + + /** + * Returns the Firebase data at the specified endpoint as an Array instead of an Object. + */ + asArray?: boolean; + + /** + * Will keep any firebase generated keys intact when manipulating data using the asArray option. + */ + keepKeys?: boolean; + + /** + * Queries to be used with your read operations. See [Query Options](https://github.com/tylermcginnis/re-base#queries) for more details. + */ + queries?: QueryOptions; + + /** + * The callback function that will be invoked when the initial listener is established with Firebase. Typically used (with syncState) to change this.state.loading to false. + */ + then?: () => void; + + /** + * A callback function that will be invoked if the current user does not have read or write permissions at the location. + */ + onFailure?: () => void; + } + + interface BindToStateOptions { + /** + * The context of your component. + */ + context: {}; + + /** + * The state property you want to sync with Firebase; can be an arbitrarily nested property a là `foo.bar` (no arrays). + */ + state: string; + + /** + * Returns the Firebase data at the specified endpoint as an Array instead of an Object. + */ + asArray?: boolean; + + /** + * Queries to be used with your read operations. See [Query Options](https://github.com/tylermcginnis/re-base#queries) for more details. + */ + queries?: QueryOptions; + + /** + * The callback function that will be invoked when the initial listener is established with Firebase. Typically used (with bindToState) to change this.state.loading to false. + */ + then?: () => void; + + /** + * A callback function that will be invoked if the current user does not have read permissions at the location. + */ + onFailure?: () => void; + } + + interface ListenToOptions { + /** + * The context of your component. + */ + context: {}; + + /** + * Returns the Firebase data at the specified endpoint as an Array instead of an Object. + */ + asArray?: boolean; + + /** + * The callback function that will be invoked with the data from the specified endpoint when the endpoint changes. + */ + then: (result: any) => void; + + /** + * The callback function that will be invoked if the current user does not have read permissions at the location. + */ + onFailure?: (error: any) => void; + + /** + * Queries to be used with your read operations. See [Query Options](https://github.com/tylermcginnis/re-base#queries) for more details. + */ + queries?: QueryOptions; + } + + interface FetchOptions { + /** + * The context of your component. + */ + context: {}; + + /** + * Returns the Firebase data at the specified endpoint as an Array instead of an Object. + */ + asArray?: boolean; + + /** + * The callback function that will be invoked with the data from the specified endpoint when the endpoint changes. + */ + then?: (result: any) => void; + + /** + * The callback function that will be invoked with an error that occurs reading data from the specified endpoint. + */ + onFailure?: () => void; + + /** + * Queries to be used with your read operations. See [Query Options](https://github.com/tylermcginnis/re-base#queries) for more details. + */ + queries?: QueryOptions; + } + + interface PostOptions { + /** + * The data you're wanting to persist to Firebase. + */ + data: any; + + /** + * A callback that will get invoked once the new data has been saved to Firebase. If there is an error, it will be the only argument to this function. + */ + then?: (result: any) => void; + } + + interface PushOptions { + /** + * The data you're wanting to persist to Firebase. + */ + data: any; + + /** + * A callback that will get invoked once the new data has been saved to Firebase. If there is an error, it will be the only argument to this function. + */ + then?: (result: any) => void; + } + + interface UpdateOptions { + /** + * The data you're wanting to persist to Firebase. + */ + data: any; + + /** + * A callback that will get invoked once the new data has been saved to Firebase. If there is an error, it will be the only argument to this function. + */ + then?: (result: any) => void; + } + + interface DocumentReference {} + + interface CollectionReference {} + + interface bindDocOptions { + /** + * Your react component. + */ + context: {}; + + /** + * A property name on your state to bind your document to, if omitted the document will be merged into your existing state. + */ + state?: string; + + /** + * A callback that will be called when the listener is set, use for loading indicators. + */ + then?: () => void; + + /** + * A callback that will be called with any errors such as permissions errors. + */ + onFailure?: () => void; + } + + interface listenToDocOptions { + /** + * Your react component. + */ + context: {}; + + /** + * A callback that will be called when the listener is set, use for loading indicators. + */ + then?: () => void; + + /** + * A callback that will be called with any errors such as permissions errors. + */ + onFailure?: () => void; + } + + interface bindCollectionOptions { + /** + * Your react component. + */ + context: {}; + + /** + * The state property to bind the collection to. + */ + state?: string; + + /** + * A function that receives the created ref as its only argument. You can chain any Firestore queries you want to perform. See [Using Firestore Queries](https://github.com/tylermcginnis/re-base#firestorequeries). + */ + query?: () => void; + + /** + * Will embed firestore generated document ids inside each document in your collections on the `id` property. + */ + withIds?: boolean; + + /** + * will embed the DocumentReference inside each document in your collection on the `ref` property. + */ + withRefs?: boolean; + + /** + * A callback that will be called when the listener is set, use for loading indicators. + */ + then?: () => void; + + /** + * A callback that will be called with any errors such as permissions errors. + */ + onFailure?: () => void; + } + + interface listenToCollectionOptions { + /** + * Your react component. + */ + context: {}; + + /** + * A callback that will be called with the data. + */ + then: () => void; + + /** + * A function that receives the created ref as its only argument. You can chain any Firestore queries you want to perform. See [Using Firestore Queries](https://github.com/tylermcginnis/re-base#firestorequeries). + */ + query?: () => void; + + /** + * Will embed firestore generated document ids inside each document in your collections on the `id` property. + */ + withIds?: boolean; + + /** + * will embed the DocumentReference inside each document in your collection on the `ref` property. + */ + withRefs?: boolean; + + /** + * A callback that will be called with any errors such as permissions errors. + */ + onFailure?: () => void; + } + + interface getOptions { + /** + * A function that receives the created ref as its only argument. You can chain any Firestore queries you want to perform. See [Using Firestore Queries](https://github.com/tylermcginnis/re-base#firestorequeries). + */ + query?: () => void; + + /** + * Will embed firestore generated document ids inside each document in your collections on the `id` property. + */ + withIds?: boolean; + + /** + * will embed the DocumentReference inside each document in your collection on the `ref` property. + */ + withRefs?: boolean; + } + + interface removeFromCollectionOptions { + /** + * A function that receives the created ref as its only argument. You can chain any Firestore queries you want to perform. See [Using Firestore Queries](https://github.com/tylermcginnis/re-base#firestorequeries). + */ + query?: () => void; + } + + interface syncDocOptions { + /** + * Your react component. + */ + context: {}; + + /** + * The state property to sync. + */ + state: string; + + /** + * A callback that will be called when the listener is set, use for loading indicators. + */ + then?: () => void; + + /** + * A callback that will be called with any errors such as permissions errors. + */ + onFailure?: () => void; + } + + interface Rebase { + /** + * This property contains the initialized firebase app that was passed into re-base. You can access any of the firebase services that you are using off this object. For instance, if you want to use some firebase database methods that re-base doesn't have helpers for or if you are using the auth service and want to quickly access it off of re-base. + */ + initializedApp: firebase.app.App; + + /** + * This property contains an object that you can use when adding data that will be converted to a timestamp by Firebase. See [the docs](https://firebase.google.com/docs/reference/js/firebase.database.ServerValue) for more info. + */ + timestamp: {}; + + /** + * Allows you to set up two way data binding between your component's state and your Firebase. Whenever your Firebase changes, your component's state will change. Whenever your component's state changes, Firebase will change. + * @param endpoint The relative Firebase endpoint to which you'd like to bind your component's state. + * @param options syncState Options. + * @returns An object which you can pass to `removeBinding` if you want to remove the listener while the component is still mounted. + */ + syncState(endpoint: string, options: SyncStateOptions): RebaseBinding; + + /** + * One way data binding from Firebase to your component's state. Allows you to bind a component's state property to a Firebase endpoint so whenever that Firebase endpoint changes, your component's state will be updated with that change. + * @param endpoint The relative Firebase endpoint that you'd like to bind to your component's state. + * @param options bindToState Options. + * @returns An object which you can pass to removeBinding if you want to remove the listener while the component is still mounted. + */ + bindToState( + endpoint: string, + options: BindToStateOptions + ): RebaseBinding; + + /** + * Allows you to listen to Firebase endpoints without binding those changes to a state property. Instead, a callback will be invoked with the newly updated data. + * @param endpoint The relative Firebase endpoint which contains the data with which you'd like to invoke your callback function. + * @param options listenTo Options. + * @returns An object which you can pass to removeBinding when your component unmounts to remove the Firebase listeners. + */ + listenTo(endpoint: string, options: ListenToOptions): RebaseBinding; + + /** + * Allows you to retrieve the data from a Firebase endpoint just once without subscribing or listening for data changes. + * @param endpoint The relative Firebase endpoint which contains the data you're wanting to fetch. + * @param options fetch Options. + * @returns A Firebase [Promise](https://firebase.google.com/docs/reference/js/firebase.Promise) which resolves when the write is complete and rejects if there is an error. + */ + fetch(endpoint: string, options: FetchOptions): Promise; + + /** + * Allows you to update a Firebase endpoint with new data. *Replace all the data at this endpoint with the new data*. + * @param endpoint The relative Firebase endpoint that you'd like to update with the new data. + * @param options post Options. + * @returns A Firebase [Promise](https://firebase.google.com/docs/reference/js/firebase.Promise) which resolves when the write is complete and rejects if there is an error. + */ + post(endpoint: string, options: PostOptions): Promise; + + /** + * Allows you to add data to a Firebase endpoint. *Adds data to a child of the endpoint with a new Firebase push key*. + * @param endpoint The relative Firebase endpoint that you'd like to push the new data to. + * @param options push Options. + * @returns A Firebase [ThenableReference](https://firebase.google.com/docs/reference/js/firebase.database.ThenableReference) which is defined by Firebase as a "Combined Promise and reference; resolves when write is complete, but can be used immediately as the reference to the child location." + */ + push(endpoint: string, options: PushOptions): Promise; + + /** + * Allows you to update data at a Firebase endpoint changing only the properties you pass to it. **Warning: calling update with `options.data` being null will remove all the data at that endpoint**. + * @param endpoint The relative Firebase endpoint that you'd like to update. + * @param options update Options. + * @returns A Firebase [Promise](https://firebase.google.com/docs/reference/js/firebase.Promise) which resolves when the write is complete and rejects if there is an error. + */ + update(endpoint: string, options: UpdateOptions): Promise; + + /** + * Allows you to delete all data at the endpoint location. + * @param endpoint The relative Firebase endpoint that you'd like to delete data from. + * @param callback A callback that will get invoked once the data is successfully removed Firebase. If there is an error, it will be the only argument to this function. + * @returns A Firebase [Promise](https://firebase.google.com/docs/reference/js/firebase.Promise) which resolves when the write is complete and rejects if there is an error. + */ + remove( + endpoint: string, + callback?: (result: Promise) => void + ): Promise; + + /** + * Bind a document to your component. When then document changes in firestore, your component will re-render with the latest data. + * @param refOrPath DocumentReference or path. + * @param options bindDoc Options. + * @returns An object which you can pass to `removeBinding` if you want to remove the listener while the component is still mounted. + */ + bindDoc( + refOrPath: DocumentReference | string, + options: bindDocOptions + ): RebaseBinding; + + /** + * Listen to a document, when the data changes it will invoke a callback passing it the new data from Firestore. + * @param refOrPath DocumentReference or path. + * @param options listenToDoc Options. + * @returns An object which you can pass to `removeBinding` if you want to remove the listener while the component is still mounted. + */ + listenToDoc( + refOrPath: DocumentReference | string, + options: listenToDocOptions + ): RebaseBinding; + + /** + * Bind a collection to a state property in your component. When then collection changes in firestore, your component will re-render with the latest data. + * @param refOrPath CollectionReference or path. + * @param options bindCollection Options. + * @returns An object which you can pass to `removeBinding` if you want to remove the listener while the component is still mounted. + */ + bindCollection( + refOrPath: CollectionReference | string, + options: bindCollectionOptions + ): RebaseBinding; + + /** + * Listen to a collection, when the data changes it will invoke a callback passing it the new data from Firestore. + * @param refOrPath CollectionReference or path. + * @param options listenToCollection Options. + * @returns An object which you can pass to `removeBinding` if you want to remove the listener while the component is still mounted. + */ + listenToCollection( + refOrPath: CollectionReference | string, + options: listenToCollectionOptions + ): RebaseBinding; + + /** + * Fetch either a Collection or Document. + * @param refOrPath CollectionReference, DocumentReference or path. + * @param options get Options. + * @returns A Promise thats resolve with the resulting data or rejects if the document/collection does not exist or there are any read errors. + */ + get( + refOrPath: CollectionReference | DocumentReference | string, + options: listenToCollectionOptions + ): Promise; + + /** + * Add a new Document to a Collection. + * @param refOrPath CollectionReference or path. + * @param data The document data. + * @param id The id for the document. If omitted, the Firestore will generate an id for you. + * @returns A Promise thats resolve with the resulting data or rejects if the document/collection does not exist or there are any read errors. + */ + addToCollection( + refOrPath: CollectionReference | string, + data: {}, + id?: string + ): Promise; + + /** + * Update an existing document. + * @param refOrPath DocumentReference or path. + * @param data The document data. + * @returns A Promise thats resolve with the resulting data or rejects if the document/collection does not exist or there are any read errors. + */ + updateDoc( + refOrPath: DocumentReference | string, + data: {} + ): Promise; + + /** + * Deletes a document. + * @param refOrPath DocumentReference or path. + * @param data The document data. + * @returns A Promise thats resolve with the resulting data or rejects if the document/collection does not exist or there are any read errors. + */ + removeDoc( + refOrPath: DocumentReference | string, + data: {} + ): Promise; + + /** + * Removes documents from a collection. If no query is supplied, it will remove all the documents. If a query is supplied, it will only remove documents that match the query. + * @param refOrPath CollectionReference or path. + * @param options removeFromCollection Options. + * @returns A Promise thats resolve with the resulting data or rejects if the document/collection does not exist or there are any read errors. + */ + removeFromCollection( + refOrPath: CollectionReference | string, + options: removeFromCollectionOptions + ): Promise; + + /** + * Syncs a component's local state with a document in Firestore. + * @param refOrPath DocumentReference or path. + * @param options removeFromCollection Options. + * @returns A Promise thats resolve with the resulting data or rejects if the document/collection does not exist or there are any read errors. + */ + syncDoc( + refOrPath: DocumentReference | string, + options: syncDocOptions + ): RebaseBinding; + + /** + * Clean up a listener. Listeners are automatically cleaned up when components unmount, however if you wish to remove a listener while the component is still mounted this will allow you to do that. An example would be if you want to start listening to a new document or change a query on all collection in response to a prop change. + * @param ref The return value of syncState`, `bindToState`, `listenTo`, `listenToCollection`, `bindCollection`, `listenToDoc`, `bindDoc` or `syncDoc`. + */ + removeBinding(ref: RebaseBinding): void; + + /** + * Removes every Firebase/Firestore listener. + */ + reset(): void; + } +} + +declare module "re-base" { + /** + * Accepts an initialized firebase database or firestore database object. + * @param {object} firebaseDatabase Initialized firebase or firestore + * database. + * @return {Rebase} An instance of re-base. + */ + export function createClass( + firebaseDatabase: + | firebase.database.Database + | firebase.firestore.Firestore + ): rebase.Rebase; +} diff --git a/types/re-base/re-base-tests.ts b/types/re-base/re-base-tests.ts new file mode 100644 index 0000000000..702a6ab2dd --- /dev/null +++ b/types/re-base/re-base-tests.ts @@ -0,0 +1,9 @@ +import * as rebase from "re-base"; + +const database = {}; +const base = rebase.createClass(database); + +const _ref = base.syncState("foo", { + context: "bar", + state: "baz" +}); diff --git a/types/re-base/tsconfig.json b/types/re-base/tsconfig.json new file mode 100644 index 0000000000..eb916d27f6 --- /dev/null +++ b/types/re-base/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "re-base-tests.ts" + ] +} diff --git a/types/re-base/tslint.json b/types/re-base/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/re-base/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } From d065c5666ee2d2bf31e2d89bfa32b863c6d493ef Mon Sep 17 00:00:00 2001 From: Jordan Janzen Date: Thu, 17 May 2018 17:51:13 -0700 Subject: [PATCH 06/11] Fix linting errors. --- types/re-base/index.d.ts | 96 ++++++++++--------------------------- types/re-base/tsconfig.json | 14 ++---- types/re-base/tslint.json | 8 +++- 3 files changed, 36 insertions(+), 82 deletions(-) diff --git a/types/re-base/index.d.ts b/types/re-base/index.d.ts index 928bca7c47..5934b67295 100644 --- a/types/re-base/index.d.ts +++ b/types/re-base/index.d.ts @@ -3,27 +3,7 @@ // Definitions by: jordandrako // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -interface QueryOptions {} - -interface RebaseBinding {} - -declare namespace firebase { - -} - -declare namespace firebase.app { - interface App {} -} - -declare namespace firebase.database { - interface Database {} -} - -declare namespace firebase.firestore { - class Firestore {} -} - -declare namespace rebase { +export namespace rebase { interface SyncStateOptions { /** * The context of your component. @@ -53,7 +33,7 @@ declare namespace rebase { /** * Queries to be used with your read operations. See [Query Options](https://github.com/tylermcginnis/re-base#queries) for more details. */ - queries?: QueryOptions; + queries?: {}; /** * The callback function that will be invoked when the initial listener is established with Firebase. Typically used (with syncState) to change this.state.loading to false. @@ -85,7 +65,7 @@ declare namespace rebase { /** * Queries to be used with your read operations. See [Query Options](https://github.com/tylermcginnis/re-base#queries) for more details. */ - queries?: QueryOptions; + queries?: {}; /** * The callback function that will be invoked when the initial listener is established with Firebase. Typically used (with bindToState) to change this.state.loading to false. @@ -122,7 +102,7 @@ declare namespace rebase { /** * Queries to be used with your read operations. See [Query Options](https://github.com/tylermcginnis/re-base#queries) for more details. */ - queries?: QueryOptions; + queries?: {}; } interface FetchOptions { @@ -149,7 +129,7 @@ declare namespace rebase { /** * Queries to be used with your read operations. See [Query Options](https://github.com/tylermcginnis/re-base#queries) for more details. */ - queries?: QueryOptions; + queries?: {}; } interface PostOptions { @@ -188,10 +168,6 @@ declare namespace rebase { then?: (result: any) => void; } - interface DocumentReference {} - - interface CollectionReference {} - interface bindDocOptions { /** * Your react component. @@ -350,7 +326,7 @@ declare namespace rebase { /** * This property contains the initialized firebase app that was passed into re-base. You can access any of the firebase services that you are using off this object. For instance, if you want to use some firebase database methods that re-base doesn't have helpers for or if you are using the auth service and want to quickly access it off of re-base. */ - initializedApp: firebase.app.App; + initializedApp: {}; /** * This property contains an object that you can use when adding data that will be converted to a timestamp by Firebase. See [the docs](https://firebase.google.com/docs/reference/js/firebase.database.ServerValue) for more info. @@ -363,7 +339,7 @@ declare namespace rebase { * @param options syncState Options. * @returns An object which you can pass to `removeBinding` if you want to remove the listener while the component is still mounted. */ - syncState(endpoint: string, options: SyncStateOptions): RebaseBinding; + syncState(endpoint: string, options: SyncStateOptions): {}; /** * One way data binding from Firebase to your component's state. Allows you to bind a component's state property to a Firebase endpoint so whenever that Firebase endpoint changes, your component's state will be updated with that change. @@ -371,10 +347,7 @@ declare namespace rebase { * @param options bindToState Options. * @returns An object which you can pass to removeBinding if you want to remove the listener while the component is still mounted. */ - bindToState( - endpoint: string, - options: BindToStateOptions - ): RebaseBinding; + bindToState(endpoint: string, options: BindToStateOptions): {}; /** * Allows you to listen to Firebase endpoints without binding those changes to a state property. Instead, a callback will be invoked with the newly updated data. @@ -382,7 +355,7 @@ declare namespace rebase { * @param options listenTo Options. * @returns An object which you can pass to removeBinding when your component unmounts to remove the Firebase listeners. */ - listenTo(endpoint: string, options: ListenToOptions): RebaseBinding; + listenTo(endpoint: string, options: ListenToOptions): {}; /** * Allows you to retrieve the data from a Firebase endpoint just once without subscribing or listening for data changes. @@ -433,10 +406,7 @@ declare namespace rebase { * @param options bindDoc Options. * @returns An object which you can pass to `removeBinding` if you want to remove the listener while the component is still mounted. */ - bindDoc( - refOrPath: DocumentReference | string, - options: bindDocOptions - ): RebaseBinding; + bindDoc(refOrPath: {} | string, options: bindDocOptions): {}; /** * Listen to a document, when the data changes it will invoke a callback passing it the new data from Firestore. @@ -444,10 +414,7 @@ declare namespace rebase { * @param options listenToDoc Options. * @returns An object which you can pass to `removeBinding` if you want to remove the listener while the component is still mounted. */ - listenToDoc( - refOrPath: DocumentReference | string, - options: listenToDocOptions - ): RebaseBinding; + listenToDoc(refOrPath: {} | string, options: listenToDocOptions): {}; /** * Bind a collection to a state property in your component. When then collection changes in firestore, your component will re-render with the latest data. @@ -456,9 +423,9 @@ declare namespace rebase { * @returns An object which you can pass to `removeBinding` if you want to remove the listener while the component is still mounted. */ bindCollection( - refOrPath: CollectionReference | string, + refOrPath: {} | string, options: bindCollectionOptions - ): RebaseBinding; + ): {}; /** * Listen to a collection, when the data changes it will invoke a callback passing it the new data from Firestore. @@ -467,9 +434,9 @@ declare namespace rebase { * @returns An object which you can pass to `removeBinding` if you want to remove the listener while the component is still mounted. */ listenToCollection( - refOrPath: CollectionReference | string, + refOrPath: {} | string, options: listenToCollectionOptions - ): RebaseBinding; + ): {}; /** * Fetch either a Collection or Document. @@ -478,7 +445,7 @@ declare namespace rebase { * @returns A Promise thats resolve with the resulting data or rejects if the document/collection does not exist or there are any read errors. */ get( - refOrPath: CollectionReference | DocumentReference | string, + refOrPath: {} | {} | string, options: listenToCollectionOptions ): Promise; @@ -490,7 +457,7 @@ declare namespace rebase { * @returns A Promise thats resolve with the resulting data or rejects if the document/collection does not exist or there are any read errors. */ addToCollection( - refOrPath: CollectionReference | string, + refOrPath: {} | string, data: {}, id?: string ): Promise; @@ -501,10 +468,7 @@ declare namespace rebase { * @param data The document data. * @returns A Promise thats resolve with the resulting data or rejects if the document/collection does not exist or there are any read errors. */ - updateDoc( - refOrPath: DocumentReference | string, - data: {} - ): Promise; + updateDoc(refOrPath: {} | string, data: {}): Promise; /** * Deletes a document. @@ -512,10 +476,7 @@ declare namespace rebase { * @param data The document data. * @returns A Promise thats resolve with the resulting data or rejects if the document/collection does not exist or there are any read errors. */ - removeDoc( - refOrPath: DocumentReference | string, - data: {} - ): Promise; + removeDoc(refOrPath: {} | string, data: {}): Promise; /** * Removes documents from a collection. If no query is supplied, it will remove all the documents. If a query is supplied, it will only remove documents that match the query. @@ -524,7 +485,7 @@ declare namespace rebase { * @returns A Promise thats resolve with the resulting data or rejects if the document/collection does not exist or there are any read errors. */ removeFromCollection( - refOrPath: CollectionReference | string, + refOrPath: {} | string, options: removeFromCollectionOptions ): Promise; @@ -534,16 +495,13 @@ declare namespace rebase { * @param options removeFromCollection Options. * @returns A Promise thats resolve with the resulting data or rejects if the document/collection does not exist or there are any read errors. */ - syncDoc( - refOrPath: DocumentReference | string, - options: syncDocOptions - ): RebaseBinding; + syncDoc(refOrPath: {} | string, options: syncDocOptions): {}; /** * Clean up a listener. Listeners are automatically cleaned up when components unmount, however if you wish to remove a listener while the component is still mounted this will allow you to do that. An example would be if you want to start listening to a new document or change a query on all collection in response to a prop change. * @param ref The return value of syncState`, `bindToState`, `listenTo`, `listenToCollection`, `bindCollection`, `listenToDoc`, `bindDoc` or `syncDoc`. */ - removeBinding(ref: RebaseBinding): void; + removeBinding(ref: {}): void; /** * Removes every Firebase/Firestore listener. @@ -555,13 +513,9 @@ declare namespace rebase { declare module "re-base" { /** * Accepts an initialized firebase database or firestore database object. - * @param {object} firebaseDatabase Initialized firebase or firestore + * @param firebaseDatabase Initialized firebase or firestore * database. - * @return {Rebase} An instance of re-base. + * @return An instance of re-base. */ - export function createClass( - firebaseDatabase: - | firebase.database.Database - | firebase.firestore.Firestore - ): rebase.Rebase; + function createClass(firebaseDatabase: {}): rebase.Rebase; } diff --git a/types/re-base/tsconfig.json b/types/re-base/tsconfig.json index eb916d27f6..ff88a407d4 100644 --- a/types/re-base/tsconfig.json +++ b/types/re-base/tsconfig.json @@ -1,22 +1,16 @@ { "compilerOptions": { "module": "commonjs", - "lib": [ - "es6" - ], + "lib": ["es6"], "noImplicitAny": true, "noImplicitThis": true, + "strictFunctionTypes": true, "strictNullChecks": true, "baseUrl": "../", - "typeRoots": [ - "../" - ], + "typeRoots": ["../"], "types": [], "noEmit": true, "forceConsistentCasingInFileNames": true }, - "files": [ - "index.d.ts", - "re-base-tests.ts" - ] + "files": ["index.d.ts", "re-base-tests.ts"] } diff --git a/types/re-base/tslint.json b/types/re-base/tslint.json index 3db14f85ea..c077883976 100644 --- a/types/re-base/tslint.json +++ b/types/re-base/tslint.json @@ -1 +1,7 @@ -{ "extends": "dtslint/dt.json" } +{ + "extends": "dtslint/dt.json", + "rules": { + "max-line-length": false, + "no-declare-current-package": false + } +} From e5af6babf3691a7e80dee85f7d699b275f17510a Mon Sep 17 00:00:00 2001 From: Jordan Janzen Date: Thu, 17 May 2018 19:38:18 -0700 Subject: [PATCH 07/11] Stricter linting rules --- types/re-base/tslint.json | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/types/re-base/tslint.json b/types/re-base/tslint.json index c077883976..3db14f85ea 100644 --- a/types/re-base/tslint.json +++ b/types/re-base/tslint.json @@ -1,7 +1 @@ -{ - "extends": "dtslint/dt.json", - "rules": { - "max-line-length": false, - "no-declare-current-package": false - } -} +{ "extends": "dtslint/dt.json" } From 8fc58a68c35469a9796cd0e9dc5f76e982515fd2 Mon Sep 17 00:00:00 2001 From: Jordan Janzen Date: Thu, 17 May 2018 19:40:03 -0700 Subject: [PATCH 08/11] - JSdoc line lengths - remove needless namespace - remove declare namesake module --- types/re-base/index.d.ts | 1155 +++++++++++++++++++++----------------- 1 file changed, 642 insertions(+), 513 deletions(-) diff --git a/types/re-base/index.d.ts b/types/re-base/index.d.ts index 5934b67295..ba838df0aa 100644 --- a/types/re-base/index.d.ts +++ b/types/re-base/index.d.ts @@ -3,519 +3,648 @@ // Definitions by: jordandrako // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -export namespace rebase { - interface SyncStateOptions { - /** - * The context of your component. - */ - context: {}; - - /** - * The state property you want to sync with Firebase; can be an arbitrarily nested property a là `foo.bar`. - */ - state: string; - - /** - * A default value to set when the Firebase endpoint has no value (i.e., on init) (use this if you want a value other than an empty object or empty array) - */ - defaultValue?: string | boolean | number | {}; - - /** - * Returns the Firebase data at the specified endpoint as an Array instead of an Object. - */ - asArray?: boolean; - - /** - * Will keep any firebase generated keys intact when manipulating data using the asArray option. - */ - keepKeys?: boolean; - - /** - * Queries to be used with your read operations. See [Query Options](https://github.com/tylermcginnis/re-base#queries) for more details. - */ - queries?: {}; - - /** - * The callback function that will be invoked when the initial listener is established with Firebase. Typically used (with syncState) to change this.state.loading to false. - */ - then?: () => void; - - /** - * A callback function that will be invoked if the current user does not have read or write permissions at the location. - */ - onFailure?: () => void; - } - - interface BindToStateOptions { - /** - * The context of your component. - */ - context: {}; - - /** - * The state property you want to sync with Firebase; can be an arbitrarily nested property a là `foo.bar` (no arrays). - */ - state: string; - - /** - * Returns the Firebase data at the specified endpoint as an Array instead of an Object. - */ - asArray?: boolean; - - /** - * Queries to be used with your read operations. See [Query Options](https://github.com/tylermcginnis/re-base#queries) for more details. - */ - queries?: {}; - - /** - * The callback function that will be invoked when the initial listener is established with Firebase. Typically used (with bindToState) to change this.state.loading to false. - */ - then?: () => void; - - /** - * A callback function that will be invoked if the current user does not have read permissions at the location. - */ - onFailure?: () => void; - } - - interface ListenToOptions { - /** - * The context of your component. - */ - context: {}; - - /** - * Returns the Firebase data at the specified endpoint as an Array instead of an Object. - */ - asArray?: boolean; - - /** - * The callback function that will be invoked with the data from the specified endpoint when the endpoint changes. - */ - then: (result: any) => void; - - /** - * The callback function that will be invoked if the current user does not have read permissions at the location. - */ - onFailure?: (error: any) => void; - - /** - * Queries to be used with your read operations. See [Query Options](https://github.com/tylermcginnis/re-base#queries) for more details. - */ - queries?: {}; - } - - interface FetchOptions { - /** - * The context of your component. - */ - context: {}; - - /** - * Returns the Firebase data at the specified endpoint as an Array instead of an Object. - */ - asArray?: boolean; - - /** - * The callback function that will be invoked with the data from the specified endpoint when the endpoint changes. - */ - then?: (result: any) => void; - - /** - * The callback function that will be invoked with an error that occurs reading data from the specified endpoint. - */ - onFailure?: () => void; - - /** - * Queries to be used with your read operations. See [Query Options](https://github.com/tylermcginnis/re-base#queries) for more details. - */ - queries?: {}; - } - - interface PostOptions { - /** - * The data you're wanting to persist to Firebase. - */ - data: any; - - /** - * A callback that will get invoked once the new data has been saved to Firebase. If there is an error, it will be the only argument to this function. - */ - then?: (result: any) => void; - } - - interface PushOptions { - /** - * The data you're wanting to persist to Firebase. - */ - data: any; - - /** - * A callback that will get invoked once the new data has been saved to Firebase. If there is an error, it will be the only argument to this function. - */ - then?: (result: any) => void; - } - - interface UpdateOptions { - /** - * The data you're wanting to persist to Firebase. - */ - data: any; - - /** - * A callback that will get invoked once the new data has been saved to Firebase. If there is an error, it will be the only argument to this function. - */ - then?: (result: any) => void; - } - - interface bindDocOptions { - /** - * Your react component. - */ - context: {}; - - /** - * A property name on your state to bind your document to, if omitted the document will be merged into your existing state. - */ - state?: string; - - /** - * A callback that will be called when the listener is set, use for loading indicators. - */ - then?: () => void; - - /** - * A callback that will be called with any errors such as permissions errors. - */ - onFailure?: () => void; - } - - interface listenToDocOptions { - /** - * Your react component. - */ - context: {}; - - /** - * A callback that will be called when the listener is set, use for loading indicators. - */ - then?: () => void; - - /** - * A callback that will be called with any errors such as permissions errors. - */ - onFailure?: () => void; - } - - interface bindCollectionOptions { - /** - * Your react component. - */ - context: {}; - - /** - * The state property to bind the collection to. - */ - state?: string; - - /** - * A function that receives the created ref as its only argument. You can chain any Firestore queries you want to perform. See [Using Firestore Queries](https://github.com/tylermcginnis/re-base#firestorequeries). - */ - query?: () => void; - - /** - * Will embed firestore generated document ids inside each document in your collections on the `id` property. - */ - withIds?: boolean; - - /** - * will embed the DocumentReference inside each document in your collection on the `ref` property. - */ - withRefs?: boolean; - - /** - * A callback that will be called when the listener is set, use for loading indicators. - */ - then?: () => void; - - /** - * A callback that will be called with any errors such as permissions errors. - */ - onFailure?: () => void; - } - - interface listenToCollectionOptions { - /** - * Your react component. - */ - context: {}; - - /** - * A callback that will be called with the data. - */ - then: () => void; - - /** - * A function that receives the created ref as its only argument. You can chain any Firestore queries you want to perform. See [Using Firestore Queries](https://github.com/tylermcginnis/re-base#firestorequeries). - */ - query?: () => void; - - /** - * Will embed firestore generated document ids inside each document in your collections on the `id` property. - */ - withIds?: boolean; - - /** - * will embed the DocumentReference inside each document in your collection on the `ref` property. - */ - withRefs?: boolean; - - /** - * A callback that will be called with any errors such as permissions errors. - */ - onFailure?: () => void; - } - - interface getOptions { - /** - * A function that receives the created ref as its only argument. You can chain any Firestore queries you want to perform. See [Using Firestore Queries](https://github.com/tylermcginnis/re-base#firestorequeries). - */ - query?: () => void; - - /** - * Will embed firestore generated document ids inside each document in your collections on the `id` property. - */ - withIds?: boolean; - - /** - * will embed the DocumentReference inside each document in your collection on the `ref` property. - */ - withRefs?: boolean; - } - - interface removeFromCollectionOptions { - /** - * A function that receives the created ref as its only argument. You can chain any Firestore queries you want to perform. See [Using Firestore Queries](https://github.com/tylermcginnis/re-base#firestorequeries). - */ - query?: () => void; - } - - interface syncDocOptions { - /** - * Your react component. - */ - context: {}; - - /** - * The state property to sync. - */ - state: string; - - /** - * A callback that will be called when the listener is set, use for loading indicators. - */ - then?: () => void; - - /** - * A callback that will be called with any errors such as permissions errors. - */ - onFailure?: () => void; - } - - interface Rebase { - /** - * This property contains the initialized firebase app that was passed into re-base. You can access any of the firebase services that you are using off this object. For instance, if you want to use some firebase database methods that re-base doesn't have helpers for or if you are using the auth service and want to quickly access it off of re-base. - */ - initializedApp: {}; - - /** - * This property contains an object that you can use when adding data that will be converted to a timestamp by Firebase. See [the docs](https://firebase.google.com/docs/reference/js/firebase.database.ServerValue) for more info. - */ - timestamp: {}; - - /** - * Allows you to set up two way data binding between your component's state and your Firebase. Whenever your Firebase changes, your component's state will change. Whenever your component's state changes, Firebase will change. - * @param endpoint The relative Firebase endpoint to which you'd like to bind your component's state. - * @param options syncState Options. - * @returns An object which you can pass to `removeBinding` if you want to remove the listener while the component is still mounted. - */ - syncState(endpoint: string, options: SyncStateOptions): {}; - - /** - * One way data binding from Firebase to your component's state. Allows you to bind a component's state property to a Firebase endpoint so whenever that Firebase endpoint changes, your component's state will be updated with that change. - * @param endpoint The relative Firebase endpoint that you'd like to bind to your component's state. - * @param options bindToState Options. - * @returns An object which you can pass to removeBinding if you want to remove the listener while the component is still mounted. - */ - bindToState(endpoint: string, options: BindToStateOptions): {}; - - /** - * Allows you to listen to Firebase endpoints without binding those changes to a state property. Instead, a callback will be invoked with the newly updated data. - * @param endpoint The relative Firebase endpoint which contains the data with which you'd like to invoke your callback function. - * @param options listenTo Options. - * @returns An object which you can pass to removeBinding when your component unmounts to remove the Firebase listeners. - */ - listenTo(endpoint: string, options: ListenToOptions): {}; - - /** - * Allows you to retrieve the data from a Firebase endpoint just once without subscribing or listening for data changes. - * @param endpoint The relative Firebase endpoint which contains the data you're wanting to fetch. - * @param options fetch Options. - * @returns A Firebase [Promise](https://firebase.google.com/docs/reference/js/firebase.Promise) which resolves when the write is complete and rejects if there is an error. - */ - fetch(endpoint: string, options: FetchOptions): Promise; - - /** - * Allows you to update a Firebase endpoint with new data. *Replace all the data at this endpoint with the new data*. - * @param endpoint The relative Firebase endpoint that you'd like to update with the new data. - * @param options post Options. - * @returns A Firebase [Promise](https://firebase.google.com/docs/reference/js/firebase.Promise) which resolves when the write is complete and rejects if there is an error. - */ - post(endpoint: string, options: PostOptions): Promise; - - /** - * Allows you to add data to a Firebase endpoint. *Adds data to a child of the endpoint with a new Firebase push key*. - * @param endpoint The relative Firebase endpoint that you'd like to push the new data to. - * @param options push Options. - * @returns A Firebase [ThenableReference](https://firebase.google.com/docs/reference/js/firebase.database.ThenableReference) which is defined by Firebase as a "Combined Promise and reference; resolves when write is complete, but can be used immediately as the reference to the child location." - */ - push(endpoint: string, options: PushOptions): Promise; - - /** - * Allows you to update data at a Firebase endpoint changing only the properties you pass to it. **Warning: calling update with `options.data` being null will remove all the data at that endpoint**. - * @param endpoint The relative Firebase endpoint that you'd like to update. - * @param options update Options. - * @returns A Firebase [Promise](https://firebase.google.com/docs/reference/js/firebase.Promise) which resolves when the write is complete and rejects if there is an error. - */ - update(endpoint: string, options: UpdateOptions): Promise; - - /** - * Allows you to delete all data at the endpoint location. - * @param endpoint The relative Firebase endpoint that you'd like to delete data from. - * @param callback A callback that will get invoked once the data is successfully removed Firebase. If there is an error, it will be the only argument to this function. - * @returns A Firebase [Promise](https://firebase.google.com/docs/reference/js/firebase.Promise) which resolves when the write is complete and rejects if there is an error. - */ - remove( - endpoint: string, - callback?: (result: Promise) => void - ): Promise; - - /** - * Bind a document to your component. When then document changes in firestore, your component will re-render with the latest data. - * @param refOrPath DocumentReference or path. - * @param options bindDoc Options. - * @returns An object which you can pass to `removeBinding` if you want to remove the listener while the component is still mounted. - */ - bindDoc(refOrPath: {} | string, options: bindDocOptions): {}; - - /** - * Listen to a document, when the data changes it will invoke a callback passing it the new data from Firestore. - * @param refOrPath DocumentReference or path. - * @param options listenToDoc Options. - * @returns An object which you can pass to `removeBinding` if you want to remove the listener while the component is still mounted. - */ - listenToDoc(refOrPath: {} | string, options: listenToDocOptions): {}; - - /** - * Bind a collection to a state property in your component. When then collection changes in firestore, your component will re-render with the latest data. - * @param refOrPath CollectionReference or path. - * @param options bindCollection Options. - * @returns An object which you can pass to `removeBinding` if you want to remove the listener while the component is still mounted. - */ - bindCollection( - refOrPath: {} | string, - options: bindCollectionOptions - ): {}; - - /** - * Listen to a collection, when the data changes it will invoke a callback passing it the new data from Firestore. - * @param refOrPath CollectionReference or path. - * @param options listenToCollection Options. - * @returns An object which you can pass to `removeBinding` if you want to remove the listener while the component is still mounted. - */ - listenToCollection( - refOrPath: {} | string, - options: listenToCollectionOptions - ): {}; - - /** - * Fetch either a Collection or Document. - * @param refOrPath CollectionReference, DocumentReference or path. - * @param options get Options. - * @returns A Promise thats resolve with the resulting data or rejects if the document/collection does not exist or there are any read errors. - */ - get( - refOrPath: {} | {} | string, - options: listenToCollectionOptions - ): Promise; - - /** - * Add a new Document to a Collection. - * @param refOrPath CollectionReference or path. - * @param data The document data. - * @param id The id for the document. If omitted, the Firestore will generate an id for you. - * @returns A Promise thats resolve with the resulting data or rejects if the document/collection does not exist or there are any read errors. - */ - addToCollection( - refOrPath: {} | string, - data: {}, - id?: string - ): Promise; - - /** - * Update an existing document. - * @param refOrPath DocumentReference or path. - * @param data The document data. - * @returns A Promise thats resolve with the resulting data or rejects if the document/collection does not exist or there are any read errors. - */ - updateDoc(refOrPath: {} | string, data: {}): Promise; - - /** - * Deletes a document. - * @param refOrPath DocumentReference or path. - * @param data The document data. - * @returns A Promise thats resolve with the resulting data or rejects if the document/collection does not exist or there are any read errors. - */ - removeDoc(refOrPath: {} | string, data: {}): Promise; - - /** - * Removes documents from a collection. If no query is supplied, it will remove all the documents. If a query is supplied, it will only remove documents that match the query. - * @param refOrPath CollectionReference or path. - * @param options removeFromCollection Options. - * @returns A Promise thats resolve with the resulting data or rejects if the document/collection does not exist or there are any read errors. - */ - removeFromCollection( - refOrPath: {} | string, - options: removeFromCollectionOptions - ): Promise; - - /** - * Syncs a component's local state with a document in Firestore. - * @param refOrPath DocumentReference or path. - * @param options removeFromCollection Options. - * @returns A Promise thats resolve with the resulting data or rejects if the document/collection does not exist or there are any read errors. - */ - syncDoc(refOrPath: {} | string, options: syncDocOptions): {}; - - /** - * Clean up a listener. Listeners are automatically cleaned up when components unmount, however if you wish to remove a listener while the component is still mounted this will allow you to do that. An example would be if you want to start listening to a new document or change a query on all collection in response to a prop change. - * @param ref The return value of syncState`, `bindToState`, `listenTo`, `listenToCollection`, `bindCollection`, `listenToDoc`, `bindDoc` or `syncDoc`. - */ - removeBinding(ref: {}): void; - - /** - * Removes every Firebase/Firestore listener. - */ - reset(): void; - } -} - -declare module "re-base" { +export interface SyncStateOptions { /** - * Accepts an initialized firebase database or firestore database object. - * @param firebaseDatabase Initialized firebase or firestore - * database. - * @return An instance of re-base. + * The context of your component. */ - function createClass(firebaseDatabase: {}): rebase.Rebase; + context: {}; + + /** + * The state property you want to sync with Firebase; can be an + * arbitrarily nested property a là `foo.bar`. + */ + state: string; + + /** + * A default value to set when the Firebase endpoint has no value (i.e., + * on init) (use this if you want a value other than an empty object or + * empty array) + */ + defaultValue?: string | boolean | number | {}; + + /** + * Returns the Firebase data at the specified endpoint as an Array + * instead of an Object. + */ + asArray?: boolean; + + /** + * Will keep any firebase generated keys intact when manipulating data + * using the asArray option. + */ + keepKeys?: boolean; + + /** + * Queries to be used with your read operations. See + * [Query Options](https://github.com/tylermcginnis/re-base#queries) + * for more details. + */ + queries?: {}; + + /** + * The callback function that will be invoked when the initial listener + * is established with Firebase. Typically used (with syncState) to + * change this.state.loading to false. + */ + then?: () => void; + + /** + * A callback function that will be invoked if the current user does + * not have read or write permissions at the location. + */ + onFailure?: () => void; } + +export interface BindToStateOptions { + /** + * The context of your component. + */ + context: {}; + + /** + * The state property you want to sync with Firebase; can be an + * arbitrarily nested property a là `foo.bar` (no arrays). + */ + state: string; + + /** + * Returns the Firebase data at the specified endpoint as an Array + * instead of an Object. + */ + asArray?: boolean; + + /** + * Queries to be used with your read operations. See + * [Query Options](https://github.com/tylermcginnis/re-base#queries) + * for more details. + */ + queries?: {}; + + /** + * The callback function that will be invoked when the initial listener + * is established with Firebase. Typically used (with bindToState) to + * change this.state.loading to false. + */ + then?: () => void; + + /** + * A callback function that will be invoked if the current user does + * not have read permissions at the location. + */ + onFailure?: () => void; +} + +export interface ListenToOptions { + /** + * The context of your component. + */ + context: {}; + + /** + * Returns the Firebase data at the specified endpoint as an Array + * instead of an Object. + */ + asArray?: boolean; + + /** + * The callback function that will be invoked with the data from the + * specified endpoint when the endpoint changes. + */ + then: (result: any) => void; + + /** + * The callback function that will be invoked if the current user does + * not have read permissions at the location. + */ + onFailure?: (error: any) => void; + + /** + * Queries to be used with your read operations. See + * [Query Options](https://github.com/tylermcginnis/re-base#queries) + * for more details. + */ + queries?: {}; +} + +export interface FetchOptions { + /** + * The context of your component. + */ + context: {}; + + /** + * Returns the Firebase data at the specified endpoint as an Array + * instead of an Object. + */ + asArray?: boolean; + + /** + * The callback function that will be invoked with the data from the + * specified endpoint when the endpoint changes. + */ + then?: (result: any) => void; + + /** + * The callback function that will be invoked with an error that occurs + * reading data from the specified endpoint. + */ + onFailure?: () => void; + + /** + * Queries to be used with your read operations. See + * [Query Options](https://github.com/tylermcginnis/re-base#queries) + * for more details. + */ + queries?: {}; +} + +export interface PostOptions { + /** + * The data you're wanting to persist to Firebase. + */ + data: any; + + /** + * A callback that will get invoked once the new data has been saved to + * Firebase. If there is an error, it will be the only argument to this + * function. + */ + then?: (result: any) => void; +} + +export interface PushOptions { + /** + * The data you're wanting to persist to Firebase. + */ + data: any; + + /** + * A callback that will get invoked once the new data has been saved to + * Firebase. If there is an error, it will be the only argument to this + * function. + */ + then?: (result: any) => void; +} + +export interface UpdateOptions { + /** + * The data you're wanting to persist to Firebase. + */ + data: any; + + /** + * A callback that will get invoked once the new data has been saved to + * Firebase. If there is an error, it will be the only argument to this + * function. + */ + then?: (result: any) => void; +} + +export interface bindDocOptions { + /** + * The context of your component. + */ + context: {}; + + /** + * A property name on your state to bind your document to, if omitted + * the document will be merged into your existing state. + */ + state?: string; + + /** + * A callback that will be called when the listener is set, use for + * loading indicators. + */ + then?: () => void; + + /** + * A callback that will be called with any errors such as permissions + * errors. + */ + onFailure?: () => void; +} + +export interface listenToDocOptions { + /** + * The context of your component. + */ + context: {}; + + /** + * A callback that will be called when the listener is set, use for + * loading indicators. + */ + then?: () => void; + + /** + * A callback that will be called with any errors such as permissions + * errors. + */ + onFailure?: () => void; +} + +export interface bindCollectionOptions { + /** + * The context of your component. + */ + context: {}; + + /** + * The state property to bind the collection to. + */ + state?: string; + + /** + * A function that receives the created ref as its only argument. You + * can chain any Firestore queries you want to perform. See + * [Using Firestore Queries](https://github.com/tylermcginnis/re-base#firestorequeries). + */ + query?: () => void; + + /** + * Will embed firestore generated document ids inside each document in + * your collections on the `id` property. + */ + withIds?: boolean; + + /** + * will embed the DocumentReference inside each document in your + * collection on the `ref` property. + */ + withRefs?: boolean; + + /** + * A callback that will be called when the listener is set, use for + * loading indicators. + */ + then?: () => void; + + /** + * A callback that will be called with any errors such as permissions + * errors. + */ + onFailure?: () => void; +} + +export interface listenToCollectionOptions { + /** + * The context of your component. + */ + context: {}; + + /** + * A callback that will be called with the data. + */ + then: () => void; + + /** + * A function that receives the created ref as its only argument. You + * can chain any Firestore queries you want to perform. See + * [Using Firestore Queries](https://github.com/tylermcginnis/re-base#firestorequeries). + */ + query?: () => void; + + /** + * Will embed firestore generated document ids inside each document in + * your collections on the `id` property. + */ + withIds?: boolean; + + /** + * will embed the DocumentReference inside each document in your + * collection on the `ref` property. + */ + withRefs?: boolean; + + /** + * A callback that will be called with any errors such as permissions + * errors. + */ + onFailure?: () => void; +} + +export interface getOptions { + /** + * A function that receives the created ref as its only argument. You + * can chain any Firestore queries you want to perform. See + * [Using Firestore Queries](https://github.com/tylermcginnis/re-base#firestorequeries). + */ + query?: () => void; + + /** + * Will embed firestore generated document ids inside each document in + * your collections on the `id` property. + */ + withIds?: boolean; + + /** + * will embed the DocumentReference inside each document in your + * collection on the `ref` property. + */ + withRefs?: boolean; +} + +export interface removeFromCollectionOptions { + /** + * A function that receives the created ref as its only argument. You + * can chain any Firestore queries you want to perform. See + * [Using Firestore Queries](https://github.com/tylermcginnis/re-base#firestorequeries). + */ + query?: () => void; +} + +export interface syncDocOptions { + /** + * The context of your component. + */ + context: {}; + + /** + * The state property to sync. + */ + state: string; + + /** + * A callback that will be called when the listener is set, use for + * loading indicators. + */ + then?: () => void; + + /** + * A callback that will be called with any errors such as permissions + * errors. + */ + onFailure?: () => void; +} + +export interface Rebase { + /** + * This property contains the initialized firebase app that was passed + * into re-base. You can access any of the firebase services that you + * are using off this object. For instance, if you want to use some + * firebase database methods that re-base doesn't have helpers for or + * if you are using the auth service and want to quickly access it off + * of re-base. + */ + initializedApp: {}; + + /** + * This property contains an object that you can use when adding data + * that will be converted to a timestamp by Firebase. See + * [the docs](https://firebase.google.com/docs/reference/js/firebase.database.ServerValue) + * for more info. + */ + timestamp: {}; + + /** + * Allows you to set up two way data binding between your component's + * state and your Firebase. Whenever your Firebase changes, your + * component's state will change. Whenever your component's state + * changes, Firebase will change. + * @param endpoint The relative Firebase endpoint to which you'd like + * to bind your component's state. + * @param options syncState Options. + * @returns An object which you can pass to `removeBinding` if you want + * to remove the listener while the component is still mounted. + */ + syncState(endpoint: string, options: SyncStateOptions): {}; + + /** + * One way data binding from Firebase to your component's state. Allows + * you to bind a component's state property to a Firebase endpoint so + * whenever that Firebase endpoint changes, your component's state will + * be updated with that change. + * @param endpoint The relative Firebase endpoint that you'd like to + * bind to your component's state. + * @param options bindToState Options. + * @returns An object which you can pass to `removeBinding` if you want + * to remove the listener while the component is still mounted. + */ + bindToState(endpoint: string, options: BindToStateOptions): {}; + + /** + * Allows you to listen to Firebase endpoints without binding those + * changes to a state property. Instead, a callback will be invoked + * with the newly updated data. + * @param endpoint The relative Firebase endpoint which contains the + * data with which you'd like to invoke your callback function. + * @param options listenTo Options. + * @returns An object which you can pass to `removeBinding` when your + * component unmounts to remove the Firebase listeners. + */ + listenTo(endpoint: string, options: ListenToOptions): {}; + + /** + * Allows you to retrieve the data from a Firebase endpoint just once + * without subscribing or listening for data changes. + * @param endpoint The relative Firebase endpoint which contains the + * data you're wanting to fetch. + * @param options fetch Options. + * @returns A Firebase [Promise](https://firebase.google.com/docs/reference/js/firebase.Promise) + * which resolves when the write is complete and rejects if there is an + * error. + */ + fetch(endpoint: string, options: FetchOptions): Promise; + + /** + * Allows you to update a Firebase endpoint with new data. *Replace all + * the data at this endpoint with the new data*. + * @param endpoint The relative Firebase endpoint that you'd like to + * update with the new data. + * @param options post Options. + * @returns A Firebase [Promise](https://firebase.google.com/docs/reference/js/firebase.Promise) + * which resolves when the write is complete and rejects if there is an + * error. + */ + post(endpoint: string, options: PostOptions): Promise; + + /** + * Allows you to add data to a Firebase endpoint. *Adds data to a child + * of the endpoint with a new Firebase push key*. + * @param endpoint The relative Firebase endpoint that you'd like to + * push the new data to. + * @param options push Options. + * @returns A Firebase [ThenableReference](https://firebase.google.com/docs/reference/js/firebase.database.ThenableReference) + * which is defined by Firebase as a "Combined Promise and reference; + * resolves when write is complete, but can be used immediately as the + * reference to the child location." + */ + push(endpoint: string, options: PushOptions): Promise; + + /** + * Allows you to update data at a Firebase endpoint changing only the + * properties you pass to it. **Warning: calling update with + * `options.data` being null will remove all the data at that + * endpoint**. + * @param endpoint The relative Firebase endpoint that you'd like to + * update. + * @param options update Options. + * @returns A Firebase [Promise](https://firebase.google.com/docs/reference/js/firebase.Promise) + * which resolves when the write is complete and rejects if there is an + * error. + */ + update(endpoint: string, options: UpdateOptions): Promise; + + /** + * Allows you to delete all data at the endpoint location. + * @param endpoint The relative Firebase endpoint that you'd like to + * delete data from. + * @param callback A callback that will get invoked once the data is + * successfully removed Firebase. If there is an error, it will be the + * only argument to this function. + * @returns A Firebase [Promise](https://firebase.google.com/docs/reference/js/firebase.Promise) + * which resolves when the write is complete and rejects if there is an + * error. + */ + remove( + endpoint: string, + callback?: (result: Promise) => void + ): Promise; + + /** + * Bind a document to your component. When then document changes in + * firestore, your component will re-render with the latest data. + * @param refOrPath DocumentReference or path. + * @param options bindDoc Options. + * @returns An object which you can pass to `removeBinding` if you want + * to remove the listener while the component is still mounted. + */ + bindDoc(refOrPath: {} | string, options: bindDocOptions): {}; + + /** + * Listen to a document, when the data changes it will invoke a + * callback passing it the new data from Firestore. + * @param refOrPath DocumentReference or path. + * @param options listenToDoc Options. + * @returns An object which you can pass to `removeBinding` if you want + * to remove the listener while the component is still mounted. + */ + listenToDoc(refOrPath: {} | string, options: listenToDocOptions): {}; + + /** + * Bind a collection to a state property in your component. When then + * collection changes in firestore, your component will re-render with + * the latest data. + * @param refOrPath CollectionReference or path. + * @param options bindCollection Options. + * @returns An object which you can pass to `removeBinding` if you want + * to remove the listener while the component is still mounted. + */ + bindCollection( + refOrPath: {} | string, + options: bindCollectionOptions + ): {}; + + /** + * Listen to a collection, when the data changes it will invoke a + * callback passing it the new data from Firestore. + * @param refOrPath CollectionReference or path. + * @param options listenToCollection Options. + * @returns An object which you can pass to `removeBinding` if you want + * to remove the listener while the component is still mounted. + */ + listenToCollection( + refOrPath: {} | string, + options: listenToCollectionOptions + ): {}; + + /** + * Fetch either a Collection or Document. + * @param refOrPath CollectionReference, DocumentReference or path. + * @param options get Options. + * @returns A Promise thats resolve with the resulting data or rejects + * if the document/collection does not exist or there are any read + * errors. + */ + get( + refOrPath: {} | {} | string, + options: listenToCollectionOptions + ): Promise; + + /** + * Add a new Document to a Collection. + * @param refOrPath CollectionReference or path. + * @param data The document data. + * @param id The id for the document. If omitted, the Firestore will + * generate an id for you. + * @returns A Promise thats resolve with the resulting data or rejects + * if the document/collection does not exist or there are any read + * errors. + */ + addToCollection( + refOrPath: {} | string, + data: {}, + id?: string + ): Promise; + + /** + * Update an existing document. + * @param refOrPath DocumentReference or path. + * @param data The document data. + * @returns A Promise thats resolve with the resulting data or rejects + * if the document/collection does not exist or there are any read + * errors. + */ + updateDoc(refOrPath: {} | string, data: {}): Promise; + + /** + * Deletes a document. + * @param refOrPath DocumentReference or path. + * @param data The document data. + * @returns A Promise thats resolve with the resulting data or rejects + * if the document/collection does not exist or there are any read + * errors. + */ + removeDoc(refOrPath: {} | string, data: {}): Promise; + + /** + * Removes documents from a collection. If no query is supplied, it + * will remove all the documents. If a query is supplied, it will only + * remove documents that match the query. + * @param refOrPath CollectionReference or path. + * @param options removeFromCollection Options. + * @returns A Promise thats resolve with the resulting data or rejects + * if the document/collection does not exist or there are any read + * errors. + */ + removeFromCollection( + refOrPath: {} | string, + options: removeFromCollectionOptions + ): Promise; + + /** + * Syncs a component's local state with a document in Firestore. + * @param refOrPath DocumentReference or path. + * @param options removeFromCollection Options. + * @returns A Promise thats resolve with the resulting data or rejects + * if the document/collection does not exist or there are any read + * errors. + */ + syncDoc(refOrPath: {} | string, options: syncDocOptions): {}; + + /** + * Clean up a listener. Listeners are automatically cleaned up when + * components unmount, however if you wish to remove a listener while + * the component is still mounted this will allow you to do that. An + * example would be if you want to start listening to a new document or + * change a query on all collection in response to a prop change. + * @param ref The return value of syncState`, `bindToState`, `listenTo`, + * `listenToCollection`, `bindCollection`, `listenToDoc`, `bindDoc` or + * `syncDoc`. + */ + removeBinding(ref: {}): void; + + /** + * Removes every Firebase/Firestore listener. + */ + reset(): void; +} + +/** + * Accepts an initialized firebase/firestore database object. + * @param database Initialized firebase/firestore + * database. + * @return An instance of re-base. + */ +export function createClass(database: {}): Rebase; From 120f16f2445f25ab8ebe7cf310c0bc6b9c643ccc Mon Sep 17 00:00:00 2001 From: Jordan Janzen Date: Tue, 29 May 2018 16:37:34 -0700 Subject: [PATCH 09/11] Replace `{}` with `object`, add RebaseBinding interface. --- types/re-base/index.d.ts | 75 ++++++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 34 deletions(-) diff --git a/types/re-base/index.d.ts b/types/re-base/index.d.ts index ba838df0aa..235ba513dc 100644 --- a/types/re-base/index.d.ts +++ b/types/re-base/index.d.ts @@ -3,11 +3,18 @@ // Definitions by: jordandrako // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +export interface RebaseBinding { + context: object; + endpoint: string; + id: number; + method: string; +} + export interface SyncStateOptions { /** * The context of your component. */ - context: {}; + context: object; /** * The state property you want to sync with Firebase; can be an @@ -20,7 +27,7 @@ export interface SyncStateOptions { * on init) (use this if you want a value other than an empty object or * empty array) */ - defaultValue?: string | boolean | number | {}; + defaultValue?: string | boolean | number | object; /** * Returns the Firebase data at the specified endpoint as an Array @@ -39,7 +46,7 @@ export interface SyncStateOptions { * [Query Options](https://github.com/tylermcginnis/re-base#queries) * for more details. */ - queries?: {}; + queries?: object; /** * The callback function that will be invoked when the initial listener @@ -59,7 +66,7 @@ export interface BindToStateOptions { /** * The context of your component. */ - context: {}; + context: object; /** * The state property you want to sync with Firebase; can be an @@ -78,7 +85,7 @@ export interface BindToStateOptions { * [Query Options](https://github.com/tylermcginnis/re-base#queries) * for more details. */ - queries?: {}; + queries?: object; /** * The callback function that will be invoked when the initial listener @@ -98,7 +105,7 @@ export interface ListenToOptions { /** * The context of your component. */ - context: {}; + context: object; /** * Returns the Firebase data at the specified endpoint as an Array @@ -123,14 +130,14 @@ export interface ListenToOptions { * [Query Options](https://github.com/tylermcginnis/re-base#queries) * for more details. */ - queries?: {}; + queries?: object; } export interface FetchOptions { /** * The context of your component. */ - context: {}; + context: object; /** * Returns the Firebase data at the specified endpoint as an Array @@ -155,7 +162,7 @@ export interface FetchOptions { * [Query Options](https://github.com/tylermcginnis/re-base#queries) * for more details. */ - queries?: {}; + queries?: object; } export interface PostOptions { @@ -204,7 +211,7 @@ export interface bindDocOptions { /** * The context of your component. */ - context: {}; + context: object; /** * A property name on your state to bind your document to, if omitted @@ -229,7 +236,7 @@ export interface listenToDocOptions { /** * The context of your component. */ - context: {}; + context: object; /** * A callback that will be called when the listener is set, use for @@ -248,7 +255,7 @@ export interface bindCollectionOptions { /** * The context of your component. */ - context: {}; + context: object; /** * The state property to bind the collection to. @@ -291,7 +298,7 @@ export interface listenToCollectionOptions { /** * The context of your component. */ - context: {}; + context: object; /** * A callback that will be called with the data. @@ -358,7 +365,7 @@ export interface syncDocOptions { /** * The context of your component. */ - context: {}; + context: object; /** * The state property to sync. @@ -387,7 +394,7 @@ export interface Rebase { * if you are using the auth service and want to quickly access it off * of re-base. */ - initializedApp: {}; + initializedApp: object; /** * This property contains an object that you can use when adding data @@ -395,7 +402,7 @@ export interface Rebase { * [the docs](https://firebase.google.com/docs/reference/js/firebase.database.ServerValue) * for more info. */ - timestamp: {}; + timestamp: object; /** * Allows you to set up two way data binding between your component's @@ -408,7 +415,7 @@ export interface Rebase { * @returns An object which you can pass to `removeBinding` if you want * to remove the listener while the component is still mounted. */ - syncState(endpoint: string, options: SyncStateOptions): {}; + syncState(endpoint: string, options: SyncStateOptions): RebaseBinding; /** * One way data binding from Firebase to your component's state. Allows @@ -421,7 +428,7 @@ export interface Rebase { * @returns An object which you can pass to `removeBinding` if you want * to remove the listener while the component is still mounted. */ - bindToState(endpoint: string, options: BindToStateOptions): {}; + bindToState(endpoint: string, options: BindToStateOptions): RebaseBinding; /** * Allows you to listen to Firebase endpoints without binding those @@ -433,7 +440,7 @@ export interface Rebase { * @returns An object which you can pass to `removeBinding` when your * component unmounts to remove the Firebase listeners. */ - listenTo(endpoint: string, options: ListenToOptions): {}; + listenTo(endpoint: string, options: ListenToOptions): RebaseBinding; /** * Allows you to retrieve the data from a Firebase endpoint just once @@ -510,7 +517,7 @@ export interface Rebase { * @returns An object which you can pass to `removeBinding` if you want * to remove the listener while the component is still mounted. */ - bindDoc(refOrPath: {} | string, options: bindDocOptions): {}; + bindDoc(refOrPath: object | string, options: bindDocOptions): object; /** * Listen to a document, when the data changes it will invoke a @@ -520,7 +527,7 @@ export interface Rebase { * @returns An object which you can pass to `removeBinding` if you want * to remove the listener while the component is still mounted. */ - listenToDoc(refOrPath: {} | string, options: listenToDocOptions): {}; + listenToDoc(refOrPath: object | string, options: listenToDocOptions): object; /** * Bind a collection to a state property in your component. When then @@ -532,9 +539,9 @@ export interface Rebase { * to remove the listener while the component is still mounted. */ bindCollection( - refOrPath: {} | string, + refOrPath: object | string, options: bindCollectionOptions - ): {}; + ): RebaseBinding; /** * Listen to a collection, when the data changes it will invoke a @@ -545,9 +552,9 @@ export interface Rebase { * to remove the listener while the component is still mounted. */ listenToCollection( - refOrPath: {} | string, + refOrPath: object | string, options: listenToCollectionOptions - ): {}; + ): RebaseBinding; /** * Fetch either a Collection or Document. @@ -558,7 +565,7 @@ export interface Rebase { * errors. */ get( - refOrPath: {} | {} | string, + refOrPath: object | object | string, options: listenToCollectionOptions ): Promise; @@ -573,8 +580,8 @@ export interface Rebase { * errors. */ addToCollection( - refOrPath: {} | string, - data: {}, + refOrPath: object | string, + data: object, id?: string ): Promise; @@ -586,7 +593,7 @@ export interface Rebase { * if the document/collection does not exist or there are any read * errors. */ - updateDoc(refOrPath: {} | string, data: {}): Promise; + updateDoc(refOrPath: object | string, data: object): Promise; /** * Deletes a document. @@ -596,7 +603,7 @@ export interface Rebase { * if the document/collection does not exist or there are any read * errors. */ - removeDoc(refOrPath: {} | string, data: {}): Promise; + removeDoc(refOrPath: object | string, data: object): Promise; /** * Removes documents from a collection. If no query is supplied, it @@ -609,7 +616,7 @@ export interface Rebase { * errors. */ removeFromCollection( - refOrPath: {} | string, + refOrPath: object | string, options: removeFromCollectionOptions ): Promise; @@ -621,7 +628,7 @@ export interface Rebase { * if the document/collection does not exist or there are any read * errors. */ - syncDoc(refOrPath: {} | string, options: syncDocOptions): {}; + syncDoc(refOrPath: object | string, options: syncDocOptions): object; /** * Clean up a listener. Listeners are automatically cleaned up when @@ -633,7 +640,7 @@ export interface Rebase { * `listenToCollection`, `bindCollection`, `listenToDoc`, `bindDoc` or * `syncDoc`. */ - removeBinding(ref: {}): void; + removeBinding(ref: object): void; /** * Removes every Firebase/Firestore listener. @@ -647,4 +654,4 @@ export interface Rebase { * database. * @return An instance of re-base. */ -export function createClass(database: {}): Rebase; +export function createClass(database: object): Rebase; From 98fcd1ae6e250c401f71342aa3c040e1cbb902f2 Mon Sep 17 00:00:00 2001 From: Jordan Janzen Date: Tue, 29 May 2018 17:24:53 -0700 Subject: [PATCH 10/11] Require ts v2.2 --- types/re-base/index.d.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/types/re-base/index.d.ts b/types/re-base/index.d.ts index 26d8622a0e..4367476b9b 100644 --- a/types/re-base/index.d.ts +++ b/types/re-base/index.d.ts @@ -2,6 +2,7 @@ // Project: https://github.com/tylermcginnis/re-base#readme // Definitions by: jordandrako // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 export interface RebaseBinding { context: object; From 8b3949c37489cc5e67451a94cdbd1f7ff06e7b4b Mon Sep 17 00:00:00 2001 From: Jordan Janzen Date: Tue, 29 May 2018 17:25:02 -0700 Subject: [PATCH 11/11] Fix test. --- types/re-base/re-base-tests.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/types/re-base/re-base-tests.ts b/types/re-base/re-base-tests.ts index 702a6ab2dd..0edc9ea542 100644 --- a/types/re-base/re-base-tests.ts +++ b/types/re-base/re-base-tests.ts @@ -2,8 +2,9 @@ import * as rebase from "re-base"; const database = {}; const base = rebase.createClass(database); +const bar = {}; const _ref = base.syncState("foo", { - context: "bar", - state: "baz" + context: bar, + state: "foo" });