###

``

### Telepat Javascript Client

UMD client for Telepat, built with Webpack to run in browsers. Available on GitHub.

Telepat is an open-source backend stack, designed to deliver information and information updates in real-time to clients, while allowing for flexible deployment and simple scaling. Read more about how it works and why it's different here.

Documentation is available here, and you can check out a simple demo app here.

#### Installing

- `bower install telepat-js`, if you're using Bower
- `npm install telepat-js`, if you're using NPM
- or the classic `<script src="lib/telepat.js"></script>`

#### Building from source

Clone the repo, then run `npm install`. After editing the sources in the /src directory, run `npm run build-all` to compile the libraries, and `npm run docs` to generate the documentation.

#### License

Released under the Apache 2 License.

### Telepat

The `Telepat` object is the first object you want to instantiate while working with the Telepat SDK. It exposes methods and properties that enable you to register, login, subscribe to objects and to users.

new Telepat()
Example

    let telepat = new Telepat();
    telepat.connect({
     apiEndpoint: 'TELEPAT-API-ENDPOINT',
     socketEndpoint: 'TELEPAT-SOCKET-ENDPOINT',
     apiKey: 'APP-API-KEY',
     appId: 'APP-ID'
    }, (err, res) => {
     if (err) {
       // Treat connection error
       console.log(err);
       return;
     }

     // Display all collections
     console.log(telepat.collections);

     // Login, display and update user data
     telepat.on('login', () => {
       console.log(telepat.user.data);
       telepat.user.data.change = true;
     });
     telepat.user.login('user', 'pass');

     // Subscribe to data
     let articleChannel = telepat.subscribe({
       channel: {
         context: 'collection-identifier',
         model: 'article'
       }
     }, () => {
       console.log(articleChannel.objectsArray);
       articleChannel.objects['object-identifier'].title = 'new title';

       articleChannel.on('update', (operationType, objectId, object, oldObject) => {
         // Update interface on data updates
       });
     });
    });

Instance Members
▸ connected

Indicates whether the current instance is connected to the backend

connected
▸ connecting

Indicates whether the current instance is in the process of connecting to the backend. If true, the `connect` event will be fired as soon as connection is established.

connecting
▸ configured

Indicates whether the current instance is properly configured and ready for connection.

configured
▸ currentAppId

If connected, this property reflects the current app id.

currentAppId
▸ collections

This object contains details about all the collections available for the currently connected application. You can read this after the `connect` event is emitted, or if the `connected` property is true. Each available collection is stored as an Object, using a key whose name is equal to the collection's id. Modifications to collection objects stored within will be automatically synchronized with the Telepat backend.

collections
▸ subscriptions

This object contains references to all of the Channels that are actively subscribed. Each channel is stored using a key equal to the channel's unique identifier.

subscriptions
▸ admin

This property becomes available after successfully logging in as an administrator. It gives you access to a instance of the Admin class, allowing you access to administrator functionality.

admin
Example

    telepat.user.loginAdmin('admin@email.com', 'password', (err) => {
     if (err) {
       // Treat login error
     } else {
       telepat.admin.getAppUsers((err) => {
         if (err) {
           // Treat error
         } else {
           // Treat success
           console.log(telepat.admin.users);
         }
       })
     }
    });

▸ user

An instance of the User class, this allows you to access user functionality like logging in, accessing and modifying current user data or registering new user accounts.

user
Example

    telepat.user.login('email', 'password', (err) => {
     if (err) {
       // Treat login error
     } else {
       // Treat successful login
       console.log(telepat.user.data);
     }
    });

▸ configure([options], [callback])

Call this to configure Telepat server endpoints without connecting to a specific app.

configure(options: [Object](default {}), callback: [TelepatCallback](default () => {}))
Parameters
options `([Object](default {}))` Object containing all configuration options for connection

Name | Description
--- | ---
options.apiEndpoint `string` | The Telepat API endpoint URL
options.socketEndpoint `string` | The Telepat socket endpoint URL
options.reauth `[boolean]` (default `false`) | Should reauth previously logged in user on connection

callback `([TelepatCallback](default () => {}))` Callback invoked after configuration is finished
Example

    let telepat = new Telepat();
    telepat.configure({
     apiEndpoint: 'TELEPAT-API-ENDPOINT',
     socketEndpoint: 'TELEPAT-SOCKET-ENDPOINT'
    }, (err, res) => {
     // Handle configuration
    });

▸ connect([options], [callback])

Call this to connect to a specific Telepat app. This is usually the first thing you need to do after instantiating the Telepat object.

connect(options: [Object](default {}), callback: [TelepatCallback](default () => {}))
Parameters
options `([Object](default {}))` Object containing all configuration options for connection

Name | Description
--- | ---
options.appId `string` | Your app id
options.apiEndpoint `[string]` | The Telepat API endpoint URL. If this is absent from the connect options, it must have been previously set by calling configure .
options.socketEndpoint `[string]` | The Telepat socket endpoint URL. If this is absent from the connect options, it must have been previously set by calling configure .
options.apiKey `string` | Your app API key
options.persistentConnection `[Object]` (default `null`) | Set this to configure receiving updates via persistent channels, like push notifications.
options.ioOptions `[Object]` (default `{}`) | Configuration options for socket.io
options.updateUDID `[boolean]` (default `false`) | Set this to true to force the client to update the saved device identifier.
options.timerInterval `[number]` (default `150`) | Frequency of running diff (in miliseconds) to check for object updates.
options.reauth `[boolean]` (default `false`) | Should reauth previously logged in user on connection

callback `([TelepatCallback](default () => {}))` Callback invoked after configuration is finished
Example

    // Simple connection to backend

    let telepat = new Telepat();
    telepat.connect({
     apiEndpoint: 'TELEPAT-API-ENDPOINT',
     socketEndpoint: 'TELEPAT-SOCKET-ENDPOINT',
     apiKey: 'APP-API-KEY',
     appId: 'APP-ID'
    }, (err, res) => {
     // Handle connection
    });

    // Using connection event

    let telepat = new Telepat();
    telepat.connect({
     apiEndpoint: 'TELEPAT-API-ENDPOINT',
     socketEndpoint: 'TELEPAT-SOCKET-ENDPOINT',
     apiKey: 'APP-API-KEY',
     appId: 'APP-ID'
    });
    let connectCallbackId = telepat.on('connect', {
     telepat.removeCallback(connectCallbackId);
     // Handle connection
    });

    // Activating the push notifications transport.
    // Do this when running inside a mobile OS, for example.
    telepat.connect({
     apiEndpoint: 'TELEPAT-API-ENDPOINT',
     socketEndpoint: 'TELEPAT-SOCKET-ENDPOINT',
     apiKey: 'APP-API-KEY',
     appId: 'APP-ID',
     persistentConnection: {
       type: 'ios',
       token: 'DEVICE-NOTIFICATION-TOKEN',
       active: 1
     }
    });

▸ disconnect()

Call this function to disconnect the client from the Telepat backend.

disconnect()
▸ processMessage(message)

Forwards messages reveived via external channels to the processing unit. Use this if you've configured external transports (like push notifications), and you need to pass received payloads to the processing engine.

processMessage(message: string)
Parameters
message `(string)` The delta update notification received from Telepat
▸ setLogLevel(level)

You can tweak the logger verbosity using this function.

setLogLevel(level: string)
Parameters
level `(string)` One of `'debug'` , `'info'` , `'warn'` or `'error'`
▸ on(name, callback)

Call this function to add callbacks to be invoked on event triggers. Available callbacks:

Name | Description
--- | ---
connect | Invoked when client has connected to the backend
disconnect | Invoked when client has disconnected from the backend
configure | Invoked when client configuration has completed
error | Invoked on any operation error
collections-update | Invoked when the available collections have updated
login | Invoked when client has successfully logged in
login-error | Invoked when there was an error with logging in
logout | Invoked when client has successfully logged out
logout-error | Invoked when there was an error with logging out

on(name: string, callback: function): number
Parameters
name `(string)` The name of the event to associate the callback with
callback `(function)` The callback to be executed
Returns
`number`: A callback id. Save this in order to later remove the callback from the event (using removeCallback )
Example

    telepat.on('connect', () => {
     console.log('connected');
    });

▸ removeCallback(name, callbackId, index)

Call this function to remove callbacks that have been set using on.

removeCallback(name: string, callbackId: number, index: any)
Parameters
name `(string)` The name of the event the callback was associated with
callbackId `(number)` The callback id returned by calling on
index `(any)`
Example

    let connectCallbackId = telepat.on('connect', () => {
     // Remove the callback after the first connection event
     telepat.removeCallback(connectCallbackId);
    });

▸ subscribe(options, onSubscribe)

Use this function to create a new Channel object and retrieve its objects.

subscribe(options: Object, onSubscribe: function): Channel
Parameters
options `(Object)` The object describing the required subscription

Name | Description
--- | ---
options.limit `[number]` | The maximum number of objects to be returned in this batch (for pagination)
options.channel `[Object]` | Describes the basic properties of the objects requested
options.sort `[Object]` | An object that defines how returned objects should be sorted. Each object key is a property name, and each value can be either `asc` or `desc` .
options.filters `[Object]` | An object describing how returned objects should be filtered.
options.offset `[number]` | The offset that should be applied for the returned objects (for pagination)

onSubscribe `(function)` Callback invoked when subscription is ready
Returns
`Channel`: The new Channel object
Example

    // A simple subscription to all objects of type `article`
    // in a specific collection

    let articleChannel = telepat.subscribe({
     channel: {
       context: 'context-unique-identifier',
       model: 'article'
     }
    }, () => {
     console.log(articleChannel.objectsArray);
    });

    // A filtered subscription to all objects of type `article`
    // in a specific collection, that have one of two specific tag values

    let articleChannel = telepat.subscribe({
     channel: {
       context: 'context-unique-identifier',
       model: 'article'
     },
     filters: {
       or: [
         {
           is: {
             tag: 'specific-tag-value'
           }
         },
         {
           is: {
             tag: 'another-tag-value'
           }
         }
       ]
     }
    }, () => {
     console.log(articleChannel.objectsArray);
    });

    // A simple subscription to all objects of type `article`
    // in a specific collection, sorted by created date descending

    let articleChannel = telepat.subscribe({
     channel: {
       context: 'context-unique-identifier',
       model: 'article'
     },
     sort: {
       created: 'desc'
     }
    }, () => {
     console.log(articleChannel.objectsArray);
    });

    // A simple subscription to all objects of type `comment`
    // in a specific collection, that belong to a specific article parent

    let articleChannel = telepat.subscribe({
     channel: {
       context: 'context-unique-identifier',
       model: 'comment',
       parent: {
         model: 'article',
         id: 'article-parent-unique-identifier'
       }
     }
    }, () => {
     console.log(articleChannel.objectsArray);
    });

▸ getChannel(options)

Same as subscribe method, but returns the new Channel object without calling subscribe on it.

getChannel(options: Object): Channel
Parameters
options `(Object)` Same as subscribe Options
Returns
`Channel`: The new Channel object
Events
▸ configure

Invoked when client configuration has completed.

configure
Parameters
name `(any)`
callback `(any)`
▸ connect

Invoked when client has connected to the backend.

connect
Parameters
name `(any)`
callback `(any)`
▸ disconnect

Invoked when client has disconnected from the backend.

disconnect
Parameters
name `(any)`
callback `(any)`
▸ login-error

Invoked when there was an error with logging in.

login-error
Parameters
name `(any)`
callback `(any)`
▸ error

Invoked on any operation error.

error
Parameters
name `(any)`
callback `(any)`
▸ logout-error

Invoked when there was an error with logging out.

logout-error
Parameters
name `(any)`
callback `(any)`
▸ logout

Invoked when client has successfully logged out.

logout
Parameters
name `(any)`
callback `(any)`
▸ collections-update

Invoked when the available collections have updated.

collections-update
Parameters
name `(any)`
callback `(any)`
▸ login

Invoked when client has successfully logged in.

login
Parameters
name `(any)`
callback `(any)`

### Channel

Use Channels to create, update and remove Telepat objects. You can create new Channels using the subscribe or the getChannel methods of the main Telepat object.

new Channel()
Example

    let articleChannel = telepat.subscribe({
     channel: {
       context: 'context-unique-identifier',
       model: 'article'
     }
    }, () => {
     console.log(articleChannel.objectsArray);

     // Create a new article object
     articleChannel.objects['new'] = {
       title: 'New article',
       text: 'Article body'
     };

     // Update a specific article
     articleChannel.objects['article-unique-id'].title = 'New title';

     // Delete a specific article
     delete articleChannel.objects['article-unique-id'];

     // React to object updates
     articleChannel.on('update', (operationType, objectId, object, oldObject) => {
       console.log(`Received article update of type ${operationType}, for object with id ${objectId}`);
       // Objects are already updated
       console.log(articleChannel.objects);
     });

     // Unsubscribe and clear objects
     articleChannel.unsubscribe();
    });

Instance Members
▸ objects

A container object referencing all of the objects retrieved via subscription. Each object is stored on a key equal to its own unique id.

objects
▸ objectsArray

A container array referencing all of the objects retrieved via subscription. The order of the objects reflects the sorting options set for the channel.

objectsArray
▸ objectsCount

The current object count.

objectsCount
▸ subscribe([callback])

Call this function to perform the actual subscribe for the configured channel. This is usually invoked by the subscribe method on the main Telepat object.

subscribe(callback: [TelepatCallback](default () => {}))
Parameters
callback `([TelepatCallback](default () => {}))` Callback invoked after subscribe is finished
▸ unsubscribe([callback])

Call this function to unsubscribe from the configured channel. All channel properties will be reset to original values.

unsubscribe(callback: [TelepatCallback](default () => {}))
Parameters
callback `([TelepatCallback](default () => {}))` Callback invoked after unsubscribe is finished
▸ getCount([callback])

Call this to retrieve the number of objects available for this channel. Value will be available on the objectsCount property.

getCount(callback: [TelepatCallback](default () => {}))
Parameters
callback `([TelepatCallback](default () => {}))` Callback invoked after getting object count is finished
▸ add(object, [callback])

Add a new Telepat object to the current channel. Instead of using this function, you can also add the object to Channel.objects, on any new key. The key will be automatically replaced with the new object id after the backend processes the operation.

add(object: Object, callback: [TelepatCallback](default () => {}))
Parameters
object `(Object)` The new object to add
callback `([TelepatCallback](default () => {}))` Callback invoked after notifying the Telepat backend of the new object
Example

    // This is one way of adding a new object using the channel instance.
    // The new key will be picked up by the monitoring system, which will signal the new event creation
    //   to the Telepat backend, and then delete the new key you just set from the objects property.
    // After processing the request, Telepat will signal the change back to the client, and the new object will be
    //   re-added to the objects property, but this time on the right key (equal to the new object's assigned id).
    //   This is when the 'update' event will get triggered on the channel.
    channel.objects['new'] = newObject;

    // Alternatively, you can call the add method:
    channel.add(newObject, err => {
     if (err) {
       // There's been a server error, check err. The object will not be added.
     } else {
       // We've successfully signaled that we want to add the object.
       // Keep in mind that this is not a sync process, so we still have to wait for the 'update'
       //   event on the channel, signaling the availability of the new object on the channel.objects property.
     }
    });

▸ remove(id, [callback])

Remove a Telepat object from the current channel. Instead of using this function, you can also delete the object from Channel.objects.

remove(id: string, callback: [TelepatCallback](default () => {}))
Parameters
id `(string)` The id of the object to delete
callback `([TelepatCallback](default () => {}))` Callback invoked after notifying the Telepat backend of the deleted object
Example

    // This is one way of deleting an object using the channel instance.
    // The deleted key will be picked up by the monitoring system, which will signal the object removal
    //   to the Telepat backend. The change will be then signaled back to the client as confirmation, triggering
    //   the channel's 'update' event.
    // This is the optimistic way of doing a delete, as the removed object will be instantly gone from the local
    //   state, before the 'update' event is triggered (and even if it is not).
    delete channel.objects[objectId];

    // Alternatively, the pessimistic approach is to call the remove method:
    channel.remove(objectId, err => {
     if (err) {
       // There's been a server error, check err. The object will not be deleted.
     } else {
       // We've successfully signaled that we want to delete the object.
       // Keep in mind that this is not a sync process, so we still have to wait for the 'update'
       //   event on the channel, signaling that the object has been removed from the channel.objects property.
     }
    });

▸ update(id, patches, [callback])

Updates a Telepat object from the current channel. To call this function, you need to create an array containing 'patch' objects, representing the modifications that need to be persisted. The structure of a patch object is:

`{'op': 'replace', 'path': channel + '/' + object_id + '/' + object_property, 'value': modified_value}`

Instead of using this function, you can also update the object directly from Channel.objects.

update(id: number, patches: Array<Object>, callback: [TelepatCallback](default () => {}))
Parameters
id `(number)` The id of the object to update
patches `(Array<Object>)` The array of patches representing the modifications that need to be persisted.
callback `([TelepatCallback](default () => {}))` Callback invoked after notifying the Telepat backend of the updated object
Example

    // This is one way of updating an object using the channel instance.
    // The updated key will be picked up by the monitoring system, which will signal the object update
    //   to the Telepat backend. The change will be then signaled back to the client as confirmation, triggering
    //   the channel's 'update' event.
    // This is the optimistic way of doing a delete, as the updated object will be instantly modified within the local
    //   state, before the 'update' event is triggered (and even if it is not).
    channel.objects[objectId].title = "New title";

    // Alternatively, the pessimistic approach is to call the update method:
    channel.update(objectId, [
     {
       'op': 'replace',
       'path': `article/${objectId}/title`,
       'value': 'New title'
     }
    ], err => {
     if (err) {
       // There's been a server error, check err. The object will not be updated.
     } else {
       // We've successfully signaled that we want to update the object.
       // Keep in mind that this is not a sync process, so we still have to wait for the 'update'
       //   event on the channel, signaling that the object has been updated within the channel.objects property.
     }
    });

▸ on(name, callback)

Call this function to add callbacks to be invoked on event triggers. Available callbacks:

Name | Description
--- | ---
error | Invoked when there was an error processing the requested operation
subscribe | Invoked when channel subscription is successful
unsubscribe | Invoked when channel unsubscription is successful
update | Invoked when objects in the subscription have been modified (update of an existing object, new object or deleted object)

on(name: string, callback: function): number
Parameters
name `(string)` The name of the event to associate the callback with
callback `(function)` The callback to be executed
Returns
`number`: A callback id. Save this in order to later remove the callback from the event (using removeCallback )
Example

    // React to object updates
     articleChannel.on('update', (operationType, objectId, object, oldObject) => {
       console.log(`Received article update of type ${operationType}, for object with id ${objectId}`);
       // Objects are already updated
       console.log(articleChannel.objects);
     });

▸ removeCallback(name, callbackId, index)

Call this function to remove callbacks that have been set using on.

removeCallback(name: string, callbackId: number, index: any)
Parameters
name `(string)` The name of the event the callback was associated with
callbackId `(number)` The callback id returned by calling on
index `(any)`
Example

    let updateCallbackId = channel.on('update', () => {
     // Remove the callback after the first update event
     channel.removeCallback(updateCallbackId);
    });

Events
▸ subscribe

Invoked when channel subscription is successful.

subscribe
Parameters
name `(any)`
callback `(any)`
▸ unsubscribe

Invoked when channel unsubscription is successful.

unsubscribe
Parameters
name `(any)`
callback `(any)`
▸ update

Invoked when objects in the subscription have been modified (there was an update of an existing object, a new object has been added or an object has been deleted).

update
Parameters
eventType `(string)` One of 'add', 'delete' or 'replace'
objectId `(string)` The id of the affected object
object `(Object)` The affected object itself
oldObject `(Object)` Present only for the 'replace' eventType, the old state of the affected object
name `(any)`
callback `(any)`
▸ error

Invoked when there was an error processing the requested operation.

error
Parameters
name `(any)`
callback `(any)`

### User

You can access an instance of this class using the user property of the Telepat object.

new User()
Example

    telepat.user.login('email', 'password', (err) => {
     if (err) {
       // Treat login error
     } else {
       // Treat successful login
       console.log(telepat.user.data);

       // Update user data
       telepat.user.data.points++;
     }
    });

Instance Members
▸ isAdmin

Indicates if the currently logged in user is an admin

isAdmin
▸ canReauth

Indicates if there's a saved authentication token that can be used to re-login

canReauth
▸ data

Object that holds all key-value data about the currently logged in user

data
▸ reauth([callback])

If there is a saved authentication token from previous connections, try to use it to login again. You can call this method if the canReauth property is true.

reauth(callback: [TelepatCallback](default () => {}))
Parameters
callback `([TelepatCallback](default () => {}))` Callback invoked after reauth operation is finished
▸ update(id, patches, [callback])

Call this to update your profile.

To call this method, you need to create an array containing 'patch' objects, representing the modifications that need to be persisted. The structure of a patch object is:

`{'op': 'replace', 'path': user or admin + '/' + user_id + '/' + property, 'value': modified_value}`

Instead of using this function, you can also update the user directly from User.data.

update(id: string, patches: Array<Object>, callback: [TelepatCallback](default () => {}))
Parameters
id `(string)` The user id of the updated user profile
patches `(Array<Object>)` The array of patches representing the modifications that need to be persisted
callback `([TelepatCallback](default () => {}))` Callback invoked after operation is finished
▸ requestPasswordReset(email, [callbackURL], [callback])

Call this to request a password reset for the logged in user. The process involves a confirmation email, with a link that needs to be clicked on in order to get a unique pass reset token. You then use that token to call the resetPassword method that finishes the process by setting a new password.

requestPasswordReset(email: string, callbackURL: [string](default null), callback: [TelepatCallback](default () => {}))
Parameters
email `(string)` The email/username of the user to reset the pass for
callbackURL `([string](default null))` The URL the user will be pointed to after verifying the request by clicking the link in the sent email
callback `([TelepatCallback](default () => {}))` Callback invoked after the operation is finished
▸ resetPassword(userId, id, token, newPassword, [callback])
resetPassword(userId: string, id: any, token: string, newPassword: string, callback: [TelepatCallback](default () => {}))
Parameters
userId `(string)` The id of the user that needs the password reset
id `(any)`
token `(string)` The token obtained from the redirect generated by calling requestPasswordReset .
newPassword `(string)` The new password
callback `([TelepatCallback](default () => {}))` Callback invoked after the operation is finished
▸ register(user, [callback])

This function creates a new user profile.

register(user: Object, callback: [TelepatCallback](default () => {}))
Parameters
user `(Object)` The object representing the new user profile
callback `([TelepatCallback](default () => {}))` Callback invoked after the operation is finished
▸ registerAdmin(admin, [callback])

This function creates a new admin profile.

registerAdmin(admin: Object, callback: [TelepatCallback](default () => {}))
Parameters
admin `(Object)` The object representing the new admin profile
callback `([TelepatCallback](default () => {}))` Callback invoked after the operation is finished
▸ loginWithFacebook(facebookToken, [callback])

This function associates the current anonymous device to a Telepat user profile, using a Facebook account for authentication.

loginWithFacebook(facebookToken: string, callback: [TelepatCallback](default () => {}))
Parameters
facebookToken `(string)` The user token obtained from Facebook after login
callback `([TelepatCallback](default () => {}))` Callback invoked after the operation is finished
▸ login(email, password, [callback])

This function associates the current anonymous device to a Telepat user profile, using a password for authentication.

login(email: string, password: string, callback: [TelepatCallback](default () => {}))
Parameters
email `(string)` The user's email address
password `(string)` The user's password
callback `([TelepatCallback](default () => {}))` Callback invoked after the operation is finished
Example

    telepat.user.login('email', 'password', (err) => {
     if (err) {
       // Treat login error
     } else {
       // Treat successful login
       console.log(telepat.user.data);
     }
    });

▸ loginAdmin(email, password, [callback])

This function associates the current anonymous device to a Telepat administrator profile, using a password for authentication.

loginAdmin(email: string, password: string, callback: [TelepatCallback](default () => {}))
Parameters
email `(string)` The admin email address
password `(string)` The admin password
callback `([TelepatCallback](default () => {}))` Callback invoked after the operation is finished
Example

    telepat.user.loginAdmin('admin@email.com', 'password', (err) => {
     if (err) {
       // Treat login error
     } else {
       telepat.admin.getAppUsers((err) => {
         if (err) {
           // Treat error
         } else {
           // Treat success
           console.log(telepat.admin.users);
         }
       })
     }
    });

▸ get(userId, [callback])

Call this to retrieve a specific application user object. Results will be sent as a callback argument.

get(userId: string, callback: [TelepatCallback](default () => {}))
Parameters
userId `(string)` The id of the requested user
callback `([TelepatCallback](default () => {}))` Callback invoked after operation is finished
▸ me([callback])

This function retrieves the currently logged in user's information.

me(callback: [TelepatCallback](default () => {}))
Parameters
callback `([TelepatCallback](default () => {}))` Callback invoked after the operation is finished
▸ logout([callback])

Logs the current user out.

logout(callback: [TelepatCallback](default () => {}))
Parameters
callback `([TelepatCallback](default () => {}))` Callback invoked after the operation is finished

### Admin

You can access an instance of this class using the admin property of the Telepat object. This instance becomes available after successfully logging in as an administrator.

new Admin()
Example

    telepat.user.loginAdmin('admin@email.com', 'password', (err) => {
     if (err) {
       // Treat login error
     } else {
       telepat.admin.getAppUsers((err) => {
         if (err) {
           // Treat error
         } else {
           // Treat success
           console.log(telepat.admin.users);

           // Update users
           telepat.admin.users[goodUserId].isAwesome = true;
           delete telepat.admin.users[badUserId];

           // Update collection metadata
           telepat.collections[currentCollectionId].topic = 'Cats';
         }
       })
     }
    });

Instance Members
▸ userChannel

This Channel instance allows registering callbacks for 'update' events on user data.

userChannel
▸ apps

This object contains data about all of the applications the current administrator can access. Each app data is stored using a key equal to the application unique identifier. You can access this after calling getApps. Modifications to app objects stored within will be automatically synchronized with the Telepat backend.

apps
▸ app

This object contains data about the currently connected app. You can access this after calling getApps. Modifications to this object will be automatically synchronized with the Telepat backend.

app
▸ users

This object contains data about all of the users of the current app. Each user data is stored using a key equal to the user unique identifier. You can access this after calling getAppUsers. Modifications to user objects stored within will be automatically synchronized with the Telepat backend.

users
▸ getApps([callback])

Call this to retrieve all the application objects the current administrator can access. Results will be sent as a callback argument, and persisted on the apps property. The object belonging to the currently connected app will also be made available, on the app property.

getApps(callback: [TelepatCallback](default function () {}))
Parameters
callback `([TelepatCallback](default function () {}))` Callback invoked after operation is finished
▸ addApp(properties, [callback])

Call this to create a new application.

addApp(properties: Object, callback: [TelepatCallback](default function () {}))
Parameters
properties `(Object)` Data about the new application. Can hold any key-value data. Must contain at least a 'keys' key, with an array of string values that will be used as valid API keys for the app.
callback `([TelepatCallback](default function () {}))` Callback invoked after operation is finished
▸ updateApp(id, patches, [callback])

Updates key-value data within an application object.

To call this method, you need to create an array containing 'patch' objects, representing the modifications that need to be persisted. The structure of a patch object is:

`{'op': 'replace', 'path': application + '/' + app_id + '/' + property, 'value': modified_value}`

Instead of using this function, you can also update the app directly from Admin.apps.

updateApp(id: string, patches: Array<Object>, callback: [TelepatCallback](default () => {}))
Parameters
id `(string)` The application id
patches `(Array<Object>)` The array of patches representing the modifications that need to be persisted
callback `([TelepatCallback](default () => {}))` Callback invoked after operation is finished
▸ deleteApp(id, [callback])

Call this to delete an application. Instead of using this function, you can also delete the app directly from Admin.apps.

deleteApp(id: string, callback: [TelepatCallback](default function () {}))
Parameters
id `(string)` The application id
callback `([TelepatCallback](default function () {}))` Callback invoked after operation is finished
▸ addCollection(collection, [callback])

Call this to create a new collection.

addCollection(collection: Object, callback: [TelepatCallback](default () => {}))
Parameters
collection `(Object)` Data about the new application. Can hold any key-value data. May be empty object.
callback `([TelepatCallback](default () => {}))` Callback invoked after operation is finished
▸ updateCollection(id, patches, [callback])

Updates key-value data within a collection object.

To call this method, you need to create an array containing 'patch' objects, representing the modifications that need to be persisted. The structure of a patch object is:

`{'op': 'replace', 'path': context + '/' + collection_id + '/' + property, 'value': modified_value}`

Instead of using this function, you can also update the collection directly from Telepat.collections.

updateCollection(id: string, patches: Array<Object>, callback: [TelepatCallback](default () => {}))
Parameters
id `(string)` The collection id
patches `(Array<Object>)` The array of patches representing the modifications that need to be persisted
callback `([TelepatCallback](default () => {}))` Callback invoked after operation is finished
▸ deleteCollection(id, [callback])

Call this to delete a collection. Instead of using this function, you can also delete the collection directly from Telepat.collections.

deleteCollection(id: string, callback: [TelepatCallback](default () => {}))
Parameters
id `(string)` The collection id
callback `([TelepatCallback](default () => {}))` Callback invoked after operation is finished
▸ deleteModel(type, callback)

Call this to remove a model from the schema, together with all objects of that specific type.

deleteModel(type: string, callback: TelepatCallback)
Parameters
type `(string)` The model name
callback `(TelepatCallback)` Callback invoked after operation is finished
▸ getAppUsers([callback])

Call this to retrieve all the current application user objects. Results will be sent as a callback argument, and persisted on the users property.

getAppUsers(callback: [TelepatCallback](default function () {}))
Parameters
callback `([TelepatCallback](default function () {}))` Callback invoked after operation is finished
▸ addUser(user, [callback])

This is an alias for #User#register.

addUser(user: Object, callback: [TelepatCallback](default function () {}))
Parameters
user `(Object)` The object representing the new user profile
callback `([TelepatCallback](default function () {}))` Callback invoked after operation is finished
▸ deleteUser(username, [callback])

Call this to delete a user profile. Instead of using this function, you can also delete the user directly from Admin.users.

deleteUser(username: string, callback: [TelepatCallback](default function () {}))
Parameters
username `(string)` The email address of the user profile to delete
callback `([TelepatCallback](default function () {}))` Callback invoked after operation is finished
▸ updateUser(id, patches, [callback])

Call this to update a user profile.

To call this method, you need to create an array containing 'patch' objects, representing the modifications that need to be persisted. The structure of a patch object is:

`{'op': 'replace', 'path': user + '/' + user_id + '/' + property, 'value': modified_value}`

Instead of using this function, you can also update the user directly from Admin.users.

updateUser(id: string, patches: Array<Object>, callback: [TelepatCallback](default function () {}))
Parameters
id `(string)` The id of the user to be updated
patches `(Array<Object>)` The array of patches representing the modifications that need to be persisted
callback `([TelepatCallback](default function () {}))` Callback invoked after operation is finished
▸ authorize(user, callback)

Call this to authorize access to the current app for another administrator account within the same Telepat instance.

authorize(user: string, callback: TelepatCallback)
Parameters
user `(string)` The email associated with the account of the new administrator
callback `(TelepatCallback)` Callback invoked after operation is finished
▸ deauthorize(user, callback)

Call this to deauthorize access to the current app for another administrator account within the same Telepat instance.

deauthorize(user: string, callback: TelepatCallback)
Parameters
user `(string)` The email associated with the account of the administrator to be removed
callback `(TelepatCallback)` Callback invoked after operation is finished

### TelepatCallback

This callback is displayed as part of the Requester class.

TelepatCallback
Parameters
err `(Error)` If there was an error processing the requested operation, this will reference the error object resulted
res `(any)` The operation response