UNPKG

@hoodie/client

Version:
406 lines (334 loc) 10 kB
# hoodie-client > Client API for the Hoodie server [![Build Status](https://travis-ci.org/hoodiehq/hoodie-client.svg?branch=master)](https://travis-ci.org/hoodiehq/hoodie-client) [![Coverage Status](https://coveralls.io/repos/hoodiehq/hoodie-client/badge.svg?branch=master)](https://coveralls.io/github/hoodiehq/hoodie-client?branch=master) [![Dependency Status](https://david-dm.org/hoodiehq/hoodie-client.svg)](https://david-dm.org/hoodiehq/hoodie-client) [![devDependency Status](https://david-dm.org/hoodiehq/hoodie-client/dev-status.svg)](https://david-dm.org/hoodiehq/hoodie-client#info=devDependencies) `@hoodie/client` integrates Hoodie’s client core modules: - [account-client](https://github.com/hoodiehq/hoodie-account-client) - [store-client](https://github.com/hoodiehq/hoodie-store-client) - [connection-status](https://github.com/hoodiehq/hoodie-connection-status) - [log](https://github.com/hoodiehq/hoodie-log) ## Example ```js var Hoodie = require('@hoodie/client') var hoodie = new Hoodie({ url: 'https://myhoodieapp.com', PouchDB: require('pouchdb') }) hoodie.account.signUp({ username: 'pat@Example.com', password: 'secret' }).then(function (accountAttributes) { hoodie.log.info('Signed up as %s', accountAttributes.username) }).catch(function (error) { hoodie.log.error(error) }) ``` ## API - [Constructor](#constructor) - [hoodie.url](#hoodieurl) - [hoodie.account](#hoodieaccount) - [hoodie.store](#hoodiestore) - [hoodie.connectionStatus](#hoodieconnectionstatus) - [hoodie.log](#hoodielog) - [hoodie.request](#hoodierequest) - [hoodie.plugin](#hoodieplugin) - [hoodie.on](#hoodieon) - [hoodie.one](#hoodieone) - [hoodie.off](#hoodieoff) - [hoodie.trigger](#hoodietrigger) - [Events](#events) ### Constructor ```js new Hoodie(options) ``` <table> <thead> <tr> <th align="left">Argument</th> <th align="left">Type</th> <th align="left">Description</th> <th align="left">Required</th> </tr> </thead> <tr> <th align="left">options.PouchDB</th> <td>Constructor</td> <td> PouchDB constructor, see also <a href="https://pouchdb.com/custom.html">PouchDB custom builds</a> </td> <td>Yes</td> </tr> <tr> <th align="left">options.url</th> <td>String</td> <td> Set to hostname where Hoodie server runs, if your app runs on a different host </td> <td>Yes</td> </tr> <tr> <th align="left">options.account</th> <td>String</td> <td> <a href="https://github.com/hoodiehq/hoodie-account-client#constructor">account options</a>. <code>options.url</code> is always set to <code>hoodie.url</code> + '/account/api' </td> <td>No</td> </tr> <tr> <th align="left">options.store</th> <td>String</td> <td> <a href="https://github.com/hoodiehq/hoodie-store#constructor">store options</a>. <code>options.PouchDB</code> is always set to <a href="#constructor">Hoodie Client’s constructor</a>’s <code>options.PouchDB</code>. <code>options.dbName</code> is always set to <code>'store'</code>. <code>options.remote</code> is always set to <code>hoodie.url + '/store/api/user%2f' + account id</code>. </td> <td>No</td> </tr> <tr> <th align="left">options.task</th> <td>String</td> <td> <a href="https://github.com/hoodiehq/hoodie-client-task#constructor">task options</a>. <code>options.userId</code> is always set to <code>account id</code>. <code>options.remote</code> is always set to <code>hoodie.url</code> + '/task/api' </td> <td>No</td> </tr> <tr> <th align="left">options.connectionStatus</th> <td>String</td> <td> <a href="https://github.com/hoodiehq/hoodie-connection-status#constructor">connectionStatus options</a>. <code>options.url</code> is always set to <code>hoodie.url</code> + '/connection-status/api'. <code>options.method</code> is always set to <code>HEAD</code> </td> <td>No</td> </tr> </table> ### hoodie.url _Read-only_ ```js hoodie.url ``` full url to the hoodie server, e.g. `http://example.com/hoodie` ### hoodie.account `hoodie.account` is an instance of [hoodie-account-client](https://github.com/hoodiehq/hoodie-account-client). See [account API](https://github.com/hoodiehq/hoodie-account-client#api) ### hoodie.store `hoodie.store` is an instance of [hoodie-store](https://github.com/hoodiehq/hoodie-store). See [store API](https://github.com/hoodiehq/hoodie-store#api) ### hoodie.connectionStatus `hoodie.connectionStatus` is an instance of [hoodie-connection-status](https://github.com/hoodiehq/hoodie-connection-status). See [connectionStatus API](https://github.com/hoodiehq/hoodie-connection-status#api) ### hoodie.log `hoodie.log` is an instance of [hoodie-log](https://github.com/hoodiehq/hoodie-log). See [log API](https://github.com/hoodiehq/hoodie-log#api) ### hoodie.request Sends an http request ```js hoodie.request(url) // or hoodie.request(options) ``` <table> <thead> <tr> <th align="left">Argument</th> <th align="left">Type</th> <th align="left">Description</th> <th align="left">Required</th> </tr> </thead> <tr> <th align="left">url</th> <td>String</td> <td> Relative path or full URL. A path must start with <code>/</code> and sends a <code>GET</code> request to the path, prefixed by <code>hoodie.url</code>. In case a full URL is passed, a <code>GET</code> request to the url is sent. </td> <td>Yes</td> </tr> <tr> <th align="left">options.url</th> <td>String</td> <td> Relative path or full URL. A path must start with <code>/</code> and sends a <code>GET</code> request to the path, prefixed by <code>hoodie.url</code>. In case a full URL is passed, a <code>GET</code> request to the url is sent. </td> <td>Yes</td> </tr> <tr> <th align="left">options.method</th> <td>String</td> <td> <em>Defaults to <code>GET</code></em>. One of <code>GET</code>, <code>HEAD</code>, <code>POST</code>, <code>PUT</code>, <code>DELETE</code>. </td> <td>No</td> </tr> <tr> <th align="left">options.data</th> <td>Object, Array, String or Number</td> <td> For <code>PUT</code> and <code>POST</code> requests, an optional payload can be sent. It will be stringified before sending the request. </td> <td>No</td> </tr> <tr> <th align="left">options.headers</th> <td>Object</td> <td> Map of Headers to be sent with the request. </td> <td>No</td> </tr> </table> Examples ```js // sends a GET request to hoodie.url + '/foo/api/bar' hoodie.request('/foo/api/bar') // sends a GET request to another host hoodie.request('https://example.com/foo/bar') // sends a PATCH request to /foo/api/bar hoodie.request({ method: 'PATCH', url: '/foo/api/bar', headers: { 'x-my-header': 'my value' }, data: { foo: 'bar' } }) ``` ### hoodie.plugin Initialise hoodie plugin ```js hoodie.plugin(methods) hoodie.plugin(plugin) ``` <table> <thead> <tr> <th align="left">Argument</th> <th align="left">Type</th> <th align="left">Description</th> <th align="left">Required</th> </tr> </thead> <tr> <th align="left">methods</th> <td>Object</td> <td> Method names as keys, functions as values. Methods get directly set on <code>hoodie</code>, e.g. <code>hoodie.plugin({foo: function () {}})</code> sets <code>hoodie.foo</code> to <code>function () {}</code> </td> <td>Yes</td> </tr> <tr> <th align="left">plugin</th> <td>Function</td> <td> The passed function gets called with `hoodie` as first argument, and can directly set new methods / properties on it. </td> <td>Yes</td> </tr> </table> Examples ```js hoodie.plugin({ sayHi: function () { alert('hi') } }) hoodie.plugin(function (hoodie) { hoodie.sayHi = function () { alert('hi') } }) ``` ### hoodie.on Subscribe to event. ```js hoodie.on(eventName, handler) ``` Example ```js hoodie.on('account:signin', function (accountProperties) { alert('Hello there, ' + accountProperties.username) }) ``` ### hoodie.one Call function once at given event. ```js hoodie.one(eventName, handler) ``` Example ```js hoodie.one('mycustomevent', function (options) { console.log('foo is %s', options.bar) }) hoodie.trigger('mycustomevent', { foo: 'bar' }) hoodie.trigger('mycustomevent', { foo: 'baz' }) // logs "foo is bar" // DOES NOT log "foo is baz" ``` ### hoodie.off Removes event handler that has been added before ```js hoodie.off(eventName, handler) ``` Example ```js hoodie.off('connectionstatus:disconnect', showNotification) ``` ### hoodie.trigger Trigger custom events ```js hoodie.trigger(eventName[, option1, option2, ...]) ``` Example ```js hoodie.trigger('mycustomevent', { foo: 'bar' }) ``` ### Events <table> <tr> <th align="left"><code>account:*</code></th> <td>events, see <a href="https://github.com/hoodiehq/hoodie-account-client#events">account events</a></td> </tr> <tr> <th align="left"><code>store:*</code></th> <td>events, see <a href="https://github.com/hoodiehq/hoodie-store#events">store events</a></td> </tr> <tr> <th align="left"><code>connectionStatus:*</code></th> <td>events, see <a href="https://github.com/hoodiehq/hoodie-connection-status#events">connectionStatus events</a></td> </tr> </table> ## Testing Local setup ``` git clone https://github.com/hoodiehq/hoodie-client.git cd hoodie-client npm install ``` Run all tests ``` npm test ``` Run test from one file only ``` node tests/specs/id ``` ## Contributing Have a look at the Hoodie project's [contribution guidelines](https://github.com/hoodiehq/hoodie/blob/master/CONTRIBUTING.md). If you want to hang out you can join our [Hoodie Community Chat](http://hood.ie/chat/). ## License [Apache 2.0](http://www.apache.org/licenses/LICENSE-2.0)